forked from chakra-ui/chakra-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.tsx
77 lines (69 loc) · 1.78 KB
/
preview.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
ChakraProvider,
extendTheme,
Flex,
IconButton,
useColorMode,
useColorModeValue,
} from "@chakra-ui/react"
import { Parameters, StoryContext } from "@storybook/react"
import * as React from "react"
import { FaMoon, FaSun } from "react-icons/fa"
import { withPerformance } from "storybook-addon-performance"
/**
* Add global context for RTL-LTR switching
*/
export const globalTypes = {
direction: {
name: "Direction",
description: "Direction for layout",
defaultValue: "LTR",
toolbar: {
icon: "globe",
items: ["LTR", "RTL"],
},
},
}
const ColorModeToggleBar = () => {
const { toggleColorMode } = useColorMode()
const SwitchIcon = useColorModeValue(FaMoon, FaSun)
const nextMode = useColorModeValue("dark", "light")
return (
<Flex justify="flex-end" mb={4}>
<IconButton
size="md"
fontSize="lg"
aria-label={`Switch to ${nextMode} mode`}
variant="ghost"
color="current"
marginLeft="2"
onClick={toggleColorMode}
icon={<SwitchIcon />}
/>
</Flex>
)
}
const withChakra = (StoryFn: Function, context: StoryContext) => {
const { direction } = context.globals
const dir = direction.toLowerCase()
React.useEffect(() => {
document.documentElement.dir = dir
}, [dir])
return (
<ChakraProvider theme={extendTheme({ direction: dir })}>
<div dir={dir} id="story-wrapper" style={{ minHeight: "100vh" }}>
<ColorModeToggleBar />
<StoryFn />
</div>
</ChakraProvider>
)
}
export const parameters: Parameters = {
options: {
storySort: (a, b) =>
a[1].kind === b[1].kind
? 0
: a[1].id.localeCompare(b[1].id, undefined, { numeric: true }),
},
}
export const decorators = [withChakra, withPerformance]