-
Notifications
You must be signed in to change notification settings - Fork 7
feat(design-system): add DsStack component [AR-53842] #351
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
vpolessky-dn
merged 7 commits into
drivenets:main
from
vpolessky-dn:feat/AR-53844-add-stack-component
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ae556b
feat(design-system): add DsStack component [AR-53842]
vpolessky-dn 33397a1
Merge branch 'main' into feat/AR-53844-add-stack-component
vpolessky-dn e4a4f97
feat(design-system): update DsStack display name [AR-53842]
vpolessky-dn 1cac0f6
feat(design-system): update DsStack type definition [AR-53842]
vpolessky-dn 417999e
Merge branch 'main' into feat/AR-53844-add-stack-component
iromanchuk-dn 8d5ef2a
feat(design-system): use csstype [AR-53844]
vpolessky-dn dfac768
Merge branch 'main' into feat/AR-53844-add-stack-component
iromanchuk-dn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@drivenets/design-system': minor | ||
| --- | ||
|
|
||
| Add `DsStack` layout component |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
packages/design-system/src/components/ds-stack/__tests__/ds-stack.browser.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { page } from 'vitest/browser'; | ||
|
|
||
| import { DsStack } from '../ds-stack'; | ||
|
|
||
| describe('DsStack', () => { | ||
| it('renders children in a flex column by default', async () => { | ||
| await page.render( | ||
| <DsStack> | ||
| <button type="button">First</button> | ||
| <button type="button">Second</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const first = page.getByRole('button', { name: 'First' }); | ||
| const second = page.getByRole('button', { name: 'Second' }); | ||
| await expect.element(first).toBeVisible(); | ||
| await expect.element(second).toBeVisible(); | ||
|
|
||
| const firstRect = first.element().getBoundingClientRect(); | ||
| const secondRect = second.element().getBoundingClientRect(); | ||
| expect(secondRect.top).toBeGreaterThanOrEqual(firstRect.bottom); | ||
| }); | ||
|
|
||
| it('renders children in a row when direction is "row"', async () => { | ||
| await page.render( | ||
| <DsStack direction="row"> | ||
| <button type="button">Left</button> | ||
| <button type="button">Right</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const left = page.getByRole('button', { name: 'Left' }); | ||
| const right = page.getByRole('button', { name: 'Right' }); | ||
|
|
||
| const leftRect = left.element().getBoundingClientRect(); | ||
| const rightRect = right.element().getBoundingClientRect(); | ||
| expect(rightRect.left).toBeGreaterThanOrEqual(leftRect.right); | ||
| }); | ||
|
|
||
| it('applies gap between children', async () => { | ||
| await page.render( | ||
| <DsStack direction="row" gap={20}> | ||
| <button type="button">A</button> | ||
| <button type="button">B</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const a = page.getByRole('button', { name: 'A' }); | ||
| const b = page.getByRole('button', { name: 'B' }); | ||
|
|
||
| const aRect = a.element().getBoundingClientRect(); | ||
| const bRect = b.element().getBoundingClientRect(); | ||
| const actualGap = bRect.left - aRect.right; | ||
| expect(actualGap).toBeCloseTo(20, 0); | ||
| }); | ||
|
|
||
| it('applies alignItems and justifyContent', async () => { | ||
| await page.render( | ||
| <DsStack | ||
| direction="row" | ||
| justifyContent="center" | ||
| alignItems="center" | ||
| style={{ height: 200, width: 400 }} | ||
| > | ||
| <button type="button">Centered</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const container = page.getByRole('button', { name: 'Centered' }).element().parentElement; | ||
| expect(container).toBeTruthy(); | ||
| const containerStyle = getComputedStyle(container as Element); | ||
| expect(containerStyle.justifyContent).toBe('center'); | ||
| expect(containerStyle.alignItems).toBe('center'); | ||
| }); | ||
|
|
||
| it('applies flex, flexWrap, and width', async () => { | ||
| await page.render( | ||
| <DsStack flex="1" flexWrap="wrap" width="500px"> | ||
| <button type="button">Child</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const container = page.getByRole('button', { name: 'Child' }).element().parentElement; | ||
| expect(container).toBeTruthy(); | ||
| const containerStyle = getComputedStyle(container as Element); | ||
| expect(containerStyle.flexWrap).toBe('wrap'); | ||
| expect(containerStyle.width).toBe('500px'); | ||
| }); | ||
|
|
||
| it('merges custom className and style', async () => { | ||
| await page.render( | ||
| <DsStack className="custom-class" style={{ padding: 12 }}> | ||
| <button type="button">Styled</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const container = page.getByRole('button', { name: 'Styled' }).element().parentElement as HTMLElement; | ||
| expect(container).toBeTruthy(); | ||
| expect(container.classList.contains('custom-class')).toBe(true); | ||
| expect(container.style.padding).toBe('12px'); | ||
| }); | ||
|
|
||
| it('forwards ref to the root div', async () => { | ||
| let refElement: HTMLDivElement | null = null; | ||
|
|
||
| await page.render( | ||
| <DsStack | ||
| ref={(el) => { | ||
| refElement = el; | ||
| }} | ||
| > | ||
| <button type="button">Ref test</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| await expect.element(page.getByRole('button', { name: 'Ref test' })).toBeVisible(); | ||
| expect(refElement).toBeInstanceOf(HTMLDivElement); | ||
| }); | ||
|
|
||
| it('user style overrides layout props', async () => { | ||
| await page.render( | ||
| <DsStack gap={10} style={{ gap: 30 }}> | ||
| <button type="button">X</button> | ||
| <button type="button">Y</button> | ||
| </DsStack>, | ||
| ); | ||
|
|
||
| const container = page.getByRole('button', { name: 'X' }).element().parentElement as HTMLElement; | ||
| expect(container).toBeTruthy(); | ||
| expect(container.style.gap).toBe('30px'); | ||
| }); | ||
| }); |
4 changes: 4 additions & 0 deletions
4
packages/design-system/src/components/ds-stack/ds-stack.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .root { | ||
| display: flex; | ||
| flex-direction: column; | ||
| } |
15 changes: 15 additions & 0 deletions
15
packages/design-system/src/components/ds-stack/ds-stack.stories.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| .box { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| min-width: 80px; | ||
| padding: var(--xs) var(--sm); | ||
| border: 1px solid var(--color-border-default); | ||
| border-radius: 4px; | ||
| background-color: var(--color-bg-surface-1); | ||
| font-size: var(--font-size-body-md); | ||
| } | ||
|
|
||
| .container { | ||
| width: 600px; | ||
| } |
123 changes: 123 additions & 0 deletions
123
packages/design-system/src/components/ds-stack/ds-stack.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { DsStack } from './index'; | ||
| import styles from './ds-stack.stories.module.scss'; | ||
|
|
||
| const Box = ({ children }: { children: React.ReactNode }) => <div className={styles.box}>{children}</div>; | ||
|
|
||
| const meta: Meta<typeof DsStack> = { | ||
| title: 'Design System/Stack', | ||
| component: DsStack, | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| argTypes: { | ||
| direction: { control: 'select', options: ['row', 'column', 'row-reverse', 'column-reverse'] }, | ||
| gap: { control: 'text' }, | ||
| alignItems: { control: 'select', options: ['flex-start', 'center', 'flex-end', 'stretch', 'baseline'] }, | ||
| justifyContent: { | ||
| control: 'select', | ||
| options: ['flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly'], | ||
| }, | ||
| flexWrap: { control: 'select', options: ['nowrap', 'wrap', 'wrap-reverse'] }, | ||
| width: { control: 'text' }, | ||
| flex: { control: 'text' }, | ||
| className: { table: { disable: true } }, | ||
| style: { table: { disable: true } }, | ||
| ref: { table: { disable: true } }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof DsStack>; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| direction: 'column', | ||
| gap: 8, | ||
| }, | ||
| render: (args) => ( | ||
| <DsStack {...args}> | ||
| <Box>Item 1</Box> | ||
| <Box>Item 2</Box> | ||
| <Box>Item 3</Box> | ||
| </DsStack> | ||
| ), | ||
| }; | ||
|
|
||
| export const Row: Story = { | ||
| args: { | ||
| direction: 'row', | ||
| gap: 16, | ||
| alignItems: 'center', | ||
| }, | ||
| render: (args) => ( | ||
| <DsStack {...args}> | ||
| <Box>Item 1</Box> | ||
| <Box>Item 2</Box> | ||
| <Box>Item 3</Box> | ||
| </DsStack> | ||
| ), | ||
| }; | ||
|
|
||
| export const Responsive: Story = { | ||
| args: { | ||
| direction: { md: 'column', lg: 'row' }, | ||
| gap: { md: 8, lg: 24 }, | ||
| alignItems: 'center', | ||
| }, | ||
| render: (args) => ( | ||
| <DsStack {...args} className={styles.container}> | ||
| <Box>Item 1</Box> | ||
| <Box>Item 2</Box> | ||
| <Box>Item 3</Box> | ||
| </DsStack> | ||
| ), | ||
| }; | ||
|
|
||
| export const SpaceBetween: Story = { | ||
| args: { | ||
| direction: 'row', | ||
| justifyContent: 'space-between', | ||
| alignItems: 'center', | ||
| width: '100%', | ||
| }, | ||
| render: (args) => ( | ||
| <DsStack {...args} className={styles.container}> | ||
| <Box>Left</Box> | ||
| <Box>Right</Box> | ||
| </DsStack> | ||
| ), | ||
| }; | ||
|
|
||
| export const Wrapping: Story = { | ||
| args: { | ||
| direction: 'row', | ||
| gap: 8, | ||
| flexWrap: 'wrap', | ||
| }, | ||
| render: (args) => ( | ||
| <DsStack {...args} className={styles.container}> | ||
| {Array.from({ length: 10 }, (_, i) => ( | ||
| <Box key={i}>Item {i + 1}</Box> | ||
| ))} | ||
| </DsStack> | ||
| ), | ||
| }; | ||
|
|
||
| export const Nested: Story = { | ||
| render: () => ( | ||
| <DsStack gap={24}> | ||
| <DsStack direction="row" gap={16} alignItems="center"> | ||
| <Box>Row 1 - A</Box> | ||
| <Box>Row 1 - B</Box> | ||
| <Box>Row 1 - C</Box> | ||
| </DsStack> | ||
|
|
||
| <DsStack direction="row" gap={16} alignItems="center"> | ||
| <Box>Row 2 - A</Box> | ||
| <Box>Row 2 - B</Box> | ||
| </DsStack> | ||
| </DsStack> | ||
| ), | ||
| }; |
35 changes: 35 additions & 0 deletions
35
packages/design-system/src/components/ds-stack/ds-stack.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import classNames from 'classnames'; | ||
|
|
||
| import styles from './ds-stack.module.scss'; | ||
| import type { DsStackProps } from './ds-stack.types'; | ||
|
|
||
| export const DsStack = ({ | ||
| direction, | ||
| gap, | ||
| alignItems, | ||
| justifyContent, | ||
| flex, | ||
| flexWrap, | ||
| width, | ||
| children, | ||
| className, | ||
| style, | ||
| ref, | ||
| }: DsStackProps) => { | ||
| const layoutStyle: React.CSSProperties = { | ||
| flexDirection: direction, | ||
| gap, | ||
| alignItems, | ||
| justifyContent, | ||
| flex, | ||
| flexWrap, | ||
| width, | ||
| ...style, | ||
| }; | ||
|
|
||
| return ( | ||
| <div ref={ref} className={classNames(styles.root, className)} style={layoutStyle}> | ||
| {children} | ||
| </div> | ||
| ); | ||
| }; |
19 changes: 19 additions & 0 deletions
19
packages/design-system/src/components/ds-stack/ds-stack.types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { Properties } from 'csstype'; | ||
|
|
||
| type CSSProps = Properties<string | number>; | ||
| import type { CSSProperties, ReactNode, Ref } from 'react'; | ||
|
|
||
| export interface DsStackProps { | ||
| direction?: CSSProps['flexDirection']; | ||
| gap?: CSSProps['gap']; | ||
| alignItems?: CSSProps['alignItems']; | ||
| justifyContent?: CSSProps['justifyContent']; | ||
| flex?: CSSProps['flex']; | ||
| flexWrap?: CSSProps['flexWrap']; | ||
| width?: CSSProps['width']; | ||
|
|
||
| children?: ReactNode; | ||
| className?: string; | ||
| style?: CSSProperties; | ||
| ref?: Ref<HTMLDivElement>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type { ComponentProps } from 'react'; | ||
|
|
||
| import { withResponsiveProps } from '../../utils/responsive'; | ||
| import { DsStack as DsStackBase } from './ds-stack'; | ||
|
|
||
| export const DsStack = withResponsiveProps(DsStackBase, [ | ||
| 'direction', | ||
| 'gap', | ||
| 'alignItems', | ||
| 'justifyContent', | ||
| 'flex', | ||
| 'flexWrap', | ||
| 'width', | ||
| ]); | ||
|
|
||
| DsStack.displayName = 'DsStack'; | ||
|
|
||
| export type DsStackProps = ComponentProps<typeof DsStack>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you tell me where you use this package? I can't find it's imported anywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
csstypeis genuinely needed for TypeScript declaration emit (not for runtime).Added obvious usage case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm seeing this only in a single place and it's used as an
import typecan't we import these types from React itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StyleShit
csstypeis needed becauseComponentProps<typeof DsStack>forces TypeScript to expand CSS property types in the.d.tsoutput, and those types come from csstype. Without it as a dependency, TypeScript refuses to emit the declaration file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk... tbh i don't really understand why we need this component if it's just using plain css as props
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if do not want to redefine properties in the types file this dependency is required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me and Valentine have looked together into other solutions and they require writing some bolierplate code. This solution is the least effort and has no downsides, except the fact that we need additional package. This solution seems unintuitive, but we can't find a better way. Probably this problem will be resolved by itself with tsdown, typescript, react updates.