|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + AddonPanel, |
| 4 | + Icons, |
| 5 | + IconButton, |
| 6 | + Placeholder, |
| 7 | + Link, |
| 8 | +} from '@storybook/components'; |
| 9 | +import {PureArgsTable} from '@storybook/blocks'; |
| 10 | +import {addons, types} from '@storybook/manager-api'; |
| 11 | +import {styled} from '@storybook/theming'; |
| 12 | +import {useGlobals} from '@storybook/api'; |
| 13 | + |
| 14 | +addons.register('global-controls', () => { |
| 15 | + addons.add('grid-options/panel', { |
| 16 | + type: types.PANEL, |
| 17 | + title: 'Grid', |
| 18 | + match: ({viewMode}) => viewMode === 'story', |
| 19 | + render: ({active, key}) => <GridPanel active={active} key={key} />, |
| 20 | + }); |
| 21 | + addons.add('feature-flags/panel', { |
| 22 | + type: types.PANEL, |
| 23 | + title: 'Feature flags', |
| 24 | + match: ({viewMode}) => viewMode === 'story', |
| 25 | + render: ({active, key}) => <FeatureFlagPanel active={active} key={key} />, |
| 26 | + }); |
| 27 | + addons.add('github/link', { |
| 28 | + type: types.TOOLEXTRA, |
| 29 | + title: 'GitHub', |
| 30 | + match: ({viewMode}) => viewMode === 'story' || viewMode === 'docs', |
| 31 | + render: GitHubToolbar, |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +export const featureFlagOptions = { |
| 36 | + // For example: |
| 37 | + // polarisSummerEditions2023: { // This name must match the name accepted by `AppProvider#features` |
| 38 | + // name: 'polarisSummerEditions2023', |
| 39 | + // description: 'Toggle the summer editions feature flag', |
| 40 | + // defaultValue: false, |
| 41 | + // control: {type: 'boolean'}, |
| 42 | + // }, |
| 43 | +}; |
| 44 | + |
| 45 | +export const gridOptions = { |
| 46 | + showGrid: { |
| 47 | + name: 'Show grid overlay', |
| 48 | + description: 'Show or hide a 4 / 12 column grid, overlaying components', |
| 49 | + defaultValue: false, |
| 50 | + control: {type: 'boolean'}, |
| 51 | + }, |
| 52 | + gridInFrame: { |
| 53 | + name: 'Grid in frame', |
| 54 | + description: 'Show grid within app frame context', |
| 55 | + defaultValue: false, |
| 56 | + control: {type: 'boolean'}, |
| 57 | + }, |
| 58 | + gridWidth: { |
| 59 | + name: 'Grid width', |
| 60 | + description: 'Set a max width for the grid overlay', |
| 61 | + default: '100%', |
| 62 | + control: {type: 'select'}, |
| 63 | + options: ['560px', '768px', '1008px', '100%'], |
| 64 | + }, |
| 65 | + gridLayer: { |
| 66 | + name: 'Grid layer', |
| 67 | + description: 'Set the grid layer above or below content', |
| 68 | + default: 'below', |
| 69 | + control: {type: 'select'}, |
| 70 | + options: ['above', 'below'], |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
| 74 | +function GridPanel(props) { |
| 75 | + const [globals, updateGlobals] = useGlobals(); |
| 76 | + |
| 77 | + return ( |
| 78 | + <AddonPanel {...props}> |
| 79 | + <PureArgsTable |
| 80 | + inAddonPanel |
| 81 | + rows={gridOptions} |
| 82 | + args={globals} |
| 83 | + updateArgs={updateGlobals} |
| 84 | + /> |
| 85 | + </AddonPanel> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +function FeatureFlagPanel(props) { |
| 90 | + const [globals, updateGlobals] = useGlobals(); |
| 91 | + return ( |
| 92 | + <AddonPanel {...props}> |
| 93 | + {Object.keys(featureFlagOptions).length ? ( |
| 94 | + <PureArgsTable |
| 95 | + inAddonPanel |
| 96 | + rows={featureFlagOptions} |
| 97 | + args={globals} |
| 98 | + updateArgs={updateGlobals} |
| 99 | + /> |
| 100 | + ) : ( |
| 101 | + <Placeholder> |
| 102 | + <p>No feature flags configured</p> |
| 103 | + <p> |
| 104 | + <Link |
| 105 | + withArrow |
| 106 | + href="https://github.com/search?q=repo%3AShopify%2Fpolaris+featureFlagOptions+path%3Apolaris-react%2F.storybook%2Faddons%2Fglobal-controls-panel%2Fmanager.jsx&type=code" |
| 107 | + > |
| 108 | + Edit configuration |
| 109 | + </Link> |
| 110 | + </p> |
| 111 | + </Placeholder> |
| 112 | + )} |
| 113 | + </AddonPanel> |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +const IconButtonLink = styled(IconButton)({ |
| 118 | + textDecoration: 'none', |
| 119 | +}); |
| 120 | + |
| 121 | +function GitHubToolbar() { |
| 122 | + let link; |
| 123 | + let title; |
| 124 | + let buttonText; |
| 125 | + // All of the storybook manager leverages the `@storybook/builder-manager` package, which builds with esbuild under the hood, and not vite. |
| 126 | + // This means that we can't use the `import.meta.env` syntax to access environment variables. |
| 127 | + // See https://github.com/storybookjs/storybook/blob/d112cbac280f23530b366918b9d54818006db963/code/builders/builder-manager/src/index.ts#L103 |
| 128 | + if (process.env.STORYBOOK_GITHUB_REPO_URL) { |
| 129 | + link = `${process.env.STORYBOOK_GITHUB_REPO_URL}`; |
| 130 | + title = 'View source on GitHub'; |
| 131 | + buttonText = 'GitHub'; |
| 132 | + if (process.env.STORYBOOK_GITHUB_PR) { |
| 133 | + link = `${link}/pull/${process.env.STORYBOOK_GITHUB_PR}`; |
| 134 | + title = `Built from PR #${process.env.STORYBOOK_GITHUB_PR}`; |
| 135 | + buttonText = `PR #${process.env.STORYBOOK_GITHUB_PR}`; |
| 136 | + } else if (process.env.STORYBOOK_GITHUB_SHA) { |
| 137 | + link = `${link}/commit/${process.env.STORYBOOK_GITHUB_SHA}`; |
| 138 | + title = `Built from commit ${process.env.STORYBOOK_GITHUB_SHA}`; |
| 139 | + buttonText = <code>{process.env.STORYBOOK_GITHUB_SHA.slice(0, 7)}</code>; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return link ? ( |
| 144 | + <IconButtonLink as="a" href={link} title={title} target="_blank"> |
| 145 | + <Icons icon="github" /> |
| 146 | + {buttonText} |
| 147 | + </IconButtonLink> |
| 148 | + ) : null; |
| 149 | +} |
0 commit comments