/* * Portions of this file are based on code from react-spectrum. * Apache License Version 2.0, Copyright 2020 Adobe. * * Credits to the React Spectrum team: * https://github.com/adobe/react-spectrum/blob/810579b671791f1593108f62cdc1893de3a220e3/packages/@react-aria/overlays/src/useOverlayTrigger.ts */ import { callHandler, mergeRefs } from "@kobalte/utils"; import { type Component, type JSX, type ValidComponent, splitProps, } from "solid-js"; import * as Button from "../button"; import type { ElementOf, PolymorphicProps } from "../polymorphic"; import { useDialogContext } from "./dialog-context"; export interface DialogTriggerOptions {} export interface DialogTriggerCommonProps extends Button.ButtonRootCommonProps { onClick: JSX.EventHandlerUnion; } export interface DialogTriggerRenderProps extends DialogTriggerCommonProps, Button.ButtonRootRenderProps { "aria-haspopup": "dialog"; "aria-expanded": boolean; "aria-controls": string | undefined; "data-expanded": string | undefined; "data-closed": string | undefined; } export type DialogTriggerProps< T extends ValidComponent | HTMLElement = HTMLElement, > = DialogTriggerOptions & Partial>>; /** * The button that opens the dialog. */ export function DialogTrigger( props: PolymorphicProps>, ) { const context = useDialogContext(); const [local, others] = splitProps(props as DialogTriggerProps, [ "ref", "onClick", ]); const onClick: JSX.EventHandlerUnion = (e) => { callHandler(e, local.onClick); context.toggle(); }; return ( > > ref={mergeRefs(context.setTriggerRef, local.ref)} aria-haspopup="dialog" aria-expanded={context.isOpen()} aria-controls={context.isOpen() ? context.contentId() : undefined} data-expanded={context.isOpen() ? "" : undefined} data-closed={!context.isOpen() ? "" : undefined} onClick={onClick} {...others} /> ); }