Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/good-bars-see.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Stack.Item: add support for `shrink` prop
5 changes: 5 additions & 0 deletions packages/react/src/Stack/Stack.docs.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,6 +50,11 @@
"type": "boolean | ResponsiveValue<boolean>",
"description": "Allow item to keep size or expand to fill the available space."
},
{
"name": "shrink",
"type": "boolean | ResponsiveValue<boolean>",
"description": "Allow item to keep size or shrink to fit the available space."
},
{
"name": "className",
"type": "string"
Expand Down
25 changes: 25 additions & 0 deletions packages/react/src/Stack/Stack.features.stories.tsx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
import type {Meta} from '@storybook/react-vite'
import {Stack} from '../Stack'
import {ShieldLockIcon} from '@primer/octicons-react'

export default {
title: 'Components/Stack/Features',
component: Stack,
} as Meta<typeof Stack>

export const ShrinkingStackItems = () => (
<div style={{maxWidth: '200px', padding: 'var(--base-size-8)'}}>
<Stack direction="horizontal" gap="condensed">
<Stack.Item shrink={false}>
<ShieldLockIcon size="small" />
</Stack.Item>
<Stack.Item>This stack has the leading icon set to prevent shrinking</Stack.Item>
</Stack>
<Stack direction="horizontal" gap="condensed">
<Stack.Item shrink={true}>
<ShieldLockIcon size="small" />
</Stack.Item>
<Stack.Item>This stack item does not have the icon set to prevent shrinking</Stack.Item>
</Stack>
</div>
)
21 changes: 21 additions & 0 deletions packages/react/src/Stack/Stack.module.css
Original file line numberDiff line numberDiff line change
Expand Up@@ -296,6 +296,11 @@
flex: 0 1 auto;
min-inline-size: 0;

&[data-shrink='false'],
&[data-shrink-narrow='false'] {
flex-shrink: 0;
}

&[data-grow='true'],
&[data-grow-narrow='true'] {
flex-grow: 1;
Expand All@@ -309,6 +314,14 @@
&[data-grow-regular='false'] {
flex-grow: 0;
}

&[data-shrink-regular='true'] {
flex-shrink: 1;
}

&[data-shrink-regular='false'] {
flex-shrink: 0;
}
}

@media (--viewportRange-wide) {
Expand All@@ -319,5 +332,13 @@
&[data-grow-wide='false'] {
flex-grow: 0;
}

&[data-shrink-wide='true'] {
flex-shrink: 1;
}

&[data-shrink-wide='false'] {
flex-shrink: 0;
}
}
}
78 changes: 34 additions & 44 deletions packages/react/src/Stack/Stack.stories.tsx
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
import type {Meta, StoryObj} from '@storybook/react-vite'
import {Stack} from '../Stack'
import type {ResponsiveValue} from '../hooks/useResponsiveValue'
import type {InputType} from '@storybook/csf'

type Story = StoryObj<typeof Stack>

Expand DownExpand Up@@ -468,58 +469,42 @@ export const Playground: Story = {
},
}

function createArgMetaData(category: string): InputType {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ohh this is so clean 😍

return {
control: {
type: 'boolean',
},
table: {
category,
defaultValue: {
summary: 'true',
},
},
}
}

export const StackItemPlayground: Story = {
args: {
grow: true,
growNarrow: true,
growRegular: true,
growWide: true,

shrink: true,
shrinkNarrow: true,
shrinkRegular: true,
shrinkWide: true,
},
argTypes: {
grow: {
control: {
type: 'boolean',
},
table: {
category: 'Properties',
defaultValue: {
summary: 'true',
},
},
},
growNarrow: {
control: {
type: 'boolean',
},
table: {
category: 'Narrow properties',
defaultValue: {
summary: 'true',
},
},
},
growRegular: {
control: {
type: 'boolean',
},
table: {
category: 'Regular properties',
defaultValue: {
summary: 'true',
},
},
},
growWide: {
control: {
type: 'boolean',
},
table: {
category: 'Wide properties',
defaultValue: {
summary: 'true',
},
},
},
grow: createArgMetaData('Properties'),
growNarrow: createArgMetaData('Narrow properties'),
growRegular: createArgMetaData('Regular properties'),
growWide: createArgMetaData('Wide properties'),

shrink: createArgMetaData('Properties'),
shrinkNarrow: createArgMetaData('Narrow properties'),
shrinkRegular: createArgMetaData('Regular properties'),
shrinkWide: createArgMetaData('Wide properties'),
},
render: args => {
return (
Expand All@@ -530,6 +515,11 @@ export const StackItemPlayground: Story = {
regular: args.growRegular,
wide: args.growWide,
})}
shrink={getControlValues(args.shrink, {
narrow: args.shrinkNarrow,
regular: args.shrinkRegular,
wide: args.shrinkWide,
})}
>
<div
style={{
Expand Down
10 changes: 9 additions & 1 deletion packages/react/src/Stack/Stack.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -114,17 +114,25 @@ type StackItemProps<As> = React.PropsWithChildren<{
* @default false
*/
grow?: boolean | ResponsiveValue<boolean>

/**
* Allow item to keep size or expand to fill the available space
* @default true
*/
shrink?: boolean | ResponsiveValue<boolean>

className?: string
}>

const StackItem = forwardRef(({as, children, grow, className, ...rest}, forwardedRef) => {
const StackItem = forwardRef(({as, children, grow, shrink, className, ...rest}, forwardedRef) => {
return (
<BoxWithFallback
as={as}
ref={forwardedRef}
{...rest}
className={clsx(className, classes.StackItem)}
{...getResponsiveAttributes('grow', grow)}
{...getResponsiveAttributes('shrink', shrink)}
>
{children}
</BoxWithFallback>
Expand Down
22 changes: 22 additions & 0 deletions packages/react/src/Stack/__tests__/StackItem.test.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,6 +46,28 @@ describe('StackItem', () => {
expect(screen.getByTestId('responsive-grow')).toHaveAttribute('data-grow-wide', 'true')
})

it('should support the `shrink` prop', () => {
render(
<Stack>
<StackItem data-testid="shrink-true" shrink={true} />
<StackItem data-testid="shrink-false" />
</Stack>,
)
expect(screen.getByTestId('shrink-true')).toHaveAttribute('data-shrink', 'true')
expect(screen.getByTestId('shrink-false')).not.toHaveAttribute('data-shrink', 'false')
})

it('should support responsive `shrink` values', () => {
render(
<Stack>
<StackItem data-testid="responsive-shrink" shrink={{narrow: true, regular: false, wide: true}} />
</Stack>,
)
expect(screen.getByTestId('responsive-shrink')).toHaveAttribute('data-shrink-narrow', 'true')
expect(screen.getByTestId('responsive-shrink')).toHaveAttribute('data-shrink-regular', 'false')
expect(screen.getByTestId('responsive-shrink')).toHaveAttribute('data-shrink-wide', 'true')
})

it('should render a custom component with the `as` prop', () => {
const CustomComponent = vi.fn(({children}: React.PropsWithChildren) => {
return <div data-testid="custom-stack-item">{children}</div>
Expand Down