MCPcopy Index your code
hub / github.com/ionic-team/ionic-framework / useController

Function useController

packages/react/src/hooks/useController.ts:13–66  ·  view source on GitHub ↗
(
  displayName: string,
  controller: { create: (options: OptionsType) => Promise<OverlayType> },
  defineCustomElement: () => void
)

Source from the content-addressed store, hash-verified

11}
12
13export function useController<OptionsType, OverlayType extends OverlayBase>(
14 displayName: string,
15 controller: { create: (options: OptionsType) => Promise<OverlayType> },
16 defineCustomElement: () => void
17) {
18 const overlayRef = useRef<OverlayType>();
19 const didDismissEventName = useMemo(() => `on${displayName}DidDismiss`, [displayName]);
20 const didPresentEventName = useMemo(() => `on${displayName}DidPresent`, [displayName]);
21 const willDismissEventName = useMemo(() => `on${displayName}WillDismiss`, [displayName]);
22 const willPresentEventName = useMemo(() => `on${displayName}WillPresent`, [displayName]);
23
24 defineCustomElement();
25
26 const present = useCallback(
27 async (options: OptionsType & HookOverlayOptions) => {
28 if (overlayRef.current) {
29 return;
30 }
31
32 const { onDidDismiss, onWillDismiss, onDidPresent, onWillPresent, ...rest } = options;
33
34 const handleDismiss = (event: CustomEvent<OverlayEventDetail<any>>) => {
35 if (onDidDismiss) {
36 onDidDismiss(event);
37 }
38 overlayRef.current = undefined;
39 };
40
41 overlayRef.current = await controller.create({
42 ...(rest as any),
43 });
44
45 attachProps(overlayRef.current, {
46 [didDismissEventName]: handleDismiss,
47 [didPresentEventName]: (e: CustomEvent) => onDidPresent && onDidPresent(e),
48 [willDismissEventName]: (e: CustomEvent) => onWillDismiss && onWillDismiss(e),
49 [willPresentEventName]: (e: CustomEvent) => onWillPresent && onWillPresent(e),
50 });
51
52 overlayRef.current.present();
53 },
54 [controller]
55 );
56
57 const dismiss = useCallback(async () => {
58 overlayRef.current && (await overlayRef.current.dismiss());
59 overlayRef.current = undefined;
60 }, []);
61
62 return {
63 present,
64 dismiss,
65 };
66}

Callers 5

useIonToastFunction · 0.90
useIonPickerFunction · 0.90
useIonLoadingFunction · 0.90
useIonAlertFunction · 0.90
useIonActionSheetFunction · 0.90

Calls 5

attachPropsFunction · 0.90
createMethod · 0.65
presentMethod · 0.65
dismissMethod · 0.65
defineCustomElementFunction · 0.50

Tested by

no test coverage detected