import React, { forwardRef, useRef, useState } from 'react'; import { Tone, Surface, Option, cn, useClickOutside, toneMap, surfaceClasses, useEffectiveSurface, ChevronDownIcon, } from '../common'; import { PixelButton } from './PixelButton'; /* ───────────────────────────────────────────────────────────────────────── PixelSplitButton — primary action + chevron dropdown for secondary options. ───────────────────────────────────────────────────────────────────────── */ /** Public prop bag for {@link PixelSplitButton}. */ export interface PixelSplitButtonProps { /** Text shown on the primary (left) button. */ label: string; /** Options shown in the dropdown menu. */ options: Option[]; /** Color tone (maps to `toneMap`). */ tone?: Tone; /** Surface aesthetic override; defaults to nearest provider. */ surface?: Surface; /** When true, both primary button and chevron trigger are disabled. */ disabled?: boolean; /** Fires when the primary (label) button is clicked. */ onPrimary?: () => void; /** Fires with the selected option's `value` when a menu item is chosen. */ onSelect?: (value: string) => void; } export const PixelSplitButton = forwardRef(function PixelSplitButton( { label, options, tone = 'purple', surface: surfaceProp, disabled = false, onPrimary, onSelect, }, ref, ) { const surface = useEffectiveSurface(surfaceProp); const s = surfaceClasses(surface); const [open, setOpen] = useState(false); const [alignRight, setAlignRight] = useState(false); const rootRef = useRef(null); useClickOutside(rootRef, () => setOpen(false)); return (
{ rootRef.current = node; if (typeof ref === 'function') ref(node); else if (ref) (ref as React.MutableRefObject).current = node; }} className="relative inline-flex" >
{label}
{open && (
{options.map((opt) => ( ))}
)}
); }); PixelSplitButton.displayName = 'PixelSplitButton';