preview.tsx 5.7 KB

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