Skip to content

Commit

Permalink
Merge pull request #5532 from chakra-ui/feat/re-hoist-css-vars
Browse files Browse the repository at this point in the history
feat: add semantic token support for data-theme attributes
  • Loading branch information
segunadebayo committed Mar 25, 2022
2 parents 8e201b9 + ab16166 commit e6fcf17
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/mighty-monkeys-wash.md
@@ -0,0 +1,6 @@
---
"@chakra-ui/styled-system": minor
---

Updated `_dark` and `_light` pseudo selectors to allow semantic tokens to change
with the `data-theme` attributes.
7 changes: 7 additions & 0 deletions .changeset/twenty-cooks-visit.md
@@ -0,0 +1,7 @@
---
"@chakra-ui/system": minor
---

Added `[data-theme]` to the CSS variables root selector. This allows the
semantic tokens to change according to `data-theme="dark"` and
`data-theme="light"` DOM element attributes.
15 changes: 14 additions & 1 deletion .storybook/preview.tsx
Expand Up @@ -56,7 +56,20 @@ const withChakra = (StoryFn: Function, context: StoryContext) => {
}, [dir])

return (
<ChakraProvider theme={extendTheme({ direction: dir })}>
<ChakraProvider
theme={extendTheme({
direction: dir,
semanticTokens: {
colors: {
semantic: {
default: "red.500",
_light: "red.500",
_dark: "blue.400",
},
},
},
})}
>
<div dir={dir} id="story-wrapper" style={{ minHeight: "100vh" }}>
<ColorModeToggleBar />
<StoryFn />
Expand Down
10 changes: 8 additions & 2 deletions packages/styled-system/src/pseudos.ts
Expand Up @@ -298,12 +298,18 @@ export const pseudoSelectors = {
* Styles for when `data-theme` is applied to any parent of
* this component or element.
*/
_dark: ".chakra-ui-dark &, [data-theme=dark] &, &[data-theme=dark]",
_dark:
".chakra-ui-dark &:not([data-theme])," +
"[data-theme=dark] &:not([data-theme])," +
"&[data-theme=dark]",
/**
* Styles for when `data-theme` is applied to any parent of
* this component or element.
*/
_light: ".chakra-ui-light &, [data-theme=light] &, &[data-theme=light]",
_light:
".chakra-ui-light &:not([data-theme])," +
"[data-theme=light] &:not([data-theme])," +
"&[data-theme=light]",
}

export type Pseudos = typeof pseudoSelectors
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-system/tests/css-var.test.ts
Expand Up @@ -482,7 +482,7 @@ test("should convert semantic tokens", () => {
"--colors-red-800": "#ff0080",
"--colors-secondary": "var(--colors-red-800)",
"--colors-success": "var(--colors-red-100)",
".chakra-ui-dark &, [data-theme=dark] &, &[data-theme=dark]": Object {
".chakra-ui-dark &:not([data-theme]),[data-theme=dark] &:not([data-theme]),&[data-theme=dark]": Object {
"--colors-primary": "var(--colors-red-400)",
"--colors-secondary": "var(--colors-red-700)",
},
Expand Down
10 changes: 7 additions & 3 deletions packages/system/src/providers.tsx
Expand Up @@ -39,9 +39,13 @@ export interface CSSVarsProps {
root?: string
}

export const CSSVars = ({ root = ":host, :root" }: CSSVarsProps) => (
<Global styles={(theme: any) => ({ [root]: theme.__cssVars })} />
)
export const CSSVars = ({ root = ":host, :root" }: CSSVarsProps) => {
/**
* Append color mode selector to allow semantic tokens to change according to the color mode
*/
const selector = [root, `[data-theme]`].join(",")
return <Global styles={(theme: any) => ({ [selector]: theme.__cssVars })} />
}

export function useTheme<T extends object = Dict>() {
const theme = React.useContext(
Expand Down
19 changes: 19 additions & 0 deletions packages/system/stories/system.stories.tsx
Expand Up @@ -195,3 +195,22 @@ export const WithCSSVarToken = () => {
</chakra.div>
)
}

export const WithSemanticTokens = () => {
return (
<div>
<chakra.p color="semantic">I am in the default color mode</chakra.p>
<div data-theme="light">
<chakra.p color="semantic">I am forced to light mode (red)</chakra.p>
</div>
<div data-theme="dark">
<chakra.p color="semantic">I am forced to dark mode (blue)</chakra.p>
<div data-theme="light">
<chakra.p pl="4" color="semantic">
I am nested and forced to light mode (red)
</chakra.p>
</div>
</div>
</div>
)
}

1 comment on commit e6fcf17

@vercel
Copy link

@vercel vercel bot commented on e6fcf17 Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.