-
Notifications
You must be signed in to change notification settings - Fork 2.9k
chore(storybook): Add theme picker to storybook toolbar #20346
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
Merged
ling1726
merged 24 commits into
microsoft:master
from
ling1726:chore/storybook-toolbar-theme
Nov 2, 2021
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ec117c0
experiment(storybook): Version picker
ling1726 85e17c3
Change files
ling1726 a8db329
use fluent menu
ling1726 8dcc438
cleanup
ling1726 153d565
add network fetch
ling1726 c00b676
use addon
ling1726 4afccab
remove old
ling1726 f26fb4c
update deps
ling1726 5a797d9
update stypes
ling1726 89001c0
update md
ling1726 b9e145c
remove export
ling1726 e046069
Merge branch 'master' into experiment/storybook-version-picker
ling1726 75bc101
chore(storybook): Add theme picker to storybook toolbar
ling1726 e79117f
remove unnecessary changes
ling1726 e72aead
update change
ling1726 69f0aee
remove font family style
ling1726 06b0056
pr suggestions
ling1726 1f4b141
merge master
ling1726 fe01f37
re-show the other items in the toolbar
ling1726 4cdce66
revert export
ling1726 c98631c
update readme
ling1726 9a54f98
remove usage of make-styles
ling1726 dfad2c2
update syncpack
ling1726 b84dd0e
update md
ling1726 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-components-639d9023-4a53-403d-a3eb-fa50ca331ac6.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "type": "none", | ||
| "comment": "chore(storybook): Theme picker as storybook addon", | ||
| "packageName": "@fluentui/react-components", | ||
| "email": "lingfangao@hotmail.com", | ||
| "dependentChangeType": "none" | ||
| } |
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Empty file.
76 changes: 76 additions & 0 deletions
76
packages/react-storybook-addon/src/components/ThemePicker.tsx
This file contains hidden or 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,76 @@ | ||
| import * as React from 'react'; | ||
| import { IconButton, Icons, TooltipLinkList, WithTooltip } from '@storybook/components'; | ||
|
|
||
| import { ThemeIds, themes, defaultTheme } from '../theme'; | ||
| import { THEME_ID } from '../constants'; | ||
| import { useGlobals } from '../hooks'; | ||
|
|
||
| export interface ThemeSelectorItem { | ||
| id: string; | ||
| title: string; | ||
| onClick: () => void; | ||
| value: string; | ||
| active: boolean; | ||
| } | ||
|
|
||
| function createThemeItems( | ||
| value: typeof themes, | ||
| changeTheme: (id: ThemeIds) => void, | ||
| getCurrentTheme: () => ThemeIds, | ||
| ): ThemeSelectorItem[] { | ||
| return value.map(item => { | ||
| return { | ||
| id: item.id, | ||
| title: item.id === defaultTheme.id ? `${item.label} (Default)` : item.label, | ||
| onClick: () => { | ||
| changeTheme(item.id); | ||
| }, | ||
| value: item.id, | ||
| active: getCurrentTheme() === item.id, | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| export const ThemePicker = () => { | ||
| const [globals, updateGlobals] = useGlobals(); | ||
| const selectedThemeId = globals[THEME_ID] ?? defaultTheme.id; | ||
| const selectedTheme = themes.find(entry => entry.id === selectedThemeId); | ||
|
|
||
| const isActive = selectedThemeId !== defaultTheme.id; | ||
|
|
||
| const setTheme = React.useCallback( | ||
| (id: ThemeIds) => { | ||
| updateGlobals({ [THEME_ID]: id }); | ||
| }, | ||
| [updateGlobals], | ||
| ); | ||
|
|
||
| const renderTooltip = React.useCallback( | ||
| (props: { onHide: () => void }) => { | ||
| return ( | ||
| <TooltipLinkList | ||
| links={createThemeItems( | ||
| themes, | ||
| id => { | ||
| setTheme(id); | ||
| props.onHide(); | ||
| }, | ||
| () => selectedThemeId, | ||
| )} | ||
| /> | ||
| ); | ||
| }, | ||
| [selectedThemeId, setTheme], | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| <WithTooltip placement="top" trigger="click" closeOnClick tooltip={renderTooltip}> | ||
| <IconButton key={THEME_ID} title="Change Fluent theme" active={isActive}> | ||
| <Icons icon="chevrondown" /> | ||
| <span style={{ marginLeft: 5 }}>Theme: {selectedTheme?.label}</span> | ||
| </IconButton> | ||
| </WithTooltip> | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or 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 +1,2 @@ | ||
| // @TODO - add addon constants | ||
| export const ADDON_ID = 'storybook/fluentui-react-addon'; | ||
| export const THEME_ID = `${ADDON_ID}/theme` as const; |
32 changes: 32 additions & 0 deletions
32
packages/react-storybook-addon/src/decorators/withFluentProvider.tsx
This file contains hidden or 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,32 @@ | ||
| import * as React from 'react'; | ||
| import { StoryFn as StoryFunction } from '@storybook/addons'; | ||
|
|
||
| import { themes, defaultTheme, FluentProvider } from '../theme'; | ||
| import { THEME_ID } from '../constants'; | ||
| import { FluentGlobals, FluentStoryContext } from '../hooks'; | ||
|
|
||
| import { Theme } from '@fluentui/react-theme'; | ||
|
|
||
| const getActiveFluentTheme = (globals: FluentGlobals) => { | ||
| const selectedThemeId = globals[THEME_ID]; | ||
| const { theme } = themes.find(value => value.id === selectedThemeId) ?? defaultTheme; | ||
|
|
||
| return { theme }; | ||
| }; | ||
|
|
||
| export const withFluentProvider = (StoryFn: StoryFunction<React.ReactElement>, context: FluentStoryContext) => { | ||
| const { theme } = getActiveFluentTheme(context.globals); | ||
|
|
||
| return ( | ||
| <FluentProvider theme={theme}> | ||
| <FluentExampleContainer theme={theme}>{StoryFn()}</FluentExampleContainer> | ||
| </FluentProvider> | ||
| ); | ||
| }; | ||
|
|
||
| const FluentExampleContainer: React.FC<{ theme: Theme }> = props => { | ||
| const { theme } = props; | ||
|
|
||
| const backgroundColor = theme.colorNeutralBackground1; | ||
| return <div style={{ padding: 10, backgroundColor: backgroundColor }}>{props.children}</div>; | ||
| }; | ||
This file contains hidden or 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,20 @@ | ||
| import { useGlobals as useStorybookGlobals, Args as StorybookArgs } from '@storybook/api'; | ||
| import { StoryContext as StorybookContext } from '@storybook/addons'; | ||
|
|
||
| import { THEME_ID } from './constants'; | ||
| import { ThemeIds } from './theme'; | ||
|
|
||
| export interface FluentStoryContext extends StorybookContext { | ||
| globals: FluentGlobals; | ||
| } | ||
|
|
||
| /** | ||
| * Extends the storybook globals object to include fluent specific propoerties | ||
| */ | ||
| export interface FluentGlobals extends StorybookArgs { | ||
| [THEME_ID]?: ThemeIds; | ||
| } | ||
|
|
||
| export function useGlobals(): [FluentGlobals, (newGlobals: FluentGlobals) => void] { | ||
| return useStorybookGlobals(); | ||
| } |
This file contains hidden or 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 +1,13 @@ | ||
| // @TODO - Register the addon | ||
| import { addons, types } from '@storybook/addons'; | ||
|
|
||
| import { ADDON_ID, THEME_ID } from '../constants'; | ||
| import { ThemePicker } from '../components/ThemePicker'; | ||
|
|
||
| addons.register(ADDON_ID, () => { | ||
| addons.add(THEME_ID, { | ||
| title: 'Fluent Theme Picker', | ||
| type: types.TOOL, | ||
| match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)), | ||
| render: ThemePicker, | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,27 @@ | ||
| import { | ||
| webLightTheme, | ||
| webDarkTheme, | ||
| webHighContrastTheme, | ||
| teamsLightTheme, | ||
| teamsDarkTheme, | ||
| teamsHighContrastTheme, | ||
| Theme, | ||
| } from '@fluentui/react-theme'; | ||
|
|
||
| export { FluentProvider } from '@fluentui/react-provider'; | ||
|
|
||
| export const themes = [ | ||
| { id: 'web-light', label: 'Web Light', theme: webLightTheme }, | ||
| { id: 'web-dark', label: 'Web Dark', theme: webDarkTheme }, | ||
| { id: 'web-high-contrast', label: 'Web High Contrast', theme: webHighContrastTheme }, | ||
| { id: 'teams-light', label: 'Teams Light', theme: teamsLightTheme }, | ||
| { id: 'teams-dark', label: 'Teams Dark', theme: teamsDarkTheme }, | ||
| { id: 'teams-high-contrast', label: 'Teams High Contrast', theme: teamsHighContrastTheme }, | ||
| ] as const; | ||
|
|
||
| export const defaultTheme = themes[0]; | ||
|
|
||
| export type ThemeIds = typeof themes[number]['id']; | ||
| export type ThemeLabels = typeof themes[number]['label']; | ||
|
|
||
| export { Theme }; |
This file contains hidden or 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 hidden or 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 hidden or 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,2 +1 @@ | ||
| export * from './withFluentProvider'; | ||
| export * from './withStrictMode'; |
20 changes: 0 additions & 20 deletions
20
packages/react-storybook/src/decorators/withFluentProvider.tsx
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,2 +1,2 @@ | ||
| export * from './decorators/index'; | ||
| export { withFluentProvider, withStrictMode } from '@fluentui/react-storybook'; | ||
|
ling1726 marked this conversation as resolved.
|
||
| export { withStrictMode } from '@fluentui/react-storybook'; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.