# Alert Dialog URL: /react/stackflow/alert-dialog Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/stackflow/alert-dialog.mdx Guide to using Alert Dialog with Stackflow. 사용 가능 버전: @seed-design/stackflow@1.1.5, @seed-design/css@1.1.5 Learn more about the Alert Dialog component. ## Making an Alert Dialog Activity **It is generally recommended to create Alert Dialog as an [Activity](https://stackflow.so/docs/get-started/activity).** - Since it is managed as an Activity, it is easy to manage z-index to be higher than child Activities and lower than parent Activities. - Deep linking is possible. (You can open Alert Dialog by accessing the URL.) - Migration from [@stackflow/plugin-basic-ui](https://www.npmjs.com/package/@stackflow/plugin-basic-ui) `Modal` is easy. ```tsx title='ActivityAlertDialogActivity.tsx' import { VStack } from "@seed-design/react"; import { useFlow, type StaticActivityComponentType } from "@stackflow/react/future"; import { AppBar, AppBarBackButton, AppBarLeft, AppBarMain, AppBarIconButton, AppBarRight, } from "seed-design/ui/app-bar"; import { AppScreen, AppScreenContent } from "seed-design/ui/app-screen"; import { ActionButton } from "seed-design/ui/action-button"; import { IconHouseLine } from "@karrotmarket/react-monochrome-icon"; declare module "@stackflow/config" { interface Register { ActivityAlertDialogActivity: {}; } } const ActivityAlertDialogActivity: StaticActivityComponentType< "ActivityAlertDialogActivity" > = () => { const { push } = useFlow(); return ( push("ActivityHome", {})}> push("ActivityAlertDialog", {})} > ActivityAlertDialog를 Push push("ActivityAlertDialogActivity", {})} > 지금 열린 이 Activity를 Push ); }; export default ActivityAlertDialogActivity; ``` ```tsx title='ActivityAlertDialog.tsx' import { useActivity, useFlow, type StaticActivityComponentType } from "@stackflow/react/future"; import { useState } from "react"; import { ActionButton } from "seed-design/ui/action-button"; import { AlertDialogAction, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogRoot, AlertDialogTitle, } from "seed-design/ui/alert-dialog"; import { ResponsivePair, VStack } from "@seed-design/react"; import { send } from "@stackflow/compat-await-push"; import { useActivityZIndexBase } from "@seed-design/stackflow"; import { Switch } from "seed-design/ui/switch"; declare module "@stackflow/config" { interface Register { ActivityAlertDialog: {}; } } const ActivityAlertDialog: StaticActivityComponentType<"ActivityAlertDialog"> = () => { const activity = useActivity(); const { pop, push } = useFlow(); const [keepMounted, setKeepMounted] = useState(false); const handleClose = (open: boolean) => { if (!open) { pop(); send({ activityId: activity.id, data: { message: "hello", }, }); } }; const open = keepMounted ? activity.transitionState === "enter-active" || activity.transitionState === "enter-done" : activity.isActive; const onOpenChange = keepMounted ? (open: boolean) => !open && activity.isActive && handleClose(open) : handleClose; return ( 제목 다람쥐 헌 쳇바퀴에 타고파 확인 push("ActivityDetail", { title: "AlertDialog에서 Push됨", body: keepMounted ? "AlertDialog가 언마운트되지 않았으므로, 현재 Activity를 pop하는 경우 AlertDialog가 열린 상태로 표시됩니다." : "AlertDialog가 언마운트되었으므로, 현재 Activity를 pop하는 경우 AlertDialog가 다시 enter 트랜지션을 재생하며 마운트됩니다.", }) } > Push ); }; export default ActivityAlertDialog; ``` ### Usage ```tsx import { useActivityZIndexBase } from "@seed-design/stackflow"; import { useActivity, useFlow, type ActivityComponentType } from "@stackflow/react/future"; // ... more imports const ActivityAlertDialog: ActivityComponentType<"ActivityAlertDialog"> = () => { const { pop, push } = useFlow(); return ( {/* [!code highlight] */} !open && pop()}> {/* [!code highlight] */} Title Description text Confirm push("AnotherActivity", {})} > Go to another screen ); }; ``` 1. Pass `useActivity().isActive` to the `open` prop so that Alert Dialog opens when the Activity is activated. 2. Execute `pop()` through `onOpenChange` when Alert Dialog closes to terminate the Activity. 3. Pass the z-index base point of Alert Dialog Activity with `layerIndex={useActivityZIndexBase()}`. - [Learn more about `useActivityZIndexBase`](#about-useactivityzindexbase) #### Keeping Alert Dialog Mounted To prevent Alert Dialog from unmounting when pushing another Activity on top of Alert Dialog Activity: - Manage the `open` state with `transitionState` instead of `isActive` - Set the `modal` prop to `isActive` - Execute `pop()` in the `onOpenChange` handler when `!isOpen && isActive` This pattern is useful when you want to nest other overlay component activities on top of Alert Dialog Activity. ```tsx const { isActive, transitionState } = useActivity(); return ( !open && isActive && pop()} > {/* ... */} ); ``` 1. Manage `open` with `transitionState` so that Alert Dialog does not unmount even if another Activity is pushed on top. 2. Set `modal={isActive}` so that `modal` is `false` when Alert Dialog Activity is inactive. If you don't do this, focus and screen reader access won't work in the Activity pushed on top. 3. Execute `pop()` only when `isActive` in the `onOpenChange` handler to prevent unintended Activity termination in an inactive state. ## Syncing Alert Dialog State with a Step If you cannot create Alert Dialog as an Activity, you can create the state where Alert Dialog is displayed as a [Step](https://stackflow.so/docs/get-started/navigating-step). - You can close Alert Dialog with a back button while maintaining the current Activity. - You can open and close Alert Dialog using `AlertDialogTrigger`. **Before pushing another Activity from an Alert Dialog that is not created as an Activity, close the Alert Dialog to prevent z-index issues.** If you cannot close Alert Dialog, or if Alert Dialog needs to remain open when returning to the Activity that opened it, it is recommended to [create Alert Dialog as an Activity](#making-an-alert-dialog-activity). To provide smooth transitions between Activities, some child AppScreen elements are positioned above parent AppScreen elements. Due to this limitation, it is impossible to position an open Alert Dialog between child and parent Activities if it is not created as an independent Activity. ```tsx title='ActivityAlertDialogStep.tsx' import { HStack, Portal, VStack } from "@seed-design/react"; import { useActivityZIndexBase } from "@seed-design/stackflow"; import { useActivityParams, useFlow, useStepFlow, type StaticActivityComponentType, } from "@stackflow/react/future"; import { useEffect, useState } from "react"; import { ActionButton } from "seed-design/ui/action-button"; import { AppBar, AppBarMain, AppBarIconButton, AppBarRight } from "seed-design/ui/app-bar"; import { AppScreen, AppScreenContent } from "seed-design/ui/app-screen"; import { IconHouseLine } from "@karrotmarket/react-monochrome-icon"; import { AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogRoot, AlertDialogTitle, AlertDialogTrigger, } from "seed-design/ui/alert-dialog"; declare module "@stackflow/config" { interface Register { ActivityAlertDialogStep: { "alert-dialog"?: "open"; }; } } const ActivityAlertDialogStep: StaticActivityComponentType<"ActivityAlertDialogStep"> = () => { const [open, setOpen] = useState(false); const { push } = useFlow(); const { pushStep, popStep } = useStepFlow("ActivityAlertDialogStep"); const params = useActivityParams<"ActivityAlertDialogStep">(); const isOverlayOpen = params["alert-dialog"] === "open"; useEffect(() => setOpen(isOverlayOpen), [isOverlayOpen]); const onOpenChange = (newOpen: boolean) => { setOpen(newOpen); if (newOpen && !isOverlayOpen) { pushStep((params) => ({ ...params, "alert-dialog": "open" })); return; } if (!newOpen && isOverlayOpen) { popStep(); return; } }; return ( push("ActivityHome", {})}> Alert Dialog 열기 Step Alert Dialog가 Step으로 만들어져 있기 때문에 뒤로 가기로 닫을 수 있습니다. setOpen(false)} variant="neutralWeak"> 닫기 { // 이 Alert Dialog는 Activity로 만들어지지 않았기 때문에, z-index 정리를 위해 // Alert Dialog를 먼저 닫고 다음 Activity를 push해야 합니다. setOpen(false); push("ActivityDetail", { title: "Alert Dialog에서 이동한 화면", body: "Alert Dialog를 닫고 이동했습니다.", }); }} > Push ); }; export default ActivityAlertDialogStep; ``` ### Usage ```tsx import { useActivityZIndexBase } from "@seed-design/stackflow"; import { Portal } from "@seed-design/react"; import { useActivity, useActivityParams, useFlow, useStepFlow, type ActivityComponentType, } from "@stackflow/react/future"; import { useEffect, useState } from "react"; // ... more imports declare module "@stackflow/config" { interface Register { ActivityHome: { "alert-dialog"?: "open"; }; } } const ActivityHome: ActivityComponentType<"ActivityHome"> = () => { const [open, setOpen] = useState(false); const { push } = useFlow(); const { pushStep, popStep } = useStepFlow("ActivityHome"); const params = useActivityParams<"ActivityHome">(); // [!code highlight] const isOverlayOpen = params["alert-dialog"] === "open"; // [!code highlight] useEffect(() => { if (!isOverlayOpen) { setOpen(false); } }, [isOverlayOpen]); // [!code highlight] const onOpenChange = (newOpen: boolean) => { setOpen(newOpen); if (newOpen && !isOverlayOpen) { pushStep((params) => ({ ...params, "alert-dialog": "open" })); return; } if (!newOpen && isOverlayOpen) { popStep(); return; } }; return ( Open {/* [!code highlight] */} {/* [!code highlight] */} Title Description text setOpen(false)}>Cancel { // [!code highlight] setOpen(false); // Close Alert Dialog before navigating to another Activity. push("ActivityNext"); }} > Next {/* [!code highlight] */} ); }; ``` 1. Use `Portal` so that Alert Dialog is rendered outside the current Activity in the DOM. 2. Manage the `open` prop and synchronize it with Step state through the `onOpenChange` handler. 3. Synchronize the Alert Dialog's `open` state when Activity parameters change through a back button, etc. 4. Pass the z-index base point one level higher than the current Activity with `layerIndex={useActivityZIndexBase({ activityOffset: 1 })}`. - [Learn more about `useActivityZIndexBase`](#about-useactivityzindexbase) ### `useStepOverlay` It is convenient to use `useStepOverlay` by generalizing #2 and #3. For an example implementation of `useStepOverlay`, refer to the [code](https://github.com/daangn/seed-design/blob/dev/examples/stackflow-spa/src/seed-design/stackflow/use-step-overlay.ts). ```tsx import { useActivityZIndexBase } from "@seed-design/stackflow"; import { Portal } from "@seed-design/react"; import { useStepOverlay } from "./use-step-overlay"; // ... more imports const MyActivity: ActivityComponentType = () => { // [!code highlight] const { overlayProps, setOpen } = useStepOverlay(); const { popStep } = useStepFlow("MyActivity"); const { push } = useFlow(); return ( {/* [!code highlight] */} Open {/* [!code highlight] */} {/* [!code highlight] */} Title Description text setOpen(false)}>Cancel { // [!code highlight] setOpen(false); // Close Alert Dialog before navigating to another Activity. push("ActivityNext"); }} > Next {/* [!code highlight] */} ); }; ``` ## About `useActivityZIndexBase` `useActivityZIndexBase` is a hook that returns the z-index base point for each Activity. - It returns `useActivity().zIndex * 5`. `useActivity().zIndex` is a value indicating which Activity the current Activity is among entered Activities. - The elements that make up AppScreen (layer, appbar, dim, edge) have z-index values calculated based on the `useActivityZIndexBase()` value. ``` ┌─ 2nd AlertDialog Activity │ └─ AlertDialog: 12 (2 × 5 + 2) ├─ 1st AppScreen Activity │ ├─ appbar: 12 (1 × 5 + 7) │ ├─ edge: 9 (1 × 5 + 4) │ ├─ layer: 7 (1 × 5 + 2) │ └─ dim: 5 (1 × 5 + 0) └─ 0th AppScreen Activity ├─ appbar: 7 (0 × 5 + 7) ├─ edge: 4 (0 × 5 + 4) ├─ layer: 2 (0 × 5 + 2) └─ dim: 0 (0 × 5 + 0) ``` - The `activityOffset` option is used when you want to use the z-index base point of the next Activity instead of the current Activity. - For example, `useActivityZIndexBase({ activityOffset: 1 })` returns the z-index base point of the next Activity. - This is useful when displaying AlertDialog above the current Activity in the Step pattern. ``` ┌─ AlertDialog displayed with activityOffset: 1 from 1st AppScreen Activity (e.g., Step pattern) │ └─ AlertDialog: 12 ((1 + 1) × 5 + 2) ├─ 1st AppScreen Activity │ ├─ appbar: 12 (1 × 5 + 7) │ ├─ edge: 9 (1 × 5 + 4) │ ├─ layer: 7 (1 × 5 + 2) │ └─ dim: 5 (1 × 5 + 0) └─ 0th AppScreen Activity ├─ appbar: 7 (0 × 5 + 7) ├─ edge: 4 (0 × 5 + 4) ├─ layer: 2 (0 × 5 + 2) └─ dim: 0 (0 × 5 + 0) ``` - Both hooks return values needed for z-index styling based on which Activity the current Activity is among entered Activities. - While both hooks return the same values, when using Crispy components instead of [@stackflow/plugin-basic-ui](https://www.npmjs.com/package/@stackflow/plugin-basic-ui), it is recommended to use the `useActivityZIndexBase` hook from the `@seed-design/stackflow` package managed by Crispy. - The `useZIndexBase` hook from the `@stackflow/react-ui-core` package does not provide the `activityOffset` option. - The `useActivityZIndexBase` hook is provided from `@seed-design/stackflow@1.1.1`. If you were specifying `layerIndex` in the form of `useActivity().zIndex * 5 + n`, you can change it to `useActivityZIndexBase() + n`. As part of the change, review whether `+ n` is needed.