Add primitive Text component to @wordpress/ui#75870
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: 0 B Total Size: 6.89 MB ℹ️ View Unchanged
|
ciampo
left a comment
There was a problem hiding this comment.
The general approach looks good, although I thought we discussed splitting Text (defaults to span) from Heading (defaults to h2), and split the variants accordingly.
Also, I wonder if we should add, to both components, an unstyled variant — although that can be something that we add only if/when necessary (and maybe never).
Also, we could add some basic unit tests (checking ref forwarding and render prop)
Hm, my initial reaction would be that this is exactly what So rather than... <Text variant="body-md" />;
<Text render={ <h2 /> } variant="heading-2xl" />;We could have: <Text variant="md" />;
<Heading variant="2xl" />;But a couple problems:
|
|
@aduth you make good points. Maybe we could continue to adopt a "lazy" approach, starting with a unified |
Sounds good to me 👍 |
|
@jameskoster we have consensus re. keeping only Let's also think about follow-ups:
|
|
I had the same conversation about a dedicated I'll address the remaining feedback shortly. |
|
@aduth I updated |
aduth
left a comment
There was a problem hiding this comment.
Couple non-blocking comments. Looking good 👍 I think we should make the change to avoid the div render override in Notice.Description.
aduth
left a comment
There was a problem hiding this comment.
LGTM 🚀
It could be nice to have some basic component tests here, particularly behaviors like ref forwarding, etc. Component tests probably end up being a little less valuable for a component like this, since it's largely responsible for controlling styles, and tests would end up testing implementation details rather than expected behaviors. This would be a good example for visual regression testing if we had it.
|
Flaky tests detected in 6f04d4f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/22867237462
|
| * Built on design tokens for consistent typography across the UI. | ||
| */ | ||
| export const Text = forwardRef< HTMLSpanElement, TextProps >( function Text( | ||
| { variant, render, className, ...props }, |
There was a problem hiding this comment.
Should we have a default value for variant? maybe body-md ? In case we do, we should also change the JSDoc for the type to add a @default tag
There was a problem hiding this comment.
I could go either way on this, to be honest. Having a default is nice because it keeps the most common case (body text) cleaner: <Text>Hello</Text> instead of <Text variant="body-md">Hello</Text>. That said, if variant is optional and relies on a default, changing that default later could be considered a breaking change; any <Text> instance without an explicit variant would suddenly render differently. I guess that's quite unlikely though, and maybe it's okay anyway?
I suppose another question is if it would ever be desirable for a consumer to use Text with custom styling (to utilise potential future enhancements like truncation etc). If so then supplying a default would probably mean we also need to add an 'Unstyled' variant.
I'm torn on this, so I'll defer to y'all :)
There was a problem hiding this comment.
Personally, I'm leaning toward picking a default variant, since:
- picking a
variantis the main reason why consumers would useText, and body text would be overwhelmingly the most frequent choice (and a healthy default); - we will very likely continue to offer a
body-mdvariant in the future, and therefore, we'll rarely introduce a hard breaking change (I don't think that keeping the variant name but tweaking styles should be seen as breaking); - the only two "breaking" scenarios would be:
- if we changed the default variant to another variant (visualy breaking, although it won't cuase build/browser errors)
- if we completely overhauled the text variant naming. But even them, given that this package is not bundled, we would be able to relese a major version and flag it as a brekaing change
- if we needed an
unstyledversion (TBD), we can add it at a later point - this API design would be consistent with other components, eg
Button
What do the rest of @WordPress/gutenberg-components folks think?
There was a problem hiding this comment.
I have a very soft preference for the default as implemented too, particularly if we think it'll be a common enough default for body text. I don't think it really hurts, and if we didn't provide a default, then I'd think variant ought be required, since it's kinda the whole point of the component, no? If we need support for an unstyled variant (and I'm not convinced we do), agree that we can add that as an option, consistent with other components like Button.
Although in practice I wonder how much this would be used. A lot of people would probably be adopting the component with a specific variant in mind, or maybe it's auto-generated from some Figma design that has the variant specified. It doesn't hurt to redundantly specify variant="body-md", but it does take that extra mental step to think to remove it in favor of the default.
ciampo
left a comment
There was a problem hiding this comment.
LGTM 🚀
I guess we can merge as-is to unblock other work, and iterate as needed.
Good to merge when CHANGELOG tweaks (+ conflict resolution) are applied :) Thank you, Jay 🙌
9601093 to
d240a10
Compare
Introduces a Text component with predefined typographic variants (heading-2xl through heading-sm, body-xl through body-sm) built on design tokens from @wordpress/theme. Includes Storybook stories. Co-authored-by: Cursor <[email protected]>
Replace manual font declarations in Notice's Title and Description with the Text component, delegating typography to the shared heading-md and body-md variants. Made-with: Cursor
Use Text's default span element for Description, simplifying the implementation to match Title's pattern. Made-with: Cursor
Made-with: Cursor
Co-authored-by: Andrew Duthie <[email protected]>
Update ref type and assertion to HTMLSpanElement to match the Description component now rendering as a span via Text. Made-with: Cursor
Make variant optional with a body-md default, consistent with other components like Button that provide sensible defaults. Made-with: Cursor
Co-authored-by: Marco Ciampini <[email protected]>
Co-authored-by: Marco Ciampini <[email protected]>
d240a10 to
8df73c5
Compare
What
Adds a primitive
Textcomponent to@wordpress/uiwith 9 predefined typographic variants:heading-2xlheading-xlheading-lgheading-mdheading-smbody-xlbody-lgbody-mdbody-smWhy
There's currently no standard way to render text with consistent typographic styles in
@wordpress/ui. Components like Button and Badge each define their own typography inline, but for standalone text content (headings, paragraphs, captions), consumers have to manually compose the correct combination of font tokens. A dedicatedTextcomponent gives a single, token-backed API for this.How
BadgeandStack:forwardRef,useRender/mergePropsfrom@base-ui/react, CSS Modules with design tokens, and therenderprop for element customization.--wpds-font-*custom properties from@wordpress/theme. No inline style computation — all styles are static CSS.variant(required) controls typography,renderallows changing the HTML element (e.g.,render={<h1 />}), and standardclassName/childrenprops are passed through. No individual typography override props — consumers use CSS custom properties in their own stylesheets when overrides are needed.renderprop example.Testing
npm run storybookTextcomponent at http://localhost:50240/?path=/docs/design-system-components-text--docs&args=variant:body-sm