React

Bottom Sheet

Guide to using Bottom Sheet with Stackflow.

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

Bottom Sheet

Learn more about the Bottom Sheet component.

Making a Bottom Sheet Activity

In typical cases, it is recommended to create a Bottom Sheet as an Activity and use it.

  • Since it is managed as an Activity, it is easy to manage with z-index higher than lower Activities and lower than upper Activities.
  • Deep linking is possible. (You can open the Bottom Sheet by accessing a URL.)
  • Migration from @stackflow/plugin-basic-ui BottomSheet is easy.
Loading...

Usage

import { useActivityZIndexBase } from "@seed-design/stackflow";
import { useActivity, useFlow, type ActivityComponentType } from "@stackflow/react/future";
// ... more imports

const ActivityBottomSheetSimple: ActivityComponentType<"ActivityBottomSheetSimple"> = () => {
  const { pop } = useFlow();

  return (
    <BottomSheetRoot open={useActivity().isActive} onOpenChange={(open) => !open && pop()}>
      <BottomSheetContent
        title="Bottom Sheet created as an Activity"
        layerIndex={useActivityZIndexBase()}
      >
        <BottomSheetFooter>
          <ActionButton onClick={pop}>
            Confirm
          </ActionButton>
        </BottomSheetFooter>
      </BottomSheetContent>
    </BottomSheetRoot>
  );
};
  1. Pass useActivity().isActive to the open prop so that the Bottom Sheet opens when the Activity is activated.
  2. When the Bottom Sheet closes, run pop() through onOpenChange to terminate the Activity.
  3. Pass the z-index base point of the Bottom Sheet Activity with layerIndex={useActivityZIndexBase()}.

Keeping Bottom Sheet Mounted

To prevent the Bottom Sheet from being unmounted when pushing another Activity on top of the Bottom Sheet 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 in the following situations:

  • When you want to nest and display another overlay component Activity on top of the Bottom Sheet Activity
  • When you want to maintain the state of uncontrolled form elements that exist inside the lower Bottom Sheet Activity
const { isActive, transitionState } = useActivity();

return (
  <BottomSheetRoot
    open={
      transitionState === "enter-active" || transitionState === "enter-done"
    }
    modal={isActive}
    onOpenChange={(open) => !open && isActive && pop()}
  >
    {/* ... */}
  </BottomSheetRoot>
);
  1. Manage open with transitionState so that the Bottom Sheet is not unmounted even when another Activity is pushed on top.
  2. Set modal to false when the Bottom Sheet Activity is inactive with modal={isActive}. If you don't do this, interactions such as focus, screen reader access, and scrolling will not work in the Activity pushed above.
  3. Execute pop() only when isActive in the onOpenChange handler to prevent unintended Activity termination in an inactive state.

Stacking Another Modal Overlay on Top

When pushing another modal Activity with its own backdrop (e.g., Alert Dialog Activity) on top of the Bottom Sheet Activity, it is recommended to always set modal to true instead of modal={isActive}.

The modal={isActive} pattern is to ensure interactions such as scrolling when the Activity pushed above is a normal AppScreen, but if the Activity pushed above is a modal overlay with its own backdrop, the backdrop of the Bottom Sheet may disappear at the point of becoming inactive, which can make the dim layering appear broken.

Loading...
const { isActive, transitionState } = useActivity();

return (
  <BottomSheetRoot
    open={
      transitionState === "enter-active" || transitionState === "enter-done"
    }
    modal
    onOpenChange={(open) => !open && isActive && pop()}
  >
    {/* ... */}
  </BottomSheetRoot>
);
  1. Keep modal always true so that the Bottom Sheet's backdrop layering is not broken even when another modal Activity is pushed above.
  2. Thanks to the isActive guard in onOpenChange, pop() does not occur unintentionally from outside interactions in an inactive state.
When should I keep `modal={isActive}`?

