Skip to content

Cleanup styled-components in *.stories.tsx files and convert to CSS modules #6197

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 7 additions & 8 deletions packages/react/src/LabelGroup/LabelGroup.features.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import styled from 'styled-components'
import type React from 'react'
import LabelGroup from './LabelGroup'
import type {Meta, StoryFn} from '@storybook/react'
import Token from '../Token/Token'
import Label from '../Label/Label'
import classes from './LabelGroupStories.module.css'

const meta: Meta = {
title: 'Components/LabelGroup/Features',
Expand All @@ -18,13 +19,11 @@ const meta: Meta = {
],
}

const ResizableContainer = styled.div`
outline: 1px solid black;
overflow: auto;
padding: 0.25rem;
resize: horizontal;
width: 600px;
`
const ResizableContainer = ({children, ...props}: {children: React.ReactNode}) => (
<div className={classes.ResizableContainer} {...props}>
{children}
</div>
)

export const TruncateAuto: StoryFn = () => (
<ResizableContainer>
Expand Down
15 changes: 7 additions & 8 deletions packages/react/src/LabelGroup/LabelGroup.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import styled from 'styled-components'
import type React from 'react'
import type {LabelGroupProps} from './LabelGroup'
import LabelGroup from './LabelGroup'
import type {Meta, StoryFn} from '@storybook/react'
import Label from '../Label/Label'
import classes from './LabelGroupStories.module.css'

const meta: Meta = {
title: 'Components/LabelGroup',
component: LabelGroup,
}

const ResizableContainer = styled.div`
outline: 1px solid black;
overflow: auto;
padding: 0.25rem;
resize: horizontal;
width: 600px;
`
const ResizableContainer = ({children, ...props}: {children: React.ReactNode}) => (
<div className={classes.ResizableContainer} {...props}>
{children}
</div>
)

export const Default: StoryFn = () => (
<LabelGroup>
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/LabelGroup/LabelGroupStories.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.ResizableContainer {
/* stylelint-disable-next-line color-named */
outline: 1px solid black;
overflow: auto;
padding: var(--base-size-4);
resize: horizontal;
/* stylelint-disable-next-line primer/responsive-widths */
width: 600px;
}
1 change: 0 additions & 1 deletion packages/react/src/Text/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('Text', () => {
})

it('respects fontFamily="mono"', () => {
// styled-components removes the whitespace between font-family values
const mono = theme.fonts.mono.replace(/, /g, ',')
expect(render(<Text fontFamily="mono" />)).toHaveStyleRule('font-family', mono)
})
Expand Down
42 changes: 42 additions & 0 deletions packages/react/src/stories/AnchoredPositionStories.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.Float {
position: absolute;
border: var(--borderWidth-thin) solid var(--borderColor-default);
border-radius: var(--borderRadius-medium);
background-color: var(--bgColor-attention-muted);
display: flex;
flex-direction: column;
text-align: center;
font-size: var(--text-body-size-medium);
font-weight: var(--base-text-weight-semibold);
padding: var(--base-size-16);
}

.Anchor {
position: absolute;
border: var(--borderWidth-thin) solid var(--borderColor-default);
border-radius: var(--borderRadius-medium);
background-color: var(--bgColor-accent-muted);
display: flex;
flex-direction: column;
text-align: center;
font-size: var(--text-body-size-medium);
font-weight: var(--base-text-weight-semibold);
padding: var(--base-size-16);
}

.Nav {
width: 300px;
padding: var(--base-size-16);
position: relative;
overflow: hidden;
border-right: var(--borderWidth-thin) solid var(--borderColor-default);
}

.Main {
display: flex;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
5 changes: 5 additions & 0 deletions packages/react/src/stories/FocusTrapStories.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.MarginButton {
margin: var(--base-size-8) 0;
}

/* Helper styles for focus trap visual aids */
3 changes: 3 additions & 0 deletions packages/react/src/stories/FocusZoneStories.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.MarginButton {
margin: var(--base-size-8);
}
12 changes: 2 additions & 10 deletions packages/react/src/stories/ThemeProvider.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {Meta, StoryFn} from '@storybook/react'

import {ThemeProvider, BaseStyles, Box, themeGet, useTheme} from '..'
import {ThemeProvider, BaseStyles, Box, useTheme} from '..'
import type {ThemeProviderProps} from '../ThemeProvider'
import {createGlobalStyle} from 'styled-components'
import './ThemeProviderStories.module.css'

export default {
title: 'Behaviors/ThemeProvider',
Expand All @@ -17,12 +17,6 @@ export default {
},
} as Meta

const GlobalStyle = createGlobalStyle`
body {
background-color: ${themeGet('colors.bg.canvas')};
}
`

function ActiveColorScheme() {
const {colorScheme} = useTheme()
return <span>Active color scheme: {colorScheme}</span>
Expand All @@ -31,7 +25,6 @@ function ActiveColorScheme() {
export const Default: StoryFn<ThemeProviderProps> = args => {
return (
<ThemeProvider {...args}>
<GlobalStyle />
<BaseStyles>
<ActiveColorScheme />
</BaseStyles>
Expand Down Expand Up @@ -87,7 +80,6 @@ function InverseMode() {
export const Nested: StoryFn<ThemeProviderProps> = args => {
return (
<ThemeProvider {...args}>
<GlobalStyle />
<BaseStyles>
<ActiveColorScheme />
<NightMode />
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/stories/ThemeProviderStories.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:global(body) {
background-color: var(--bgColor-default);
}
35 changes: 15 additions & 20 deletions packages/react/src/stories/deprecated/ActionList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import {
} from '@primer/octicons-react'
import type {Meta} from '@storybook/react'
import React, {forwardRef} from 'react'
import styled from 'styled-components'
import {Label, ThemeProvider} from '../..'
import {ActionList as _ActionList} from '../../deprecated/ActionList'
import {Header} from '../../deprecated/ActionList/Header'
import BaseStyles from '../../BaseStyles'
import sx from '../../sx'
import {ReactRouterLikeLink} from '../../__tests__/mocks/ReactRouterLink'
import classes from './ActionListStories.module.css'

const ActionList = Object.assign(_ActionList, {
Header,
Expand All @@ -45,14 +44,16 @@ const meta: Meta = {
}
export default meta

const ErsatzOverlay = styled.div<{maxWidth?: string}>`
border-radius: 12px;
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 8px 24px rgba(149, 157, 165, 0.2);
overflow: hidden;
max-width: ${({maxWidth}) => maxWidth || 'none'};
`
const ErsatzOverlay = ({maxWidth, children, ...props}: {maxWidth?: string; children: React.ReactNode}) => (
<div
className={classes.ErsatzOverlay}
style={maxWidth ? ({'--ersatz-overlay-max-width': maxWidth} as React.CSSProperties) : undefined}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
style={maxWidth ? ({'--ersatz-overlay-max-width': maxWidth} as React.CSSProperties) : undefined}
style={maxWidth ? ({'--ersatz-overlay-max-width': maxWidth} as React.CSSProperties) : 'none'}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The suggestion as written would cause a TypeScript error since 'none' (string) is not assignable to CSSProperties | undefined. I interpreted the intent and fixed it to always set the CSS custom property, defaulting to 'none' when no maxWidth is provided, which properly replicates the original styled-component behavior. (f05f7d9)

Copy link
Member

Choose a reason for hiding this comment

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

@copilot This may make the attribute style="none"? Is that valid?

data-max-width={maxWidth ? '' : undefined}
{...props}
>
{children}
</div>
)

export function ActionsStory(): JSX.Element {
return (
Expand Down Expand Up @@ -152,9 +153,6 @@ export function MultiSelectListStory(): JSX.Element {
MultiSelectListStory.storyName = 'Multi Select'

export function ComplexListInsetVariantStory(): JSX.Element {
const StyledDiv = styled.div`
${sx}
`
return (
<>
<h1>Complex List</h1>
Expand All @@ -172,9 +170,9 @@ export function ComplexListInsetVariantStory(): JSX.Element {
<ActionList.Item
{...props}
leadingVisual={() => (
<StyledDiv sx={{'&>svg': {fill: 'white'}}}>
<div className={classes.StyledDivWithWhiteFill}>
{LeadingVisual && <LeadingVisual></LeadingVisual>}
</StyledDiv>
</div>
)}
/>
),
Expand Down Expand Up @@ -228,9 +226,6 @@ export function ComplexListInsetVariantStory(): JSX.Element {
ComplexListInsetVariantStory.storyName = 'Complex List — Inset Variant'

export function ComplexListFullVariantStory(): JSX.Element {
const StyledDiv = styled.div`
${sx}
`
return (
<>
<h1>Complex List</h1>
Expand All @@ -249,9 +244,9 @@ export function ComplexListFullVariantStory(): JSX.Element {
<ActionList.Item
{...props}
leadingVisual={() => (
<StyledDiv sx={{'&>svg': {fill: 'white'}}}>
<div className={classes.StyledDivWithWhiteFill}>
{LeadingVisual && <LeadingVisual></LeadingVisual>}
</StyledDiv>
</div>
)}
/>
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.ErsatzOverlay {
border-radius: var(--borderRadius-large);
/* stylelint-disable primer/box-shadow */
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 8px 24px rgba(149, 157, 165, 0.2);
/* stylelint-enable primer/box-shadow */
overflow: hidden;
}

.ErsatzOverlay[data-max-width] {
max-width: var(--ersatz-overlay-max-width);
}

.StyledDivWithWhiteFill > svg {
/* stylelint-disable-next-line color-named */
fill: white;
}
14 changes: 6 additions & 8 deletions packages/react/src/stories/deprecated/ActionMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
import type {Meta} from '@storybook/react'
import type React from 'react'
import {useCallback, useState, useRef} from 'react'
import styled from 'styled-components'
import {ThemeProvider} from '../..'
import type {ActionMenuProps} from '../../deprecated'
import {ActionMenu, ActionList} from '../../deprecated'
import type {ItemProps} from '../../deprecated/ActionList'
import BaseStyles from '../../BaseStyles'
import {Button, type ButtonProps} from '../../Button'
import classes from './ActionMenuStories.module.css'

const meta: Meta = {
title: 'Deprecated/Components/ActionMenu',
Expand All @@ -42,13 +42,11 @@ const meta: Meta = {
}
export default meta

const ErsatzOverlay = styled.div`
border-radius: 12px;
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 8px 24px rgba(149, 157, 165, 0.2);
padding: 8px;
`
const ErsatzOverlay = ({children, ...props}: {children: React.ReactNode}) => (
<div className={classes.ErsatzOverlay} {...props}>
{children}
</div>
)

export function ActionsStory(): JSX.Element {
const [option, setOption] = useState('Select an option')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* stylelint-disable primer/box-shadow */
.ErsatzOverlay {
border-radius: var(--borderRadius-large);
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 8px 24px rgba(149, 157, 165, 0.2);
padding: var(--base-size-8);
}
Loading
Loading