-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme): add theme hook and make storybook use internal theming
- Loading branch information
Showing
6 changed files
with
155 additions
and
70 deletions.
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 |
---|---|---|
@@ -1,26 +1,34 @@ | ||
import type { Preview } from '@storybook/react' | ||
import '../src/assets/global.css' | ||
import * as React from 'react' | ||
import { KeyboardEventProvider } from '../src/hooks/useHadKeyboardEvent' | ||
import { Provider } from '../src/components/Provider/index.js' | ||
|
||
const preview: Preview = { | ||
tags: ['autodocs'], | ||
parameters: { | ||
backgrounds: { | ||
default: 'system', | ||
values: [ | ||
{ | ||
name: 'system', | ||
value: 'var(--neutral-inverted-100)', | ||
}, | ||
], | ||
backgrounds: { disable: true }, | ||
viewport: { disable: true }, | ||
}, | ||
globalTypes: { | ||
theme: { | ||
defaultValue: 'dark', | ||
toolbar: { | ||
title: 'Theme', | ||
items: [ | ||
{ value: 'light', title: 'Light', icon: 'sun' }, | ||
{ value: 'dark', title: 'Dark', icon: 'moon' }, | ||
], | ||
dynamicTitle: true, | ||
}, | ||
}, | ||
}, | ||
decorators: (Story) => ( | ||
<KeyboardEventProvider> | ||
<Story /> | ||
</KeyboardEventProvider> | ||
), | ||
decorators: (Story, context) => { | ||
return ( | ||
<Provider forcedTheme={context.globals.theme}> | ||
<Story /> | ||
</Provider> | ||
) | ||
}, | ||
} | ||
|
||
export default preview |
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,19 @@ | ||
import { ReactNode } from 'react' | ||
import { KeyboardEventProvider } from '../../hooks/useHadKeyboardEvent.js' | ||
import { ThemeProvider, ThemeProviderProps } from '../../hooks/useTheme.js' | ||
|
||
type ProviderProps = { | ||
children: ReactNode | ||
forcedTheme: ThemeProviderProps['forcedTheme'] | ||
} | ||
|
||
function Provider({ children, forcedTheme }: ProviderProps) { | ||
return ( | ||
<ThemeProvider forcedTheme={forcedTheme}> | ||
<KeyboardEventProvider>{children}</KeyboardEventProvider> | ||
</ThemeProvider> | ||
) | ||
} | ||
|
||
export type { ProviderProps } | ||
export { Provider } |
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,58 @@ | ||
import { useState, useEffect, createContext, useContext } from 'react' | ||
|
||
const LOCAL_STORAGE_KEY = 'based-ui-theme' | ||
const DEFAULT_THEME: Theme = 'light' | ||
const ATTRIBUTE_NAME = 'data-theme' | ||
// This script is necessary because we want to set the data-theme attribute before the first render | ||
// so that light/dark does not flash | ||
// It should be placed somewhere in the body of the document, since it's sync it will stop the document parsing and so a render won't happen which is exactly what we are looking for | ||
export const THEME_PREVENT_FLASH_SCRIPT_SRC = `<script>!function(){try{var t=localStorage.getItem("${LOCAL_STORAGE_KEY}");document.documentElement.setAttribute("${ATTRIBUTE_NAME}",null!=t?t:"${DEFAULT_THEME}")}catch(t){}}();</script>` | ||
|
||
type Theme = 'light' | 'dark' | ||
|
||
type UseThemeProps = { | ||
theme: Theme | ||
setTheme: React.Dispatch<React.SetStateAction<Theme>> | ||
} | ||
|
||
const defaultContext = { | ||
setTheme: () => {}, | ||
theme: DEFAULT_THEME, | ||
} | ||
|
||
const ThemeContext = createContext<UseThemeProps>(defaultContext) | ||
|
||
export function useTheme() { | ||
return useContext(ThemeContext) | ||
} | ||
|
||
export type ThemeProviderProps = { | ||
children: React.ReactNode | ||
forcedTheme?: Theme // needed for storybook, where we don't control the theme switcher button | ||
} | ||
|
||
export function ThemeProvider({ children, forcedTheme }: ThemeProviderProps) { | ||
const [theme, setTheme] = useState(() => { | ||
let theme | ||
|
||
try { | ||
theme = localStorage.getItem(LOCAL_STORAGE_KEY) | ||
} catch {} | ||
|
||
return forcedTheme ?? ((theme ?? DEFAULT_THEME) as Theme) | ||
}) | ||
|
||
useEffect(() => { | ||
document.documentElement.setAttribute(ATTRIBUTE_NAME, forcedTheme ?? theme) | ||
|
||
try { | ||
localStorage.setItem(LOCAL_STORAGE_KEY, forcedTheme ?? theme) | ||
} catch {} | ||
}, [theme, forcedTheme]) | ||
|
||
return ( | ||
<ThemeContext.Provider value={{ theme, setTheme }}> | ||
{children} | ||
</ThemeContext.Provider> | ||
) | ||
} |
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