-
Notifications
You must be signed in to change notification settings - Fork 119
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(theme): add option to use css variables #4264
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5a99865
feat(desgn-tokens): build a data-theme css file
krisantrobus 263a235
feat(css-theme): add the index file for theme
krisantrobus da31b85
feat(css-theme): add the index file for theme
krisantrobus 20b1045
feat(css-theme): added tests and working
krisantrobus 3f2f3f5
chore(ci): changeset
krisantrobus edba90d
feat(docs): added prop to theme provider table
krisantrobus 94cbc73
chore(ci): lint fix
krisantrobus f526059
chore(ci): lint fix
krisantrobus cc20ea3
Update packages/paste-theme/src/generateThemeFromTokens.ts
krisantrobus 1cd640e
fix(theme): variable name change
krisantrobus 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@twilio-paste/core": minor | ||
"@twilio-paste/design-tokens": minor | ||
--- | ||
|
||
[Design Tokens] added a new build script to generate a CSS file that applies variables for individual themes using the body[data-theme] attribute. | ||
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,6 @@ | ||
--- | ||
"@twilio-paste/core": minor | ||
"@twilio-paste/theme": minor | ||
--- | ||
|
||
[Theme] Added the property `useCssVariables` which allows the color values to be pulled from CSS variables instead of static values |
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
28 changes: 28 additions & 0 deletions
28
packages/paste-theme/__tests__/__snapshots__/themes.spec.ts.snap
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
backgroundColors, | ||
borderColors, | ||
borderWidths, | ||
boxShadows, | ||
colorSchemes, | ||
colors, | ||
dataVisualization, | ||
fontSizes, | ||
fontWeights, | ||
fonts, | ||
lineHeights, | ||
radii, | ||
sizings, | ||
spacings, | ||
textColors, | ||
zIndices, | ||
} from "@twilio-paste/design-tokens/dist/tokens.es6"; | ||
|
||
import { GenerateThemeFromTokensArgs, generateThemeFromTokens } from "../../generateThemeFromTokens"; | ||
|
||
const convertToCSSVariables = ( | ||
tokens: GenerateThemeFromTokensArgs | object, | ||
): Partial<Record<keyof GenerateThemeFromTokensArgs, string | object>> => { | ||
const cssVariables: Partial<Record<keyof GenerateThemeFromTokensArgs, string | object>> = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typescript ftw 🙌 |
||
|
||
for (const [key, value] of Object.entries(tokens) as Array<[keyof GenerateThemeFromTokensArgs, string | object]>) { | ||
if (typeof value === "object") { | ||
cssVariables[key] = convertToCSSVariables(value); | ||
} else { | ||
// Convert the key to a CSS variable name colorBagroundPrimary -> --color-background-primary size10 -> --size-10 | ||
const cssVariableName = `--${key.replace(/([A-Z])/g, "-$1").replace(/(\d+)/g, "-$1").toLowerCase()}`; | ||
cssVariables[key] = `var(${cssVariableName})`; | ||
} | ||
} | ||
|
||
return cssVariables; | ||
}; | ||
|
||
export const CSSVariablesTheme = generateThemeFromTokens( | ||
convertToCSSVariables({ | ||
backgroundColors, | ||
borderColors, | ||
borderWidths, | ||
radii, | ||
fonts, | ||
fontSizes, | ||
fontWeights, | ||
lineHeights, | ||
boxShadows, | ||
sizings, | ||
spacings, | ||
textColors, | ||
zIndices, | ||
dataVisualization, | ||
colors, | ||
colorSchemes, | ||
}) as GenerateThemeFromTokensArgs, | ||
); |
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,56 @@ | ||
import { Box } from "@twilio-paste/box"; | ||
import { Button } from "@twilio-paste/button"; | ||
import { Combobox } from "@twilio-paste/combobox"; | ||
import "@twilio-paste/design-tokens/dist/themes/dark/tokens.data-theme.css"; | ||
import "@twilio-paste/design-tokens/dist/themes/evergreen/tokens.data-theme.css"; | ||
import "@twilio-paste/design-tokens/dist/themes/twilio-dark/tokens.data-theme.css"; | ||
import "@twilio-paste/design-tokens/dist/themes/twilio/tokens.data-theme.css"; | ||
import "@twilio-paste/design-tokens/dist/tokens.custom-properties.css"; | ||
import { Input } from "@twilio-paste/input"; | ||
import { Paragraph } from "@twilio-paste/paragraph"; | ||
import { Stack } from "@twilio-paste/stack"; | ||
import { TextArea } from "@twilio-paste/textarea"; | ||
import * as React from "react"; | ||
|
||
import { ThemeProvider } from "../src/themeProvider"; | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default { | ||
title: "Theme/ThemeProvider/CSSVariables", | ||
component: ThemeProvider, | ||
}; | ||
|
||
export const StylingThemeProviderElement = (): React.ReactNode => ( | ||
<ThemeProvider useCSSVariables={true}> | ||
<Paragraph> | ||
This theme provider uses CSS variables. You can change the theme using this combobox to switch with theme | ||
variables should be applied. Note Storbook also ahs a theme provider wrapping the components in the view. You will | ||
see the body color not get applied | ||
</Paragraph> | ||
<Combobox | ||
labelText="Select a theme" | ||
items={["twilio", "twilio-dark", "dark", "evergreen"]} | ||
onSelectedItemChange={(value) => { | ||
document.body.setAttribute("data-theme", value.selectedItem); | ||
}} | ||
/> | ||
<Box marginTop="space60"> | ||
<Paragraph> | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore | ||
magna aliqua. Nisi porta lorem mollis aliquam ut porttitor leo. Hendrerit gravida rutrum quisque non. A arcu | ||
cursus vitae congue mauris rhoncus aenean vel elit. Tortor dignissim convallis aenean et tortor at risus. | ||
Vestibulum lorem sed risus ultricies. Tempor nec feugiat nisl pretium fusce id. Morbi tempus iaculis urna id | ||
volutpat lacus laoreet non curabitur. In ante metus dictum at. Sit amet risus nullam eget felis eget nunc | ||
lobortis. | ||
</Paragraph> | ||
<Stack orientation="vertical" spacing="space50"> | ||
<Button variant="primary" onClick={() => {}}> | ||
Click me | ||
</Button> | ||
<Input aria-label="Search" placeholder="Search options..." type="text" /> | ||
|
||
<TextArea aria-label="Feedback" value="Lorem ipsum dolor sit amet, consectetur adipiscing elit" /> | ||
</Stack> | ||
</Box> | ||
</ThemeProvider> | ||
); |
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.
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.
question: does the gulp task require a changeset?
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.
It didn't kick one off but I think it would be good to include a changeset for this, something we should be communicating I feel.