Repository navigation
feat(mosaic): add Separator #10613
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
feat(mosaic): add Separator #10613
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,2 @@ | ||
| --- | ||
| --- |
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,2 @@ | ||
| export { Separator } from './separator'; | ||
| export type { SeparatorProps } from './separator'; |
33 changes: 33 additions & 0 deletions
33
packages/mosaic/src/components/separator/separator.styles.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,33 @@ | ||
| import * as stylex from '@stylexjs/stylex'; | ||
|
|
||
| import { colorVars, fontFamilyVars, space, typeScaleVars } from '../../tokens.stylex'; | ||
|
|
||
| export const styles = stylex.create({ | ||
| plain: { | ||
| backgroundColor: colorVars['--cl-color-border'], | ||
| height: '1px', | ||
| width: '100%', | ||
| }, | ||
| labelled: { | ||
| gap: space['4'], | ||
| alignItems: 'center', | ||
| color: colorVars['--cl-color-foreground-secondary'], | ||
| display: 'flex', | ||
| fontFamily: fontFamilyVars['--cl-font-family-sans'], | ||
| fontSize: typeScaleVars['--cl-text-sm-size'], | ||
| lineHeight: typeScaleVars['--cl-text-sm-leading'], | ||
| width: '100%', | ||
| '::after': { | ||
| backgroundColor: colorVars['--cl-color-border'], | ||
| content: '""', | ||
| flexGrow: 1, | ||
| height: '1px', | ||
| }, | ||
| '::before': { | ||
| backgroundColor: colorVars['--cl-color-border'], | ||
| content: '""', | ||
| flexGrow: 1, | ||
| height: '1px', | ||
| }, | ||
| }, | ||
| }); | ||
55 changes: 55 additions & 0 deletions
55
packages/mosaic/src/components/separator/separator.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,55 @@ | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { Separator } from './separator'; | ||
|
|
||
| const atoms = stylex.create({ | ||
| spaced: { marginTop: '8px' }, | ||
| }); | ||
|
|
||
| describe('Mosaic Separator', () => { | ||
| it('renders a plain separator without children', () => { | ||
| render(<Separator />); | ||
| const separator = screen.getByRole('separator'); | ||
| expect(separator).toHaveClass('cl-separator'); | ||
| expect(separator).not.toHaveAttribute('data-labelled'); | ||
| expect(separator).toBeEmptyDOMElement(); | ||
| }); | ||
|
|
||
| it('renders a labelled separator with children', () => { | ||
| render(<Separator>or</Separator>); | ||
| const separator = screen.getByRole('separator'); | ||
| expect(separator).toHaveTextContent('or'); | ||
| expect(separator).toHaveAttribute('data-labelled', ''); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['null', null], | ||
| ['false', false], | ||
| ['an empty string', ''], | ||
| ])('renders the plain separator when children is %s', (_, children) => { | ||
| render(<Separator>{children}</Separator>); | ||
| expect(screen.getByRole('separator')).not.toHaveAttribute('data-labelled'); | ||
| }); | ||
|
|
||
| it('merges xstyle atoms after the slot atoms', () => { | ||
| render(<Separator xstyle={atoms.spaced} />); | ||
| expect(screen.getByRole('separator')).toHaveClass('cl-separator', stylex.props(atoms.spaced).className ?? ''); | ||
| }); | ||
|
|
||
| it('renders a custom element via render, keeping the styling contract', () => { | ||
| render(<Separator render={<section />}>or</Separator>); | ||
| const separator = screen.getByRole('separator'); | ||
| expect(separator.tagName).toBe('SECTION'); | ||
| expect(separator).toHaveClass('cl-separator'); | ||
| expect(separator).toHaveAttribute('data-labelled', ''); | ||
| }); | ||
|
|
||
| it('forwards the ref', () => { | ||
| const ref = React.createRef<HTMLDivElement>(); | ||
| render(<Separator ref={ref} />); | ||
| expect(ref.current).toBe(screen.getByRole('separator')); | ||
| }); | ||
| }); |
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,45 @@ | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| import React from 'react'; | ||
|
|
||
| import { useRender } from '../../primitives/utils'; | ||
| import type { MosaicComponentProps } from '../../props'; | ||
| import { mergeStyleProps, themeProps } from '../../props'; | ||
| import { reset } from '../../styles/reset.styles'; | ||
| import { styles } from './separator.styles'; | ||
|
|
||
| export type SeparatorProps = MosaicComponentProps<'div'>; | ||
|
|
||
| /** | ||
| * A horizontal rule between blocks of content. Without children it is a plain 1px line; | ||
| * with children it is a labelled divider, with a line on each side of the label. | ||
| * | ||
| * @example | ||
| * <Separator /> | ||
| * | ||
| * @example | ||
| * <Separator>or</Separator> | ||
| */ | ||
| export const Separator = React.forwardRef<HTMLDivElement, SeparatorProps>(function MosaicSeparator( | ||
| { children, render, xstyle, ...rest }, | ||
| ref, | ||
| ) { | ||
| const labelled = hasLabel(children); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return useRender({ | ||
| defaultTagName: 'div', | ||
| render, | ||
| ref, | ||
| props: { | ||
| ...mergeStyleProps( | ||
| themeProps('separator', { labelled }), | ||
| stylex.props(reset.base, labelled ? styles.labelled : styles.plain, xstyle), | ||
| rest, | ||
| ), | ||
| role: 'separator', | ||
| children, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| function hasLabel(children: React.ReactNode): boolean { | ||
| return children != null && children !== false && children !== ''; | ||
| } | ||
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
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
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
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,46 @@ | ||
| import * as SeparatorStories from './separator.stories'; | ||
|
|
||
| # Separator | ||
|
|
||
| Separator draws a horizontal line between blocks of content. Without children it is a plain 1px line. With children it is a labelled divider, with a line on each side of the label. It renders a `div` with `role='separator'`; the label is presentational and is not announced by screen readers. | ||
|
|
||
| ## Example | ||
|
|
||
| <Story | ||
| name='Default' | ||
| storyModule={SeparatorStories} | ||
| /> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```tsx | ||
| import { Separator } from '@clerk/mosaic/components/separator'; | ||
|
|
||
| <Separator /> | ||
| <Separator>or</Separator> | ||
| ``` | ||
|
|
||
| <Story | ||
| name='Labelled' | ||
| storyModule={SeparatorStories} | ||
| /> | ||
|
|
||
| ## Parts | ||
|
|
||
| | Part | Element | Class | Description | | ||
| | ----------- | ------- | --------------- | --------------------------------------------------------------- | | ||
| | `Separator` | `div` | `.cl-separator` | The line, or with children the label and a line on either side. | | ||
|
|
||
| `Separator` accepts `render` and `xstyle` and forwards its ref. | ||
|
|
||
| ## Styling | ||
|
|
||
| `data-labelled` is set when the separator has children. The labelled lines are the root's `::before` and `::after`. Both forms read `--cl-color-border` for the line; the label reads `--cl-color-foreground-secondary`. | ||
|
|
||
| ```css | ||
| .cl-separator:not([data-labelled]), | ||
| .cl-separator::before, | ||
| .cl-separator::after { | ||
| background-color: var(--cl-color-border-subtle); | ||
| } | ||
| ``` |
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,20 @@ | ||
| import { Separator } from '@clerk/mosaic/components/separator'; | ||
|
|
||
| import type { StoryMeta } from '@/lib/types'; | ||
|
|
||
| export { default as __source } from './separator.stories?raw'; | ||
|
|
||
| export const meta: StoryMeta = { | ||
| group: 'Components', | ||
| status: 'stable', | ||
| title: 'Separator', | ||
| source: 'packages/mosaic/src/components/separator/separator.tsx', | ||
| }; | ||
|
|
||
| export function Default() { | ||
| return <Separator />; | ||
| } | ||
|
|
||
| export function Labelled() { | ||
| return <Separator>or</Separator>; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.