forked from microsoft/fluentui
-
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(docs): Adding global direction switch to dynamically change betw…
…een ltr and rtl in storybook (microsoft#29954) * feat(docs): Adding global direction switch to dynamically change between ltr and rtl in storybook. * Updating API. * Moving dir switch to the same line as theme picker.
- Loading branch information
Showing
7 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
apps/public-docsite-v9/src/DocsComponents/DirSwitch.stories.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,50 @@ | ||
import * as React from 'react'; | ||
import { makeStyles, Label, Switch, useId, typographyStyles } from '@fluentui/react-components'; | ||
import type { SwitchProps } from '@fluentui/react-components'; | ||
import { DIR_ID } from '@fluentui/react-storybook-addon'; | ||
import addons from '@storybook/addons'; | ||
|
||
const useStyles = makeStyles({ | ||
container: { | ||
alignItems: 'center', | ||
display: 'flex', | ||
justifyContent: 'start', | ||
}, | ||
label: { | ||
...typographyStyles.subtitle2, | ||
}, | ||
}); | ||
|
||
/** | ||
* Dir switch used in the react-components docs header | ||
*/ | ||
export const DirSwitch: React.FC<{ dir?: 'ltr' | 'rtl' }> = ({ dir }) => { | ||
const switchId = useId('dir-switch'); | ||
|
||
const styles = useStyles(); | ||
|
||
const [currentDir, setCurrentDir] = React.useState(dir); | ||
const checked = currentDir === 'rtl'; | ||
|
||
const setGlobalDir = (newDir: 'ltr' | 'rtl'): void => { | ||
addons.getChannel().emit('updateGlobals', { globals: { [DIR_ID]: newDir } }); | ||
}; | ||
|
||
const onChange = React.useCallback<NonNullable<SwitchProps['onChange']>>((_, data) => { | ||
const newDir = data.checked ? 'rtl' : 'ltr'; | ||
setGlobalDir(newDir); | ||
setCurrentDir(newDir); | ||
}, []); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Label className={styles.label} htmlFor={currentDir === 'ltr' ? undefined : switchId}> | ||
LTR | ||
</Label> | ||
<Switch checked={checked} id={switchId} onChange={onChange} /> | ||
<Label className={styles.label} htmlFor={currentDir === 'rtl' ? switchId : undefined}> | ||
RTL | ||
</Label> | ||
</div> | ||
); | ||
}; |
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
3 changes: 2 additions & 1 deletion
3
packages/react-components/react-storybook-addon/src/constants.ts
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,4 +1,5 @@ | ||
export const ADDON_ID = 'storybook_fluentui-react-addon'; | ||
export const THEME_ID = `${ADDON_ID}_theme` as const; | ||
|
||
export const DIR_ID = `${ADDON_ID}_dir` as const; | ||
export const STRICT_MODE_ID = `${ADDON_ID}_strict-mode` as const; | ||
export const THEME_ID = `${ADDON_ID}_theme` as const; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type { FluentGlobals, FluentParameters, FluentStoryContext } from './hooks'; | ||
export type { ThemeIds } from './theme'; | ||
export { themes } from './theme'; | ||
export { THEME_ID } from './constants'; | ||
export { DIR_ID, THEME_ID } from './constants'; | ||
export { parameters } from './hooks'; |