@react-spectrum/s2
Version:
Spectrum 2 UI components in React
1 lines • 3.61 kB
Source Map (JSON)
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AAaM,SAAS,0CAAgB,KAA2B;IACzD,IAAI,YACF,QAAQ,aACR,SAAS,EACV,GAAG;IAEJ,IAAI,aAAa,CAAA,GAAA,YAAI,EAAE,QAAQ,CAAC,OAAO,CAAC;IACxC,IAAI,WAAW,MAAM,GAAG,GACtB,MAAM,IAAI,MAAM;IAGlB,IAAI,CAAC,WAAW,aAAa,GAAG,CAAA,GAAA,eAAO,EAAuB;IAE9D,6EAA6E;IAC7E,0DAA0D;IAC1D,IAAI,QAAkC;IACtC,IAAI,MAAM,OAAO,CAAC,WAChB,QAAQ,SAAS,IAAI,CAAC,CAAA,GAAA,YAAI,EAAE,cAAc;SACrC,kBAAI,CAAA,GAAA,YAAI,EAAE,cAAc,CAAC,WAC9B,QAAQ;IAGV,IAAI,SAAS,UAAU,WACrB,aAAa;IAGf,IAAI,eAAe,CAAC;QAClB,IAAI,CAAC,QACH;IAEJ;IAEA,qBACE,gBAAC,CAAA,GAAA,mBAAW,EAAE,QAAQ;QAAC,OAAO;YAAC,QAAQ,CAAC,CAAC;0BAAO;QAAY;kBACzD;;AAGP;AASO,SAAS;IACd,IAAI,UAAU,CAAA,GAAA,wBAAgB,EAAE,CAAA,GAAA,mBAAW;IAC3C,IAAI,CAAC,SACH,MAAM,IAAI,MAAM;IAGlB,OAAO;QACL;YACE,SAAS,eAAe;QAC1B;IACF;AACF","sources":["packages/@react-spectrum/s2/src/DialogContainer.tsx"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {ModalContext, useSlottedContext} from 'react-aria-components';\nimport React, {ReactElement, ReactNode, useState} from 'react';\nimport {SpectrumDialogContainerProps} from '@react-types/dialog';\n\nexport interface DialogContainerProps extends Omit<SpectrumDialogContainerProps, 'type' | 'isDismissable' | 'isKeyboardDismissDisabled'> {}\n\n/**\n * A DialogContainer accepts a single Dialog as a child, and manages showing and hiding\n * it in a modal. Useful in cases where there is no trigger element\n * or when the trigger unmounts while the dialog is open.\n */\nexport function DialogContainer(props: DialogContainerProps): ReactNode {\n let {\n children,\n onDismiss\n } = props;\n\n let childArray = React.Children.toArray(children);\n if (childArray.length > 1) {\n throw new Error('Only a single child can be passed to DialogContainer.');\n }\n\n let [lastChild, setLastChild] = useState<ReactElement | null>(null);\n\n // React.Children.toArray mutates the children, and we need them to be stable\n // between renders so that the lastChild comparison works.\n let child: ReactElement | undefined = undefined;\n if (Array.isArray(children)) {\n child = children.find(React.isValidElement);\n } else if (React.isValidElement(children)) {\n child = children;\n }\n\n if (child && child !== lastChild) {\n setLastChild(child);\n }\n\n let onOpenChange = (isOpen: boolean) => {\n if (!isOpen) {\n onDismiss();\n }\n };\n\n return (\n <ModalContext.Provider value={{isOpen: !!child, onOpenChange}}>\n {lastChild}\n </ModalContext.Provider>\n );\n}\n\nexport interface DialogContainerValue {\n /**\n * A handler to programmatically dismiss the dialog.\n */\n dismiss(): void\n}\n\nexport function useDialogContainer(): DialogContainerValue {\n let context = useSlottedContext(ModalContext);\n if (!context) {\n throw new Error('Cannot call useDialogContext outside a <DialogTrigger> or <DialogContainer>.');\n }\n\n return {\n dismiss() {\n context?.onOpenChange?.(false);\n }\n };\n}\n"],"names":[],"version":3,"file":"DialogContainer.mjs.map"}