-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theming): add
ColorSchemeProvider
(#1991)
...and associated `useColorScheme` hook
- Loading branch information
Showing
11 changed files
with
461 additions
and
34 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
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,22 @@ | ||
import { Meta, ArgsTable, Canvas, Story, Markdown } from '@storybook/addon-docs'; | ||
import { ColorSchemeProvider } from '@zendeskgarden/react-theming'; | ||
import { ColorSchemeProviderStory } from './stories/ColorSchemeProviderStory'; | ||
import README from '../README.md'; | ||
|
||
<Meta title="Packages/Theming/ColorSchemeProvider" component={ColorSchemeProvider} /> | ||
|
||
# API | ||
|
||
<ArgsTable /> | ||
|
||
# Demo | ||
|
||
## ColorSchemeProvider | ||
|
||
<Canvas> | ||
<Story name="ColorSchemeProvider" args={{ initialColorScheme: 'system' }}> | ||
{args => <ColorSchemeProviderStory {...args} />} | ||
</Story> | ||
</Canvas> | ||
|
||
<Markdown>{README}</Markdown> |
131 changes: 131 additions & 0 deletions
131
packages/theming/demo/stories/ColorSchemeProviderStory.tsx
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,131 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import styled, { ThemeProvider, useTheme } from 'styled-components'; | ||
import { StoryFn } from '@storybook/react'; | ||
import ClearIcon from '@zendeskgarden/svg-icons/src/16/x-stroke.svg'; | ||
import DarkIcon from '@zendeskgarden/svg-icons/src/16/moon-stroke.svg'; | ||
import LightIcon from '@zendeskgarden/svg-icons/src/16/sun-stroke.svg'; | ||
import SystemIcon from '@zendeskgarden/svg-icons/src/16/monitor-stroke.svg'; | ||
import { | ||
ColorScheme, | ||
ColorSchemeProvider, | ||
getColor, | ||
IColorSchemeProviderProps, | ||
IGardenTheme, | ||
useColorScheme, | ||
useWindow | ||
} from '@zendeskgarden/react-theming'; | ||
import { Grid } from '@zendeskgarden/react-grid'; | ||
import { IconButton } from '@zendeskgarden/react-buttons'; | ||
import { IMenuProps, Item, ItemGroup, Menu } from '@zendeskgarden/react-dropdowns'; | ||
import { Field, Input } from '@zendeskgarden/react-forms'; | ||
import { Code } from '@zendeskgarden/react-typography'; | ||
import { Tooltip } from '@zendeskgarden/react-tooltips'; | ||
|
||
const StyledGrid = styled(Grid)` | ||
background-color: ${p => getColor({ theme: p.theme, variable: 'background.default' })}; | ||
`; | ||
|
||
const StyledIconButton = styled(IconButton)` | ||
position: absolute; | ||
right: ${p => p.theme.space.base * 3}px; | ||
bottom: ${p => p.theme.space.base}px; | ||
`; | ||
|
||
const Content = ({ | ||
colorSchemeKey = 'color-scheme' | ||
}: { | ||
colorSchemeKey: IColorSchemeProviderProps['colorSchemeKey']; | ||
}) => { | ||
const win = useWindow(); | ||
const localStorage = win?.localStorage; | ||
const { colorScheme, isSystem, setColorScheme } = useColorScheme(); | ||
const [inputValue, setInputValue] = useState(''); | ||
const _theme = useTheme() as IGardenTheme; | ||
const theme = { ..._theme, colors: { ..._theme.colors, base: colorScheme } }; | ||
|
||
const handleChange: IMenuProps['onChange'] = changes => { | ||
if (changes.value) { | ||
setColorScheme(changes.value as ColorScheme); | ||
} | ||
}; | ||
|
||
const handleClear = () => { | ||
localStorage?.removeItem(colorSchemeKey); | ||
setInputValue(''); | ||
}; | ||
|
||
useEffect(() => { | ||
setInputValue(localStorage?.getItem(colorSchemeKey) || ''); | ||
}, [colorSchemeKey, colorScheme, isSystem, localStorage]); | ||
|
||
return ( | ||
<ThemeProvider theme={theme}> | ||
<StyledGrid gutters="xl"> | ||
<Grid.Row style={{ height: 'calc(100vh - 80px)' }}> | ||
<Grid.Col alignSelf="center" sm={5}> | ||
<div style={{ position: 'relative' }}> | ||
<Field> | ||
<Field.Label> | ||
Local {!!colorSchemeKey && <Code>{colorSchemeKey}</Code>} storage | ||
</Field.Label> | ||
<Input placeholder="unspecified" readOnly value={inputValue} /> | ||
{!!inputValue && ( | ||
<Tooltip content={`Clear ${colorSchemeKey} storage`}> | ||
<StyledIconButton focusInset onClick={handleClear} size="small"> | ||
<ClearIcon /> | ||
</StyledIconButton> | ||
</Tooltip> | ||
)} | ||
</Field> | ||
</div> | ||
</Grid.Col> | ||
<Grid.Col textAlign="center" alignSelf="center"> | ||
<Menu | ||
/* eslint-disable-next-line react/no-unstable-nested-components */ | ||
button={props => ( | ||
<IconButton {...props}> | ||
{theme.colors.base === 'dark' ? <DarkIcon /> : <LightIcon />} | ||
</IconButton> | ||
)} | ||
onChange={handleChange} | ||
placement="bottom-end" | ||
selectedItems={[{ value: isSystem ? 'system' : colorScheme }]} | ||
> | ||
<ItemGroup type="radio"> | ||
<Item icon={<LightIcon />} value="light"> | ||
Light | ||
</Item> | ||
<Item icon={<DarkIcon />} value="dark"> | ||
Dark | ||
</Item> | ||
<Item icon={<SystemIcon />} isSelected value="system"> | ||
System | ||
</Item> | ||
</ItemGroup> | ||
</Menu> | ||
</Grid.Col> | ||
</Grid.Row> | ||
</StyledGrid> | ||
</ThemeProvider> | ||
); | ||
}; | ||
|
||
export const ColorSchemeProviderStory: StoryFn<IColorSchemeProviderProps> = ({ | ||
colorSchemeKey, | ||
initialColorScheme | ||
}) => ( | ||
<ColorSchemeProvider | ||
key={initialColorScheme} | ||
colorSchemeKey={colorSchemeKey} | ||
initialColorScheme={initialColorScheme} | ||
> | ||
<Content colorSchemeKey={colorSchemeKey} /> | ||
</ColorSchemeProvider> | ||
); |
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,43 @@ | ||
import { Meta, ArgsTable, Canvas, Story, Markdown } from '@storybook/addon-docs'; | ||
import { ThemeProvider, DEFAULT_THEME, PALETTE } from '@zendeskgarden/react-theming'; | ||
import { PaletteStory } from './stories/PaletteStory'; | ||
import README from '../README.md'; | ||
|
||
<Meta | ||
title="Packages/Theming/ThemeProvider" | ||
component={ThemeProvider} | ||
subcomponents={{ DEFAULT_THEME, PALETTE }} | ||
/> | ||
|
||
# API | ||
|
||
<ArgsTable /> | ||
|
||
# Demo | ||
|
||
## ThemeProvider | ||
|
||
<Canvas> | ||
<Story name="ThemeProvider" args={{ theme: DEFAULT_THEME }}> | ||
{args => <ThemeProvider {...args} />} | ||
</Story> | ||
</Canvas> | ||
|
||
## PALETTE | ||
|
||
<Canvas> | ||
<Story | ||
name="PALETTE" | ||
args={{ palette: PALETTE }} | ||
argTypes={{ | ||
palette: { control: { type: 'object' }, name: 'PALETTE' }, | ||
colorSchemeKey: { table: { disable: true } }, | ||
initialColorScheme: { table: { disable: true } }, | ||
theme: { table: { disable: true } } | ||
}} | ||
> | ||
{args => <PaletteStory {...args} />} | ||
</Story> | ||
</Canvas> | ||
|
||
<Markdown>{README}</Markdown> |
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.