|
| 1 | +import { ModalPortal, ModalPortalProps } from '~/ui/components/ModalPortal' |
| 2 | +import { |
| 3 | + ComponentChild, |
| 4 | + ComponentChildren, |
| 5 | + createContext, |
| 6 | + FunctionComponent, |
| 7 | + h, |
| 8 | +} from 'preact' |
| 9 | +import { |
| 10 | + MutableRef, |
| 11 | + Ref, |
| 12 | + useContext, |
| 13 | + useMemo, |
| 14 | + useRef, |
| 15 | + useState, |
| 16 | +} from 'preact/hooks' |
| 17 | +import { useRefState } from '~/utils/useRefState' |
| 18 | + |
| 19 | +export interface ModalComponent { |
| 20 | + id: string |
| 21 | + component: ComponentChildren |
| 22 | +} |
| 23 | + |
| 24 | +export type ModalOverlayRef = MutableRef<HTMLDivElement | null> |
| 25 | + |
| 26 | +export type ModalOnCloseRef = MutableRef<(() => unknown) | null> |
| 27 | + |
| 28 | +export interface ModalAPI { |
| 29 | + modal: ModalComponent | undefined |
| 30 | + props: ShowModalInnerProps |
| 31 | + showModal: ShowModal |
| 32 | + overlayRef: ModalOverlayRef |
| 33 | + onCloseRef: ModalOnCloseRef |
| 34 | +} |
| 35 | + |
| 36 | +export type ShowModalInnerProps = Pick< |
| 37 | + ModalPortalProps, |
| 38 | + 'size' | 'bare' | 'closeOnOverlayClick' | 'adjusted' |
| 39 | +> |
| 40 | + |
| 41 | +export interface ShowModalProps { |
| 42 | + modalId: string |
| 43 | + component: ComponentChild | null |
| 44 | + props?: ShowModalInnerProps |
| 45 | + onClose?: () => unknown |
| 46 | +} |
| 47 | + |
| 48 | +export type ShowModal = (props: ShowModalProps) => void |
| 49 | + |
| 50 | +export const ModalsContext = createContext<ModalAPI>({ |
| 51 | + modal: undefined, |
| 52 | + props: {}, |
| 53 | + showModal: () => {}, |
| 54 | + overlayRef: { current: null }, |
| 55 | + onCloseRef: { current: null }, |
| 56 | +}) |
| 57 | + |
| 58 | +export interface ModalsProps { |
| 59 | + api: ModalAPI |
| 60 | +} |
| 61 | + |
| 62 | +export function Modals({ |
| 63 | + api: { modal, props, showModal, overlayRef }, |
| 64 | +}: ModalsProps) { |
| 65 | + if (!modal) return null |
| 66 | + return ( |
| 67 | + <ModalPortal |
| 68 | + close={() => { |
| 69 | + showModal({ modalId: modal.id, component: undefined }) |
| 70 | + }} |
| 71 | + overlayRef={overlayRef} |
| 72 | + {...props} |
| 73 | + > |
| 74 | + {modal.component} |
| 75 | + </ModalPortal> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +export function useModals(): ModalAPI { |
| 80 | + const [modal, setModal] = useRefState<ModalComponent | undefined>(undefined) |
| 81 | + const [props, setProps] = useState<ShowModalInnerProps>({ size: 'medium' }) |
| 82 | + const overlayRef: ModalOverlayRef = useRef(null) |
| 83 | + const onCloseRef: ModalOnCloseRef = useRef(null) |
| 84 | + |
| 85 | + const showModal: ShowModal = ({ |
| 86 | + modalId, |
| 87 | + component, |
| 88 | + props: newProps, |
| 89 | + onClose, |
| 90 | + }) => { |
| 91 | + // Call current modal onClose callback |
| 92 | + if (!component) onCloseRef.current?.() |
| 93 | + // Assign new callback |
| 94 | + onCloseRef.current = onClose || null |
| 95 | + |
| 96 | + // Ignore close if another modal got opened |
| 97 | + if (!component && modalId !== modal.current?.id) return |
| 98 | + |
| 99 | + setModal(component ? { id: modalId, component } : undefined) |
| 100 | + setProps(newProps || {}) |
| 101 | + } |
| 102 | + |
| 103 | + return { modal: modal.current, props, showModal, overlayRef, onCloseRef } |
| 104 | +} |
| 105 | + |
| 106 | +export interface ModalPropsBase { |
| 107 | + close: () => void |
| 108 | + overlayRef: ModalOverlayRef |
| 109 | +} |
| 110 | + |
| 111 | +export interface ModalPropsExtra { |
| 112 | + onClose: () => unknown |
| 113 | +} |
| 114 | + |
| 115 | +export function createModal<Props extends Record<string, any>>( |
| 116 | + Component: FunctionComponent<Props & ModalPropsBase>, |
| 117 | + innerProps?: Omit<ModalPortalProps, keyof ModalPropsBase | 'children'> |
| 118 | +) { |
| 119 | + return (): ((props: Props & ModalPropsExtra) => void) => { |
| 120 | + const modalId = useMemo(() => Date.now().toString(), []) |
| 121 | + const { showModal, overlayRef } = useContext(ModalsContext) |
| 122 | + |
| 123 | + return ({ onClose, ...props }) => { |
| 124 | + showModal({ |
| 125 | + modalId, |
| 126 | + component: ( |
| 127 | + // @ts-ignore: we're tricking TypeScript, this is ok |
| 128 | + <Component |
| 129 | + {...props} |
| 130 | + close={() => { |
| 131 | + showModal({ modalId, component: null }) |
| 132 | + }} |
| 133 | + overlayRef={overlayRef} |
| 134 | + /> |
| 135 | + ), |
| 136 | + props: innerProps, |
| 137 | + onClose, |
| 138 | + }) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments