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(Divider): render content inside #1701

Merged
merged 6 commits into from
Aug 23, 2024
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
58 changes: 58 additions & 0 deletions src/components/Divider/Divider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,71 @@
$block: '.#{variables.$ns}divider';

#{$block} {
--_--content-gap: 8px;
--_--size: 1px;

&:not(:empty) {
border: none;
display: flex;
align-items: center;

&::before,
&::after {
content: '';
}
}

&::before,
&::after {
flex-grow: 1;
background: var(--g-divider-color, var(--g-color-line-generic));
}

&_orientation {
&_vertical {
flex-direction: column;

&::before,
&::after {
width: var(--_--size);
}

&::before {
margin-block-end: var(--_--content-gap);
}

&::after {
margin-block-start: var(--_--content-gap);
}

border-inline-start: 1px solid var(--g-divider-color, var(--g-color-line-generic));
}

&_horizontal {
&::before,
&::after {
height: var(--_--size);
}

&::before {
margin-inline-end: var(--_--content-gap);
}

&::after {
margin-inline-start: var(--_--content-gap);
}

border-block-start: 1px solid var(--g-divider-color, var(--g-color-line-generic));
}
}

&_align {
&_start::before {
display: none;
}

&_end::after {
display: none;
}
}
}
11 changes: 8 additions & 3 deletions src/components/Divider/Divider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ import {block} from '../utils/cn';
import './Divider.scss';

export type DividerOrientation = 'vertical' | 'horizontal';
export type DividerAlign = 'start' | 'center' | 'end';

export interface DividerProps extends DOMProps, QAProps {
orientation?: DividerOrientation;
align?: DividerAlign;
children?: React.ReactNode;
}

const b = block('divider');

export const Divider = React.forwardRef<HTMLDivElement, DividerProps>(function Divider(props, ref) {
const {orientation = 'horizontal', className, style, qa} = props;
const {orientation = 'horizontal', className, style, qa, children, align = 'start'} = props;

return (
<div
className={b({orientation}, className)}
className={b({orientation, align}, className)}
ref={ref}
style={style}
data-qa={qa}
role="separator"
aria-orientation={orientation === 'vertical' ? 'vertical' : undefined}
/>
>
{children}
</div>
);
});
90 changes: 90 additions & 0 deletions src/components/Divider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,102 @@ import {Flex, Label, Divider} from '@gravity-ui/uikit';

<!--/GITHUB_BLOCK-->

### Custom content

<!--LANDING_BLOCK

<ExampleBlock
code={`
<Container>
<Flex gap={3}>
<Divider>Custom content</Divider>
<Divider align="center">
<Icon data={CheckIcon} size={16} />
</Divider>
</Flex>
</Container>
`}
>
<UIKit.Container>
<UIKit.Flex gap={3}>
<UIKit.Divider>Custom content</UIKit.Divider>
<UIKit.Divider>
<Icon data={CheckIcon} size={16} />
</UIKit.Divider>
</UIKit.Flex>
</UIKit.Container>
</ExampleBlock>

LANDING_BLOCK-->

<!--GITHUB_BLOCK-->

```tsx
import {Divider, Flex, Container, Icon} from '@gravity-ui/uikit';
import {CheckIcon} from '@gravity-ui/icons';

<Container>
<Flex gap={3}>
<Divider>Custom content</Divider>
<Divider>
<Icon data={CheckIcon} size={16} />
</Divider>
</Flex>
</Container>;
```

<!--/GITHUB_BLOCK-->

### Alignment

<!--LANDING_BLOCK

<ExampleBlock
code={`
<Container>
<Flex gap={3}>
<Divider align="start">Start content</Divider>
<Divider align="center">Center content</Divider>
<Divider align="end">End content</Divider>
</Flex>
</Container>
`}
>
<UIKit.Container>
<UIKit.Flex gap={3}>
<UIKit.Divider align="start">Start content</UIKit.Divider>
<UIKit.Divider align="center">Center content</UIKit.Divider>
<UIKit.Divider align="end">End content</UIKit.Divider>
</UIKit.Flex>
</Container>
</ExampleBlock>

