import * as ak from "@ariakit/react"; import type { RecipeLike, VariantProps, VariantPropsWithRecipe } from "clava"; import { cx, splitProps } from "clava"; import type * as React from "react"; import { appendOptionalChild, createOptionalRender, prependOptionalSeparator, } from "../react-utils/create-render.react.ts"; import { button, buttonBar, buttonContent, buttonDescription, buttonGroup, buttonLabel, buttonSeparator, buttonSlot, } from "../styles/button.ts"; import { carouselViewportBase } from "../styles/carousel.ts"; import { frame } from "../styles/frame.ts"; import { Carousel, CarouselViewport } from "./carousel.ariakit.react.tsx"; import type { CarouselButtonRender } from "./carousel.ariakit.react.tsx"; export type ButtonProps< R extends RecipeLike = typeof button, > = ak.ButtonProps & VariantPropsWithRecipe & { /** * The recipe applied in place of `button`. It must supply every `button` * variant. */ recipe?: R; /** * A bar that marks the button while checked or current, as `true`, an * element, or `ButtonBar` props. Rendered after the children. Children of a * `render` element replace it, so pass them to the button instead. */ bar?: boolean | React.ReactElement | ButtonBarProps; /** * A separator before the button, as `true`, an element, or * `ButtonSeparator` props. It renders as a sibling of the button, hidden * from assistive technologies. Pass `!!index` when mapping the buttons of a * `ButtonGroup` to separate each button from the one before it. */ separator?: boolean | React.ReactElement | ButtonSeparatorProps; }; /** * A button with shared control styles. * @example * * @example * * * * */ export function Button = typeof button>({ recipe, $disabled, bar, separator, ...props }: ButtonProps) { const styles = recipe ?? button; const [variantProps, rest] = splitProps(props, styles); const disabled = props.disabled || props["aria-disabled"] === true || props["aria-disabled"] === "true"; const barElement = createOptionalRender(ButtonBar, bar === true ? {} : bar); return prependOptionalSeparator( ButtonSeparator, separator, {appendOptionalChild(props.children, barElement)} , ); } export interface ButtonGroupProps extends ak.GroupProps, VariantProps { /** * Renders scroll buttons before and after a segmented group with padding, * outside the element with the `group` role. They show with a fine pointer * while the group can scroll toward their side. Defaults to `true`. */ buttons?: boolean; /** * The button before a segmented group, as `true`, an element, or * `CarouselButton` props. `false` omits it. Defaults to a button named * "Scroll backward". */ previousButton?: CarouselButtonRender; /** * The button after a segmented group, as `true`, an element, or * `CarouselButton` props. `false` omits it. Defaults to a button named * "Scroll forward". */ nextButton?: CarouselButtonRender; } /** * Groups related buttons in one surface. With `$kind="segmented"` and padding, * the group scrolls sideways when it doesn't fit, fades its segments at each * end while more segments lie that way, and shows buttons that scroll it with a * fine pointer. A carousel root holds the group and the buttons, which sit * outside the element with the `group` role. The root paints the surface of the * group and takes `className` and `style`. * @example * * * * */ export function ButtonGroup({ ref, dir, buttons, previousButton, nextButton, ...props }: ButtonGroupProps) { const [variantProps, rest] = splitProps(props, buttonGroup); const variants = buttonGroup.getVariants(variantProps); if (variants.$kind !== "segmented" || variants.$p === "none") { return ( ); } // A segmented group with padding scrolls in a carousel row. Its fades would // fade its surface too, so the carousel paints the surface with the frame of // the group, and the group covers the carousel. const [surfaceProps] = splitProps(variants, frame); const { className, style, ...groupProps } = variantProps; return ( ); } export interface ButtonBarProps extends ak.RoleProps<"span">, VariantProps {} /** * A thin bar that marks a checked or current button. Pass it, or its props, to * the `bar` prop of `Button` so it renders as the last child. * @example * * * * */ export function ButtonBar(props: ButtonBarProps) { const [variantProps, rest] = splitProps(props, buttonBar); return ( ); } export interface ButtonSeparatorProps extends ak.RoleProps<"div">, VariantProps {} /** * A visual divider between grouped buttons. Pass it, or its props, to the * `separator` prop of `Button` or `RadioCard` so it renders before the control, * hidden from assistive technologies. * @example * * * * */ export function ButtonSeparator(props: ButtonSeparatorProps) { const [variantProps, rest] = splitProps(props, buttonSeparator); return ; } export interface ButtonContentProps extends ak.RoleProps<"span">, VariantProps {} /** * Stacks a button label and description. * @example * */ export function ButtonContent(props: ButtonContentProps) { const [variantProps, rest] = splitProps(props, buttonContent); return ; } export interface ButtonLabelProps extends ak.RoleProps<"span">, VariantProps { /** * The label text, rendered when `children` is omitted. A current segment * shows its label in a heavier weight only when this is set, because the text * reserves the heavier width in advance and keeps the segment size. Keep it * equal to the visible text of custom `children`. */ text?: string; } /** * The main text of a button. Truncates to one line by default; set * `$truncate={false}` to let it wrap. * @example * * @example * * * * */ export function ButtonLabel({ text, children = text, ...props }: ButtonLabelProps) { const [variantProps, rest] = splitProps(props, buttonLabel); return ( {children} ); } export interface ButtonDescriptionProps extends ak.RoleProps<"span">, VariantProps {} /** * Secondary text below a button label. Truncates to one line by default; set * `$truncate={false}` to let it wrap. * @example * */ export function ButtonDescription(props: ButtonDescriptionProps) { const [variantProps, rest] = splitProps(props, buttonDescription); return ; } export interface ButtonSlotProps extends ak.RoleProps<"span">, VariantProps {} /** * An icon or other content beside a button label. Pass `aria-hidden` when the * entire slot is decorative. * @example * */ export function ButtonSlot(props: ButtonSlotProps) { const [variantProps, rest] = splitProps(props, buttonSlot); return ; }