import { clsx, type ClassValue } from 'clsx' import { twMerge } from 'tailwind-merge' import * as React from 'react' export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } type PossibleRef = React.Ref | undefined function setRef(ref: PossibleRef, value: T): (() => void) | void { if (typeof ref === 'function') { return ref(value) as (() => void) | void } else if (ref != null) { ;(ref as React.RefObject).current = value } } export function composeRefs(...refs: PossibleRef[]) { return (node: T): void | (() => void) => { let hasCleanup = false const cleanups = refs.map((ref) => { const cleanup = setRef(ref, node) if (!hasCleanup && typeof cleanup === 'function') { hasCleanup = true } return cleanup }) if (hasCleanup) { return () => { for (let i = 0; i < cleanups.length; i++) { const cleanup = cleanups[i] if (typeof cleanup === 'function') { cleanup() } else { setRef(refs[i], null as T) } } } } return undefined } } export function useComposedRefs(...refs: PossibleRef[]) { // eslint-disable-next-line react-hooks/exhaustive-deps return React.useCallback(composeRefs(...refs), refs) } export function focusableProps(disabled = false) { if (disabled) { return { tabIndex: -1, 'aria-disabled': true, 'data-disabled': '' } as const } return { tabIndex: 0 } as const }