-
Notifications
You must be signed in to change notification settings - Fork 461
feat(ui): add Mosaic Badge component #9221
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import * as BadgeStories from './badge.stories'; | ||
|
|
||
| # Badge | ||
|
|
||
| Badge labels the status or category of the thing next to it. It is presentational only — it renders a `span`, has no interactive behavior, and its `intent` carries meaning through color, so keep the label itself self-describing for anyone who can't see it. | ||
|
|
||
| ## Playground | ||
|
|
||
| <Preview | ||
| name='Primary' | ||
| storyModule={BadgeStories} | ||
| /> | ||
|
|
||
| ## Props | ||
|
|
||
| <PropTable meta={BadgeStories.meta} /> | ||
|
|
||
| ## Usage | ||
|
|
||
| <Usage | ||
| component='Badge' | ||
| module='@clerk/ui/mosaic/components/badge' | ||
| > | ||
| Badge Label | ||
| </Usage> | ||
|
|
||
| --- | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Intents | ||
|
|
||
| <Story | ||
| name='Intents' | ||
| storyModule={BadgeStories} | ||
| /> | ||
|
|
||
| ### With an icon | ||
|
|
||
| <Story | ||
| name='WithIcon' | ||
| storyModule={BadgeStories} | ||
| /> |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,95 @@ | ||||||||||||||
| import type { BadgeProps } from '@clerk/ui/mosaic/components/badge'; | ||||||||||||||
| import { Badge } from '@clerk/ui/mosaic/components/badge'; | ||||||||||||||
|
Comment on lines
+1
to
+2
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add the required Emotion JSX pragma. This story renders a styled Mosaic component but lacks the required top-level +/** `@jsxImportSource` `@emotion/react` */
+
import type { BadgeProps } from '`@clerk/ui/mosaic/components/badge`';As per coding guidelines, “Use Emotion pragma 📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||||||
|
|
||||||||||||||
| import type { StoryMeta } from '@/lib/types'; | ||||||||||||||
|
|
||||||||||||||
| // Exposes this file's own source (via the `?raw` webpack rule) so each `<Story>` example | ||||||||||||||
| // renders a code footer with its function's source. See `StoryModule.__source`. | ||||||||||||||
| export { default as __source } from './badge.stories?raw'; | ||||||||||||||
|
|
||||||||||||||
| // StyleX has no runtime recipe to derive knobs from, so the variant surface is described | ||||||||||||||
| // here to drive the playground + prop table. Keys mirror `BadgeProps`. | ||||||||||||||
| export const meta: StoryMeta = { | ||||||||||||||
| group: 'Components', | ||||||||||||||
| title: 'Badge', | ||||||||||||||
| source: 'packages/ui/src/mosaic/components/badge/badge.tsx', | ||||||||||||||
| styles: { | ||||||||||||||
| _variants: { | ||||||||||||||
| intent: { primary: {}, secondary: {}, warning: {}, destructive: {}, success: {} }, | ||||||||||||||
| }, | ||||||||||||||
| _defaultVariants: { | ||||||||||||||
| intent: 'primary', | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| // Story functions accept Record<string,unknown> (knob values) and cast to BadgeProps. | ||||||||||||||
| // The cast is unavoidable: knobs are dynamically typed; Badge has a strict prop interface. | ||||||||||||||
| function knobsAsProps(props: Record<string, unknown>) { | ||||||||||||||
| return props as unknown as BadgeProps; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function Primary(props: Record<string, unknown>) { | ||||||||||||||
| return <Badge {...knobsAsProps(props)}>Badge Label</Badge>; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function Intents(props: Record<string, unknown>) { | ||||||||||||||
| return ( | ||||||||||||||
| <div style={{ display: 'flex', gap: 8, alignItems: 'center', flexWrap: 'wrap' }}> | ||||||||||||||
| <Badge | ||||||||||||||
| {...knobsAsProps(props)} | ||||||||||||||
| intent='primary' | ||||||||||||||
| > | ||||||||||||||
| Primary | ||||||||||||||
| </Badge> | ||||||||||||||
| <Badge | ||||||||||||||
| {...knobsAsProps(props)} | ||||||||||||||
| intent='secondary' | ||||||||||||||
| > | ||||||||||||||
| Secondary | ||||||||||||||
| </Badge> | ||||||||||||||
| <Badge | ||||||||||||||
| {...knobsAsProps(props)} | ||||||||||||||
| intent='warning' | ||||||||||||||
| > | ||||||||||||||
| Warning | ||||||||||||||
| </Badge> | ||||||||||||||
| <Badge | ||||||||||||||
| {...knobsAsProps(props)} | ||||||||||||||
| intent='destructive' | ||||||||||||||
| > | ||||||||||||||
| Destructive | ||||||||||||||
| </Badge> | ||||||||||||||
| <Badge | ||||||||||||||
| {...knobsAsProps(props)} | ||||||||||||||
| intent='success' | ||||||||||||||
| > | ||||||||||||||
| Success | ||||||||||||||
| </Badge> | ||||||||||||||
| </div> | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function WithIcon(props: Record<string, unknown>) { | ||||||||||||||
| return ( | ||||||||||||||
| <Badge | ||||||||||||||
| {...knobsAsProps(props)} | ||||||||||||||
| intent='success' | ||||||||||||||
| > | ||||||||||||||
| <svg | ||||||||||||||
| width='10' | ||||||||||||||
| height='10' | ||||||||||||||
| viewBox='0 0 24 24' | ||||||||||||||
| fill='none' | ||||||||||||||
| stroke='currentColor' | ||||||||||||||
| strokeWidth='3' | ||||||||||||||
| strokeLinecap='round' | ||||||||||||||
| strokeLinejoin='round' | ||||||||||||||
| style={{ flexShrink: 0 }} | ||||||||||||||
| > | ||||||||||||||
| <path d='M20 6 9 17l-5-5' /> | ||||||||||||||
| </svg> | ||||||||||||||
| Verified | ||||||||||||||
| </Badge> | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import * as stylex from '@stylexjs/stylex'; | ||
|
|
||
| import { colorVars, radiusVars, space, typeScaleVars } from '../../tokens.stylex'; | ||
|
|
||
| // Every intent is the same recipe applied to one token: the token itself is the | ||
| // text color, a 12% mix is the fill and a 24% mix the border. Mixing toward | ||
| // `transparent` rather than a surface token keeps the badge legible on any | ||
| // background it's dropped onto. | ||
| export const styles = stylex.create({ | ||
| base: { | ||
| borderRadius: radiusVars['--cl-radius-inner'], | ||
| borderStyle: 'solid', | ||
| borderWidth: '1px', | ||
| gap: space['1'], | ||
| paddingInline: space['1.5'], | ||
| alignItems: 'center', | ||
| boxSizing: 'border-box', | ||
| display: 'inline-flex', | ||
| fontFamily: 'inherit', | ||
| fontSize: typeScaleVars['--cl-text-label-sm-size'], | ||
| fontWeight: typeScaleVars['--cl-text-label-sm-weight'], | ||
| justifyContent: 'center', | ||
| lineHeight: typeScaleVars['--cl-text-label-sm-leading'], | ||
| whiteSpace: 'nowrap', | ||
| height: space['5'], | ||
| }, | ||
|
|
||
| primary: { | ||
| borderColor: `color-mix(in oklab, ${colorVars['--cl-color-primary']} 24%, transparent)`, | ||
| backgroundColor: `color-mix(in oklab, ${colorVars['--cl-color-primary']} 12%, transparent)`, | ||
| color: colorVars['--cl-color-primary'], | ||
| }, | ||
| secondary: { | ||
| borderColor: `color-mix(in oklab, ${colorVars['--cl-color-muted-foreground']} 24%, transparent)`, | ||
| backgroundColor: `color-mix(in oklab, ${colorVars['--cl-color-muted-foreground']} 12%, transparent)`, | ||
| color: colorVars['--cl-color-muted-foreground'], | ||
| }, | ||
| warning: { | ||
| borderColor: `color-mix(in oklab, ${colorVars['--cl-color-warning']} 24%, transparent)`, | ||
| backgroundColor: `color-mix(in oklab, ${colorVars['--cl-color-warning']} 12%, transparent)`, | ||
| color: colorVars['--cl-color-warning'], | ||
| }, | ||
| destructive: { | ||
| borderColor: `color-mix(in oklab, ${colorVars['--cl-color-destructive']} 24%, transparent)`, | ||
| backgroundColor: `color-mix(in oklab, ${colorVars['--cl-color-destructive']} 12%, transparent)`, | ||
| color: colorVars['--cl-color-destructive'], | ||
| }, | ||
| success: { | ||
| borderColor: `color-mix(in oklab, ${colorVars['--cl-color-success']} 24%, transparent)`, | ||
| backgroundColor: `color-mix(in oklab, ${colorVars['--cl-color-success']} 12%, transparent)`, | ||
| color: colorVars['--cl-color-success'], | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { Badge } from './badge'; | ||
|
|
||
| describe('Mosaic Badge', () => { | ||
| it('renders its children', () => { | ||
| render(<Badge>Active</Badge>); | ||
| expect(screen.getByText('Active')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('applies the default intent when none is passed', () => { | ||
| render(<Badge>Active</Badge>); | ||
| const badge = screen.getByText('Active'); | ||
| expect(badge).toHaveClass('cl-badge'); | ||
| expect(badge).toHaveAttribute('data-intent', 'primary'); | ||
| }); | ||
|
|
||
| it.each(['primary', 'secondary', 'warning', 'destructive', 'success'] as const)('reflects the %s intent', intent => { | ||
| render(<Badge intent={intent}>Active</Badge>); | ||
| expect(screen.getByText('Active')).toHaveAttribute('data-intent', intent); | ||
| }); | ||
|
|
||
| it('lets the consumer className and style win', () => { | ||
| render( | ||
| <Badge | ||
| className='my-badge' | ||
| style={{ marginTop: '8px' }} | ||
| > | ||
| Active | ||
| </Badge>, | ||
| ); | ||
| const badge = screen.getByText('Active'); | ||
| expect(badge).toHaveClass('cl-badge', 'my-badge'); | ||
| expect(badge).toHaveStyle({ marginTop: '8px' }); | ||
| }); | ||
|
|
||
| it('forwards arbitrary span props and the ref', () => { | ||
| const ref = React.createRef<HTMLSpanElement>(); | ||
| render( | ||
| <Badge | ||
| ref={ref} | ||
| id='status' | ||
| aria-label='Status' | ||
| > | ||
| Active | ||
| </Badge>, | ||
| ); | ||
| const badge = screen.getByText('Active'); | ||
| expect(ref.current).toBe(badge); | ||
| expect(badge).toHaveAttribute('id', 'status'); | ||
| expect(badge).toHaveAttribute('aria-label', 'Status'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||||||
| import * as stylex from '@stylexjs/stylex'; | ||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||
|
|
||||||||||||||||||
| import { mergeProps, themeProps } from '../../props'; | ||||||||||||||||||
| import { styles } from './badge.styles'; | ||||||||||||||||||
|
|
||||||||||||||||||
| export interface BadgeProps extends React.ComponentPropsWithRef<'span'> { | ||||||||||||||||||
| intent?: 'primary' | 'secondary' | 'warning' | 'destructive' | 'success'; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export const Badge = React.forwardRef<HTMLSpanElement, BadgeProps>(function MosaicBadge( | ||||||||||||||||||
| { intent = 'primary', className, style, children, ...rest }, | ||||||||||||||||||
| ref, | ||||||||||||||||||
| ) { | ||||||||||||||||||
| return ( | ||||||||||||||||||
| <span | ||||||||||||||||||
| ref={ref} | ||||||||||||||||||
| {...mergeProps(themeProps('badge', { intent }), stylex.props(styles.base, styles[intent]), className, style)} | ||||||||||||||||||
| {...rest} | ||||||||||||||||||
|
Comment on lines
+18
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Prevent consumers from overriding the resolved intent.
Proposed fix <span
ref={ref}
+ {...rest}
{...mergeProps(themeProps('badge', { intent }), stylex.props(styles.base, styles[intent]), className, style)}
- {...rest}
>📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| > | ||||||||||||||||||
| {children} | ||||||||||||||||||
| </span> | ||||||||||||||||||
| ); | ||||||||||||||||||
| }); | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { Badge } from './badge'; | ||
| export type { BadgeProps } from './badge'; |
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.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Add an
@clerk/uirelease entry.This introduces a public
Badgecomponent and props API, but the empty changeset produces no package bump or changelog entry. Add an appropriate minor@clerk/uichangeset.Based on learnings, empty changesets are acceptable only for documentation-only or non-published work.
🤖 Prompt for AI Agents
Sources: Coding guidelines, Learnings