Skip to content

Commit

Permalink
feat(storybook): add support for feature flag toggle in toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblack committed Aug 1, 2024
1 parent b7c90ea commit 08e1041
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/react/.storybook/manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {addons, types} from '@storybook/addons'
import {useGlobals} from '@storybook/manager-api'
import {IconButton, WithTooltip, TooltipLinkList} from '@storybook/components'
import {BeakerIcon} from '@primer/octicons-react'
import React from 'react'
import {Tool, TOOL_ID, ADDON_ID} from './src/accessibility-tool'
import theme from './theme'

Expand All @@ -15,3 +19,54 @@ addons.register(ADDON_ID, () => {
render: Tool,
})
})

const featureFlagList = ['primer_react_action_list_item_as_button', 'primer_react_css_modules']

addons.register('FEATURE_FLAG_ADDON', () => {
addons.add('FEATURE_FLAG_ADDON/toolbar', {
type: types.TOOL,
match: ({tabId, viewMode}) => {
return !tabId && viewMode === 'story'
},
render: () => {
const [{featureFlags}, updateGlobals] = useGlobals()
const hasFeatureEnabled = Object.values(featureFlags ?? {}).find(value => {
return value
})
return (
<WithTooltip
placement="top"
trigger="click"
closeOnOutsideClick
tooltip={({onHide}) => {
return (
<TooltipLinkList
links={featureFlagList.map(featureFlag => {
const active = featureFlags?.[featureFlag]
return {
id: featureFlag,
title: active ? `✅ ${featureFlag}` : featureFlag,
active,
onClick: () => {
updateGlobals({
featureFlags: {
...featureFlags,
[featureFlag]: !active,
},
})
onHide()
},
}
})}
/>
)
}}
>
<IconButton active={hasFeatureEnabled} title="Toggle feature flags">
<BeakerIcon size={14} />
</IconButton>
</WithTooltip>
)
},
})
})
13 changes: 13 additions & 0 deletions packages/react/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,22 @@ export const globalTypes = {
},
showSurroundingElements: {},
},
featureFlags: {
name: 'Feature flags',
description: 'Toggle feature flags',
defaultValue: {},
},
}

export const decorators = [
(Story, context) => {
const {featureFlags} = context.globals
return (
<FeatureFlags flags={featureFlags}>
<Story {...context} />
</FeatureFlags>
)
},
(Story, context) => {
const {colorScheme} = context.globals

Expand Down

0 comments on commit 08e1041

Please sign in to comment.