Skip to content

Commit b692b49

Browse files
fixs for storybook (#1382)
1 parent 05e6744 commit b692b49

6 files changed

Lines changed: 62 additions & 12 deletions

File tree

docs/storybook/stories/Motion/Functional.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const Functional = () => {
8787
<CubicBezier bezier={item.value} />
8888
<p>[{item.value.join(', ')}]</p>
8989
<InlineCode value={item.name} cssVar={true} copyClipboard />
90-
<p style={{fontSize: 12, textAlign: 'center', color: '#666'}}>{item.description}</p>
90+
<p style={{fontSize: 12, textAlign: 'center', color: 'var(--fgColor-muted)'}}>{item.description}</p>
9191
</Card>
9292
))}
9393
</div>

docs/storybook/stories/StorybookComponents/CSSTokenSwatch/CSSTokenShadowSwatch.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@ export const CSSTokenShadowSwatch = ({color, shadow, borderColor}: CSSTokenSwatc
1111
const ref = React.useRef<HTMLDivElement | null>(null)
1212
const [value, setValue] = React.useState<string | null>(null)
1313

14-
React.useEffect(() => {
14+
const recalculate = React.useCallback(() => {
1515
if (ref.current === null) {
1616
return
1717
}
18-
1918
const style = getComputedStyle(ref.current)
2019
const rgb = style.getPropertyValue('box-shadow')
2120
setValue(rgb)
22-
}, [shadow])
21+
}, [])
22+
23+
React.useEffect(() => {
24+
recalculate()
25+
}, [shadow, recalculate])
26+
27+
React.useEffect(() => {
28+
const observer = new MutationObserver(recalculate)
29+
observer.observe(document.body, {
30+
attributes: true,
31+
attributeFilter: ['data-color-mode', 'data-light-theme', 'data-dark-theme'],
32+
})
33+
return () => observer.disconnect()
34+
}, [recalculate])
2335

2436
if (shadow === undefined) {
2537
return null

docs/storybook/stories/StorybookComponents/CSSTokenSwatch/CSSTokenSwatch.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,27 @@ export const CSSTokenSwatch = ({color, prevColor, shadow}: CSSTokenSwatchProps)
3030
const ref = React.useRef<HTMLDivElement | null>(null)
3131
const [hex, setHex] = React.useState<string | null>(null)
3232

33-
React.useEffect(() => {
33+
const recalculate = React.useCallback(() => {
3434
if (ref.current === null) {
3535
return
3636
}
37-
3837
const style = getComputedStyle(ref.current)
3938
const rgb = style.getPropertyValue('background-color')
4039
setHex(rgb && rgb !== '' ? toHex(rgb) : null)
41-
}, [color])
40+
}, [])
41+
42+
React.useEffect(() => {
43+
recalculate()
44+
}, [color, recalculate])
45+
46+
React.useEffect(() => {
47+
const observer = new MutationObserver(recalculate)
48+
observer.observe(document.body, {
49+
attributes: true,
50+
attributeFilter: ['data-color-mode', 'data-light-theme', 'data-dark-theme'],
51+
})
52+
return () => observer.disconnect()
53+
}, [recalculate])
4254

4355
if (color === undefined || color === '') {
4456
return null

docs/storybook/stories/StorybookComponents/ColorScale/ColorScale.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const ColorScale = ({color, border}: ColorScaleProps) => {
1212
const [hex, setHex] = React.useState<string | null>(null)
1313
const [textColor, setTextColor] = React.useState<string>('currentColor')
1414

15-
React.useEffect(() => {
15+
const recalculate = React.useCallback(() => {
1616
setTimeout(() => {
1717
if (ref.current === null) {
1818
return
@@ -23,7 +23,20 @@ export const ColorScale = ({color, border}: ColorScaleProps) => {
2323
setHex(asHex)
2424
setTextColor(asHex ? readableColor(asHex) : 'currentColor')
2525
}, 0)
26-
}, [color])
26+
}, [])
27+
28+
React.useEffect(() => {
29+
recalculate()
30+
}, [color, recalculate])
31+
32+
React.useEffect(() => {
33+
const observer = new MutationObserver(recalculate)
34+
observer.observe(document.body, {
35+
attributes: true,
36+
attributeFilter: ['data-color-mode', 'data-light-theme', 'data-dark-theme'],
37+
})
38+
return () => observer.disconnect()
39+
}, [recalculate])
2740

2841
return (
2942
<div

docs/storybook/stories/StorybookComponents/ColorScale/ColorScaleByName.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const ColorBlock = ({color}: {color: string}) => {
1212
const [hex, setHex] = React.useState<string | null>(null)
1313
const [textColor, setTextColor] = React.useState<string>('currentColor')
1414

15-
React.useEffect(() => {
15+
const recalculate = React.useCallback(() => {
1616
setTimeout(() => {
1717
if (ref.current === null) {
1818
return
@@ -23,7 +23,20 @@ const ColorBlock = ({color}: {color: string}) => {
2323
setHex(asHex)
2424
setTextColor(asHex ? readableColor(asHex) : 'currentColor')
2525
}, 0)
26-
}, [color])
26+
}, [])
27+
28+
React.useEffect(() => {
29+
recalculate()
30+
}, [color, recalculate])
31+
32+
React.useEffect(() => {
33+
const observer = new MutationObserver(recalculate)
34+
observer.observe(document.body, {
35+
attributes: true,
36+
attributeFilter: ['data-color-mode', 'data-light-theme', 'data-dark-theme'],
37+
})
38+
return () => observer.disconnect()
39+
}, [recalculate])
2740

2841
return (
2942
<div

docs/storybook/stories/VisualRegressionTesting/AllColorsTable.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const AllThemesInOneTable = () => {
4545
</Stack>
4646
<Stack>
4747
{data.map(({id}) => (
48-
<Stack direction="horizontal">
48+
<Stack key={id} direction="horizontal">
4949
<Stack.Item className={styles.name}>
5050
<InlineCode value={id} />
5151
</Stack.Item>

0 commit comments

Comments
 (0)