React

Alert Dialog

Guide to using Alert Dialog with Stackflow.

Available since@seed-design/stackflow@1.1.5, @seed-design/css@1.1.5

Alert Dialog

Learn more about the Alert Dialog component.

Making an Alert Dialog Activity

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.
Loading...

Usage

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>
  );
};
  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()}.

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.

const { isActive, transitionState } = useActivity();

return (
  <AlertDialogRoot
    open={
      transitionState === "enter-active" || transitionState === "enter-done"
    }
    modal={isActive}
    onOpenChange={(open) => !open && isActive && pop()}
  >
    {/* ... */}
  </AlertDialogRoot>
);
  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.

  • 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.

Loading...

Usage

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>
  );
};
  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 })}.

useStepOverlay

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>
  );
};

About useActivityZIndexBase

useActivityZIndexBase is a hook that returns the z-index base point for each Activity.

Table of contents