-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
Copy pathuseIonModal.ts
44 lines (37 loc) · 1.49 KB
/
useIonModal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { ModalOptions } from '@ionic/core/components';
import { modalController } from '@ionic/core/components';
import { defineCustomElement } from '@ionic/core/components/ion-modal.js';
import { useCallback } from 'react';
import type { ReactComponentOrElement } from '../models/ReactComponentOrElement';
import type { HookOverlayOptions } from './HookOverlayOptions';
import { useOverlay } from './useOverlay';
// TODO(FW-2959): types
/**
* A hook for presenting/dismissing an IonModal component
* @param component The component that the modal will show. Can be a React Component, a functional component, or a JSX Element
* @param componentProps The props that will be passed to the component, if required
* @returns Returns the present and dismiss methods in an array
*/
export function useIonModal(component: ReactComponentOrElement, componentProps?: any): UseIonModalResult {
const controller = useOverlay<ModalOptions, HTMLIonModalElement>(
'IonModal',
modalController,
defineCustomElement,
component,
componentProps
);
const present = useCallback(
(options: Omit<ModalOptions, 'component' | 'componentProps'> & HookOverlayOptions = {}) => {
controller.present(options as any);
},
[controller.present]
);
return [present, controller.dismiss];
}
export type UseIonModalResult = [
(options?: Omit<ModalOptions, 'component' | 'componentProps'> & HookOverlayOptions) => void,
/**
* Dismisses the modal
*/
(data?: any, role?: string) => void
];