diff --git a/.changeset/mosaic-component-entry-points.md b/.changeset/mosaic-component-entry-points.md new file mode 100644 index 00000000000..b24ecf805d7 --- /dev/null +++ b/.changeset/mosaic-component-entry-points.md @@ -0,0 +1,11 @@ +--- +'@clerk/mosaic': patch +--- + +Add component entry points: + +- `Card` from `@clerk/mosaic/components/card` and `Field` from `@clerk/mosaic/components/field` +- `Popover` from `@clerk/mosaic/components/popover` +- `ProviderButton` and `ProviderButtonGroup` from `@clerk/mosaic/components/provider-button` +- `Switcher` from `@clerk/mosaic/components/switcher`: `Switcher.Root`, `Switcher.Trigger` and `Switcher.Popup`, and the content parts `Switcher.Header`, `Switcher.Separator`, `Switcher.Group`, `Switcher.Item` (with `ItemMedia`, `ItemContent`, `ItemLabel`, `ItemDescription` and `ItemTrailing`), `Switcher.Avatar` and `Switcher.BadgedAvatar`. `UserButton` and `OrganizationSwitcher` are built from them +- The `Building`, `Checkmark`, `ChevronDown`, `ChevronRight`, `Cog6Teeth`, `EllipsisHorizontal`, `Key`, `SecurityLockSquare`, `SecurityPhone`, `SignOut`, `SwitchAccount` and `UsersAddRight` glyphs from `@clerk/mosaic/icons/glyphs/` diff --git a/packages/mosaic/package.json b/packages/mosaic/package.json index b73407d8ff9..ca1aaa26e26 100644 --- a/packages/mosaic/package.json +++ b/packages/mosaic/package.json @@ -56,6 +56,11 @@ "import": "./dist/components/button.js", "default": "./dist/components/button.js" }, + "./components/card": { + "types": "./dist/components/card.d.ts", + "import": "./dist/components/card.js", + "default": "./dist/components/card.js" + }, "./components/data-list": { "types": "./dist/components/data-list.d.ts", "import": "./dist/components/data-list.js", @@ -71,6 +76,11 @@ "import": "./dist/components/entity.js", "default": "./dist/components/entity.js" }, + "./components/field": { + "types": "./dist/components/field.d.ts", + "import": "./dist/components/field.js", + "default": "./dist/components/field.js" + }, "./components/heading": { "types": "./dist/components/heading.d.ts", "import": "./dist/components/heading.js", @@ -96,11 +106,21 @@ "import": "./dist/components/panel.js", "default": "./dist/components/panel.js" }, + "./components/popover": { + "types": "./dist/components/popover.d.ts", + "import": "./dist/components/popover.js", + "default": "./dist/components/popover.js" + }, "./components/profile": { "types": "./dist/components/profile.d.ts", "import": "./dist/components/profile.js", "default": "./dist/components/profile.js" }, + "./components/provider-button": { + "types": "./dist/components/provider-button.d.ts", + "import": "./dist/components/provider-button.js", + "default": "./dist/components/provider-button.js" + }, "./components/radio": { "types": "./dist/components/radio.d.ts", "import": "./dist/components/radio.js", @@ -121,6 +141,11 @@ "import": "./dist/components/skeleton.js", "default": "./dist/components/skeleton.js" }, + "./components/switcher": { + "types": "./dist/components/switcher.d.ts", + "import": "./dist/components/switcher.js", + "default": "./dist/components/switcher.js" + }, "./components/table": { "types": "./dist/components/table.d.ts", "import": "./dist/components/table.js", @@ -136,31 +161,91 @@ "import": "./dist/icons/glyphs/arrow-up-right.js", "default": "./dist/icons/glyphs/arrow-up-right.js" }, + "./icons/glyphs/building": { + "types": "./dist/icons/glyphs/building.d.ts", + "import": "./dist/icons/glyphs/building.js", + "default": "./dist/icons/glyphs/building.js" + }, + "./icons/glyphs/checkmark": { + "types": "./dist/icons/glyphs/checkmark.d.ts", + "import": "./dist/icons/glyphs/checkmark.js", + "default": "./dist/icons/glyphs/checkmark.js" + }, + "./icons/glyphs/chevron-down": { + "types": "./dist/icons/glyphs/chevron-down.d.ts", + "import": "./dist/icons/glyphs/chevron-down.js", + "default": "./dist/icons/glyphs/chevron-down.js" + }, + "./icons/glyphs/chevron-right": { + "types": "./dist/icons/glyphs/chevron-right.d.ts", + "import": "./dist/icons/glyphs/chevron-right.js", + "default": "./dist/icons/glyphs/chevron-right.js" + }, "./icons/glyphs/code": { "types": "./dist/icons/glyphs/code.d.ts", "import": "./dist/icons/glyphs/code.js", "default": "./dist/icons/glyphs/code.js" }, + "./icons/glyphs/cog-6-teeth": { + "types": "./dist/icons/glyphs/cog-6-teeth.d.ts", + "import": "./dist/icons/glyphs/cog-6-teeth.js", + "default": "./dist/icons/glyphs/cog-6-teeth.js" + }, + "./icons/glyphs/ellipsis-horizontal": { + "types": "./dist/icons/glyphs/ellipsis-horizontal.d.ts", + "import": "./dist/icons/glyphs/ellipsis-horizontal.js", + "default": "./dist/icons/glyphs/ellipsis-horizontal.js" + }, "./icons/glyphs/external": { "types": "./dist/icons/glyphs/external.d.ts", "import": "./dist/icons/glyphs/external.js", "default": "./dist/icons/glyphs/external.js" }, + "./icons/glyphs/key": { + "types": "./dist/icons/glyphs/key.d.ts", + "import": "./dist/icons/glyphs/key.js", + "default": "./dist/icons/glyphs/key.js" + }, "./icons/glyphs/plus": { "types": "./dist/icons/glyphs/plus.d.ts", "import": "./dist/icons/glyphs/plus.js", "default": "./dist/icons/glyphs/plus.js" }, + "./icons/glyphs/security-lock-square": { + "types": "./dist/icons/glyphs/security-lock-square.d.ts", + "import": "./dist/icons/glyphs/security-lock-square.js", + "default": "./dist/icons/glyphs/security-lock-square.js" + }, + "./icons/glyphs/security-phone": { + "types": "./dist/icons/glyphs/security-phone.d.ts", + "import": "./dist/icons/glyphs/security-phone.js", + "default": "./dist/icons/glyphs/security-phone.js" + }, "./icons/glyphs/shield-check": { "types": "./dist/icons/glyphs/shield-check.d.ts", "import": "./dist/icons/glyphs/shield-check.js", "default": "./dist/icons/glyphs/shield-check.js" }, + "./icons/glyphs/sign-out": { + "types": "./dist/icons/glyphs/sign-out.d.ts", + "import": "./dist/icons/glyphs/sign-out.js", + "default": "./dist/icons/glyphs/sign-out.js" + }, + "./icons/glyphs/switch-account": { + "types": "./dist/icons/glyphs/switch-account.d.ts", + "import": "./dist/icons/glyphs/switch-account.js", + "default": "./dist/icons/glyphs/switch-account.js" + }, "./icons/glyphs/user-circle": { "types": "./dist/icons/glyphs/user-circle.d.ts", "import": "./dist/icons/glyphs/user-circle.js", "default": "./dist/icons/glyphs/user-circle.js" }, + "./icons/glyphs/users-add-right": { + "types": "./dist/icons/glyphs/users-add-right.d.ts", + "import": "./dist/icons/glyphs/users-add-right.js", + "default": "./dist/icons/glyphs/users-add-right.js" + }, "./styles.css": { "types": "./styles.css.d.ts", "default": "./dist/styles.css" diff --git a/packages/mosaic/src/components/card/index.ts b/packages/mosaic/src/components/card/index.ts index b78516bb3e4..0c0d86c8722 100644 --- a/packages/mosaic/src/components/card/index.ts +++ b/packages/mosaic/src/components/card/index.ts @@ -1,3 +1,5 @@ +'use client'; + /** * A styled surface composed through `Card.Root`, `Card.Header`, `Card.Media`, `Card.Title`, * `Card.Description`, `Card.Banner`, `Card.Content`, and `Card.Footer`. Every part accepts the Mosaic diff --git a/packages/mosaic/src/components/field/index.ts b/packages/mosaic/src/components/field/index.ts index 8eff98a34ad..fab7c85e755 100644 --- a/packages/mosaic/src/components/field/index.ts +++ b/packages/mosaic/src/components/field/index.ts @@ -1,3 +1,5 @@ +'use client'; + /** Styled parts for composing an automatically associated single-control field. */ export * as Field from './field.parts'; export type { diff --git a/packages/mosaic/src/components/popover/index.ts b/packages/mosaic/src/components/popover/index.ts index 9f9a1f79cd6..b1ebacca9a1 100644 --- a/packages/mosaic/src/components/popover/index.ts +++ b/packages/mosaic/src/components/popover/index.ts @@ -1,3 +1,5 @@ +'use client'; + /** A floating box anchored to a trigger. */ export * as Popover from './popover.parts'; export type { diff --git a/packages/mosaic/src/components/provider-button/index.ts b/packages/mosaic/src/components/provider-button/index.ts index 39f99711aa9..ce5b679537e 100644 --- a/packages/mosaic/src/components/provider-button/index.ts +++ b/packages/mosaic/src/components/provider-button/index.ts @@ -1,3 +1,5 @@ +'use client'; + export { ProviderButton } from './provider-button'; export type { ProviderButtonProps } from './provider-button'; export { ProviderButtonGroup } from './provider-button-group'; diff --git a/packages/mosaic/src/components/switcher/index.ts b/packages/mosaic/src/components/switcher/index.ts new file mode 100644 index 00000000000..5538962d854 --- /dev/null +++ b/packages/mosaic/src/components/switcher/index.ts @@ -0,0 +1,56 @@ +'use client'; + +/** + * The account and organization switcher. `Switcher.Root` holds the open state, `Switcher.Trigger` + * opens it, and `Switcher.Popup` positions a card holding `Switcher.Header` and `Switcher.Group`s + * of `Switcher.Item`s. `UserButton`, `OrganizationSwitcher` and `Switcher` from `@clerk/mosaic` + * compose these parts with Clerk's session and organization state. + * + * ```tsx + * + * } + * name='Acme' + * badge='Pro' + * variant='full' + * /> + * + * } + * title='Acme' + * description='alice@example.com' + * /> + * + * + * }> + * + * + * + * + * Globex + * Member + * + * … + * + * + * + * + * ``` + */ +export * as Switcher from './switcher.parts'; +export type { SwitcherAvatarProps, SwitcherBadgedAvatarProps } from './switcher-avatar'; +export type { SwitcherHeaderProps } from './switcher-header'; +export type { + SwitcherGroupProps, + SwitcherItemContentProps, + SwitcherItemDescriptionProps, + SwitcherItemLabelProps, + SwitcherItemMediaProps, + SwitcherItemProps, + SwitcherItemTrailingProps, + SwitcherSeparatorProps, +} from './switcher-item'; +export type { SwitcherPopupProps } from './switcher-popup'; +export type { SwitcherRootProps } from './switcher-root'; +export type { SwitcherTriggerProps, SwitcherTriggerVariant } from './switcher-trigger'; diff --git a/packages/mosaic/src/features/switcher/switcher-avatar.styles.ts b/packages/mosaic/src/components/switcher/switcher-avatar.styles.ts similarity index 100% rename from packages/mosaic/src/features/switcher/switcher-avatar.styles.ts rename to packages/mosaic/src/components/switcher/switcher-avatar.styles.ts diff --git a/packages/mosaic/src/features/switcher/switcher-avatar.view.tsx b/packages/mosaic/src/components/switcher/switcher-avatar.tsx similarity index 83% rename from packages/mosaic/src/features/switcher/switcher-avatar.view.tsx rename to packages/mosaic/src/components/switcher/switcher-avatar.tsx index bcd9d60c2d4..2e118c1c04d 100644 --- a/packages/mosaic/src/features/switcher/switcher-avatar.view.tsx +++ b/packages/mosaic/src/components/switcher/switcher-avatar.tsx @@ -1,19 +1,21 @@ +'use client'; + import * as stylex from '@stylexjs/stylex'; import type { ReactElement } from 'react'; -import type { AvatarProps } from '../../components/avatar'; -import { Avatar } from '../../components/avatar'; import { mergeStyleProps, themeProps } from '../../props'; import { reset } from '../../styles/reset.styles'; +import type { AvatarProps } from '../avatar'; +import { Avatar } from '../avatar'; +import { useSlot } from './switcher.context'; import { sizes, styles } from './switcher-avatar.styles'; -import { useSlot } from './switcher-surface'; function initials(name: string): string { const [first = '', second = ''] = name.trim().split(/\s+/); return `${first.charAt(0)}${second.charAt(0)}`.toUpperCase() || '?'; } -export interface RowAvatarProps { +export interface SwitcherAvatarProps { name: string; imageUrl?: string; shape: 'circle' | 'square'; @@ -21,7 +23,7 @@ export interface RowAvatarProps { xstyle?: AvatarProps['xstyle']; } -export function RowAvatar({ name, imageUrl, shape, size, xstyle }: RowAvatarProps): ReactElement { +export function RowAvatar({ name, imageUrl, shape, size, xstyle }: SwitcherAvatarProps): ReactElement { return ( // Decorative: the same name is always in text alongside. Held at the root so the whole mark // stays out of the accessible name however the image resolves. @@ -42,14 +44,14 @@ export function RowAvatar({ name, imageUrl, shape, size, xstyle }: RowAvatarProp ); } -export interface BadgedAvatarProps { +export interface SwitcherBadgedAvatarProps { name: string; imageUrl?: string; badge: { name: string; imageUrl?: string }; size: 'sm' | 'md'; } -export function BadgedAvatar({ name, imageUrl, badge, size }: BadgedAvatarProps): ReactElement { +export function BadgedAvatar({ name, imageUrl, badge, size }: SwitcherBadgedAvatarProps): ReactElement { const slot = useSlot(); return ( >(function SwitcherItem( +export type SwitcherItemProps = MosaicComponentProps<'div'>; +export type SwitcherItemMediaProps = MosaicElementProps<'div'>; +export type SwitcherItemContentProps = MosaicElementProps<'div'>; +export type SwitcherItemDescriptionProps = MosaicElementProps<'div'>; +export type SwitcherItemTrailingProps = MosaicElementProps<'div'>; +export type SwitcherGroupProps = MosaicElementProps<'div'>; +export type SwitcherSeparatorProps = MosaicElementProps<'hr'>; + +export const SwitcherItem = React.forwardRef(function SwitcherItem( { render, xstyle, ...rest }, ref, ) { @@ -28,14 +38,14 @@ export const SwitcherItem = React.forwardRef) { +export function SwitcherItemMedia({ xstyle, ...rest }: SwitcherItemMediaProps) { const slot = useSlot(); return (
); } -export function SwitcherItemContent({ xstyle, ...rest }: MosaicElementProps<'div'>) { +export function SwitcherItemContent({ xstyle, ...rest }: SwitcherItemContentProps) { const slot = useSlot(); return (
) { +export function SwitcherItemDescription({ xstyle, ...rest }: SwitcherItemDescriptionProps) { const slot = useSlot(); return (
) { +export function SwitcherItemTrailing({ xstyle, ...rest }: SwitcherItemTrailingProps) { const slot = useSlot(); return (
) { +export function SwitcherGroup({ xstyle, ...rest }: SwitcherGroupProps) { const slot = useSlot(); return
; } -export function SwitcherSeparator({ xstyle, ...rest }: MosaicElementProps<'hr'>) { +export function SwitcherSeparator({ xstyle, ...rest }: SwitcherSeparatorProps) { const slot = useSlot(); return (
diff --git a/packages/mosaic/src/components/switcher/switcher-popup.styles.ts b/packages/mosaic/src/components/switcher/switcher-popup.styles.ts new file mode 100644 index 00000000000..0a675c3eb0a --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher-popup.styles.ts @@ -0,0 +1,7 @@ +import * as stylex from '@stylexjs/stylex'; + +export const styles = stylex.create({ + popup: { + width: 'min(21.875rem, calc(100vw - 2rem))', + }, +}); diff --git a/packages/mosaic/src/components/switcher/switcher-popup.tsx b/packages/mosaic/src/components/switcher/switcher-popup.tsx new file mode 100644 index 00000000000..d375d832a46 --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher-popup.tsx @@ -0,0 +1,39 @@ +'use client'; + +import React from 'react'; + +import type { MosaicElementProps } from '../../props'; +import { themeProps } from '../../props'; +import { Card } from '../card'; +import { Popover } from '../popover'; +import { useSlot } from './switcher.context'; +import { styles } from './switcher-popup.styles'; + +export interface SwitcherPopupProps extends MosaicElementProps<'div'> { + /** Names the popup. */ + 'aria-label': string; + /** + * Shows Clerk branding at the foot of the card. + * + * @default true + */ + renderBranding?: boolean; +} + +/** The floating popup: portals, positions against the trigger, and paints a `Card` holding the header and rows. */ +export const SwitcherPopup = React.forwardRef(function SwitcherPopup( + { renderBranding, children, xstyle, ...rest }, + ref, +) { + const slot = useSlot(); + return ( + + {children} + + ); +}); diff --git a/packages/mosaic/src/components/switcher/switcher-root.tsx b/packages/mosaic/src/components/switcher/switcher-root.tsx new file mode 100644 index 00000000000..f6d8447e3cd --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher-root.tsx @@ -0,0 +1,24 @@ +'use client'; + +import type { ReactElement } from 'react'; + +import type { PopoverRootProps } from '../popover'; +import { Popover } from '../popover'; +import { SwitcherRootProvider } from './switcher.context'; + +export type SwitcherRootProps = Pick< + PopoverRootProps, + 'open' | 'defaultOpen' | 'onOpenChange' | 'placement' | 'sideOffset' | 'alignOffset' | 'children' +>; + +/** Holds the popover's open state; renders no element of its own. */ +export function SwitcherRoot({ placement = 'bottom-start', children, ...rest }: SwitcherRootProps): ReactElement { + return ( + + {children} + + ); +} diff --git a/packages/mosaic/src/components/switcher/switcher-trigger.styles.ts b/packages/mosaic/src/components/switcher/switcher-trigger.styles.ts new file mode 100644 index 00000000000..433b0006c9c --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher-trigger.styles.ts @@ -0,0 +1,68 @@ +import * as stylex from '@stylexjs/stylex'; + +import { colorVars, durationVars, fontWeightVars, radiusVars, space, typeScaleVars } from '../../tokens.stylex'; + +export const styles = stylex.create({ + trigger: { + padding: 0, + borderRadius: radiusVars['--cl-radius-md'], + borderStyle: 'none', + alignItems: 'center', + backgroundColor: 'transparent', + cursor: 'pointer', + display: 'inline-flex', + transitionDuration: { + default: durationVars['--cl-duration-base'], + '@media (hover: hover)': { + default: null, + ':hover:not(:disabled, [data-disabled], [aria-disabled="true"])': durationVars['--cl-duration-instant'], + }, + }, + transitionTimingFunction: 'linear', + maxWidth: '100%', + minWidth: 0, + }, + + triggerLabelled: { + padding: space['1.5'], + borderRadius: radiusVars['--cl-radius-xl'], + gap: space['1.5'], + backgroundColor: { + default: 'transparent', + ':is([data-open])': `color-mix(in oklab, ${colorVars['--cl-color-neutral']} 4%, transparent)`, + '@media (hover: hover)': { + ':hover:not(:disabled, [data-disabled], [aria-disabled="true"])': `color-mix(in oklab, ${colorVars['--cl-color-neutral']} 4%, transparent)`, + }, + }, + transitionProperty: 'background-color', + }, + + triggerAvatarOnly: { + opacity: { + default: 1, + ':is([data-open])': 0.8, + '@media (hover: hover)': { + ':hover:not(:disabled, [data-disabled], [aria-disabled="true"])': 0.8, + }, + }, + transitionProperty: 'opacity', + }, + + triggerRound: { + borderRadius: radiusVars['--cl-radius-full'], + }, + + // Capped, because the trigger sits in a host app's chrome and a long organization name would push it apart. + triggerName: { + color: colorVars['--cl-color-foreground'], + fontSize: typeScaleVars['--cl-text-sm-size'], + fontWeight: fontWeightVars['--cl-font-medium'], + lineHeight: typeScaleVars['--cl-text-sm-leading'], + maxWidth: '12rem', + }, + + triggerCaret: { + '--_cl-icon-color': colorVars['--cl-color-foreground-secondary'], + marginInline: space['1'], + }, +}); diff --git a/packages/mosaic/src/components/switcher/switcher-trigger.tsx b/packages/mosaic/src/components/switcher/switcher-trigger.tsx new file mode 100644 index 00000000000..7303d16dfdd --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher-trigger.tsx @@ -0,0 +1,141 @@ +'use client'; + +import * as stylex from '@stylexjs/stylex'; +import type { ReactElement, ReactNode } from 'react'; +import React from 'react'; + +import { ChevronDown } from '../../icons/glyphs/chevron-down'; +import type { MosaicElementProps } from '../../props'; +import { mergeStyleProps, themeProps } from '../../props'; +import { focusOutline } from '../../styles/focus-outline.styles'; +import { truncationStyles } from '../../styles/typography.styles'; +import { Badge } from '../badge'; +import { Icon } from '../icon'; +import { Popover } from '../popover'; +import { useInSwitcherRoot, useSlot } from './switcher.context'; +import { styles } from './switcher-trigger.styles'; + +function triggerStyles(showLabel: boolean, shape: 'circle' | 'square') { + return [ + focusOutline.visible, + styles.trigger, + showLabel ? styles.triggerLabelled : styles.triggerAvatarOnly, + !showLabel && shape === 'circle' ? styles.triggerRound : null, + ]; +} + +function SwitcherTriggerContent({ + avatar, + showLabel, + name, + badge, + badgeId, +}: { + avatar: ReactNode; + showLabel: boolean; + name?: string; + badge?: string; + badgeId?: string; +}): ReactElement { + const slot = useSlot(); + return ( + <> + {avatar} + {showLabel ? ( + <> + + {name} + + {typeof badge === 'string' ? ( + + {badge} + + ) : null} + + + ) : null} + + ); +} + +export interface SwitcherTriggerProps extends Omit, 'children'> { + /** The lead's mark, such as a `Switcher.BadgedAvatar`. */ + avatar: ReactNode; + /** The lead's name. Leave it out for the avatar-only trigger. */ + name?: string; + /** A plan or status label beside the name. */ + badge?: string; + /** Set as `data-badge-id` on the badge. */ + badgeId?: string; + /** Rounds the avatar-only trigger to match a circular avatar. */ + shape?: 'circle' | 'square'; + /** + * Which mark the trigger carries, set as `data-variant`: `full` for a `Switcher.BadgedAvatar`, + * `condensed` for a `Switcher.Avatar`. + * + * @default 'condensed' + */ + variant?: SwitcherTriggerVariant; +} + +export type SwitcherTriggerVariant = 'full' | 'condensed'; + +/** Opens the popup. */ +export const SwitcherTrigger = React.forwardRef(function SwitcherTrigger( + { avatar, name, badge, badgeId, shape = 'square', variant = 'condensed', xstyle, ...rest }, + ref, +) { + const slot = useSlot(); + const inRoot = useInSwitcherRoot(); + const showLabel = name !== undefined; + const content = ( + + ); + + if (inRoot) { + return ( + + {content} + + ); + } + + // Outside a `Switcher.Root` there is no popover to wire up, so render the same look, slots and + // data attributes on a plain button rather than throw. This lets the trigger render on its own. + return ( + + ); +}); diff --git a/packages/mosaic/src/components/switcher/switcher.context.ts b/packages/mosaic/src/components/switcher/switcher.context.ts new file mode 100644 index 00000000000..bb2416af4a1 --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher.context.ts @@ -0,0 +1,26 @@ +import React from 'react'; + +/** Which component the parts belong to. It prefixes every part's `.cl-*` class, such as `cl-user-button-item`. */ +export type SwitcherSurface = 'switcher' | 'user-button' | 'organization-switcher'; + +const SurfaceContext = React.createContext('switcher'); + +export const SwitcherSurfaceProvider = SurfaceContext.Provider; + +export function useSwitcherSurface(): SwitcherSurface { + return React.useContext(SurfaceContext); +} + +export function useSlot(): (part: string) => string { + const surface = useSwitcherSurface(); + return part => `${surface}-${part}`; +} + +const RootContext = React.createContext(false); + +export const SwitcherRootProvider = RootContext.Provider; + +/** Whether a part renders inside a `Switcher.Root`. Outside one, `Switcher.Trigger` renders a plain button. */ +export function useInSwitcherRoot(): boolean { + return React.useContext(RootContext); +} diff --git a/packages/mosaic/src/components/switcher/switcher.doc.ts b/packages/mosaic/src/components/switcher/switcher.doc.ts new file mode 100644 index 00000000000..a3de6f8b97c --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher.doc.ts @@ -0,0 +1,205 @@ +import type { ComponentDoc } from '../../docs-schema'; +import type { SwitcherAvatarProps, SwitcherBadgedAvatarProps } from './switcher-avatar'; +import type { SwitcherHeaderProps } from './switcher-header'; +import type { + SwitcherGroupProps, + SwitcherItemContentProps, + SwitcherItemDescriptionProps, + SwitcherItemLabelProps, + SwitcherItemMediaProps, + SwitcherItemProps, + SwitcherItemTrailingProps, + SwitcherSeparatorProps, +} from './switcher-item'; +import type { SwitcherPopupProps } from './switcher-popup'; +import type { SwitcherRootProps } from './switcher-root'; +import type { SwitcherTriggerProps } from './switcher-trigger'; + +export default { + name: 'Switcher', + description: + "The account and organization switcher. `Switcher.Root` holds the open state, `Switcher.Trigger` opens it, and `Switcher.Popup` positions a `Card` holding `Switcher.Header`, then `Switcher.Group`s of `Switcher.Item`s split by `Switcher.Separator`s. `UserButton`, `OrganizationSwitcher` and `Switcher` from `@clerk/mosaic` compose these parts with Clerk's session and organization state. The parts carry `cl-switcher-*` classes; inside `UserButton` and `OrganizationSwitcher` the same parts carry `cl-user-button-*` and `cl-organization-switcher-*`. `Switcher.Avatar` carries `cl-avatar`.", + useFor: ['an account or organization switcher composed from its parts'], + keywords: ['switcher', 'user button', 'organization switcher', 'account switcher', 'menu rows'], + practices: [ + { + do: true, + text: 'Put `Switcher.Trigger` and `Switcher.Popup` inside one `Switcher.Root`, and label the popup with `aria-label`.', + }, + { + do: true, + text: 'Wrap rows in a `Switcher.Group`, and split groups with a `Switcher.Separator`.', + }, + { + do: true, + text: 'Pass `render` to `Switcher.Item` to make a row a button or link. It then takes the hover and press styles.', + }, + { + do: false, + text: 'Expect the parts to read Clerk. They render the props they are given; `UserButton` and `OrganizationSwitcher` connect them to Clerk.', + }, + ], + parts: { + 'Switcher.Root': { + description: 'Holds the open state. Renders no element of its own.', + props: { + open: { type: 'boolean', doc: 'The controlled open state.' }, + defaultOpen: { type: 'boolean', doc: 'The initial open state when uncontrolled.', default: 'false' }, + onOpenChange: { type: '(open: boolean) => void', doc: 'Called when the popup opens or closes.' }, + placement: { type: 'Placement', doc: 'Where the popup sits against the trigger.', default: "'bottom-start'" }, + sideOffset: { type: 'number', doc: 'The gap between the trigger and the popup.', default: '4' }, + alignOffset: { type: 'number', doc: 'Shifts the popup along the trigger edge.', default: '0' }, + }, + }, + 'Switcher.Trigger': { + description: + 'Opens the popup. With a `name` it shows the name, the badge and a caret; without one, the avatar alone.', + slot: 'cl-switcher-trigger', + props: { + avatar: { type: 'ReactNode', doc: "The lead's mark, such as a `Switcher.BadgedAvatar`." }, + name: { type: 'string', doc: "The lead's name. Leave it out for the avatar-only trigger." }, + badge: { type: 'string', doc: 'A plan or status label beside the name.' }, + badgeId: { type: 'string', doc: 'Set as `data-badge-id` on the badge.' }, + shape: { + type: "'circle' | 'square'", + doc: 'Rounds the avatar-only trigger to match a circular avatar.', + default: "'square'", + }, + variant: { + type: "'full' | 'condensed'", + doc: 'Which mark the trigger carries, set as `data-variant`: `full` for a `Switcher.BadgedAvatar`, `condensed` for a `Switcher.Avatar`.', + default: "'condensed'", + }, + }, + }, + 'Switcher.Popup': { + description: + 'The floating popup: portals, positions against the trigger, and paints a `Card` holding its children. A dialog named by `aria-label`.', + slot: 'cl-switcher-popover', + props: { + 'aria-label': { type: 'string', doc: 'Names the popup.' }, + renderBranding: { type: 'boolean', doc: 'Shows Clerk branding at the foot of the card.', default: 'true' }, + }, + }, + 'Switcher.Header': { + description: "The popover's header: the lead's avatar, its name and a description.", + slot: 'cl-switcher-header', + props: { + avatar: { type: 'ReactNode', doc: 'The leading mark.' }, + title: { type: 'string', doc: 'The name, on one line.' }, + description: { type: 'string', doc: 'A second line, such as an email address.' }, + }, + }, + 'Switcher.Avatar': { + description: 'A decorative avatar with initials as its fallback, hidden from assistive tech.', + slot: 'cl-avatar', + props: { + name: { type: 'string', doc: 'The name the initials come from.' }, + imageUrl: { type: 'string', doc: 'The image.' }, + shape: { type: "'circle' | 'square'", doc: 'Circle for a person, square for an organization.' }, + size: { type: "'xs' | 'sm' | 'md' | 'lg' | 'fit'", doc: 'Box size. `fit` fills the parent.' }, + }, + }, + 'Switcher.BadgedAvatar': { + description: "An organization's square avatar with a person's round avatar cut into its corner.", + slot: 'cl-switcher-avatar', + props: { + name: { type: 'string', doc: 'The lead name.' }, + imageUrl: { type: 'string', doc: 'The lead image.' }, + badge: { type: '{ name: string; imageUrl?: string }', doc: 'The person in the corner.' }, + size: { type: "'sm' | 'md'", doc: 'Box size.' }, + }, + }, + 'Switcher.Group': { + description: 'A run of rows inside the popover.', + slot: 'cl-switcher-group', + }, + 'Switcher.Separator': { + description: 'A rule between groups.', + slot: 'cl-switcher-separator', + }, + 'Switcher.Item': { + description: 'One row. Renders a `div`, or the `render` element, which makes it interactive.', + slot: 'cl-switcher-item', + }, + 'Switcher.ItemMedia': { + description: "The row's leading avatar or icon.", + slot: 'cl-switcher-item-media', + }, + 'Switcher.ItemContent': { + description: "The row's text column.", + slot: 'cl-switcher-item-content', + }, + 'Switcher.ItemLabel': { + description: "The row's name, on one line.", + slot: 'cl-switcher-item-label', + props: { + variant: { + type: "'default' | 'interactive'", + doc: "`default` names the row's subject. `interactive` takes the row's color, for rows such as `Sign out`.", + default: "'default'", + }, + }, + }, + 'Switcher.ItemDescription': { + description: 'A second line under the label.', + slot: 'cl-switcher-item-description', + }, + 'Switcher.ItemTrailing': { + description: 'Content at the end of the row, such as a checkmark or a badge.', + slot: 'cl-switcher-item-trailing', + }, + }, + examples: [ + { + title: 'A trigger and its popup', + code: ` + + } + name='Acme' + badge='Pro' + variant='full' + /> + + } + title='Acme' + description='alice@example.com' + /> + + + }> + + + + + Globex + Member + + … + + + + +`, + }, + ], + related: ['Avatar', 'Popover', 'Card'], +} satisfies ComponentDoc<{ + 'Switcher.Root': SwitcherRootProps; + 'Switcher.Trigger': SwitcherTriggerProps; + 'Switcher.Popup': SwitcherPopupProps; + 'Switcher.Header': SwitcherHeaderProps; + 'Switcher.Avatar': SwitcherAvatarProps; + 'Switcher.BadgedAvatar': SwitcherBadgedAvatarProps; + 'Switcher.Group': SwitcherGroupProps; + 'Switcher.Separator': SwitcherSeparatorProps; + 'Switcher.Item': SwitcherItemProps; + 'Switcher.ItemMedia': SwitcherItemMediaProps; + 'Switcher.ItemContent': SwitcherItemContentProps; + 'Switcher.ItemLabel': SwitcherItemLabelProps; + 'Switcher.ItemDescription': SwitcherItemDescriptionProps; + 'Switcher.ItemTrailing': SwitcherItemTrailingProps; +}>; diff --git a/packages/mosaic/src/components/switcher/switcher.parts.ts b/packages/mosaic/src/components/switcher/switcher.parts.ts new file mode 100644 index 00000000000..66ac65f5f5d --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher.parts.ts @@ -0,0 +1,15 @@ +export { BadgedAvatar, RowAvatar as Avatar } from './switcher-avatar'; +export { SwitcherHeader as Header } from './switcher-header'; +export { + SwitcherItem as Item, + SwitcherItemMedia as ItemMedia, + SwitcherItemContent as ItemContent, + SwitcherItemLabel as ItemLabel, + SwitcherItemDescription as ItemDescription, + SwitcherItemTrailing as ItemTrailing, + SwitcherGroup as Group, + SwitcherSeparator as Separator, +} from './switcher-item'; +export { SwitcherPopup as Popup } from './switcher-popup'; +export { SwitcherRoot as Root } from './switcher-root'; +export { SwitcherTrigger as Trigger } from './switcher-trigger'; diff --git a/packages/mosaic/src/components/switcher/switcher.test.tsx b/packages/mosaic/src/components/switcher/switcher.test.tsx new file mode 100644 index 00000000000..fc0d79acac2 --- /dev/null +++ b/packages/mosaic/src/components/switcher/switcher.test.tsx @@ -0,0 +1,339 @@ +import * as stylex from '@stylexjs/stylex'; +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import React from 'react'; +import { describe, expect, it, vi } from 'vitest'; + +import { MosaicProvider } from '../../mosaic-provider'; +import { axe } from '../../primitives/test-utils/axe'; +import { Card } from '../card'; +import type { SwitcherRootProps } from '.'; +import { Switcher } from '.'; +import { SwitcherSurfaceProvider } from './switcher.context'; + +function PopupContent() { + return ( + <> + + } + title='Acme' + description='alice@example.com' + /> + + + }> + + + + + Globex + Member + + Pro + + + + ); +} + +function atomsOf(style: stylex.StyleXStyles): string[] { + return stylex + .props(style) + .className!.split(' ') + .filter(name => !name.includes('__')); +} + +describe('Switcher', () => { + it('renders the popover parts with their switcher slots', async () => { + const { container } = render( + + + , + ); + + expect(container.querySelector('.cl-switcher-header')).toHaveTextContent('Acmealice@example.com'); + expect(container.querySelector('.cl-switcher-avatar')).toHaveAttribute('aria-hidden'); + expect(container.querySelector('.cl-switcher-separator')?.tagName).toBe('HR'); + const row = screen.getByRole('button', { name: 'Globex Member Pro' }); + expect(row).toHaveClass('cl-switcher-item'); + expect(row).toHaveAttribute('data-interactive'); + expect(container.querySelector('.cl-switcher-item-label')).toHaveAttribute('data-variant', 'default'); + expect((await axe(container)).violations).toEqual([]); + }); + + it.each(['user-button', 'organization-switcher'] as const)('takes the %s slot prefix from the surface', surface => { + const { container } = render( + + + + + , + ); + + for (const part of ['header', 'avatar', 'group', 'separator', 'item', 'item-label']) { + expect(container.querySelector(`.cl-${surface}-${part}`)).not.toBeNull(); + } + expect(container.querySelector('[class*="cl-switcher-"]')).toBeNull(); + }); + + it('shapes the avatar by its shape prop', () => { + const { container } = render( + + + + , + ); + + const [circle, square] = container.querySelectorAll('.cl-avatar'); + expect(circle).toHaveAttribute('data-shape', 'circle'); + expect(square).toHaveAttribute('data-shape', 'square'); + }); + + it('forwards refs and xstyle to the root elements', () => { + const probe = stylex.create({ root: { maxWidth: '900px' } }); + const itemRef = React.createRef(); + const triggerRef = React.createRef(); + render( + + + + Globex + + + + + , + ); + + const atoms = atomsOf(probe.root); + expect(itemRef.current).toHaveClass('cl-switcher-item', ...atoms); + expect(triggerRef.current).toBe(screen.getByRole('button', { name: 'Open account menu' })); + expect(triggerRef.current).toHaveClass('cl-switcher-trigger', ...atoms); + expect(screen.getByTestId('group')).toHaveClass('cl-switcher-group', ...atoms); + expect(screen.getByText('Globex')).toHaveClass('cl-switcher-item-label', ...atoms); + expect(document.querySelector('.cl-avatar')).toHaveClass(...atoms); + }); + + it('renders the trigger as a plain button, with or without its label', () => { + const { rerender } = render( + + + } + name='Acme' + badge='Pro' + variant='full' + /> + , + ); + + const trigger = screen.getByRole('button', { name: 'Open account menu' }); + expect(trigger).toHaveAttribute('type', 'button'); + expect(trigger).toHaveClass('cl-switcher-trigger'); + expect(trigger).toHaveAttribute('data-variant', 'full'); + expect(trigger.querySelector('.cl-switcher-trigger-label')).toHaveTextContent('Acme'); + expect(trigger).toHaveTextContent('Pro'); + expect(trigger.querySelector('.cl-badge')).toHaveTextContent('Pro'); + + rerender( + + + } + shape='circle' + /> + , + ); + + const avatarOnly = screen.getByRole('button', { name: 'Open account menu' }); + expect(avatarOnly).toHaveAttribute('data-variant', 'condensed'); + expect(avatarOnly.querySelector('.cl-switcher-trigger-label')).toBeNull(); + expect(avatarOnly.querySelector('.cl-avatar')).toHaveAttribute('data-shape', 'circle'); + }); + + it('keeps the label and an empty badge when given empty strings', () => { + render( + + + , + ); + + const trigger = screen.getByRole('button', { name: 'Open account menu' }); + expect(trigger.querySelector('.cl-switcher-trigger-label')).not.toBeNull(); + expect(trigger.querySelector('.cl-badge')).not.toBeNull(); + }); + + describe('Root, Trigger and Popup', () => { + function Composed(props: Partial) { + return ( + + + + } + name='Acme' + badge='Pro' + variant='full' + /> + + + + + + ); + } + + it('opens the popup from the trigger, and closes it with Escape', async () => { + const user = userEvent.setup(); + render(); + + const trigger = screen.getByRole('button', { name: 'Open account menu' }); + expect(trigger).toHaveClass('cl-popover-trigger', 'cl-switcher-trigger'); + expect(trigger).toHaveAttribute('data-variant', 'full'); + expect(trigger).toHaveAttribute('aria-expanded', 'false'); + expect(screen.queryByRole('dialog')).toBeNull(); + + await user.click(trigger); + + expect(trigger).toHaveAttribute('aria-expanded', 'true'); + expect(trigger).toHaveAttribute('data-open'); + const dialog = await screen.findByRole('dialog', { name: 'Account menu' }); + expect(trigger).toHaveAttribute('aria-controls', dialog.id); + const popup = dialog.querySelector('.cl-switcher-popover'); + expect(popup).toHaveClass('cl-popover-popup'); + expect(popup?.querySelector('.cl-card-root')).toContainElement( + screen.getByRole('button', { name: 'Globex Member Pro' }), + ); + expect(popup?.querySelector('.cl-switcher-header')).not.toBeNull(); + + await user.keyboard('{Escape}'); + + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + expect(trigger).toHaveAttribute('aria-expanded', 'false'); + expect(trigger).toHaveFocus(); + }); + + it('opens from the keyboard and moves focus into the popup', async () => { + const user = userEvent.setup(); + render(); + + await user.tab(); + expect(screen.getByRole('button', { name: 'Open account menu' })).toHaveFocus(); + await user.keyboard('{Enter}'); + + await screen.findByRole('dialog', { name: 'Account menu' }); + await waitFor(() => expect(screen.getByRole('button', { name: 'Globex Member Pro' })).toHaveFocus()); + }); + + it('follows a controlled open state', async () => { + const user = userEvent.setup(); + const onOpenChange = vi.fn(); + const { rerender } = render( + , + ); + + expect(await screen.findByRole('dialog', { name: 'Account menu' })).toBeInTheDocument(); + await user.click(screen.getByRole('button', { name: 'Open account menu' })); + expect(onOpenChange).toHaveBeenCalledWith(false); + + rerender( + , + ); + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()); + }); + }); + + it('renders open in normal flow from the content parts inside a Card, with the trigger beside it on its own', () => { + const { container } = render( + + + + + + } + name='Acme' + variant='full' + /> + , + ); + + expect(screen.queryByRole('dialog')).toBeNull(); + expect(container.querySelector('.cl-card-root .cl-switcher-header')).not.toBeNull(); + expect(container.querySelector('.cl-card-root .cl-switcher-item')).not.toBeNull(); + const trigger = screen.getByRole('button', { name: 'Open account menu' }); + expect(trigger).toHaveClass('cl-switcher-trigger'); + expect(trigger).not.toHaveClass('cl-popover-trigger'); + expect(trigger).toHaveAttribute('data-variant', 'full'); + expect(trigger).not.toHaveAttribute('aria-expanded'); + }); +}); diff --git a/packages/mosaic/src/features/oauth-consent/oauth-consent.view.tsx b/packages/mosaic/src/features/oauth-consent/oauth-consent.view.tsx index 210fef940e6..b0676cc7a1f 100644 --- a/packages/mosaic/src/features/oauth-consent/oauth-consent.view.tsx +++ b/packages/mosaic/src/features/oauth-consent/oauth-consent.view.tsx @@ -13,6 +13,7 @@ import { Radio } from '../../components/radio'; import { Reveal } from '../../components/reveal'; import { scrollAreaViewport } from '../../components/scroll-area'; import { Select } from '../../components/select'; +import { RowAvatar } from '../../components/switcher/switcher-avatar'; import { Text } from '../../components/text'; import { Tooltip } from '../../components/tooltip'; import { VisuallyHidden } from '../../components/visually-hidden'; @@ -21,7 +22,6 @@ import { fill, plural, rich, useLocale } from '../../localization'; import { Form } from '../../primitives/form'; import { mergeStyleProps, themeProps } from '../../props'; import { reset } from '../../styles/reset.styles'; -import { RowAvatar } from '../switcher/switcher-avatar.view'; import { styles } from './oauth-consent.styles'; import type { OAuthConsentOrganization, diff --git a/packages/mosaic/src/features/organization-list/organization-list.skeleton.tsx b/packages/mosaic/src/features/organization-list/organization-list.skeleton.tsx index 467b7235d5b..ebbf40787cc 100644 --- a/packages/mosaic/src/features/organization-list/organization-list.skeleton.tsx +++ b/packages/mosaic/src/features/organization-list/organization-list.skeleton.tsx @@ -1,9 +1,9 @@ import { Card } from '../../components/card'; import { Item } from '../../components/item'; import { Skeleton } from '../../components/skeleton'; +import { RowAvatar } from '../../components/switcher/switcher-avatar'; import { VisuallyHidden } from '../../components/visually-hidden'; import { useMessages } from '../../localization'; -import { RowAvatar } from '../switcher/switcher-avatar.view'; import type { OrganizationListData } from './organization-list.types'; import { OrganizationListHeader } from './organization-list.view'; diff --git a/packages/mosaic/src/features/organization-list/organization-list.view.tsx b/packages/mosaic/src/features/organization-list/organization-list.view.tsx index c780cc306bc..bc51bb8b316 100644 --- a/packages/mosaic/src/features/organization-list/organization-list.view.tsx +++ b/packages/mosaic/src/features/organization-list/organization-list.view.tsx @@ -7,10 +7,10 @@ import { Flow, useFlowAutoFocus } from '../../components/flow'; import { Icon } from '../../components/icon'; import { Item } from '../../components/item'; import { Spinner } from '../../components/spinner'; +import { RowAvatar } from '../../components/switcher/switcher-avatar'; import { Plus } from '../../icons/glyphs/plus'; import { fill, useMessages } from '../../localization'; import { Button as HeadlessButton } from '../../primitives/button'; -import { RowAvatar } from '../switcher/switcher-avatar.view'; import type { OrganizationListData, OrganizationListInvitation, diff --git a/packages/mosaic/src/features/sign-in/steps/sign-in-accounts.tsx b/packages/mosaic/src/features/sign-in/steps/sign-in-accounts.tsx index 35a07dabae6..40d57a52a19 100644 --- a/packages/mosaic/src/features/sign-in/steps/sign-in-accounts.tsx +++ b/packages/mosaic/src/features/sign-in/steps/sign-in-accounts.tsx @@ -7,10 +7,10 @@ import { useFlowAutoFocus } from '../../../components/flow'; import { Icon } from '../../../components/icon'; import { Item } from '../../../components/item'; import { Spinner } from '../../../components/spinner'; +import { RowAvatar } from '../../../components/switcher/switcher-avatar'; import { Plus } from '../../../icons/glyphs/plus'; import { useMessages } from '../../../localization'; import { Button as HeadlessButton } from '../../../primitives/button'; -import { RowAvatar } from '../../switcher/switcher-avatar.view'; import type { SignInAccount } from '../sign-in.types'; interface SignInAccountsProps { diff --git a/packages/mosaic/src/features/switcher/switcher-surface.tsx b/packages/mosaic/src/features/switcher/switcher-surface.tsx index 20c7d7d2070..9cb290b9c0f 100644 --- a/packages/mosaic/src/features/switcher/switcher-surface.tsx +++ b/packages/mosaic/src/features/switcher/switcher-surface.tsx @@ -1,11 +1,9 @@ -import React from 'react'; - +import type { SwitcherSurface } from '../../components/switcher/switcher.context'; +import { useSwitcherSurface } from '../../components/switcher/switcher.context'; import type { MosaicMessages } from '../../localization'; import { useMessages } from '../../localization'; import type { SwitcherMode } from './switcher.types'; -export type SwitcherSurface = 'switcher' | 'user-button' | 'organization-switcher'; - export function surfaceForMode(mode: SwitcherMode): SwitcherSurface { switch (mode) { case 'combined': @@ -17,19 +15,10 @@ export function surfaceForMode(mode: SwitcherMode): SwitcherSurface { } } -const SurfaceContext = React.createContext('switcher'); - -export const SwitcherSurfaceProvider = SurfaceContext.Provider; - -export function useSlot(): (part: string) => string { - const surface = React.useContext(SurfaceContext); - return part => `${surface}-${part}`; -} - export type SwitcherMessages = Omit | MosaicMessages['organizationSwitcher']; export function useSwitcherMessages(): SwitcherMessages { - const surface = React.useContext(SurfaceContext); + const surface = useSwitcherSurface(); const userButton = useMessages('userButton'); const organizationSwitcher = useMessages('organizationSwitcher'); return surface === 'organization-switcher' ? organizationSwitcher : userButton; diff --git a/packages/mosaic/src/features/switcher/switcher.controller.tsx b/packages/mosaic/src/features/switcher/switcher.controller.tsx index 9c4dd517f31..fd2a0372774 100644 --- a/packages/mosaic/src/features/switcher/switcher.controller.tsx +++ b/packages/mosaic/src/features/switcher/switcher.controller.tsx @@ -4,7 +4,7 @@ import { useMachine } from '../../machine/use-machine'; import type { SwitcherModel } from './switcher.model'; import type { SwitcherMenuProps, SwitcherMode } from './switcher.types'; import { switcherBusyKeys } from './switcher.types'; -import type { SwitcherTriggerProps, SwitcherViewProps } from './switcher.view'; +import type { SwitcherTriggerOptions, SwitcherViewProps } from './switcher.view'; /** The model once Clerk has answered, which is the only shape an action can start from. */ export type SwitcherReadyModel = Extract; @@ -101,7 +101,7 @@ export type SwitcherControllerOptions = { mode?: SwitcherMode } & SwitcherMenuPr export type SwitcherController = | { status: 'loading' } | { status: 'hidden' } - | ({ status: 'ready' } & Omit); + | ({ status: 'ready' } & Omit); /** * The controller is the layer between the component (view) and the external world (model). diff --git a/packages/mosaic/src/features/switcher/switcher.styles.ts b/packages/mosaic/src/features/switcher/switcher.styles.ts index fd841ee6346..3f25c3b539e 100644 --- a/packages/mosaic/src/features/switcher/switcher.styles.ts +++ b/packages/mosaic/src/features/switcher/switcher.styles.ts @@ -1,6 +1,6 @@ import * as stylex from '@stylexjs/stylex'; -import { colorVars, durationVars, fontWeightVars, radiusVars, space, typeScaleVars } from '../../tokens.stylex'; +import { radiusVars, space } from '../../tokens.stylex'; export const styles = stylex.create({ rowAvatar: { @@ -9,68 +9,6 @@ export const styles = stylex.create({ height: space['5'], width: space['5'], }, - trigger: { - padding: 0, - borderRadius: radiusVars['--cl-radius-md'], - borderStyle: 'none', - alignItems: 'center', - backgroundColor: 'transparent', - cursor: 'pointer', - display: 'inline-flex', - transitionDuration: { - default: durationVars['--cl-duration-base'], - '@media (hover: hover)': { - default: null, - ':hover:not(:disabled, [data-disabled], [aria-disabled="true"])': durationVars['--cl-duration-instant'], - }, - }, - transitionTimingFunction: 'linear', - maxWidth: '100%', - minWidth: 0, - }, - - triggerLabelled: { - padding: space['1.5'], - borderRadius: radiusVars['--cl-radius-xl'], - gap: space['1.5'], - backgroundColor: { - default: 'transparent', - ':is([data-open])': `color-mix(in oklab, ${colorVars['--cl-color-neutral']} 4%, transparent)`, - '@media (hover: hover)': { - ':hover:not(:disabled, [data-disabled], [aria-disabled="true"])': `color-mix(in oklab, ${colorVars['--cl-color-neutral']} 4%, transparent)`, - }, - }, - transitionProperty: 'background-color', - }, - - triggerAvatarOnly: { - opacity: { - default: 1, - ':is([data-open])': 0.8, - '@media (hover: hover)': { - ':hover:not(:disabled, [data-disabled], [aria-disabled="true"])': 0.8, - }, - }, - transitionProperty: 'opacity', - }, - - triggerRound: { - borderRadius: radiusVars['--cl-radius-full'], - }, - - // Capped, because the trigger sits in a host app's chrome and a long organization name would push it apart. - triggerName: { - color: colorVars['--cl-color-foreground'], - fontSize: typeScaleVars['--cl-text-sm-size'], - fontWeight: fontWeightVars['--cl-font-medium'], - lineHeight: typeScaleVars['--cl-text-sm-leading'], - maxWidth: '12rem', - }, - - triggerCaret: { - '--_cl-icon-color': colorVars['--cl-color-foreground-secondary'], - marginInline: space['1'], - }, // The organization list scrolls; the header and footer stay put. The scroll area carries the // overflow, the edge fades, the scrollbar and the scroll padding they need, so only the cap @@ -78,8 +16,4 @@ export const styles = stylex.create({ scroll: { maxHeight: '18rem', }, - - popup: { - width: 'min(21.875rem, calc(100vw - 2rem))', - }, }); diff --git a/packages/mosaic/src/features/switcher/switcher.tsx b/packages/mosaic/src/features/switcher/switcher.tsx index 64f50e4950c..ee087f9c6b3 100644 --- a/packages/mosaic/src/features/switcher/switcher.tsx +++ b/packages/mosaic/src/features/switcher/switcher.tsx @@ -12,10 +12,10 @@ import type { import { useSwitcherModel } from './switcher.model'; import { useCustomPages, useOrganizationProfilePages, useUserProfilePages } from './switcher.pages'; import type { SwitcherMenuProps, SwitcherMode } from './switcher.types'; -import type { SwitcherTriggerProps, SwitcherTriggerVariant } from './switcher.view'; +import type { SwitcherTriggerOptions, SwitcherTriggerVariant } from './switcher.view'; import { SwitcherView } from './switcher.view'; -type SwitcherBaseProps = SwitcherTriggerProps & +type SwitcherBaseProps = SwitcherTriggerOptions & SwitcherMenuProps & { /** * Fallback while loading. diff --git a/packages/mosaic/src/features/switcher/switcher.view.tsx b/packages/mosaic/src/features/switcher/switcher.view.tsx index 0c6ab1e0d49..1f3c05366c0 100644 --- a/packages/mosaic/src/features/switcher/switcher.view.tsx +++ b/packages/mosaic/src/features/switcher/switcher.view.tsx @@ -1,21 +1,31 @@ 'use client'; -import * as stylex from '@stylexjs/stylex'; import type { ReactElement, ReactNode } from 'react'; import React from 'react'; -import { Badge } from '../../components/badge'; import { SubmitButton } from '../../components/button'; -import { Card } from '../../components/card'; import { Icon } from '../../components/icon'; import { Menu } from '../../components/menu'; -import { Popover } from '../../components/popover'; import { scrollAreaViewport } from '../../components/scroll-area'; import { Spinner } from '../../components/spinner'; +import { Switcher } from '../../components/switcher'; +import { SwitcherSurfaceProvider } from '../../components/switcher/switcher.context'; +import { BadgedAvatar, RowAvatar } from '../../components/switcher/switcher-avatar'; +import { SwitcherHeader } from '../../components/switcher/switcher-header'; +import { + SwitcherGroup, + SwitcherItem, + SwitcherItemContent, + SwitcherItemDescription, + SwitcherItemLabel, + SwitcherItemMedia, + SwitcherItemTrailing, + SwitcherSeparator, +} from '../../components/switcher/switcher-item'; +import type { SwitcherTriggerVariant } from '../../components/switcher/switcher-trigger'; import type { IconComponent } from '../../icons/glyph'; import { Building } from '../../icons/glyphs/building'; import { Checkmark } from '../../icons/glyphs/checkmark'; -import { ChevronDown } from '../../icons/glyphs/chevron-down'; import { ChevronRight } from '../../icons/glyphs/chevron-right'; import { Cog6Teeth } from '../../icons/glyphs/cog-6-teeth'; import { Plus } from '../../icons/glyphs/plus'; @@ -26,10 +36,7 @@ import { UsersAddRight } from '../../icons/glyphs/users-add-right'; import { fill, plural, useLocale, useMessages } from '../../localization'; import { Button as HeadlessButton } from '../../primitives/button'; import type { PopoverProps } from '../../primitives/popover'; -import { mergeStyleProps, themeProps } from '../../props'; -import { focusOutline } from '../../styles/focus-outline.styles'; import { rtl } from '../../styles/rtl.styles'; -import { truncationStyles } from '../../styles/typography.styles'; import { applyOrder } from '../../utils/apply-order'; import type { SwitcherLayout } from './switcher.layout'; import { resolveSwitcherLayout } from './switcher.layout'; @@ -47,25 +54,14 @@ import type { SwitcherTriggerBadge, } from './switcher.types'; import { switcherBusyKeys } from './switcher.types'; -import { BadgedAvatar, RowAvatar } from './switcher-avatar.view'; -import { SwitcherHeader } from './switcher-header.view'; -import { - SwitcherGroup, - SwitcherItem, - SwitcherItemContent, - SwitcherItemDescription, - SwitcherItemLabel, - SwitcherItemMedia, - SwitcherItemTrailing, - SwitcherSeparator, -} from './switcher-item.view'; import type { SwitcherMessages } from './switcher-surface'; -import { surfaceForMode, SwitcherSurfaceProvider, useSlot, useSwitcherMessages } from './switcher-surface'; +import { surfaceForMode, useSwitcherMessages } from './switcher-surface'; import { useSwitcherTriggerController } from './switcher-trigger.controller'; // The data contract, the mode flags, and the menu item shapes live in `switcher.types`; they are // what the model and the view agree on, so neither file owns them. export type * from './switcher.types'; +export type { SwitcherTriggerVariant }; type SwitcherContextValue = SwitcherData & SwitcherCallbacks & @@ -990,7 +986,7 @@ export interface SwitcherRootProps /** * Owns the account/organization data + callbacks and forwards the popover's open state straight to - * the headless `Popover.Root` — it does not keep a second controllable-state copy. Leaves consume + * `Switcher.Root` — it does not keep a second controllable-state copy. Leaves consume * the data through context. */ export function SwitcherRoot(props: SwitcherRootProps): ReactElement { @@ -1000,21 +996,21 @@ export function SwitcherRoot(props: SwitcherRootProps): ReactElement { const layout = resolveSwitcherLayout(mode, data); return ( - {children} - + ); } -export interface SwitcherTriggerProps { +export interface SwitcherTriggerOptions { /** * Names the lead beside its avatar — the organization wherever one heads the trigger, no * selection when personal is hidden and none is active, the account otherwise. Turn it off for the avatar alone. @@ -1034,16 +1030,13 @@ export interface SwitcherTriggerProps { triggerBadge?: SwitcherTriggerBadge; } -export type SwitcherTriggerVariant = 'full' | 'condensed'; - /** The trigger: the lead's avatar, and what it is called. */ export function SwitcherTrigger({ showTriggerLabel = true, triggerBadge = true, triggerVariant = 'condensed', -}: SwitcherTriggerProps & { triggerVariant?: SwitcherTriggerVariant } = {}): ReactElement { +}: SwitcherTriggerOptions & { triggerVariant?: SwitcherTriggerVariant } = {}): ReactElement { const m = useSwitcherMessages(); - const slot = useSlot(); const data = useSwitcherContext(); const lead = leadSelection(data, m); const { name, shape } = lead; @@ -1059,46 +1052,20 @@ export function SwitcherTrigger({ const user = leadUser(lead); return ( - - - {showTriggerLabel ? ( - <> - - {name} - - {typeof planLabel === 'string' ? ( - - {planLabel} - - ) : null} - - - ) : null} - + avatar={ + + } + name={showTriggerLabel ? name : undefined} + badge={typeof planLabel === 'string' ? planLabel : undefined} + badgeId={badge?.id} + shape={shape} + variant={triggerVariant} + /> ); } @@ -1134,27 +1101,23 @@ function TriggerAvatar({ /** The popover surface: header, organizations, what can be done about the lead, and footer. */ export function SwitcherPopup(): ReactElement { const m = useSwitcherMessages(); - const slot = useSlot(); const { renderBranding } = useSwitcherContext(); return ( - - -
- - -