| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Deferred} from 'shared/utils'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 12 | import {atom, useAtom, useSetAtom} from 'jotai'; |
| b69ab31 | | | 13 | import React, {useCallback, useEffect, useRef} from 'react'; |
| b69ab31 | | | 14 | import {defer} from 'shared/utils'; |
| b69ab31 | | | 15 | import {useCommand} from './ISLShortcuts'; |
| b69ab31 | | | 16 | import {Modal} from './Modal'; |
| b69ab31 | | | 17 | import {writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | import './useModal.css'; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | type ButtonConfig = {label: string | React.ReactNode; primary?: boolean}; |
| b69ab31 | | | 22 | type ModalConfigBase = { |
| b69ab31 | | | 23 | /** Optional codicon to show next to the title */ |
| b69ab31 | | | 24 | icon?: string; |
| b69ab31 | | | 25 | title?: React.ReactNode; |
| b69ab31 | | | 26 | width?: string | number; |
| b69ab31 | | | 27 | height?: string | number; |
| b69ab31 | | | 28 | maxWidth?: string | number; |
| b69ab31 | | | 29 | maxHeight?: string | number; |
| b69ab31 | | | 30 | dataTestId?: string; |
| b69ab31 | | | 31 | }; |
| b69ab31 | | | 32 | type ModalConfig<T> = ModalConfigBase & |
| b69ab31 | | | 33 | ( |
| b69ab31 | | | 34 | | { |
| b69ab31 | | | 35 | // hack: using 'confirm' mode requires T to be string. |
| b69ab31 | | | 36 | // The type inference goes wrong if we try to add this constraint directly to the `buttons` field. |
| b69ab31 | | | 37 | // By adding the constraint here, we get type checking that T is string in order to use this API. |
| b69ab31 | | | 38 | type: T extends string ? 'confirm' : T extends ButtonConfig ? 'confirm' : never; |
| b69ab31 | | | 39 | message: React.ReactNode; |
| b69ab31 | | | 40 | buttons: ReadonlyArray<T>; |
| b69ab31 | | | 41 | } |
| b69ab31 | | | 42 | | { |
| b69ab31 | | | 43 | type: 'custom'; |
| b69ab31 | | | 44 | component: (props: {returnResultAndDismiss: (data: T) => void}) => React.ReactNode; |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | ); |
| b69ab31 | | | 47 | type ModalState<T> = { |
| b69ab31 | | | 48 | config: ModalConfig<T>; |
| b69ab31 | | | 49 | visible: boolean; |
| b69ab31 | | | 50 | deferred: Deferred<T | undefined>; |
| b69ab31 | | | 51 | }; |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const modalState = atom<ModalState<unknown | string> | null>(null); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | /** Wrapper around <Modal>, generated by `useModal()` hooks. */ |
| b69ab31 | | | 56 | export function ModalContainer() { |
| b69ab31 | | | 57 | const [modal, setModal] = useAtom(modalState); |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | // we expect at most one button is "primary" |
| b69ab31 | | | 60 | const primaryButtonRef = useRef(null); |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | const dismiss = () => { |
| b69ab31 | | | 63 | if (modal?.visible) { |
| b69ab31 | | | 64 | modal.deferred.resolve(undefined); |
| b69ab31 | | | 65 | setModal({...modal, visible: false}); |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | }; |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | useCommand('Escape', dismiss); |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | // focus primary button on mount |
| b69ab31 | | | 72 | useEffect(() => { |
| b69ab31 | | | 73 | if (modal?.visible && primaryButtonRef.current != null) { |
| b69ab31 | | | 74 | (primaryButtonRef.current as HTMLButtonElement).focus(); |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | }, [primaryButtonRef, modal?.visible]); |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | if (modal?.visible !== true) { |
| b69ab31 | | | 79 | return null; |
| b69ab31 | | | 80 | } |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | let content; |
| b69ab31 | | | 83 | if ((modal.config as ModalConfig<string>).type === 'confirm') { |
| b69ab31 | | | 84 | const config = modal.config as ModalConfig<string> & {type: 'confirm'}; |
| b69ab31 | | | 85 | content = ( |
| b69ab31 | | | 86 | <> |
| b69ab31 | | | 87 | <div id="use-modal-message">{config.message}</div> |
| b69ab31 | | | 88 | <div className="use-modal-buttons"> |
| b69ab31 | | | 89 | {config.buttons.map((button: string | ButtonConfig, index: number) => { |
| b69ab31 | | | 90 | const label = typeof button === 'object' ? button.label : button; |
| b69ab31 | | | 91 | const isPrimary = typeof button === 'object' && button.primary != null; |
| b69ab31 | | | 92 | return ( |
| b69ab31 | | | 93 | <Button |
| b69ab31 | | | 94 | kind={isPrimary ? 'primary' : undefined} |
| b69ab31 | | | 95 | onClick={() => { |
| b69ab31 | | | 96 | modal.deferred.resolve(button); |
| b69ab31 | | | 97 | setModal({...modal, visible: false}); |
| b69ab31 | | | 98 | }} |
| b69ab31 | | | 99 | ref={isPrimary ? primaryButtonRef : undefined} |
| b69ab31 | | | 100 | key={index}> |
| b69ab31 | | | 101 | {label} |
| b69ab31 | | | 102 | </Button> |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | })} |
| b69ab31 | | | 105 | </div> |
| b69ab31 | | | 106 | </> |
| b69ab31 | | | 107 | ); |
| b69ab31 | | | 108 | } else if (modal.config.type === 'custom') { |
| b69ab31 | | | 109 | content = modal.config.component({ |
| b69ab31 | | | 110 | returnResultAndDismiss: data => { |
| b69ab31 | | | 111 | modal.deferred.resolve(data); |
| b69ab31 | | | 112 | setModal({...modal, visible: false}); |
| b69ab31 | | | 113 | }, |
| b69ab31 | | | 114 | }); |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | return ( |
| b69ab31 | | | 118 | <Modal |
| b69ab31 | | | 119 | height={modal.config.height} |
| b69ab31 | | | 120 | width={modal.config.width} |
| b69ab31 | | | 121 | maxHeight={modal.config.maxHeight} |
| b69ab31 | | | 122 | maxWidth={modal.config.maxWidth} |
| b69ab31 | | | 123 | className="use-modal" |
| b69ab31 | | | 124 | aria-labelledby="use-modal-title" |
| b69ab31 | | | 125 | aria-describedby="use-modal-message" |
| b69ab31 | | | 126 | dataTestId={modal.config.dataTestId} |
| b69ab31 | | | 127 | dismiss={dismiss}> |
| b69ab31 | | | 128 | {modal.config.title != null && ( |
| b69ab31 | | | 129 | <div id="use-modal-title"> |
| b69ab31 | | | 130 | {modal.config.icon != null ? <Icon icon={modal.config.icon} size="M" /> : null} |
| b69ab31 | | | 131 | {typeof modal.config.title === 'string' ? ( |
| b69ab31 | | | 132 | <span>{modal.config.title}</span> |
| b69ab31 | | | 133 | ) : ( |
| b69ab31 | | | 134 | modal.config.title |
| b69ab31 | | | 135 | )} |
| b69ab31 | | | 136 | </div> |
| b69ab31 | | | 137 | )} |
| b69ab31 | | | 138 | {content} |
| b69ab31 | | | 139 | </Modal> |
| b69ab31 | | | 140 | ); |
| b69ab31 | | | 141 | } |
| b69ab31 | | | 142 | |
| b69ab31 | | | 143 | /** |
| b69ab31 | | | 144 | * Hook that provides a callback to show a modal with customizable behavior. |
| b69ab31 | | | 145 | * Modal has a dismiss button & dismisses on Escape keypress, thus you must always be able to handle |
| b69ab31 | | | 146 | * returning `undefined`. |
| b69ab31 | | | 147 | * |
| b69ab31 | | | 148 | * For now, we assume all uses of useOptionModal are triggered directly from a user action. |
| b69ab31 | | | 149 | * If that's not the case, it would be possible to have multiple modals overlap. |
| b69ab31 | | | 150 | **/ |
| b69ab31 | | | 151 | export function useModal(): <T>(config: ModalConfig<T>) => Promise<T | undefined> { |
| b69ab31 | | | 152 | const setModal = useSetAtom(modalState); |
| b69ab31 | | | 153 | |
| b69ab31 | | | 154 | return useCallback( |
| b69ab31 | | | 155 | <T,>(config: ModalConfig<T>) => { |
| b69ab31 | | | 156 | const deferred = defer<T | undefined>(); |
| b69ab31 | | | 157 | // The API we provide is typed with T, but our recoil state only knows `unknown`, so we have to cast. |
| b69ab31 | | | 158 | // This is safe because only one modal is visible at a time, so we know the data type we created it with is what we'll get back. |
| b69ab31 | | | 159 | setModal({ |
| b69ab31 | | | 160 | config: config as ModalConfig<unknown>, |
| b69ab31 | | | 161 | visible: true, |
| b69ab31 | | | 162 | deferred: deferred as Deferred<unknown | undefined>, |
| b69ab31 | | | 163 | }); |
| b69ab31 | | | 164 | |
| b69ab31 | | | 165 | return deferred.promise as Promise<T>; |
| b69ab31 | | | 166 | }, |
| b69ab31 | | | 167 | [setModal], |
| b69ab31 | | | 168 | ); |
| b69ab31 | | | 169 | } |
| b69ab31 | | | 170 | |
| b69ab31 | | | 171 | export function showModal<T>(config: ModalConfig<T>): Promise<T | undefined> { |
| b69ab31 | | | 172 | const deferred = defer<T | undefined>(); |
| b69ab31 | | | 173 | writeAtom(modalState, { |
| b69ab31 | | | 174 | config: config as ModalConfig<unknown>, |
| b69ab31 | | | 175 | visible: true, |
| b69ab31 | | | 176 | deferred: deferred as Deferred<unknown | undefined>, |
| b69ab31 | | | 177 | }); |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | return deferred.promise as Promise<T>; |
| b69ab31 | | | 180 | } |