-
Notifications
You must be signed in to change notification settings - Fork 476
feat(mosaic): add ActionBar component #9859
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,5 @@ | ||
| --- | ||
| '@clerk/mosaic': patch | ||
| --- | ||
|
|
||
| Add an ActionBar component for contextual actions on a current selection. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import * as stylex from '@stylexjs/stylex'; | ||
|
|
||
| import { | ||
| colorVars, | ||
| durationVars, | ||
| easingVars, | ||
| fontWeightVars, | ||
| radiusVars, | ||
| shadowVars, | ||
| space, | ||
| typeScaleVars, | ||
| } from '../../tokens.stylex'; | ||
|
|
||
| const reduceMotion = '@media (prefers-reduced-motion: reduce)'; | ||
|
|
||
| export const styles = stylex.create({ | ||
| positioner: { | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| pointerEvents: 'none', | ||
| zIndex: 1, | ||
| bottom: space['2'], | ||
| }, | ||
| // Anchored to the nearest positioned ancestor — for a bounded container. | ||
| positionerAbsolute: { | ||
| insetInline: 0, | ||
| position: 'absolute', | ||
| }, | ||
| // Pinned to the foot of the nearest scroll container, so it stays in view while its surface | ||
| // scrolls. `margin-block-start: auto` drops it to the bottom when the column has room to spare. | ||
| positionerSticky: { | ||
| marginBlockStart: 'auto', | ||
| position: 'sticky', | ||
| }, | ||
| bar: { | ||
| borderRadius: radiusVars['--cl-radius-lg'], | ||
| gap: space['1'], | ||
| paddingBlock: space['1'], | ||
| paddingInline: space['1'], | ||
| alignItems: 'center', | ||
| backgroundColor: colorVars['--cl-color-background'], | ||
| boxShadow: shadowVars['--cl-shadow-lg'], | ||
| display: 'flex', | ||
| opacity: { | ||
| default: 1, | ||
| ':is([data-open="false"])': 0, | ||
| }, | ||
| pointerEvents: { | ||
| default: 'auto', | ||
| ':is([data-open="false"])': 'none', | ||
| }, | ||
| transform: { | ||
| default: 'translateY(0)', | ||
| ':is([data-open="false"])': 'translateY(0.25rem)', | ||
| }, | ||
| transitionDuration: { | ||
| default: `${durationVars['--cl-duration-base']}, ${durationVars['--cl-duration-base']}`, | ||
| [reduceMotion]: `${durationVars['--cl-duration-instant']}, ${durationVars['--cl-duration-instant']}`, | ||
| ':is([data-open="false"])': `${durationVars['--cl-duration-instant']}, ${durationVars['--cl-duration-instant']}`, | ||
| }, | ||
| transitionProperty: 'opacity, transform', | ||
| transitionTimingFunction: { | ||
| default: `linear, ${easingVars['--cl-ease-enter']}`, | ||
| }, | ||
| maxWidth: 'calc(100% - 2 * var(--cl-spacing))', | ||
| }, | ||
| count: { | ||
| paddingInline: space['2'], | ||
| color: colorVars['--cl-color-foreground'], | ||
| fontSize: typeScaleVars['--cl-text-sm-size'], | ||
| fontWeight: fontWeightVars['--cl-font-medium'], | ||
| lineHeight: typeScaleVars['--cl-text-sm-leading'], | ||
| whiteSpace: 'nowrap', | ||
| }, | ||
| separator: { | ||
| marginBlock: space['1'], | ||
| marginInline: space['0.5'], | ||
| alignSelf: 'stretch', | ||
| backgroundColor: colorVars['--cl-color-border'], | ||
| width: '1px', | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import React from 'react'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { ActionBar } from './action-bar'; | ||
|
|
||
| const testStyles = stylex.create({ | ||
| positioner: { | ||
| bottom: '20px', | ||
| }, | ||
| }); | ||
|
|
||
| describe('Mosaic ActionBar', () => { | ||
| it('renders a toolbar with its count and reflects data-open', () => { | ||
| render( | ||
| <ActionBar.Root | ||
| open | ||
| aria-label='Bulk actions' | ||
| > | ||
| <ActionBar.Count>3 selected</ActionBar.Count> | ||
| </ActionBar.Root>, | ||
| ); | ||
| const bar = screen.getByRole('toolbar', { name: 'Bulk actions' }); | ||
| expect(bar).toHaveClass('cl-action-bar'); | ||
| expect(bar).toHaveAttribute('data-open', 'true'); | ||
| const count = screen.getByText('3 selected'); | ||
| expect(count).toHaveClass('cl-action-bar-count'); | ||
| expect(count.querySelector('.cl-icon')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('marks the bar inert while closed', () => { | ||
| render( | ||
| <ActionBar.Root | ||
| open={false} | ||
| aria-label='Bulk actions' | ||
| > | ||
| <ActionBar.Count>0 selected</ActionBar.Count> | ||
| </ActionBar.Root>, | ||
| ); | ||
| const bar = screen.getByRole('toolbar', { name: 'Bulk actions', hidden: true }); | ||
| expect(bar).toHaveAttribute('data-open', 'false'); | ||
| expect(bar.inert).toBe(true); | ||
| }); | ||
|
|
||
| it('styles the positioner independently from the bar', () => { | ||
| render( | ||
| <ActionBar.Root | ||
| open | ||
| aria-label='Bulk actions' | ||
| positionerXstyle={testStyles.positioner} | ||
| />, | ||
| ); | ||
| expect(screen.getByRole('toolbar', { name: 'Bulk actions' }).parentElement).toHaveClass( | ||
| ...(stylex.props(testStyles.positioner).className as string).split(' '), | ||
| ); | ||
| }); | ||
|
|
||
| it('dismisses with a labelled button', async () => { | ||
| const onDismiss = vi.fn(); | ||
| render( | ||
| <ActionBar.Root | ||
| open | ||
| aria-label='Bulk actions' | ||
| > | ||
| <ActionBar.Dismiss onClick={onDismiss} /> | ||
| </ActionBar.Root>, | ||
| ); | ||
| await userEvent.click(screen.getByRole('button', { name: 'Clear selection' })); | ||
| expect(onDismiss).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,148 @@ | ||||||
| import * as stylex from '@stylexjs/stylex'; | ||||||
| import React from 'react'; | ||||||
|
|
||||||
| import { useRender } from '../../primitives/utils'; | ||||||
| import type { MosaicComponentProps, MosaicElementProps, XStyle } from '../../props'; | ||||||
| import { mergeStyleProps, themeProps } from '../../props'; | ||||||
| import { reset } from '../../utils/reset.styles'; | ||||||
| import { Button } from '../button'; | ||||||
| import { Icon } from '../icon'; | ||||||
| import { styles } from './action-bar.styles'; | ||||||
|
|
||||||
| export interface ActionBarRootProps extends MosaicComponentProps<'div'> { | ||||||
| /** Whether the bar is shown. Toggling it animates the bar in and out. */ | ||||||
| open: boolean; | ||||||
| /** | ||||||
| * Pins the bar to the foot of the nearest scroll container instead of a positioned ancestor, so | ||||||
| * it stays in view while a long surface scrolls (a `Profile` page, a modal). Place `ActionBar.Root` | ||||||
| * as the last child of that scrolling column. Off, the bar is absolute within the nearest | ||||||
| * positioned ancestor — give that ancestor `position: relative`. | ||||||
| * | ||||||
| * @default false | ||||||
| */ | ||||||
| sticky?: boolean; | ||||||
| positionerXstyle?: XStyle; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * A floating bar of actions for a selection. It sits at the foot of its container — the nearest | ||||||
| * positioned ancestor, or the nearest scroll container with `sticky` — and animates in and out with | ||||||
| * `open`. While `open` is false it is inert and click-through, so the surface underneath stays | ||||||
| * usable. Compose a count, a separator, the actions, and a dismiss: | ||||||
| * | ||||||
| * @example | ||||||
| * <ActionBar.Root open={count > 0}> | ||||||
| * <ActionBar.Count>{count} selected</ActionBar.Count> | ||||||
| * <ActionBar.Separator /> | ||||||
| * <Menu.Root>…</Menu.Root> | ||||||
| * <Button color='negative' variant='ghost' shape='square' aria-label='Remove'> | ||||||
| * <Icon name='trash' /> | ||||||
| * </Button> | ||||||
| * <ActionBar.Separator /> | ||||||
| * <ActionBar.Dismiss onClick={clearSelection} /> | ||||||
| * </ActionBar.Root> | ||||||
| */ | ||||||
| const Root = React.forwardRef<HTMLDivElement, ActionBarRootProps>(function ActionBarRoot( | ||||||
| { open, sticky = false, render, xstyle, positionerXstyle, children, ...rest }, | ||||||
| ref, | ||||||
| ) { | ||||||
| const barRef = React.useRef<HTMLDivElement>(null); | ||||||
| React.useEffect(() => { | ||||||
| if (barRef.current) { | ||||||
| barRef.current.inert = !open; | ||||||
| } | ||||||
| }, [open]); | ||||||
|
Comment on lines
+50
to
+54
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 🔎 Supported by static analysis🏁 Script executed: sed -n '27,90p' packages/mosaic/src/components/action-bar/action-bar.tsx
sed -n '185,245p' packages/mosaic/src/primitives/utils/use-render.tsx
rg -n '"react"|"`@types/react`"' package.json packages/*/package.jsonRepository: clerk/javascript Length of output: 5997 🏁 Script executed: #!/bin/bash
printf '%s\n' '--- action-bar declarations and useRender types ---'
sed -n '1,90p' packages/mosaic/src/components/action-bar/action-bar.tsx
sed -n '1,220p' packages/mosaic/src/primitives/utils/use-render.tsx
printf '%s\n' '--- mergeProps binding ---'
rg -n --glob '*.{ts,tsx}' 'function mergeProps|const mergeProps|export .*mergeProps|from .*mergeProps' packages/mosaic/src packages/shared/src | head -80
printf '%s\n' '--- inert in repository ---'
rg -n --glob '*.{ts,tsx,js,jsx,json}' '\binert\b' packages/mosaic packages/react packages/shared | head -120
printf '%s\n' '--- React and TypeScript versions/config ---'
sed -n '1,115p' packages/mosaic/package.json
sed -n '80,155p' package.json
rg -n --glob '{tsconfig*.json,*.tsx,*.ts}' 'jsx|react-dom/server|renderToString|renderToPipeableStream|renderToReadableStream' packages/mosaic packages/react | head -120Repository: clerk/javascript Length of output: 29453 🏁 Script executed: #!/bin/bash
printf '%s\n' '--- shared inert helper ---'
cat -n packages/shared/src/inert.ts
printf '%s\n' '--- React catalog versions ---'
rg -n -A8 -B3 'catalog:react|peer-react' pnpm-workspace.yaml package.json packages/shared/package.json
printf '%s\n' '--- ActionBar tests ---'
cat -n packages/mosaic/src/components/action-bar/action-bar.test.tsx
printf '%s\n' '--- helper consumers ---'
sed -n '1,80p' packages/mosaic/src/primitives/flow/flow-step.tsx
sed -n '1,70p' packages/mosaic/src/primitives/tabs/tabs-panel.tsx
sed -n '1,70p' packages/mosaic/src/primitives/toast/toast-root.tsxRepository: clerk/javascript Length of output: 14670 Render the closed state as inert.
Suggested fix+import { inertProps } from '`@clerk/shared/inert`';
import * as stylex from '`@stylexjs/stylex`';
import React from 'react';
@@
- const barRef = React.useRef<HTMLDivElement>(null);
- React.useEffect(() => {
- if (barRef.current) {
- barRef.current.inert = !open;
- }
- }, [open]);
-
const bar = useRender({
defaultTagName: 'div',
render,
- ref: barRef,
props: {
...mergeStyleProps(themeProps('action-bar', { open }), stylex.props(reset.base, styles.bar, xstyle), rest),
'data-open': open,
+ ...inertProps(!open),
role: 'toolbar',
children,
},🤖 Prompt for AI Agents |
||||||
|
|
||||||
| const bar = useRender({ | ||||||
| defaultTagName: 'div', | ||||||
| render, | ||||||
| ref: barRef, | ||||||
| props: { | ||||||
| ...mergeStyleProps(themeProps('action-bar', { open }), stylex.props(reset.base, styles.bar, xstyle), rest), | ||||||
| 'data-open': open, | ||||||
| role: 'toolbar', | ||||||
| children, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| return ( | ||||||
| <div | ||||||
| ref={ref} | ||||||
| {...mergeStyleProps( | ||||||
| themeProps('action-bar-positioner', { sticky }), | ||||||
| stylex.props( | ||||||
| reset.base, | ||||||
| styles.positioner, | ||||||
| sticky ? styles.positionerSticky : styles.positionerAbsolute, | ||||||
| positionerXstyle, | ||||||
| ), | ||||||
| )} | ||||||
| > | ||||||
| {bar} | ||||||
| </div> | ||||||
| ); | ||||||
| }); | ||||||
|
|
||||||
| export type ActionBarCountProps = MosaicComponentProps<'div'>; | ||||||
|
|
||||||
| /** The leading count of what is selected, e.g. `3 selected`. */ | ||||||
| const Count = React.forwardRef<HTMLDivElement, ActionBarCountProps>(function ActionBarCount( | ||||||
| { render, xstyle, children, ...rest }, | ||||||
| ref, | ||||||
| ) { | ||||||
| return useRender({ | ||||||
| defaultTagName: 'div', | ||||||
| render, | ||||||
| ref, | ||||||
| props: { | ||||||
| ...mergeStyleProps(themeProps('action-bar-count'), stylex.props(reset.base, styles.count, xstyle), rest), | ||||||
| children, | ||||||
| }, | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| export type ActionBarSeparatorProps = MosaicComponentProps<'div'>; | ||||||
|
|
||||||
| /** A vertical divider between groups of the bar. */ | ||||||
| const Separator = React.forwardRef<HTMLDivElement, ActionBarSeparatorProps>(function ActionBarSeparator( | ||||||
| { render, xstyle, ...rest }, | ||||||
| ref, | ||||||
| ) { | ||||||
| return useRender({ | ||||||
| defaultTagName: 'div', | ||||||
| render, | ||||||
| ref, | ||||||
| props: { | ||||||
| ...mergeStyleProps(themeProps('action-bar-separator'), stylex.props(reset.base, styles.separator), rest), | ||||||
|
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 Apply
Proposed fix- ...mergeStyleProps(themeProps('action-bar-separator'), stylex.props(reset.base, styles.separator), rest),
+ ...mergeStyleProps(themeProps('action-bar-separator'), stylex.props(reset.base, styles.separator, xstyle), rest),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| 'aria-hidden': true, | ||||||
| }, | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| export type ActionBarDismissProps = MosaicElementProps<'button'>; | ||||||
|
|
||||||
| /** Dismisses the bar. A ghost icon button; defaults its label to "Clear selection". */ | ||||||
| const Dismiss = React.forwardRef<HTMLButtonElement, ActionBarDismissProps>(function ActionBarDismiss( | ||||||
| { xstyle, 'aria-label': ariaLabel, ...rest }, | ||||||
| ref, | ||||||
| ) { | ||||||
| return ( | ||||||
| <Button | ||||||
| ref={ref} | ||||||
| variant='ghost' | ||||||
| shape='square' | ||||||
| size='md' | ||||||
| aria-label={ariaLabel ?? 'Clear selection'} | ||||||
| {...mergeStyleProps(themeProps('action-bar-dismiss'), stylex.props(xstyle), rest)} | ||||||
| > | ||||||
| <Icon name='x' /> | ||||||
| </Button> | ||||||
| ); | ||||||
| }); | ||||||
|
|
||||||
| /** | ||||||
| * A floating bar of actions for a current selection, composed through `ActionBar.Root`, | ||||||
| * `ActionBar.Count`, `ActionBar.Separator`, and `ActionBar.Dismiss`. The actions themselves | ||||||
| * (a `Menu`, a `Button`) are whatever children you place between them. | ||||||
| */ | ||||||
| export const ActionBar = { Root, Count, Separator, Dismiss }; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export { ActionBar } from './action-bar'; | ||
| export type { | ||
| ActionBarCountProps, | ||
| ActionBarDismissProps, | ||
| ActionBarRootProps, | ||
| ActionBarSeparatorProps, | ||
| } from './action-bar'; |
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: clerk/javascript
Length of output: 5901
🏁 Script executed:
Repository: clerk/javascript
Length of output: 19369
Document the entrance-only transition.
ActionBar.Rootuses a0sduration whenopenisfalse, so closing is immediate. Update both comments to describe an entrance-only animation.Suggested documentation fix
📝 Committable suggestion
🤖 Prompt for AI Agents