preview.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import 'focus-visible';
  2. import '../docs-ui/index.js';
  3. import {DocsContainer, Meta} from '@storybook/addon-docs';
  4. import {addDecorator, addParameters, DecoratorFn, Parameters} from '@storybook/react';
  5. import {ThemeProvider} from 'emotion-theming';
  6. import ColorChip from '../docs-ui/components/colorChip';
  7. import DocsLinks from '../docs-ui/components/docsLinks';
  8. import DoDont from '../docs-ui/components/doDont';
  9. import GlobalStyles from '../static/app/styles/global';
  10. import {darkTheme, lightTheme} from '../static/app/utils/theme';
  11. import PreviewGlobalStyles from './previewGlobalStyles';
  12. // Theme decorator for stories
  13. const withThemeStory: DecoratorFn = (Story, context) => {
  14. const isDark = context.globals.theme === 'dark';
  15. const currentTheme = isDark ? darkTheme : lightTheme;
  16. // Set @storybook/addon-backgrounds current color based on theme
  17. if (context.globals.theme) {
  18. context.globals.backgrounds = {value: currentTheme.bodyBackground};
  19. }
  20. return (
  21. <ThemeProvider theme={currentTheme}>
  22. <GlobalStyles isDark={isDark} theme={currentTheme} />
  23. <Story {...context} />
  24. </ThemeProvider>
  25. );
  26. };
  27. addDecorator(withThemeStory);
  28. // Theme decorator for MDX Docs
  29. const withThemeDocs: DecoratorFn = ({children, context}) => {
  30. const isDark = context.globals.theme === 'dark';
  31. const currentTheme = isDark ? darkTheme : lightTheme;
  32. // Set @storybook/addon-backgrounds current color based on theme
  33. if (context.globals.theme) {
  34. context.globals.backgrounds = {value: currentTheme.bodyBackground};
  35. }
  36. return (
  37. <ThemeProvider theme={currentTheme}>
  38. <GlobalStyles isDark={isDark} theme={currentTheme} />
  39. <PreviewGlobalStyles theme={currentTheme} />
  40. <DocsContainer context={context}>{children}</DocsContainer>
  41. </ThemeProvider>
  42. );
  43. };
  44. // Option defaults:
  45. addParameters({
  46. docs: {
  47. container: withThemeDocs,
  48. components: {Meta, ColorChip, DocsLinks, DoDont},
  49. },
  50. options: {
  51. /**
  52. * show story component as full screen
  53. * @type {Boolean}
  54. */
  55. isFullscreen: false,
  56. /**
  57. * display panel that shows a list of stories
  58. * @type {Boolean}
  59. */
  60. showNav: true,
  61. /**
  62. * display panel that shows addon configurations
  63. * @type {Boolean}
  64. */
  65. showPanel: true,
  66. /**
  67. * where to show the addon panel
  68. * @type {('bottom'|'right')}
  69. */
  70. panelPosition: 'bottom',
  71. /**
  72. * regex for finding the hierarchy separator
  73. * @example:
  74. * null - turn off hierarchy
  75. * /\// - split by `/`
  76. * /\./ - split by `.`
  77. * /\/|\./ - split by `/` or `.`
  78. * @type {Regex}
  79. */
  80. hierarchySeparator: /\/|\./,
  81. /**
  82. * regex for finding the hierarchy root separator
  83. * @example:
  84. * null - turn off multiple hierarchy roots
  85. * /\|/ - split by `|`
  86. * @type {Regex}
  87. */
  88. hierarchyRootSeparator: /\|/,
  89. /**
  90. * sidebar tree animat
  91. * ions
  92. * @type {Boolean}
  93. */
  94. sidebarAnimations: true,
  95. /**
  96. * enable/disable shortcuts
  97. * @type {Boolean}
  98. */
  99. enableShortcuts: true,
  100. /**
  101. * show/hide tool bar
  102. * @type {Boolean}
  103. */
  104. isToolshown: true,
  105. /**
  106. * function to sort stories in the tree view
  107. * common use is alphabetical `(a, b) => a[1].id.localeCompare(b[1].id)`
  108. * if left undefined, then the order in which the stories are imported will
  109. * be the order they display
  110. * @type {Function}
  111. */
  112. storySort: {
  113. order: [
  114. 'Core',
  115. ['Overview'],
  116. 'Assets',
  117. ['Logo', 'Icons', 'Platforms'],
  118. 'Components',
  119. [
  120. 'Buttons',
  121. 'Tables',
  122. 'Forms',
  123. 'Data Visualization',
  124. 'Alerts',
  125. 'Tags',
  126. 'Badges',
  127. 'Pills',
  128. 'Tooltips',
  129. 'Toast Indicators',
  130. 'Loading Indicators',
  131. 'Avatars',
  132. 'Context Data',
  133. 'Confirm',
  134. 'Well',
  135. ],
  136. 'Views',
  137. [
  138. 'Layout - Narrow',
  139. 'Layout - Thirds',
  140. 'Modals',
  141. 'Activity',
  142. 'Empty States',
  143. 'Not Available',
  144. 'Page Heading',
  145. 'Tabs',
  146. 'Breadcrumbs',
  147. 'Detailed Error',
  148. 'Onboarding Panel',
  149. ],
  150. 'Utilities',
  151. [
  152. 'Text',
  153. 'Copy',
  154. 'Clipboard',
  155. 'Highlight',
  156. 'Hidden Content',
  157. 'Lazy Load',
  158. 'Command Line',
  159. 'Get Dynamic Text',
  160. ],
  161. 'Features',
  162. 'Deprecated',
  163. ],
  164. },
  165. },
  166. });
  167. export const globalTypes = {
  168. theme: {
  169. name: 'Theme',
  170. description: 'Global theme for components',
  171. defaultValue: 'light',
  172. toolbar: {
  173. icon: 'circlehollow',
  174. // array of plain string values or MenuItem shape (see below)
  175. items: [
  176. {value: 'light', icon: 'circlehollow', title: 'light'},
  177. {value: 'dark', icon: 'circle', title: 'dark'},
  178. ],
  179. },
  180. },
  181. };
  182. export const parameters: Parameters = {
  183. /**
  184. * @storybook/addon-backgrounds background is controlled via theme
  185. */
  186. backgrounds: {
  187. grid: {
  188. disable: true,
  189. },
  190. default: 'light',
  191. values: [
  192. {
  193. name: 'light',
  194. value: lightTheme.background,
  195. },
  196. {
  197. name: 'dark',
  198. value: darkTheme.background,
  199. },
  200. ],
  201. },
  202. };