import { forwardRef, isValidElement, cloneElement, type ReactNode, type PropsWithChildren, type ForwardedRef, } from 'react'; import type { Button as ButtonElement, ButtonProperties, ButtonEvents, } from '@watching/clips/elements'; import {useCustomElementProperties} from './shared.ts'; export interface ButtonProps extends PropsWithChildren> { ref?: ForwardedRef; overlay?: ReactNode; onPress?(): void | Promise; onpress?(event: ButtonEvents['press']): void; } declare module 'react' { namespace JSX { interface IntrinsicElements { 'ui-button': Omit; } } } export const Button = forwardRef(function Button( {overlay, children, onPress, ...props}, ref, ) { const allProps: ButtonProps = { onpress: onPress ? (event) => event.respondWith(onPress()) : undefined, ...props, }; const wrapperRef = useCustomElementProperties(allProps, ref); return ( {children} {overlay && isValidElement(overlay) ? cloneElement(overlay, {slot: 'overlay'}) : null} ); });