/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import { useCallback, useMemo, useState } from 'react'; import * as React from 'react'; import Modal from '../ui/Modal'; export default function useModal(): [ JSX.Element, (title: string, showModal: (onClose: () => void) => JSX.Element) => void ] { const [modalContent, setModalContent] = useState(null); const onClose = useCallback(() => { setModalContent(null); }, []); const modal = useMemo(() => { if (modalContent === null) { return null; } const { title, content, closeOnClickOutside } = modalContent; return ( {content} ); }, [modalContent, onClose]); const showModal = useCallback( ( title, // eslint-disable-next-line no-shadow getContent: (onClose: () => void) => JSX.Element, closeOnClickOutside = false ) => { setModalContent({ closeOnClickOutside, content: getContent(onClose), title, }); }, [onClose] ); return [modal, showModal]; }