Alert Dialog 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.
It is generally recommended to create Alert Dialog as an 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 Modal is easy.
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 (
< AppScreen >
< AppBar >
< AppBarLeft >
< AppBarBackButton />
</ AppBarLeft >
< AppBarMain title = "Activity" />
< AppBarRight >
< AppBarIconButton aria-label = "Home" onClick = {() => push ( "ActivityHome" , {})}>
< IconHouseLine />
</ AppBarIconButton >
</ AppBarRight >
</ AppBar >
< AppScreenContent >
< VStack p = "x5" justify = "center" gap = "x4" >
< ActionButton
variant = "neutralSolid"
flexGrow
onClick = {() => push ( "ActivityAlertDialog" , {})}
>
ActivityAlertDialog를 Push
</ ActionButton >
< ActionButton
variant = "neutralWeak"
flexGrow
onClick = {() => push ( "ActivityAlertDialogActivity" , {})}
>
지금 열린 이 Activity를 Push
</ ActionButton >
</ VStack >
</ AppScreenContent >
</ AppScreen >
);
};
export default ActivityAlertDialogActivity;
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 (
< AlertDialogRoot
open = {open}
modal = {keepMounted ? activity.isActive : undefined }
onOpenChange = {onOpenChange}
>
< AlertDialogContent layerIndex = { useActivityZIndexBase ()}>
< AlertDialogHeader >
< AlertDialogTitle >제목</ AlertDialogTitle >
< AlertDialogDescription >다람쥐 헌 쳇바퀴에 타고파</ AlertDialogDescription >
</ AlertDialogHeader >
< AlertDialogFooter >
< VStack gap = "x4" >
< ResponsivePair gap = "x2" >
< AlertDialogAction asChild >
< ActionButton variant = "neutralWeak" >확인</ ActionButton >
</ AlertDialogAction >
< ActionButton
variant = "neutralSolid"
onClick = {() =>
push ( "ActivityDetail" , {
title: "AlertDialog에서 Push됨" ,
body: keepMounted
? "AlertDialog가 언마운트되지 않았으므로, 현재 Activity를 pop하는 경우 AlertDialog가 열린 상태로 표시됩니다."
: "AlertDialog가 언마운트되었으므로, 현재 Activity를 pop하는 경우 AlertDialog가 다시 enter 트랜지션을 재생하며 마운트됩니다." ,
})
}
>
Push
</ ActionButton >
</ ResponsivePair >
< Switch
tone = "neutral"
size = "16"
label = "Push 이후에도 AlertDialog 마운트 유지"
checked = {keepMounted}
onCheckedChange = {setKeepMounted}
style = {{ alignSelf: "center" }}
/>
</ VStack >
</ AlertDialogFooter >
</ AlertDialogContent >
</ AlertDialogRoot >
);
};
export default ActivityAlertDialog;
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 (
< AlertDialogRoot open = { useActivity ().isActive} onOpenChange = {( open ) => ! open && pop ()}>
< AlertDialogContent layerIndex = { useActivityZIndexBase ()}>
< AlertDialogHeader >
< AlertDialogTitle >Title</ AlertDialogTitle >
< AlertDialogDescription >Description text</ AlertDialogDescription >
</ AlertDialogHeader >
< AlertDialogFooter >
< VStack gap = "x2" >
< AlertDialogAction asChild >
< ActionButton >Confirm</ ActionButton >
</ AlertDialogAction >
< ActionButton
variant = "neutralSolid"
onClick = {() => push ( "AnotherActivity" , {})}
>
Go to another screen
</ ActionButton >
</ VStack >
</ AlertDialogFooter >
</ AlertDialogContent >
</ AlertDialogRoot >
);
};
Pass useActivity().isActive to the open prop so that Alert Dialog opens when the Activity is activated.
Execute pop() through onOpenChange when Alert Dialog closes to terminate the Activity.
Pass the z-index base point of Alert Dialog Activity with layerIndex={useActivityZIndexBase()}.
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.
const { isActive , transitionState } = useActivity ();
return (
< AlertDialogRoot
open = {
transitionState === "enter-active" || transitionState === "enter-done"
}
modal = {isActive}
onOpenChange = {( open ) => ! open && isActive && pop ()}
>
{ /* ... */ }
</ AlertDialogRoot >
);
Manage open with transitionState so that Alert Dialog does not unmount even if another Activity is pushed on top.
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.
Execute pop() only when isActive in the onOpenChange handler to prevent unintended Activity termination in an inactive state.
If you cannot create Alert Dialog as an Activity, you can create the state where Alert Dialog is displayed as a Step .
You can close Alert Dialog with a back button while maintaining the current Activity.
You can open and close Alert Dialog using AlertDialogTrigger.
Limitations 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 .
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.
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 (
< AppScreen >
< AppBar >
< AppBarMain title = "Step" />
< AppBarRight >
< AppBarIconButton aria-label = "Home" onClick = {() => push ( "ActivityHome" , {})}>
< IconHouseLine />
</ AppBarIconButton >
</ AppBarRight >
</ AppBar >
< AppScreenContent >
< AlertDialogRoot open = {open} onOpenChange = {onOpenChange}>
< AlertDialogTrigger asChild >
< VStack p = "x5" justify = "center" gap = "x4" >
< ActionButton variant = "neutralSolid" flexGrow >
Alert Dialog 열기
</ ActionButton >
</ VStack >
</ AlertDialogTrigger >
< Portal >
< AlertDialogContent layerIndex = { useActivityZIndexBase ({ activityOffset: 1 })}>
< AlertDialogHeader >
< AlertDialogTitle >Step</ AlertDialogTitle >
< AlertDialogDescription >
Alert Dialog가 Step으로 만들어져 있기 때문에 뒤로 가기로 닫을 수 있습니다.
</ AlertDialogDescription >
</ AlertDialogHeader >
< AlertDialogFooter >
< HStack gap = "x2" >
< ActionButton onClick = {() => setOpen ( false )} variant = "neutralWeak" >
닫기
</ ActionButton >
< ActionButton
flexGrow
variant = "neutralSolid"
onClick = {() => {
// 이 Alert Dialog는 Activity로 만들어지지 않았기 때문에, z-index 정리를 위해
// Alert Dialog를 먼저 닫고 다음 Activity를 push해야 합니다.
setOpen ( false );
push ( "ActivityDetail" , {
title: "Alert Dialog에서 이동한 화면" ,
body: "Alert Dialog를 닫고 이동했습니다." ,
});
}}
>
Push
</ ActionButton >
</ HStack >
</ AlertDialogFooter >
</ AlertDialogContent >
</ Portal >
</ AlertDialogRoot >
</ AppScreenContent >
</ AppScreen >
);
};
export default ActivityAlertDialogStep;
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" >();
const isOverlayOpen = params[ "alert-dialog" ] === "open" ;
useEffect (() => {
if ( ! isOverlayOpen) {
setOpen ( false );
}
}, [isOverlayOpen]);
const onOpenChange = ( newOpen : boolean ) => {
setOpen (newOpen);
if (newOpen && ! isOverlayOpen) {
pushStep (( params ) => ({ ... params, "alert-dialog" : "open" }));
return ;
}
if ( ! newOpen && isOverlayOpen) {
popStep ();
return ;
}
};
return (
< AppScreen >
< AlertDialogRoot open = {open} onOpenChange = {onOpenChange}>
< AlertDialogTrigger asChild >
< ActionButton >Open</ ActionButton >
</ AlertDialogTrigger >
< Portal >
< AlertDialogContent
layerIndex = { useActivityZIndexBase ({ activityOffset: 1 })}
>
< AlertDialogHeader >
< AlertDialogTitle >Title</ AlertDialogTitle >
< AlertDialogDescription >Description text</ AlertDialogDescription >
</ AlertDialogHeader >
< AlertDialogFooter >
< HStack gap = "x2" >
< ActionButton onClick = {() => setOpen ( false )}>Cancel</ ActionButton >
< ActionButton
onClick = {() => {
setOpen ( false ); // Close Alert Dialog before navigating to another Activity.
push ( "ActivityNext" );
}}
>
Next
</ ActionButton >
</ HStack >
</ AlertDialogFooter >
</ AlertDialogContent >
</ Portal >
</ AlertDialogRoot >
</ AppScreen >
);
};
Use Portal so that Alert Dialog is rendered outside the current Activity in the DOM.
Manage the open prop and synchronize it with Step state through the onOpenChange handler.
Synchronize the Alert Dialog's open state when Activity parameters change through a back button, etc.
Pass the z-index base point one level higher than the current Activity with layerIndex={useActivityZIndexBase({ activityOffset: 1 })}.
It is convenient to use useStepOverlay by generalizing #2 and #3. For an example implementation of useStepOverlay, refer to the code .
import { useActivityZIndexBase } from "@seed-design/stackflow" ;
import { Portal } from "@seed-design/react" ;
import { useStepOverlay } from "./use-step-overlay" ;
// ... more imports
const MyActivity : ActivityComponentType = () => {
const { overlayProps , setOpen } = useStepOverlay ();
const { popStep } = useStepFlow ( "MyActivity" );
const { push } = useFlow ();
return (
< AppScreen >
< AlertDialogRoot { ... overlayProps}>
< AlertDialogTrigger asChild >
< ActionButton >Open</ ActionButton >
</ AlertDialogTrigger >
< Portal >
< AlertDialogContent
layerIndex = { useActivityZIndexBase ({ activityOffset: 1 })}
>
< AlertDialogHeader >
< AlertDialogTitle >Title</ AlertDialogTitle >
< AlertDialogDescription >Description text</ AlertDialogDescription >
</ AlertDialogHeader >
< AlertDialogFooter >
< HStack gap = "x2" >
< ActionButton onClick = {() => setOpen ( false )}>Cancel</ ActionButton >
< ActionButton
onClick = {() => {
setOpen ( false ); // Close Alert Dialog before navigating to another Activity.
push ( "ActivityNext" );
}}
>
Next
</ ActionButton >
</ HStack >
</ AlertDialogFooter >
</ AlertDialogContent >
</ Portal >
</ AlertDialogRoot >
</ AppScreen >
);
};
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 , 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.