React

Menu Sheet

Guide to using Swipeable Menu Sheet with Stackflow.

Available since@seed-design/stackflow@2.0.0, @seed-design/react@2.0.0, @seed-design/css@2.0.0

Swipeable Menu Sheet

Learn more about the Swipeable Menu Sheet component.

Making a Swipeable Menu Sheet Activity

In typical cases, it is recommended to create a Swipeable Menu 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 Swipeable Menu 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 ActivitySwipeableMenuSheet: ActivityComponentType<"ActivitySwipeableMenuSheet"> = () => {
  const { pop } = useFlow();

  const handleAction = (action: string) => {
    console.log("Selected action:", action);

    pop();
  };

  return (
    <SwipeableMenuSheetRoot open={useActivity().isActive} onOpenChange={(open) => !open && pop()}>
      <SwipeableMenuSheetContent
        title="Actions"
        layerIndex={useActivityZIndexBase()}
      >
        <SwipeableMenuSheetGroup>
          <SwipeableMenuSheetItem
            onClick={() => handleAction("add")}
            label="Add"
            prefixIcon={<IconPlusLine />}
          />
          <SwipeableMenuSheetItem
            onClick={() => handleAction("edit")}
            label="Edit"
            prefixIcon={<IconPencilLine />}
          />
          <SwipeableMenuSheetItem
            onClick={() => handleAction("delete")}
            tone="critical"
            label="Delete"
            prefixIcon={<IconTrashcanLine />}
          />
        </SwipeableMenuSheetGroup>
      </SwipeableMenuSheetContent>
    </SwipeableMenuSheetRoot>
  );
};
  1. Pass useActivity().isActive to the open prop so that the Swipeable Menu Sheet opens when the Activity is activated.
  2. When the Swipeable Menu Sheet closes, run pop() through onOpenChange to terminate the Activity. Since onOpenChange is also called when closing by swipe gesture, the Activity is also terminated.
  3. Pass the z-index base point of the Swipeable Menu Sheet Activity with layerIndex={useActivityZIndexBase()}.

Syncing Swipeable Menu Sheet State with a Step

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

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

When pushing another Activity from a Swipeable Menu Sheet not created as an Activity, close the Swipeable Menu Sheet before pushing.

If you cannot close the Swipeable Menu Sheet, or if you need to maintain the Swipeable Menu Sheet in an open state when returning to the Activity that opened it, it is recommended to create the Swipeable Menu 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 Swipeable Menu 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: {
      "menu-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["menu-sheet"] === "open";

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

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

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

      return;
    }

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

      return;
    }
  };

  const handleAction = (action: string) => {
    console.log("Selected action:", action);
    setOpen(false);
  };

  return (
    <AppScreen>
      <SwipeableMenuSheetRoot open={open} onOpenChange={onOpenChange}>
        <SwipeableMenuSheetTrigger asChild>
          <ActionButton>Open</ActionButton>
        </SwipeableMenuSheetTrigger>
        <Portal>
          <SwipeableMenuSheetContent
            title="Actions"
            layerIndex={useActivityZIndexBase({ activityOffset: 1 })}
          >
            <SwipeableMenuSheetGroup>
              <SwipeableMenuSheetItem onClick={() => handleAction("add")} label="Add" />
              <SwipeableMenuSheetItem onClick={() => handleAction("edit")} label="Edit" />
              <SwipeableMenuSheetItem
                onClick={() => {
                  setOpen(false); // Close the Swipeable Menu Sheet before moving to another Activity.
                  push("ActivityNext");
                }}
                label="Push"
              />
              <SwipeableMenuSheetItem
                onClick={() => handleAction("delete")}
                tone="critical"
                label="Delete"
              />
            </SwipeableMenuSheetGroup>
          </SwipeableMenuSheetContent>
        </Portal>
      </SwipeableMenuSheetRoot>
    </AppScreen>
  );
};
  1. Use Portal to render the Swipeable Menu 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 Swipeable Menu 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();

  const handleAction = (action: string) => {
    console.log("Selected action:", action);
    popStep();
  };

  return (
    <AppScreen>
      <SwipeableMenuSheetRoot {...overlayProps}>
        <SwipeableMenuSheetTrigger asChild>
          <ActionButton>Open</ActionButton>
        </SwipeableMenuSheetTrigger>
        <Portal>
          <SwipeableMenuSheetContent
            title="Actions"
            layerIndex={useActivityZIndexBase({ activityOffset: 1 })}
          >
            <SwipeableMenuSheetGroup>
              <SwipeableMenuSheetItem onClick={() => handleAction("add")} label="Add" />
              <SwipeableMenuSheetItem onClick={() => handleAction("edit")} label="Edit" />
              <SwipeableMenuSheetItem
                onClick={() => {
                  setOpen(false); // Close the Swipeable Menu Sheet before moving to another Activity.
                  push("ActivityNext");
                }}
                label="Push"
              />
              <SwipeableMenuSheetItem
                onClick={() => handleAction("delete")}
                tone="critical"
                label="Delete"
              />
            </SwipeableMenuSheetGroup>
          </SwipeableMenuSheetContent>
        </Portal>
      </SwipeableMenuSheetRoot>
    </AppScreen>
  );
};

About useActivityZIndexBase

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

Table of contents