Repository navigation
Add primitive Text component to @wordpress/ui #75870
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d4f35e8
Add primitive Text component to @wordpress/ui
jameskoster 84c54aa
Remove superfluous stories
jameskoster ca3bf5b
Update changelog
jameskoster 25f2bb6
Make children required
jameskoster 583bcb0
Remove redundant fragment
jameskoster 3c7b7ca
Add PR link to changelog
jameskoster f6f1e81
Make children optional
jameskoster 83a44ad
Update Notice to use Text component for Title and Description
jameskoster 7cccc8e
Notice: Render Description as span instead of div
jameskoster e54314a
Text: Remove unnecessary margin reset from base class
jameskoster 1e14e9e
Update packages/ui/src/text/text.tsx
jameskoster 4241702
Fix Notice test to expect span for Description
jameskoster 330027f
Text: Default variant to body-md
jameskoster 0c3b5bf
Update packages/ui/CHANGELOG.md
jameskoster c6eaec3
Update packages/ui/CHANGELOG.md
jameskoster 8df73c5
remove duplicate CHANGELOG entry
ciampo 6f04d4f
Unit tests
ciampo 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
| @@ -1,23 +1,21 @@ | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { useRender, mergeProps } from '@base-ui/react'; | ||
| import clsx from 'clsx'; | ||
| import type { DescriptionProps } from './types'; | ||
| import { Text } from '../text'; | ||
| import styles from './style.module.css'; | ||
|
|
||
| /** | ||
| * The description text for a notice. | ||
| */ | ||
| export const Description = forwardRef< HTMLDivElement, DescriptionProps >( | ||
| function NoticeDescription( { render, ...props }, ref ) { | ||
| const element = useRender( { | ||
| defaultTagName: 'div', | ||
| render, | ||
| ref, | ||
| props: mergeProps< 'div' >( | ||
| { className: styles.description }, | ||
| props | ||
| ), | ||
| } ); | ||
|
|
||
| return element; | ||
| export const Description = forwardRef< HTMLSpanElement, DescriptionProps >( | ||
| function NoticeDescription( { className, ...props }, ref ) { | ||
| return ( | ||
| <Text | ||
| ref={ ref } | ||
| variant="body-md" | ||
| className={ clsx( styles.description, className ) } | ||
| { ...props } | ||
| /> | ||
| ); | ||
| } | ||
| ); |
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 |
|---|---|---|
| @@ -1,20 +1,21 @@ | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { useRender, mergeProps } from '@base-ui/react'; | ||
| import clsx from 'clsx'; | ||
| import type { TitleProps } from './types'; | ||
| import { Text } from '../text'; | ||
| import styles from './style.module.css'; | ||
|
|
||
| /** | ||
| * A short heading that communicates the main message of the notice. | ||
| */ | ||
| export const Title = forwardRef< HTMLSpanElement, TitleProps >( | ||
| function NoticeTitle( { render, ...props }, ref ) { | ||
| const element = useRender( { | ||
| defaultTagName: 'span', | ||
| render, | ||
| ref, | ||
| props: mergeProps< 'span' >( { className: styles.title }, props ), | ||
| } ); | ||
|
|
||
| return element; | ||
| function NoticeTitle( { className, ...props }, ref ) { | ||
| return ( | ||
| <Text | ||
| ref={ ref } | ||
| variant="heading-md" | ||
| className={ clsx( styles.title, className ) } | ||
| { ...props } | ||
| /> | ||
| ); | ||
| } | ||
| ); |
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 @@ | ||
| export { Text } from './text'; |
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,68 @@ | ||
| /* eslint-disable jsx-a11y/heading-has-content */ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite'; | ||
| import { Text } from '../index'; | ||
| import { Stack } from '../../stack'; | ||
|
|
||
| const meta: Meta< typeof Text > = { | ||
| title: 'Design System/Components/Text', | ||
| component: Text, | ||
| }; | ||
| export default meta; | ||
|
|
||
| type Story = StoryObj< typeof Text >; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| variant: 'body-md', | ||
| children: 'The quick brown fox jumps over the lazy dog.', | ||
| }, | ||
| }; | ||
|
|
||
| export const AllVariants: Story = { | ||
| render: () => ( | ||
| <Stack | ||
| direction="column" | ||
| gap="lg" | ||
| style={ { color: 'var(--wpds-color-fg-content-neutral)' } } | ||
| > | ||
| { ( | ||
| [ | ||
| 'heading-2xl', | ||
| 'heading-xl', | ||
| 'heading-lg', | ||
| 'heading-md', | ||
| 'heading-sm', | ||
| 'body-xl', | ||
| 'body-lg', | ||
| 'body-md', | ||
| 'body-sm', | ||
| ] as const | ||
| ).map( ( variant ) => ( | ||
| <Stack key={ variant } direction="column" gap="xs"> | ||
| <Text variant="heading-sm">{ variant }</Text> | ||
| <Text variant={ variant }> | ||
| The quick brown fox jumps over the lazy dog. | ||
| </Text> | ||
| </Stack> | ||
| ) ) } | ||
| </Stack> | ||
| ), | ||
| }; | ||
|
|
||
| export const WithRenderProp: Story = { | ||
| render: () => ( | ||
| <Stack direction="column" gap="md"> | ||
| <Text variant="heading-2xl" render={ <h1 /> }> | ||
| Page Title | ||
| </Text> | ||
| <Text variant="heading-xl" render={ <h2 /> }> | ||
| Section Heading | ||
| </Text> | ||
| <Text variant="body-md" render={ <p /> }> | ||
| A paragraph of body text rendered as a semantic paragraph | ||
| element. | ||
| </Text> | ||
| </Stack> | ||
| ), | ||
| }; | ||
| /* eslint-enable jsx-a11y/heading-has-content */ |
|
jameskoster marked this conversation as resolved.
|
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,67 @@ | ||
| @layer wp-ui-utilities, wp-ui-components, wp-ui-compositions, wp-ui-overrides; | ||
|
|
||
| @layer wp-ui-components { | ||
| .heading-2xl { | ||
| font-family: var(--wpds-font-family-heading); | ||
| font-size: var(--wpds-font-size-2xl); | ||
| line-height: var(--wpds-font-line-height-2xl); | ||
| font-weight: var(--wpds-font-weight-medium); | ||
| } | ||
|
|
||
| .heading-xl { | ||
| font-family: var(--wpds-font-family-heading); | ||
| font-size: var(--wpds-font-size-xl); | ||
| line-height: var(--wpds-font-line-height-md); | ||
|
ciampo marked this conversation as resolved.
|
||
| font-weight: var(--wpds-font-weight-medium); | ||
| } | ||
|
|
||
| .heading-lg { | ||
| font-family: var(--wpds-font-family-heading); | ||
| font-size: var(--wpds-font-size-lg); | ||
| line-height: var(--wpds-font-line-height-sm); | ||
| font-weight: var(--wpds-font-weight-medium); | ||
| } | ||
|
|
||
| .heading-md { | ||
| font-family: var(--wpds-font-family-heading); | ||
| font-size: var(--wpds-font-size-md); | ||
| line-height: var(--wpds-font-line-height-sm); | ||
| font-weight: var(--wpds-font-weight-medium); | ||
| } | ||
|
|
||
| .heading-sm { | ||
| font-family: var(--wpds-font-family-heading); | ||
| font-size: var(--wpds-font-size-xs); | ||
| line-height: var(--wpds-font-line-height-xs); | ||
| font-weight: var(--wpds-font-weight-medium); | ||
| text-transform: uppercase; | ||
| } | ||
|
|
||
| .body-xl { | ||
| font-family: var(--wpds-font-family-body); | ||
| font-size: var(--wpds-font-size-xl); | ||
| line-height: var(--wpds-font-line-height-xl); | ||
| font-weight: var(--wpds-font-weight-regular); | ||
| } | ||
|
|
||
| .body-lg { | ||
| font-family: var(--wpds-font-family-body); | ||
| font-size: var(--wpds-font-size-lg); | ||
| line-height: var(--wpds-font-line-height-md); | ||
| font-weight: var(--wpds-font-weight-regular); | ||
| } | ||
|
|
||
| .body-md { | ||
| font-family: var(--wpds-font-family-body); | ||
| font-size: var(--wpds-font-size-md); | ||
| line-height: var(--wpds-font-line-height-sm); | ||
| font-weight: var(--wpds-font-weight-regular); | ||
| } | ||
|
|
||
| .body-sm { | ||
| font-family: var(--wpds-font-family-body); | ||
| font-size: var(--wpds-font-size-sm); | ||
| line-height: var(--wpds-font-line-height-xs); | ||
| font-weight: var(--wpds-font-weight-regular); | ||
| } | ||
| } | ||
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 { render, screen } from '@testing-library/react'; | ||
| import { createRef } from '@wordpress/element'; | ||
| import { Text } from '../index'; | ||
|
|
||
| describe( 'Text', () => { | ||
| it( 'forwards ref', () => { | ||
| const ref = createRef< HTMLSpanElement >(); | ||
|
|
||
| render( <Text ref={ ref }>Content</Text> ); | ||
|
|
||
| expect( ref.current ).toBeInstanceOf( HTMLSpanElement ); | ||
| } ); | ||
|
|
||
| it( 'renders children', () => { | ||
| render( <Text>Hello world</Text> ); | ||
|
|
||
| expect( screen.getByText( 'Hello world' ) ).toBeVisible(); | ||
| } ); | ||
|
|
||
| it( 'forwards props to the rendered element', () => { | ||
| render( <Text data-testid="text">Content</Text> ); | ||
|
|
||
| expect( screen.getByTestId( 'text' ) ).toBeInTheDocument(); | ||
| } ); | ||
|
|
||
| it( 'forwards the className to the rendered element', () => { | ||
| render( | ||
| <Text data-testid="text" className="custom-class"> | ||
| Content | ||
| </Text> | ||
| ); | ||
|
|
||
| expect( screen.getByTestId( 'text' ) ).toHaveClass( 'custom-class' ); | ||
| } ); | ||
|
|
||
| it( 'supports the render prop', () => { | ||
| render( | ||
| // eslint-disable-next-line jsx-a11y/heading-has-content | ||
| <Text render={ <h2 /> }>Section title</Text> | ||
| ); | ||
|
|
||
| expect( | ||
| screen.getByRole( 'heading', { level: 2, name: 'Section title' } ) | ||
| ).toBeVisible(); | ||
| } ); | ||
| } ); |
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,25 @@ | ||
| import { useRender, mergeProps } from '@base-ui/react'; | ||
| import clsx from 'clsx'; | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { type TextProps } from './types'; | ||
| import styles from './style.module.css'; | ||
|
|
||
| /** | ||
| * A text component for rendering content with predefined typographic variants. | ||
| * Built on design tokens for consistent typography across the UI. | ||
| */ | ||
| export const Text = forwardRef< HTMLSpanElement, TextProps >( function Text( | ||
| { variant = 'body-md', render, className, ...props }, | ||
| ref | ||
| ) { | ||
| const element = useRender( { | ||
| render, | ||
| defaultTagName: 'span', | ||
| ref, | ||
| props: mergeProps< 'span' >( props, { | ||
| className: clsx( styles[ variant ], className ), | ||
| } ), | ||
| } ); | ||
|
|
||
| return element; | ||
| } ); |
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,25 @@ | ||
| import { type ComponentProps } from '../utils/types'; | ||
|
|
||
| export interface TextProps extends ComponentProps< 'span' > { | ||
| /** | ||
| * The typographic variant to apply, controlling font family, size, | ||
| * line height, and weight. | ||
| * | ||
| * @default "body-md" | ||
| */ | ||
| variant?: | ||
| | 'heading-2xl' | ||
| | 'heading-xl' | ||
| | 'heading-lg' | ||
| | 'heading-md' | ||
| | 'heading-sm' | ||
| | 'body-xl' | ||
| | 'body-lg' | ||
| | 'body-md' | ||
| | 'body-sm'; | ||
|
|
||
| /** | ||
| * The content to be rendered inside the component. | ||
| */ | ||
| children?: React.ReactNode; | ||
| } |
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.