If the Activity pushed above is a normal AppScreen that does not have a backdrop covering the Bottom Sheet area, the modal={isActive} guide is appropriate for interactions such as scrolling in the above Activity. Use the variation where modal is always true only when two modal overlays are stacked.

Syncing BottomSheet State with a Step

If you cannot create a Bottom Sheet as an Activity, you can create the state where the Bottom Sheet is displayed as a Step.

  • You can close the Bottom Sheet with a back button, etc., while maintaining the current Activity.
  • You can open and close the Bottom Sheet using BottomSheetTrigger.
Constraints

Before pushing another Activity from a Bottom Sheet not created as an Activity, close the Bottom Sheet to prevent z-index issues.

If you cannot close the Bottom Sheet, or if you need to maintain the Bottom Sheet in an open state when returning to the Activity that opened it, it is recommended to create the Bottom Sheet as an Activity.

To provide smooth transitions between Activities, some lower AppScreen elements are positioned above upper AppScreen elements. Due to this constraint, an open Bottom Sheet cannot be positioned between lower and upper Activities unless it is 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: {
      "bottom-sheet"?: "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["bottom-sheet"] === "open";

  useEffect(() => {
    if (!isOverlayOpen) {
      setOpen(false);
    }
  }, [isOverlayOpen]);

  const onOpenChange = (newOpen: boolean) => {
    setOpen(newOpen);

    if (newOpen && !isOverlayOpen) {
      pushStep((params) => ({ ...params, "bottom-sheet": "open" }));

      return;
    }

    if (!newOpen && isOverlayOpen) {
      popStep();

      return;
    }
  };

  return (
    <AppScreen>
      <BottomSheetRoot open={open} onOpenChange={onOpenChange}>
        <BottomSheetTrigger asChild>
          <ActionButton>Open</ActionButton>
        </BottomSheetTrigger>
        <Portal>
          <BottomSheetContent
            title="Bottom Sheet managed by Step"
            layerIndex={useActivityZIndexBase({ activityOffset: 1 })}
          >
            <BottomSheetFooter>
              <HStack gap="x2">
                <ActionButton onClick={() => setOpen(false)}>Cancel</ActionButton>
                <ActionButton
                  onClick={() => {
                    setOpen(false); // Close the Bottom Sheet before moving to another Activity.
                    push("ActivityNext");
                  }}
                >
                  Next
                </ActionButton>
              </HStack>
            </BottomSheetFooter>
          </BottomSheetContent>
        </Portal>
      </BottomSheetRoot>
    </AppScreen>
  );
};
  1. Use Portal to render the Bottom Sheet outside the current Activity in the DOM.
  2. Manage the open prop and synchronize the Step state through the onOpenChange handler.
  3. Synchronize the open state of the Bottom Sheet when Activity parameters change through back button, etc.
  4. Pass a z-index base point one level higher than the current Activity with layerIndex={useActivityZIndexBase({ activityOffset: 1 })}.

useStepOverlay

It is convenient to use useStepOverlay to generalize #2 and #3. For implementation examples of useStepOverlay, see 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>
      <BottomSheetRoot {...overlayProps}>
        <BottomSheetTrigger asChild>
          <ActionButton>Open</ActionButton>
        </BottomSheetTrigger>
        <Portal>
          <BottomSheetContent
            title="Bottom Sheet managed by Step"
            layerIndex={useActivityZIndexBase({ activityOffset: 1 })}
          >
            <BottomSheetFooter>
              <HStack gap="x2">
                <ActionButton onClick={() => setOpen(false)}>Cancel</ActionButton>
                <ActionButton
                  onClick={() => {
                    setOpen(false); // Close the Bottom Sheet before moving to another Activity.
                    push("ActivityNext");
                  }}
                >
                  Next
                </ActionButton>
              </HStack>
            </BottomSheetFooter>
          </BottomSheetContent>
        </Portal>
      </BottomSheetRoot>
    </AppScreen>
  );
};

About useActivityZIndexBase

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

Table of contents