Skip to content

refactor(storybook): migrate to v8 and update obsolete stories DEV-19 #5567

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

Merged
merged 18 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ module.exports = {
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-a11y',
'storybook-dark-mode',
// NB:
// 'storybook-addon-swc' may improve build speed in the future.
// - At time of writing, the build performance gains are negated because it
// switches to a slower refresh plugin and also causes other compatibility
// issues in Storybook 6.
// - Testing with React 16.14.0 and Storybook 7 (beta) seemed to perform
// well.
'storybook-dark-mode',
'@storybook/addon-webpack5-compiler-swc',
],

framework: {
Expand Down Expand Up @@ -83,9 +84,7 @@ module.exports = {
}
return config
},
docs: {
autodocs: true,
},
docs: {},
}

/// Apply some customizations to the config, intended to decrease build time
Expand Down
1 change: 1 addition & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ export const parameters = {
}

window.t = (str) => str
export const tags = ['autodocs']
47 changes: 34 additions & 13 deletions jsapp/js/components/common/ButtonNew.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
import { forwardRef } from 'react'

import { Button as ButtonMantine, Tooltip, createPolymorphicComponent } from '@mantine/core'
import type { ButtonProps as ButtonPropsMantine, TooltipProps } from '@mantine/core/lib/components'
import { forwardRef } from 'react'
import type { IconName } from '#/k-icons'
import Icon, { type IconSize } from './icon'

const ButtonToIconMap: Partial<Record<NonNullable<ButtonProps['size']>, IconSize>> = {
sm: 'xs',
md: 's',
lg: 'm',
}

// See boilerpate at: https://mantine.dev/guides/polymorphic/#wrapping-polymorphic-components

export interface ButtonProps extends ButtonPropsMantine {
tooltip?: React.ReactNode
tooltipProps?: Partial<Omit<TooltipProps, 'label'>>

// Standard way of using icons with deterministic sizes.
// Note: never use Button with just an icon and no text - if you need that, use `ActionIcon` instead.
leftIcon?: IconName
rightIcon?: IconName
leftSection?: never
rightSection?: never
}

const Button = forwardRef<HTMLButtonElement, ButtonProps>(({ tooltip, tooltipProps, ...others }, ref) => {
if (!tooltip) {
return <ButtonMantine {...others} ref={ref} />
}

return (
<Tooltip label={tooltip} {...tooltipProps}>
<ButtonMantine {...others} ref={ref} />
</Tooltip>
)
})
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
({ tooltip, tooltipProps, leftIcon, rightIcon, ...others }, ref) => {
const iconSize = ButtonToIconMap[others.size ?? 'sm']
const leftSection = leftIcon && <Icon name={leftIcon} size={iconSize} />
const rightSection = rightIcon && <Icon name={rightIcon} size={iconSize} />

if (!tooltip) {
return <ButtonMantine {...others} leftSection={leftSection} rightSection={rightSection} ref={ref} />
}

return (
<Tooltip label={tooltip} {...tooltipProps}>
<ButtonMantine {...others} leftSection={leftSection} rightSection={rightSection} ref={ref} />
</Tooltip>
)
},
)
Button.displayName = 'Button'

// See boilerpate at: https://mantine.dev/guides/polymorphic/#wrapping-polymorphic-components
export default createPolymorphicComponent<'button', ButtonProps>(Button)
62 changes: 33 additions & 29 deletions jsapp/js/components/common/avatar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React from 'react'

import type { ComponentMeta, ComponentStory, StoryObj } from '@storybook/react'
import type { Meta, StoryObj } from '@storybook/react'
import Avatar from './avatar'
import type { AvatarSize } from './avatar'

const avatarSizes: AvatarSize[] = ['s', 'm']

export default {
const meta: Meta<typeof Avatar> = {
title: 'common/Avatar',
component: Avatar,
argTypes: {
Expand All @@ -16,32 +14,35 @@ export default {
},
username: { type: 'string' },
isUsernameVisible: { type: 'boolean' },
hasFullName: {
type: 'boolean',
description: 'Allows testing `fullName` being empty string or not existing',
fullName: {
options: ['Josh Johnson', 'Captain Person McPerson the Third', undefined],
control: { type: 'select' },
description: 'This is optional and component renders differently when it is `undefined`',
},
fullName: { type: 'string', if: { arg: 'hasFullName', truthy: true } },
hasEmail: {
type: 'boolean',
description: 'Allows testing `email` being empty string or not existing',
email: {
options: ['[email protected]', '[email protected]', undefined],
control: { type: 'select' },
description: 'This is optional and component renders differently when it is `undefined`',
},
email: { type: 'string', if: { arg: 'hasEmail', truthy: true } },
isEmpty: {
type: 'boolean',
},
},
} as ComponentMeta<typeof Avatar>
}

const Template: ComponentStory<typeof Avatar> = (args) => <Avatar {...args} />
export default meta

export const Simple = Template.bind({})
Simple.args = {
size: avatarSizes[0],
username: 'leszek',
isUsernameVisible: true,
type Story = StoryObj<typeof Avatar>

export const Simple: Story = {
args: {
size: avatarSizes[0],
username: 'leszek',
isUsernameVisible: true,
},
}

export const Full: StoryObj<typeof Avatar> = {
export const Full: Story = {
render: () => (
<Avatar
size='m'
Expand Down Expand Up @@ -184,12 +185,15 @@ const bulkUsernames = [
'Roberto',
'Peng',
]
export const BulkColorsTest = () => (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '10px' }}>
{bulkUsernames.map((username) => (
<div key={username}>
<Avatar size='m' username={username} />
</div>
))}
</div>
)

export const BulkColorsTest: Story = {
render: () => (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '10px' }}>
{bulkUsernames.map((username) => (
<div key={username}>
<Avatar size='m' username={username} />
</div>
))}
</div>
),
}
27 changes: 14 additions & 13 deletions jsapp/js/components/common/badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React from 'react'

import type { ComponentMeta, ComponentStory } from '@storybook/react'
import type { Meta, StoryObj } from '@storybook/react'
import { IconNames } from '#/k-icons'
import Badge from './badge'
import type { BadgeColor, BadgeSize } from './badge'

const badgeColors: BadgeColor[] = ['light-storm', 'light-amber', 'light-blue', 'light-red', 'light-teal', 'light-green']
const badgeSizes: BadgeSize[] = ['s', 'm', 'l']

export default {
const meta: Meta<typeof Badge> = {
title: 'common/Badge',
component: Badge,
argTypes: {
Expand All @@ -21,18 +19,21 @@ export default {
control: { type: 'select' },
},
icon: {
options: IconNames,
options: Object.keys(IconNames),
control: { type: 'select' },
},
},
} as ComponentMeta<typeof Badge>
}

const Template: ComponentStory<typeof Badge> = (args) => <Badge {...args} />
export default meta

export const Primary = Template.bind({})
Primary.args = {
color: badgeColors[0],
label: 'deployed',
size: badgeSizes[0],
icon: IconNames['project-deployed'],
type Story = StoryObj<typeof Badge>

export const Primary: Story = {
args: {
color: badgeColors[0],
label: 'deployed',
size: badgeSizes[0],
icon: IconNames['project-deployed'],
},
}
Loading