-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/generate tailwind config #494
Draft
mircmo
wants to merge
10
commits into
sbb-design-systems:main
Choose a base branch
from
mircmo:feat/generate-tailwind-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d10f1b2
feat(tailwind-config): first test implementation of a tailwind-config…
mircmo 2d1aa2c
feat(tailwind-config): switch to custom format
mircmo 7b5ce12
feat(tailwind-config): add comment with actual value behind variable …
mircmo 5eed45e
Merge remote-tracking branch 'origin/main' into feat/generate-tailwin…
mircmo 2d0bbc2
feat(tailwind-config): change breakpoints to min-width queries instea…
mircmo 8d88649
feat(tailwind-config): add 0px spacing variant
mircmo 48d077d
feat(tailwind-config): add some zero values, add max-width queries, s…
mircmo 132cc25
Merge remote-tracking branch 'upstream/main' into feat/generate-tailw…
mircmo 9c81ace
feat(tailwind-config): add font tokens
mircmo 92d036c
feat(tailwind-config): added box-shadow
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { Format, Named, TransformedToken } from 'style-dictionary'; | ||
import * as SBBTokens from './designTokens'; | ||
import { Config, CustomThemeConfig } from 'tailwindcss/types/config'; | ||
|
||
export function createTailwindSdFormatter(): Named<Format> { | ||
return { | ||
name: 'custom/tailwind', | ||
formatter: (args) => { | ||
return createTailwindConfig(args.dictionary.allTokens); | ||
}, | ||
}; | ||
} | ||
|
||
export function createTailwindConfig(tokens: TransformedToken[]) { | ||
const sbbTokens = unnestObjects<typeof SBBTokens>(tokens); | ||
|
||
// map colors and respective transparency variants to a common color | ||
// e.g. "black" and "blackAlpha" will get merged into one color object, with the value of "black" as the default | ||
const colors = sbbTokens.color; | ||
Object.keys(colors).forEach((color) => { | ||
if (color.endsWith('Alpha') && typeof colors[color] === 'object') { | ||
const realColorName = color.replace('Alpha', ''); | ||
colors[realColorName] = withTwDefault(colors[realColorName], colors[color]); | ||
delete colors[color]; | ||
} | ||
}); | ||
|
||
const { fixed, responsive } = sbbTokens.spacing; | ||
|
||
// TODO: implement appropriate media queries (like currently done in lyne-components) | ||
const responsiveSizes = Object.fromEntries( | ||
Object.keys(responsive).map((size) => { | ||
const breakpoint = 'zero'; | ||
const variableName = responsive[size][breakpoint].replace(`-${breakpoint}`, ''); | ||
return [size, variableName]; | ||
}), | ||
); | ||
|
||
const fixedSizes = removeDashPrefix(fixed); | ||
|
||
const spacing = { ...fixedSizes, ...responsiveSizes, '0': '0' }; | ||
|
||
// ignore the max values from the breakpoint and only use the min sizes, | ||
// since min-width media queries are to be used | ||
const minWidthScreens = Object.fromEntries( | ||
Object.entries(sbbTokens.breakpoint).map(([bpName, range]) => [bpName, range.min]), | ||
); | ||
|
||
const maxWidthScreens = Object.fromEntries( | ||
Object.entries(sbbTokens.breakpoint).map(([bpName, range]) => [ | ||
`max-${bpName}`, | ||
{ max: range.max }, | ||
]), | ||
); | ||
|
||
const typeFaces = sbbTokens.typo.typeFace; | ||
|
||
const fontFamily: CustomThemeConfig['fontFamily'] = { | ||
roman: typeFaces.sbbRoman, | ||
bold: typeFaces.sbbBold, | ||
light: typeFaces.sbbLight, | ||
}; | ||
|
||
// const fontSize = | ||
|
||
// Object.fromEntries( | ||
// Object.entries(sbbTokens.typo.typeFace).map(([name, value]) => { | ||
// if (name.startsWith('sbb')) name = name.substring(0, 3); | ||
// return [name, ]; | ||
// }), | ||
// ); | ||
|
||
// const font: CustomThemeConfig["fontFamily"] | ||
|
||
const tailwindTheme: Partial<CustomThemeConfig> = { | ||
colors: { transparent: 'transparent', current: 'currentColor', ...colors }, | ||
screens: { ...minWidthScreens, ...maxWidthScreens }, | ||
transitionDuration: removeDashPrefix(sbbTokens.animation.duration), | ||
transitionTimingFunction: withTwDefault(sbbTokens.animation.easing), | ||
borderRadius: { ...sbbTokens.border.radius, '0': '0', full: '9999px' }, | ||
borderWidth: { ...sbbTokens.border.width, '0': '0' }, | ||
outlineOffset: withTwDefault(sbbTokens.focus.outline.offset), | ||
spacing, | ||
fontFamily, | ||
|
||
// TODO: | ||
// font | ||
// shadow | ||
// grid layout | ||
}; | ||
|
||
return JSON.stringify({ theme: tailwindTheme } as Config, null, 2); | ||
} | ||
const withTwDefault = <T extends object, V>(defaultValue: V, obj = {} as T) => ({ | ||
...obj, | ||
DEFAULT: defaultValue, | ||
}); | ||
|
||
// remove the dash prefix from the keys | ||
// e.g. "-1x" becomes "1x" in the key | ||
const removeDashPrefix = (obj: Record<string, any>) => | ||
Object.fromEntries( | ||
Object.entries(obj).map(([key, value]) => [ | ||
key.startsWith('-') ? key.substring(1) : key, | ||
value, | ||
]), | ||
); | ||
|
||
// this type recursively unnests objects that have a "value" property | ||
// e.g. recursively transforms objects like { a: { value: "b" } } to { a: "b" } | ||
type UnnestValue<T> = { | ||
[K in keyof T]: T[K] extends { value: any } ? T[K]['value'] : UnnestValue<T[K]>; | ||
}; | ||
|
||
function unnestObjects<T extends object>(objects: TransformedToken[]): UnnestValue<T> { | ||
const nestedObject: any = {}; | ||
|
||
for (const obj of objects) { | ||
let currentObject = nestedObject; | ||
const path = obj.path; | ||
|
||
for (let i = 0; i < path.length - 1; i++) { | ||
const key = path[i]; | ||
|
||
if (!(key in currentObject)) { | ||
currentObject[key] = {}; | ||
} | ||
|
||
currentObject = currentObject[key]; | ||
} | ||
|
||
const finalKey = path[path.length - 1]; | ||
|
||
if (path[0] === 'breakpoint') { | ||
// breakpoints don't support css variables, we need to use the actual value of the variable instead | ||
currentObject[finalKey] = `${obj.value} /* var(--${obj.name}) */`; | ||
} else { | ||
// add the actual value behind the variable as a comment for a better developer experience | ||
currentObject[finalKey] = | ||
`var(--${obj.name}) /* ${obj.attributes?.category === 'size' ? obj.original.value + 'px' : obj.value} */`; | ||
} | ||
} | ||
|
||
return nestedObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just rebase/merge the main, we have removed it