// // Copyright 2026 DXOS.org // // @import-as-namespace import { ark } from '@ark-ui/react/factory'; import React, { type ReactNode, useId } from 'react'; import { useTranslation } from 'react-i18next'; import { createContext } from '@dxos/react-hooks'; import { type MessageValence } from '@dxos/ui-types'; import { translationKey } from '#translations'; import { recipes } from '../../recipes.ts'; import { composable, composableProps, slottable } from '../../util/slots.ts'; import { Block } from '../Block/Block.tsx'; import { Button } from '../Button/Button.tsx'; import { DefaultGutterProvider, containerAttributes, useInGrid } from '../Container/Container.tsx'; import * as Icon from '../Icon/Icon.tsx'; import * as Typography from '../Typography/Typography.tsx'; const BANNER_ICONS: Record = { success: 'ph--check-circle--duotone', info: 'ph--info--duotone', warning: 'ph--warning--duotone', error: 'ph--warning-circle--duotone', neutral: 'ph--info--duotone', }; type BannerContextValue = { titleId: string; descriptionId: string; valence: MessageValence; icon?: string }; const [BannerProvider, useBannerContext] = createContext('Banner'); // // Root // type BannerRootProps = { valence?: MessageValence; /** Overrides the valence's icon in the Title's start rail. */ icon?: string; /** Inset by a gutter of its own; by default only outside a grid, so `false` for a host that already pads it. */ inset?: boolean; }; /** * A message on its valence's surface: a rail-gutter Container whose Title puts the icon in the start rail and an * optional close button in the end rail, so the Body aligns with the title text. A neutral banner is a note, * any other an alert. Buttons with `variant='valence'` inside it take its colours. */ const BannerRoot = composable( ({ children, valence = 'neutral', icon, inset: insetProp, ...props }, forwardedRef) => { const titleId = useId(); const descriptionId = useId(); // Outside a Container (a pane's body) there is no gutter to sit in, so the banner insets itself as a form's would be. const inGrid = useInGrid(); const inset = insetProp ?? !inGrid; const { style, ...attributes } = containerAttributes({ gutter: 'rail' }); const { className, style: propsStyle, ...rest } = composableProps(props, { classNames: [recipes.container(), recipes.banner()], role: valence === 'neutral' ? 'note' : 'alert', }); return (
{children}
); }, ); BannerRoot.displayName = 'Banner.Root'; // // Title // type BannerTitleProps = { /** Overrides the Root's icon for this title. */ icon?: string; /** Shows a close button in the end rail. */ onClose?: () => void; children?: ReactNode; }; const BannerTitle = composable( ({ children, icon: iconProp, onClose, ...props }, forwardedRef) => { const { t } = useTranslation(translationKey); const { titleId, valence, icon: rootIcon } = useBannerContext('Banner.Title'); const icon = iconProp ?? rootIcon ?? BANNER_ICONS[valence]; const { style, ...attributes } = containerAttributes({ layout: 'row' }); const { className, style: propsStyle, ...rest } = composableProps(props, { classNames: [recipes.container(), recipes.bannerTitle()], }); return (

{children}

{onClose && (
); }, ); BannerTitle.displayName = 'Banner.Title'; // // Body // const BannerBody = slottable(({ children, asChild, ...props }, forwardedRef) => { const { descriptionId } = useBannerContext('Banner.Body'); const { className, ...rest } = composableProps(props, { classNames: [recipes.typography(), recipes.bannerBody()], }); return ( {children} ); }); BannerBody.displayName = 'Banner.Body'; export type { BannerRootProps as RootProps, BannerTitleProps as TitleProps }; export { BannerBody as Body, BannerRoot as Root, BannerTitle as Title };