Skip to content

Commit 73b124d

Browse files
authored
chore: add Storybook tool for dark mode (#1721)
1 parent 33b8e16 commit 73b124d

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const PACKAGE_NAMES = readdirSync(path.resolve(__dirname, '../packages')).filter
1515
);
1616

1717
module.exports = {
18-
stories: ['../packages/*/demo/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
18+
stories: [`../packages/${process.env.PACKAGE || '*'}/demo/**/*.stories.@(js|jsx|ts|tsx|mdx)`],
1919
staticDirs: ['./static'],
2020
addons: ['@storybook/addon-essentials', '@storybook/addon-a11y', '@storybook/addon-designs'],
2121
framework: {

.storybook/preview.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
import React, { StrictMode } from 'react';
99
import styled, { createGlobalStyle } from 'styled-components';
1010
import { create } from '@storybook/theming/create';
11-
import { ThemeProvider, DEFAULT_THEME } from '../packages/theming/src';
11+
import { ThemeProvider, DEFAULT_THEME, getColor } from '../packages/theming/src';
1212

1313
export const parameters = {
1414
actions: { argTypesRegex: '^on[A-Z].*' },
1515
backgrounds: {
16-
disable: true,
17-
grid: { disable: true }
16+
grid: { disable: true },
17+
values: [
18+
{ name: 'light', value: DEFAULT_THEME.colors.background },
19+
{ name: 'dark', value: DEFAULT_THEME.colors.foreground }
20+
]
1821
},
1922
controls: {
2023
hideNoControlsWarning: true,
@@ -50,11 +53,19 @@ const withThemeProvider = (story, context) => {
5053
document.querySelector('link[href$="bedrock/dist/index.css"]').setAttribute('disabled', true);
5154
}
5255

53-
const theme = {
54-
...DEFAULT_THEME,
55-
colors: { ...DEFAULT_THEME.colors, primaryHue: context.globals.primaryHue },
56-
rtl
57-
};
56+
const colors = { ...DEFAULT_THEME.colors, primaryHue: context.globals.primaryHue };
57+
58+
if (
59+
context.globals.backgrounds && context.globals.backgrounds.value !== 'transparent'
60+
? context.globals.backgrounds.value === DEFAULT_THEME.colors.foreground
61+
: context.parameters.backgrounds.default === 'dark'
62+
) {
63+
colors.base = 'dark';
64+
colors.background = getColor('neutralHue', 900, DEFAULT_THEME);
65+
colors.foreground = getColor('neutralHue', 200, DEFAULT_THEME);
66+
}
67+
68+
const theme = { ...DEFAULT_THEME, colors, rtl };
5869

5970
return (
6071
<ThemeProvider theme={theme}>

packages/theming/src/elements/ThemeProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React, { PropsWithChildren, useRef } from 'react';
99
import { ThemeProvider as StyledThemeProvider } from 'styled-components';
1010
import { useFocusVisible } from '@zendeskgarden/container-focusvisible';
1111
import { getControlledValue } from '@zendeskgarden/container-utilities';
12-
import { IThemeProviderProps } from '../types';
12+
import { IGardenTheme, IThemeProviderProps } from '../types';
1313
import DEFAULT_THEME from './theme';
1414
import { useDocument } from '../utils/useDocument';
1515

@@ -20,7 +20,7 @@ export const ThemeProvider = ({
2020
...other
2121
}: PropsWithChildren<IThemeProviderProps>) => {
2222
const scopeRef = useRef<HTMLDivElement>(null);
23-
const relativeDocument = useDocument(theme);
23+
const relativeDocument = useDocument(theme as IGardenTheme);
2424
const controlledScopeRef =
2525
focusVisibleRef === null
2626
? React.createRef<HTMLElement>()

packages/theming/src/types/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* found at http://www.apache.org/licenses/LICENSE-2.0.
66
*/
77

8-
import { ThemeProps } from 'styled-components';
8+
import { ThemeProviderProps } from 'styled-components';
99

1010
export const ARROW_POSITION = [
1111
'top',
@@ -146,13 +146,13 @@ export interface IGardenTheme {
146146
palette: Record<string, Hue>;
147147
}
148148

149-
export interface IThemeProviderProps extends Partial<ThemeProps<IGardenTheme>> {
149+
export interface IThemeProviderProps extends Partial<ThemeProviderProps<IGardenTheme>> {
150150
/**
151151
* Provides values for component styling. See styled-components
152152
* [`ThemeProvider`](https://styled-components.com/docs/api#themeprovider)
153153
* for details.
154154
*/
155-
theme?: IGardenTheme;
155+
theme?: IGardenTheme | ((theme: IGardenTheme) => IGardenTheme);
156156
/**
157157
* Provides a reference to the DOM node used to scope a `:focus-visible`
158158
* polyfill. If left `undefined`, a scoping `<div>` will be rendered.

0 commit comments

Comments
 (0)