LANDING_BLOCK-->

<!--GITHUB_BLOCK-->

```tsx
import {Divider, Flex, Container} from '@gravity-ui/uikit';

<Container>
<Flex gap={3}>
<Divider align="start">Start content</Divider>
<Divider align="center">Center content</Divider>
<Divider align="end">End content</Divider>
</Flex>
</Container>;
```

<!--/GITHUB_BLOCK-->

### Properties

| Name | Description | Type | Default |
| :---------- | :-------------------------------------- | :---------------------- | :----------- |
| className | HTML `class` attribute | `string` | - |
| orientation | Sets the direction of divider | `horizontal - vertical` | `horizontal` |
| children | Custom content inside divider | `React.ReactNode` | |
| align | Custom content position | `start - center - end` | `start` |
| style | HTML `style` attribute | `React.CSSProperties` | |
| qa | HTML `data-qa` attribute, used in tests | `string` | |

v4dyar4 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
38 changes: 38 additions & 0 deletions src/components/Divider/__stories__/Divider.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React from 'react';

import {Check as CheckIcon} from '@gravity-ui/icons';
import type {Meta, StoryObj} from '@storybook/react';

import {Showcase} from '../../../demo/Showcase';
import {Card} from '../../Card';
import {Icon} from '../../Icon';
import {ListItem} from '../../List';
import {Flex} from '../../layout';
import {Divider} from '../Divider';
import type {DividerProps} from '../Divider';

const meta: Meta<typeof Divider> = {
title: 'Components/Utils/Divider',
Expand Down Expand Up @@ -57,6 +60,7 @@ export const Horizontal: Story = {
style: disabledControl,
qa: disabledControl,
className: disabledControl,
align: disabledControl,
},
render: (args) => {
return (
Expand Down Expand Up @@ -93,6 +97,7 @@ export const Vertical: Story = {
style: disabledControl,
qa: disabledControl,
className: disabledControl,
align: disabledControl,
},
render: (args) => (
<Showcase>
Expand Down Expand Up @@ -124,6 +129,9 @@ export const Custom: Story = {
className: 'custom-divider',
style: {borderWidth: '2px'},
},
argTypes: {
align: disabledControl,
},
render: (args) => (
<Showcase>
<style>
Expand Down Expand Up @@ -152,3 +160,33 @@ export const Custom: Story = {
</Showcase>
),
};

const alignCases: Array<NonNullable<DividerProps['align']>> = ['start', 'center', 'end'];

export const WithContent: Story = {
args: {
orientation: 'horizontal',
},
render: (args) => (
<Showcase>
<Flex
direction={args.orientation === 'horizontal' ? 'column' : 'row'}
height={args.orientation === 'vertical' ? 200 : undefined}
width={400}
gap={5}
>
{alignCases.map((align, index) => (
<Divider {...args} align={align} key={index}>
{align}
</Divider>
))}

<Divider align="center" {...args}>
<Icon data={CheckIcon} size={16} />
</Divider>
</Flex>
</Showcase>
),
};

WithContent.storyName = 'With content';
22 changes: 22 additions & 0 deletions src/components/Divider/__tests__/Divider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import {render, screen} from '../../../../test-utils/utils';
import {block} from '../../utils/cn';
import {Divider} from '../Divider';
import type {DividerAlign} from '../Divider';

const b = block('custom-divider');
const qa = 'divider';
Expand Down Expand Up @@ -36,4 +37,25 @@ describe('Divider', () => {
expect(element).toHaveAttribute('role', 'separator');
expect(element).toHaveClass('g-divider_orientation_vertical');
});
test('Should render children content', () => {
const childrenText = 'Children content';

render(
<Divider qa={qa}>
<span>{childrenText}</span>
</Divider>,
);
const content = screen.getByText(childrenText);

expect(content).toBeVisible();
});
test.each(new Array<DividerAlign>('start', 'center', 'end'))(
'Should render children content with given align "%s"',
(align) => {
render(<Divider align={align} qa={qa} />);
const element = screen.getByTestId(qa);

expect(element).toHaveClass(`g-divider_align_${align}`);
},
);
});
Loading