| 178 | * accessible at once. |
| 179 | */ |
| 180 | export function useModal(options?: AriaModalOptions): ModalAria { |
| 181 | // Add aria-hidden to all parent providers on mount, and restore on unmount. |
| 182 | let context = useContext(Context); |
| 183 | if (!context) { |
| 184 | throw new Error('Modal is not contained within a provider'); |
| 185 | } |
| 186 | |
| 187 | useEffect(() => { |
| 188 | if (options?.isDisabled || !context || !context.parent) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // The immediate context is from the provider containing this modal, so we only |
| 193 | // want to trigger aria-hidden on its parents not on the modal provider itself. |
| 194 | context.parent.addModal(); |
| 195 | return () => { |
| 196 | if (context && context.parent) { |
| 197 | context.parent.removeModal(); |
| 198 | } |
| 199 | }; |
| 200 | }, [context, context.parent, options?.isDisabled]); |
| 201 | |
| 202 | return { |
| 203 | modalProps: { |
| 204 | 'data-ismodal': !options?.isDisabled |
| 205 | } |
| 206 | }; |
| 207 | } |