-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(theming): add ColorSchemeProvider
#1991
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d35b353
feat(theming): add `initialColorScheme` to `ThemeProvider` to configuβ¦
jzempel 7134a0b
Apply background/foreground colors whether or not CSS bedrock is linked
jzempel 2a17668
Fill out `ThemeProvider` storybook demo
jzempel b01aa34
Add unit tests
jzempel 8914ee0
Merge branch 'main' into jzempel/color-scheme
jzempel 38e00df
Merge branch 'main' into jzempel/color-scheme
jzempel a13a1e0
Restructure to expose `ColorSchemeProvider` for consumer use
jzempel 4d55e11
Update README
jzempel 41addfc
Update prop description
jzempel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize this is currently empty β the
Story
was needed to get generated API docs. We can fill it with something meaningful in the future if/when π‘ strikes