import { Comment, Fragment, Text, cloneVNode, isVNode, type VNode } from "vue"; type Handler = (event: MouseEvent) => void; function isIgnorableVNode(node: VNode): boolean { if (node.type === Comment) return true; if (node.type === Text) { return String(node.children ?? "").trim() === ""; } return false; } /** Flatten arrays/Fragments and drop comments/blank text. */ export function meaningfulVNodes(list: unknown[]): VNode[] { const out: VNode[] = []; const walk = (nodes: unknown[]) => { for (const node of nodes) { if (node == null || node === false || node === true) continue; if (Array.isArray(node)) { walk(node); continue; } if (!isVNode(node)) continue; if (node.type === Fragment) { walk((node.children ?? []) as unknown[]); continue; } if (isIgnorableVNode(node)) continue; out.push(node); } }; walk(list); return out; } export interface AsChildTarget { root?: VNode; nodes: VNode[]; fallback: unknown; } export function resolveAsChildTarget(children: unknown): AsChildTarget { const list = Array.isArray(children) ? children : [children]; const nodes = meaningfulVNodes(list); return { root: nodes.length === 1 ? nodes[0] : undefined, nodes, fallback: children, }; } /** * Vue wraps listeners in createInvoker (fn with .value = original handler(s)). * Unwrap so composed arrays contain the real functions; Vue's array invoker * then applies native stop / stopImmediatePropagation semantics. */ function unwrapInvoker(value: unknown): unknown { if (typeof value === "function" && value && "value" in value) { return (value as { value: unknown }).value; } return value; } function flattenHandlers(value: unknown, out: Handler[] = []): Handler[] { const raw = unwrapInvoker(value); if (!raw) return out; if (Array.isArray(raw)) { for (const item of raw) flattenHandlers(item, out); return out; } if (typeof raw === "function") out.push(raw as Handler); return out; } export interface TriggerClickOptions { isDisabled: () => boolean; /** Child onClick payload (function or array), stripped before clone. */ childOnClick?: unknown; emitClick: (event: MouseEvent) => void; activate: () => void; } /** * Ordered asChild click listeners for Vue's array invoker: * 1. disabled guard (stopImmediatePropagation) * 2. child listeners (stopPropagation keeps later ones; stopImmediate stops them) * 3. emit + activate (skipped on stopImmediate; activate skipped on preventDefault) */ export function makeTriggerClickHandlers( options: TriggerClickOptions, ): Handler[] { return [ function disabledGuard(event: MouseEvent) { if (options.isDisabled()) { event.preventDefault(); event.stopImmediatePropagation(); } }, ...flattenHandlers(options.childOnClick), function emitAndActivate(event: MouseEvent) { options.emitClick(event); if (event.defaultPrevented) return; options.activate(); }, ]; } /** * Capture-phase listeners: disabled guard first, then Trigger and child captures. */ export function makeTriggerCaptureHandlers( isDisabled: () => boolean, captureGroups: unknown[], ): Handler[] { const handlers: Handler[] = [ function disabledGuard(event: MouseEvent) { if (isDisabled()) { event.preventDefault(); event.stopImmediatePropagation(); } }, ]; for (const group of captureGroups) { handlers.push(...flattenHandlers(group)); } return handlers; } export interface AsChildCloneOptions { isDisabled: () => boolean; emitClick: (event: MouseEvent) => void; activate: () => void; class: unknown; /** Trigger fallthrough attrs; onClick / onClickCapture are consumed here. */ shared: Record; } /** * Clone the unique asChild root. Child and Trigger click/capture listeners are * stripped then re-attached as ordered arrays (one execution path, correct * stop semantics via Vue's invoker). */ export function cloneAsChildRoot( root: VNode, options: AsChildCloneOptions, ): VNode { const childProps = (root.props ?? {}) as Record; const childOnClick = childProps.onClick; const childCapture = childProps.onClickCapture ?? childProps["onClick-capture"] ?? childProps.onCapture; const cloned = cloneVNode(root); const cleanedProps: Record = { ...(cloned.props ?? {}) }; delete cleanedProps.onClick; delete cleanedProps.onClickCapture; delete cleanedProps["onClick-capture"]; cloned.props = cleanedProps; const triggerCapture = options.shared.onClickCapture; const sharedRest: Record = { ...options.shared }; delete sharedRest.onClickCapture; delete sharedRest.onClick; return cloneVNode(cloned, { ...sharedRest, class: options.class, onClickCapture: makeTriggerCaptureHandlers(options.isDisabled, [ triggerCapture, childCapture, ]), onClick: makeTriggerClickHandlers({ isDisabled: options.isDisabled, childOnClick, emitClick: options.emitClick, activate: options.activate, }), }); }