import React from 'react'; import { useOverlayTrigger } from 'react-aria'; import { useOverlayTriggerState } from 'react-stately'; import { DOMAttributes, FocusableElement } from '@react-types/shared'; import clsx from 'clsx'; import { Modal } from './Modal'; import { ModalOpeningButton } from './ModalOpeningButton'; export function ModalTrigger({ label, labelClasses, showLabel, containerClasses, icon, isDisabled, children, scope, ...props }: { label: string; labelClasses?: string; containerClasses?: string; showLabel?: boolean; icon?: React.ReactNode; isDisabled?: boolean; children: (onClose: () => void, titleProps: DOMAttributes) => React.ReactElement; // Child needs to be a functions which generates the 'content' so we can pass the onClose function and title aria props scope: 'squiz-gb-scope' | 'squiz-cb-scope' | 'squiz-rb-scope'; }) { const state = useOverlayTriggerState(props); const { triggerProps, overlayProps } = useOverlayTrigger({ type: 'dialog' }, state); let ariaAttr: React.AriaAttributes = {}; if (!showLabel) { ariaAttr = { ...ariaAttr, 'aria-label': label }; } return (
{icon} {showLabel && {label}} {state.isOpen && ( {(titleProps) => children(state.close, titleProps)} )}
); }