Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storybook): add theme toggle to storybook #18123

Merged
merged 13 commits into from
Oct 26, 2023
Merged
20 changes: 0 additions & 20 deletions .storybook/decorators/with3000.tsx

This file was deleted.

8 changes: 4 additions & 4 deletions .storybook/decorators/withFeatureFlags.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useFeatureFlags } from '~/mocks/browser'
import type { DecoratorFn } from '@storybook/react'
import { setFeatureFlags } from '~/mocks/browser'
import type { Decorator } from '@storybook/react'

/** Global story decorator that allows setting feature flags.
*
Expand All @@ -13,9 +13,9 @@ import type { DecoratorFn } from '@storybook/react'
* } as ComponentMeta<typeof MyComponent>
* ```
*/
export const withFeatureFlags: DecoratorFn = (Story, { parameters }) => {
export const withFeatureFlags: Decorator = (Story, { parameters }) => {
if (parameters.featureFlags) {
useFeatureFlags(parameters.featureFlags)
setFeatureFlags(parameters.featureFlags)
}

return <Story />
Expand Down
4 changes: 2 additions & 2 deletions .storybook/decorators/withKea/withKea.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { DecoratorFn } from '@storybook/react'
import type { Decorator } from '@storybook/react'
import { useAvailableFeatures } from '~/mocks/features'

import { KeaStory } from './kea-story'

export const withKea: DecoratorFn = (Story) => {
export const withKea: Decorator = (Story) => {
// Reset enabled enterprise features. Overwrite this line within your stories.
useAvailableFeatures([])
return (
Expand Down
4 changes: 2 additions & 2 deletions .storybook/decorators/withMockDate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DecoratorFn } from '@storybook/react'
import type { Decorator } from '@storybook/react'
import MockDate from 'mockdate'

/** Global story decorator that allows mocking of dates.
Expand All @@ -13,7 +13,7 @@ import MockDate from 'mockdate'
* } as ComponentMeta<typeof MyComponent>
* ```
*/
export const withMockDate: DecoratorFn = (Story, { parameters }) => {
export const withMockDate: Decorator = (Story, { parameters }) => {
if (parameters.mockDate) {
MockDate.set(parameters.mockDate)
} else {
Expand Down
4 changes: 2 additions & 2 deletions .storybook/decorators/withSnapshotsDisabled.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DecoratorFn } from '@storybook/react'
import { Decorator } from '@storybook/react'
import { inStorybookTestRunner } from 'lib/utils'

/** Workaround for https://github.com/storybookjs/test-runner/issues/74 */
// TODO: Smoke-test all the stories by removing this decorator, once all the stories pass
export const withSnapshotsDisabled: DecoratorFn = (Story, { parameters }) => {
export const withSnapshotsDisabled: Decorator = (Story, { parameters }) => {
if (parameters?.testOptions?.skip && inStorybookTestRunner()) {
return <>Disabled for Test Runner</>
}
Expand Down
44 changes: 44 additions & 0 deletions .storybook/decorators/withTheme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Decorator } from '@storybook/react'

import { FEATURE_FLAGS } from 'lib/constants'

/** Global story decorator that is used by the theming control to
* switch between themes.
*/
export const withTheme: Decorator = (Story, context) => {
const theme = context.globals.theme

// set the body class
const actualClassState = document.body.classList.contains('posthog-3000')
const desiredClassState = theme !== 'legacy'

if (actualClassState !== desiredClassState) {
if (desiredClassState) {
document.body.classList.add('posthog-3000')
} else {
document.body.classList.remove('posthog-3000')
}
}

// set the feature flag
const actualFeatureFlagState = window.POSTHOG_APP_CONTEXT!.persisted_feature_flags?.includes(
FEATURE_FLAGS.POSTHOG_3000
)
const desiredFeatureFlagState = theme !== 'legacy'

if (actualFeatureFlagState !== desiredFeatureFlagState) {
const currentFlags = window.POSTHOG_APP_CONTEXT!.persisted_feature_flags || []
if (desiredFeatureFlagState) {
window.POSTHOG_APP_CONTEXT!.persisted_feature_flags = [...currentFlags, FEATURE_FLAGS.POSTHOG_3000]
} else {
window.POSTHOG_APP_CONTEXT!.persisted_feature_flags = currentFlags.filter(
(f) => f !== FEATURE_FLAGS.POSTHOG_3000
)
}
}

// set the theme
document.body.setAttribute('theme', theme === 'dark' ? 'dark' : 'light')

return <Story />
}
19 changes: 19 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { withMockDate } from './decorators/withMockDate'
import { defaultMocks } from '~/mocks/handlers'
import { withSnapshotsDisabled } from './decorators/withSnapshotsDisabled'
import { withFeatureFlags } from './decorators/withFeatureFlags'
import { withTheme } from './decorators/withTheme'

const setupMsw = () => {
// Make sure the msw worker is started
Expand Down Expand Up @@ -86,6 +87,8 @@ export const decorators: Meta['decorators'] = [
withMockDate,
// Allow us to easily set feature flags in stories.
withFeatureFlags,
// Set theme from global context
withTheme,
]

const preview: Preview = {
Expand All @@ -110,6 +113,22 @@ const preview: Preview = {
),
},
},
globalTypes: {
theme: {
description: '',
defaultValue: 'legacy',
toolbar: {
title: 'Theme',
items: [
{ value: 'legacy', icon: 'faceneutral', title: 'Legacy' },
{ value: 'light', icon: 'sun', title: 'Light' },
{ value: 'dark', icon: 'moon', title: 'Dark' },
],
// change the title based on the selected value
dynamicTitle: true,
},
},
},
}

export default preview
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this needs to be changed (or removed) as we now do this via the theme picker?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think for the purpose of snapshots we still need the decorator.

BTW looks like the now-unused dark mode and light mode snapshots weren't deleted, we might want to do some pruning of unused snapshots at some point. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'd like to address this by adding an option to testOptions where you can specify wether "legacy", "3000" or "all" themes should be captured.

I was hoping Storybook would provide a way to set globals for a specific story, so that we could override the theme there, but that doesn't seem to be the case.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/__snapshots__/scenes-app-notebooks--bullet-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/__snapshots__/scenes-app-notebooks--empty-notebook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/__snapshots__/scenes-app-notebooks--headings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/__snapshots__/scenes-app-notebooks--notebooks-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/__snapshots__/scenes-app-notebooks--numbered-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified frontend/__snapshots__/scenes-app-notebooks--text-formats.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, StoryFn, StoryObj } from '@storybook/react'
import { FeaturePreviewsModal as FeaturePreviewsModalComponent } from './FeaturePreviewsModal'
import { useFeatureFlags, useStorybookMocks } from '~/mocks/browser'
import { setFeatureFlags, useStorybookMocks } from '~/mocks/browser'
import { EarlyAccessFeature } from 'posthog-js'
import { CONSTRAINED_PREVIEWS } from './featurePreviewsLogic'
import { FeatureFlagKey } from 'lib/constants'
Expand Down Expand Up @@ -28,7 +28,7 @@ const Template: StoryFn<StoryProps> = ({ earlyAccessFeatures, enabledFeatureFlag
'https://app.posthog.com/api/early_access_features/': { earlyAccessFeatures },
},
})
useFeatureFlags(enabledFeatureFlags)
setFeatureFlags(enabledFeatureFlags)

return (
<div className="bg-default p-2">
Expand Down
18 changes: 1 addition & 17 deletions frontend/src/layout/navigation-3000/Navigation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { router } from 'kea-router'
import { urls } from 'scenes/urls'
import { App } from 'scenes/App'
import { EMPTY_PAGINATED_RESPONSE } from '~/mocks/handlers'
import { useActions } from 'kea'
import { themeLogic } from './themeLogic'
import { with3000 } from 'storybook/decorators/with3000'

const meta: Meta = {
title: 'PostHog 3000/Navigation',
Expand All @@ -21,7 +18,6 @@ const meta: Meta = {
'/api/projects/:team_id/session_recordings/': EMPTY_PAGINATED_RESPONSE,
},
}),
with3000,
],
parameters: {
layout: 'fullscreen',
Expand All @@ -30,21 +26,9 @@ const meta: Meta = {
},
}
export default meta
export function LightMode(): JSX.Element {
const { overrideTheme } = useActions(themeLogic)
export function Navigation(): JSX.Element {
useEffect(() => {
router.actions.push(urls.projectHomepage())
overrideTheme(false)
}, [])

return <App />
}

export function DarkMode(): JSX.Element {
const { overrideTheme } = useActions(themeLogic)
useEffect(() => {
router.actions.push(urls.projectHomepage())
overrideTheme(true)
}, [])

return <App />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { navigation3000Logic } from '../navigationLogic'
import { Sidebar } from './Sidebar'
import featureFlagsJson from '../../../scenes/feature-flags/__mocks__/feature_flags.json'
import dashboardsJson from '../../../scenes/dashboard/__mocks__/dashboards.json'
import { with3000 } from 'storybook/decorators/with3000'
import { SidebarNavbarItem } from '../types'

const meta: Meta = {
Expand All @@ -17,7 +16,6 @@ const meta: Meta = {
layout: 'fullscreen',
viewMode: 'story',
},
decorators: [with3000],
}
export default meta
/** featureFlagsJson * 6 to fill the sidebar up more. */
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/mocks/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export const mswDecorator = (mocks: Mocks): DecoratorFunction<any> => {
}
}

export const useFeatureFlags = (featureFlags: string[]): void => {
export const setFeatureFlags = (featureFlags: string[]): void => {
;(window as any).POSTHOG_APP_CONTEXT.persisted_feature_flags = featureFlags
}
4 changes: 2 additions & 2 deletions frontend/src/scenes/insights/__mocks__/createInsightScene.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InsightModel } from '~/types'
import { useFeatureFlags, useStorybookMocks } from '~/mocks/browser'
import { setFeatureFlags, useStorybookMocks } from '~/mocks/browser'
import { useEffect } from 'react'
import { router } from 'kea-router'
import { App } from 'scenes/App'
Expand Down Expand Up @@ -27,7 +27,7 @@ export function createInsightStory(
],
},
})
useFeatureFlags([FEATURE_FLAGS.RETENTION_BREAKDOWN])
setFeatureFlags([FEATURE_FLAGS.RETENTION_BREAKDOWN])

useEffect(() => {
router.actions.push(`/insights/${insight.short_id}${count}${mode === 'edit' ? '/edit' : ''}`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta, StoryFn } from '@storybook/react'
import { NotebookSelectButton } from 'scenes/notebooks/NotebookSelectButton/NotebookSelectButton'
import { useFeatureFlags, useStorybookMocks } from '~/mocks/browser'
import { setFeatureFlags, useStorybookMocks } from '~/mocks/browser'
import { NotebookNodeType } from '~/types'
import { FEATURE_FLAGS } from 'lib/constants'

Expand Down Expand Up @@ -37,7 +37,7 @@ const allNotebooks = [
]

const Template: StoryFn<typeof NotebookSelectButton> = (props) => {
useFeatureFlags([FEATURE_FLAGS.NOTEBOOKS])
setFeatureFlags([FEATURE_FLAGS.NOTEBOOKS])
useStorybookMocks({
get: {
'/api/projects/:team_id/notebooks/': (req, res, ctx) => {
Expand Down
Loading