UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 6.39 kB
{"version":3,"file":"use-modal.cjs","names":["useDisclosure","useI18n","getRootProps: PropGetter","getOverlayProps: PropGetter","getOpenTriggerProps: PropGetter<\"button\">","getCloseTriggerProps: PropGetter<\"button\">","getCloseButtonProps: PropGetter<\"button\">","getContentProps: PropGetter<\"section\">","getHeaderProps: PropGetter<\"header\">","getTitleProps: PropGetter<\"h2\">"],"sources":["../../../../src/components/modal/use-modal.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\"\nimport type { HTMLProps, PropGetter } from \"../../core\"\nimport type { UseDisclosureProps } from \"../../hooks/use-disclosure\"\nimport { useCallback, useId } from \"react\"\nimport { useDisclosure } from \"../../hooks/use-disclosure\"\nimport { useI18n } from \"../../providers/i18n-provider\"\nimport { cx, handlerAll } from \"../../utils\"\n\nexport interface UseModalProps\n extends HTMLProps,\n Omit<UseDisclosureProps, \"timing\"> {\n /**\n * If `true`, the modal will close when the `Esc` key is pressed.\n *\n * @default true\n */\n closeOnEsc?: boolean\n /**\n * If `true`, the modal will close when the overlay is clicked.\n *\n * @default true\n */\n closeOnOverlay?: boolean\n /**\n * Callback invoked when the escape key is pressed and focus is within modal.\n */\n onEsc?: () => void\n}\n\nexport const useModal = ({\n closeOnEsc = true,\n closeOnOverlay = true,\n defaultOpen,\n open: openProp,\n onClose: onCloseProp,\n onEsc,\n onOpen: onOpenProp,\n ...rest\n}: UseModalProps = {}) => {\n const { open, onClose, onOpen } = useDisclosure({\n defaultOpen,\n open: openProp,\n onClose: onCloseProp,\n onOpen: onOpenProp,\n ...rest,\n })\n const contentId = useId()\n const titleId = useId()\n const bodyId = useId()\n const { t } = useI18n(\"modal\")\n\n const onKeyDown = useCallback(\n (ev: KeyboardEvent) => {\n if (ev.key !== \"Escape\") return\n\n ev.stopPropagation()\n\n if (closeOnEsc) onClose()\n\n onEsc?.()\n },\n [closeOnEsc, onClose, onEsc],\n )\n\n const getRootProps: PropGetter = useCallback(\n (props) => ({\n ...rest,\n ...props,\n }),\n [rest],\n )\n\n const getOverlayProps: PropGetter = useCallback(\n (props = {}) => ({\n \"aria-hidden\": \"true\",\n ...props,\n onClick: handlerAll(props.onClick, (ev) => {\n ev.stopPropagation()\n\n if (closeOnOverlay) onClose()\n }),\n }),\n [closeOnOverlay, onClose],\n )\n\n const getOpenTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-controls\": open ? contentId : undefined,\n \"aria-expanded\": open,\n \"aria-haspopup\": \"dialog\",\n \"aria-label\": t(\"Open modal\"),\n ...props,\n onClick: handlerAll(props.onClick, onOpen),\n }),\n [contentId, onOpen, open, t],\n )\n\n const getCloseTriggerProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-label\": t(\"Close modal\"),\n ...props,\n onClick: handlerAll(props.onClick, onClose),\n }),\n [onClose, t],\n )\n\n const getCloseButtonProps: PropGetter<\"button\"> = useCallback(\n (props = {}) => ({\n \"aria-label\": t(\"Close modal\"),\n ...props,\n onClick: handlerAll(props.onClick, onClose),\n }),\n [onClose, t],\n )\n\n const getContentProps: PropGetter<\"section\"> = useCallback(\n ({\n \"aria-describedby\": ariaDescribedby,\n \"aria-labelledby\": ariaLabelledby,\n ...props\n } = {}) => ({\n id: contentId,\n \"aria-describedby\": cx(ariaDescribedby, bodyId),\n \"aria-labelledby\": cx(ariaLabelledby, titleId),\n \"aria-modal\": \"true\",\n role: \"dialog\",\n ...props,\n onKeyDown: handlerAll(props.onKeyDown, onKeyDown),\n }),\n [bodyId, contentId, onKeyDown, titleId],\n )\n\n const getHeaderProps: PropGetter<\"header\"> = useCallback(\n (props) => ({\n ...props,\n }),\n [],\n )\n\n const getTitleProps: PropGetter<\"h2\"> = useCallback(\n (props) => ({\n id: titleId,\n ...props,\n }),\n [titleId],\n )\n\n const getBodyProps: PropGetter = useCallback(\n (props) => ({\n id: bodyId,\n ...props,\n }),\n [bodyId],\n )\n\n const getFooterProps: PropGetter<\"footer\"> = useCallback(\n (props) => ({\n ...props,\n }),\n [],\n )\n\n return {\n open,\n getBodyProps,\n getCloseButtonProps,\n getCloseTriggerProps,\n getContentProps,\n getFooterProps,\n getHeaderProps,\n getOpenTriggerProps,\n getOverlayProps,\n getRootProps,\n getTitleProps,\n onClose,\n onOpen,\n }\n}\n\nexport type UseModalReturn = ReturnType<typeof useModal>\n"],"mappings":";;;;;;;;AA6BA,MAAa,YAAY,EACvB,aAAa,MACb,iBAAiB,MACjB,aACA,MAAM,UACN,SAAS,aACT,OACA,QAAQ,WACR,GAAG,SACc,EAAE,KAAK;CACxB,MAAM,EAAE,MAAM,SAAS,WAAWA,qCAAc;EAC9C;EACA,MAAM;EACN,SAAS;EACT,QAAQ;EACR,GAAG;EACJ,CAAC;CACF,MAAM,8BAAmB;CACzB,MAAM,4BAAiB;CACvB,MAAM,2BAAgB;CACtB,MAAM,EAAE,MAAMC,8BAAQ,QAAQ;CAE9B,MAAM,oCACH,OAAsB;AACrB,MAAI,GAAG,QAAQ,SAAU;AAEzB,KAAG,iBAAiB;AAEpB,MAAI,WAAY,UAAS;AAEzB,WAAS;IAEX;EAAC;EAAY;EAAS;EAAM,CAC7B;CAED,MAAMC,uCACH,WAAW;EACV,GAAG;EACH,GAAG;EACJ,GACD,CAAC,KAAK,CACP;CAED,MAAMC,0CACH,QAAQ,EAAE,MAAM;EACf,eAAe;EACf,GAAG;EACH,2DAAoB,MAAM,UAAU,OAAO;AACzC,MAAG,iBAAiB;AAEpB,OAAI,eAAgB,UAAS;IAC7B;EACH,GACD,CAAC,gBAAgB,QAAQ,CAC1B;CAED,MAAMC,8CACH,QAAQ,EAAE,MAAM;EACf,iBAAiB,OAAO,YAAY;EACpC,iBAAiB;EACjB,iBAAiB;EACjB,cAAc,EAAE,aAAa;EAC7B,GAAG;EACH,2DAAoB,MAAM,SAAS,OAAO;EAC3C,GACD;EAAC;EAAW;EAAQ;EAAM;EAAE,CAC7B;CAED,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