'use client'; import { useSafeLayoutEffect } from '@clerk/shared/react'; import * as stylex from '@stylexjs/stylex'; import React from 'react'; import { ExclamationCircle } from '../../icons/glyphs/exclamation-circle'; import { useTransition } from '../../primitives/hooks/use-transition'; import { useRender } from '../../primitives/utils'; import type { MosaicComponentProps } from '../../props'; import { mergeStyleProps, themeProps } from '../../props'; import { feedbackHeight, feedbackStyles } from '../../styles/feedback.styles'; import { reset } from '../../styles/reset.styles'; import { sizes as typographySizes, styles as typographyStyles, truncationStyles } from '../../styles/typography.styles'; import { FeedbackBody, hasMessage, useHeldMessage, useMessageHeight } from '../../utils/feedback'; import { withTruncatableLabel } from '../../utils/truncatable-label'; import type { HeadingProps } from '../heading'; import { Heading, useHeadingLevel } from '../heading'; import { sectionNestedItemMarker } from './section.markers.stylex'; import { styles } from './section.styles'; export type SectionRootProps = Omit, 'title'>; export type SectionGroupProps = MosaicComponentProps<'div'>; export type SectionHeaderProps = MosaicComponentProps<'div'>; export type SectionTitleProps = Omit; export type SectionBodyProps = MosaicComponentProps<'div'>; export type SectionRowProps = MosaicComponentProps<'div'>; export type SectionItemsProps = MosaicComponentProps<'ul'>; export type SectionItemProps = MosaicComponentProps<'div'> & { wrap?: boolean }; export type SectionMediaSize = 'sm' | 'md' | 'lg' | 'xl'; export type SectionMediaProps = MosaicComponentProps<'div'> & { size?: SectionMediaSize }; export type SectionContentProps = MosaicComponentProps<'div'>; export type SectionLabelProps = MosaicComponentProps<'div'>; export type SectionDescriptionProps = MosaicComponentProps<'div'>; export type SectionActionsProps = MosaicComponentProps<'div'>; export type SectionNoteProps = MosaicComponentProps<'div'> & { icon?: React.ReactNode }; export type SectionErrorProps = MosaicComponentProps<'p'>; const mediaSizes = { sm: styles.mediaSm, md: styles.mediaMd, lg: styles.mediaLg, xl: styles.mediaXl, }; const SectionGroupContext = React.createContext> | null>(null); const SectionHeaderContext = React.createContext(false); const SectionItemsContext = React.createContext(false); const SectionItemWrapContext = React.createContext(false); const Root = React.forwardRef(function SectionRoot({ render, xstyle, ...rest }, ref) { return useRender({ defaultTagName: 'section', render, ref, props: mergeStyleProps(themeProps('section'), stylex.props(reset.base, styles.root, xstyle), rest), }); }); const Group = React.forwardRef(function SectionGroup( { render, xstyle, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, ...rest }, ref, ) { const [titleId, setTitleId] = React.useState(); const element = useRender({ defaultTagName: 'div', render, ref, props: { role: 'group', ...mergeStyleProps(themeProps('section-group'), stylex.props(reset.base, styles.group, xstyle), rest), 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy ?? (ariaLabel ? undefined : titleId), }, }); return {element}; }); const Header = React.forwardRef(function SectionHeader( { render, xstyle, ...rest }, ref, ) { const element = useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps(themeProps('section-header'), stylex.props(reset.base, styles.header, xstyle), rest), }); return {element}; }); const Title = React.forwardRef(function SectionTitle( { id: idProp, xstyle, ...rest }, ref, ) { const setTitleId = React.useContext(SectionGroupContext); const generatedId = React.useId(); const level = useHeadingLevel(); const id = idProp ?? (setTitleId ? `cl-section-${generatedId}-title` : undefined); useSafeLayoutEffect(() => { if (!id || !setTitleId) { return undefined; } setTitleId(id); return () => setTitleId(current => (current === id ? undefined : current)); }, [id, setTitleId]); return ( ); }); const Body = React.forwardRef(function SectionBody({ render, xstyle, ...rest }, ref) { return useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps(themeProps('section-body'), stylex.props(reset.base, styles.body, xstyle), rest), }); }); const Row = React.forwardRef(function SectionRow({ render, xstyle, ...rest }, ref) { return useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps(themeProps('section-row'), stylex.props(reset.base, styles.row, xstyle), rest), }); }); const Items = React.forwardRef(function SectionItems( { render, xstyle, ...rest }, ref, ) { const element = useRender({ defaultTagName: 'ul', render, ref, props: mergeStyleProps(themeProps('section-items'), stylex.props(reset.base, styles.items, xstyle), rest), }); return {element}; }); const Item = React.forwardRef(function SectionItem( { wrap = false, render, xstyle, ...rest }, ref, ) { const nested = React.useContext(SectionItemsContext); const element = useRender({ defaultTagName: nested ? 'li' : 'div', render, ref, props: mergeStyleProps( themeProps('section-item', { nested, wrap }), stylex.props( reset.base, styles.item, nested && styles.nestedItem, nested && sectionNestedItemMarker, wrap && styles.itemWrap, xstyle, ), rest, ), }); return {element}; }); const Media = React.forwardRef(function SectionMedia( { size = 'md', render, xstyle, ...rest }, ref, ) { return useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps( themeProps('section-media', { size }), stylex.props(reset.base, styles.mediaBase, mediaSizes[size], xstyle), rest, ), }); }); const Content = React.forwardRef(function SectionContent( { render, xstyle, ...rest }, ref, ) { const nested = React.useContext(SectionItemsContext); const inHeader = React.useContext(SectionHeaderContext); return useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps( themeProps('section-content', { nested }), stylex.props(reset.base, styles.content, inHeader && styles.headerContent, xstyle), rest, ), }); }); const Label = React.forwardRef(function SectionLabel( { render, xstyle, children, ...rest }, ref, ) { return useRender({ defaultTagName: 'div', render, ref, props: { ...mergeStyleProps(themeProps('section-label'), stylex.props(reset.base, styles.label, xstyle), rest), children: withTruncatableLabel(children), }, }); }); const Description = React.forwardRef(function SectionDescription( { render, xstyle, ...rest }, ref, ) { return useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps( themeProps('section-description'), stylex.props(reset.base, styles.description, xstyle), rest, ), }); }); const Actions = React.forwardRef(function SectionActions( { render, xstyle, ...rest }, ref, ) { const wrap = React.useContext(SectionItemWrapContext); const inHeader = React.useContext(SectionHeaderContext); return useRender({ defaultTagName: 'div', render, ref, props: mergeStyleProps( themeProps('section-actions'), stylex.props(reset.base, styles.actions, wrap && styles.actionsWrap, inHeader && styles.headerActions, xstyle), rest, ), }); }); /** * Static text that takes an action's place in a row, stating why it offers none. Sits directly in * `Section.Item` where a `Section.Actions` would; it holds its own trailing position, so it does not * need one. `icon` renders into a fixed leading slot, sized to the text, for a logo or a lock. */ const Note = React.forwardRef(function SectionNote( { icon, children, render, xstyle, ...rest }, ref, ) { return useRender({ defaultTagName: 'div', render, ref, props: { ...mergeStyleProps(themeProps('section-note'), stylex.props(reset.base, styles.note, xstyle), rest), children: ( <> {icon ? ( {icon} ) : null} {children} ), }, }); }); /** * A row-level message, mirroring `Field.Error` for a row that holds no form control. Place it as a * sibling of `Section.Item` inside `Section.Row`, not inside `Section.Content`: the item stays a * single centered line, so the media and actions hold their position whether or not it is showing. * Carries `role='alert'` for the announcement a `Field.Root` would otherwise wire up. * * It opens and closes on the message it is given, so render it with that message — * `{error}` — rather than behind a conditional. */ const SectionError = React.forwardRef(function SectionError( { render, xstyle, children, ...rest }, ref, ) { const open = hasMessage(children); const element = React.useRef(null); const [messageElement, setMessageElement] = React.useState(null); const height = useMessageHeight(messageElement); const { mounted, transitionProps } = useTransition({ open, ref: element }); const message = useHeldMessage(children, open); const messageProps = stylex.props(reset.base, feedbackStyles.message, feedbackStyles.error); return useRender({ defaultTagName: 'p', render, enabled: mounted, ref: [ref, element], props: { role: 'alert', ...mergeStyleProps( themeProps('section-error'), stylex.props( reset.base, typographyStyles.base, typographySizes.xs, feedbackStyles.collapse, feedbackHeight.measured(height), styles.error, xstyle, ), { 'aria-hidden': open ? undefined : true, ...transitionProps }, rest, ), children: ( {message} ), }, }); }); export { Root as SectionRoot, Group as SectionGroup, Header as SectionHeader, Title as SectionTitle, Body as SectionBody, Row as SectionRow, Items as SectionItems, Item as SectionItem, Media as SectionMedia, Content as SectionContent, Label as SectionLabel, Description as SectionDescription, Actions as SectionActions, Note as SectionNote, SectionError, };