-
Notifications
You must be signed in to change notification settings - Fork 4.8k
UI: Remove Box component abstraction #74986
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
5 commits
Select commit
Hold shift + click to select a range
b533504
UI: Remove Box component abstraction
aduth 68e7713
Update Stack stories to remove references to Box
aduth 02186e4
Align Badge forwarded ref generic to rendered element
aduth f4ee7f6
Use defaultTagName over function for default render
aduth 3e4cfef
Shift Badge styles to stylesheet
aduth 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
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,88 +1,29 @@ | ||
| import { useRender, mergeProps } from '@base-ui/react'; | ||
| import clsx from 'clsx'; | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { Box } from '../box'; | ||
| import { type BoxProps } from '../box/types'; | ||
| import { type BadgeProps } from './types'; | ||
|
|
||
| /** | ||
| * Default render function that renders a span element with the given props. | ||
| */ | ||
| const DEFAULT_RENDER = ( props: React.ComponentPropsWithoutRef< 'span' > ) => ( | ||
| <span { ...props } /> | ||
| ); | ||
|
|
||
| /** | ||
| * Maps intent values to Box backgroundColor and color props. | ||
| * Uses strong emphasis styles (as emphasis prop has been removed). | ||
| */ | ||
| const getIntentStyles = ( | ||
| intent: BadgeProps[ 'intent' ] | ||
| ): Partial< BoxProps > => { | ||
| switch ( intent ) { | ||
| case 'high': | ||
| return { | ||
| backgroundColor: 'error', | ||
| color: 'error', | ||
| }; | ||
| case 'medium': | ||
| return { | ||
| backgroundColor: 'warning', | ||
| color: 'warning', | ||
| }; | ||
| case 'low': | ||
| return { | ||
| backgroundColor: 'caution', | ||
| color: 'caution', | ||
| }; | ||
| case 'stable': | ||
| return { | ||
| backgroundColor: 'success', | ||
| color: 'success', | ||
| }; | ||
| case 'informational': | ||
| return { | ||
| backgroundColor: 'info', | ||
| color: 'info', | ||
| }; | ||
| case 'draft': | ||
| return { | ||
| backgroundColor: 'neutral-weak', | ||
| color: 'neutral', | ||
| }; | ||
| case 'none': | ||
| default: | ||
| return { | ||
| backgroundColor: 'neutral', | ||
| color: 'neutral-weak', | ||
| }; | ||
| } | ||
| }; | ||
| import styles from './style.module.css'; | ||
|
|
||
| /** | ||
| * A badge component for displaying labels with semantic intent. | ||
| * Built on the Box primitive for consistent theming and accessibility. | ||
| */ | ||
| export const Badge = forwardRef< HTMLDivElement, BadgeProps >( function Badge( | ||
| { children, intent = 'none', render = DEFAULT_RENDER, ...props }, | ||
| export const Badge = forwardRef< HTMLSpanElement, BadgeProps >( function Badge( | ||
| { children, intent = 'none', render, className, ...props }, | ||
| ref | ||
| ) { | ||
| const intentStyles = getIntentStyles( intent ); | ||
| const element = useRender( { | ||
| render, | ||
| defaultTagName: 'span', | ||
| ref, | ||
| props: mergeProps< 'span' >( props, { | ||
| className: clsx( | ||
| styles.badge, | ||
| styles[ `is-${ intent }-intent` ], | ||
| className | ||
| ), | ||
| children, | ||
| } ), | ||
| } ); | ||
|
|
||
| return ( | ||
| <Box | ||
| { ...intentStyles } | ||
| padding={ { inline: 'sm', block: 'xs' } } | ||
| borderRadius="lg" | ||
| render={ render } | ||
| style={ { | ||
| fontFamily: 'var(--wpds-font-family-body)', | ||
| fontSize: 'var(--wpds-font-size-sm)', | ||
| fontWeight: 'var(--wpds-font-weight-regular)', | ||
| lineHeight: 'var(--wpds-font-line-height-xs)', | ||
| ...props.style, | ||
| } } | ||
| ref={ ref } | ||
| > | ||
| { children } | ||
| </Box> | ||
| ); | ||
| 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,48 @@ | ||
| @layer wp-ui-utilities, wp-ui-components, wp-ui-compositions, wp-ui-overrides; | ||
|
|
||
| @layer wp-ui-components { | ||
| .badge { | ||
| padding-inline: var(--wpds-dimension-padding-sm); | ||
| padding-block: var(--wpds-dimension-padding-xs); | ||
| border-radius: var(--wpds-border-radius-lg); | ||
| font-family: var(--wpds-font-family-body); | ||
| font-size: var(--wpds-font-size-sm); | ||
| font-weight: var(--wpds-font-weight-regular); | ||
| line-height: var(--wpds-font-line-height-xs); | ||
| } | ||
|
|
||
| .is-high-intent { | ||
| background-color: var(--wpds-color-bg-surface-error); | ||
| color: var(--wpds-color-fg-content-error); | ||
| } | ||
|
|
||
| .is-medium-intent { | ||
| background-color: var(--wpds-color-bg-surface-warning); | ||
| color: var(--wpds-color-fg-content-warning); | ||
| } | ||
|
|
||
| .is-low-intent { | ||
| background-color: var(--wpds-color-bg-surface-caution); | ||
| color: var(--wpds-color-fg-content-caution); | ||
| } | ||
|
|
||
| .is-stable-intent { | ||
| background-color: var(--wpds-color-bg-surface-success); | ||
| color: var(--wpds-color-fg-content-success); | ||
| } | ||
|
|
||
| .is-informational-intent { | ||
| background-color: var(--wpds-color-bg-surface-info); | ||
| color: var(--wpds-color-fg-content-info); | ||
| } | ||
|
|
||
| .is-draft-intent { | ||
| background-color: var(--wpds-color-bg-surface-neutral-weak); | ||
| color: var(--wpds-color-fg-content-neutral); | ||
| } | ||
|
|
||
| .is-none-intent { | ||
| background-color: var(--wpds-color-bg-surface-neutral); | ||
| color: var(--wpds-color-fg-content-neutral-weak); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
I considered a CSS property like what we do in Button, but ... seems overkill? And maybe encourages extensibility we don't want? This mirrors the existing implementation.