# Menu Sheet URL: /react/stackflow/menu-sheet Source: https://github.com/daangn/seed-design/blob/dev/docs/content/react/stackflow/menu-sheet.mdx Guide to using Swipeable Menu Sheet with Stackflow. 사용 가능 버전: @seed-design/stackflow@2.0.0, @seed-design/react@2.0.0, @seed-design/css@2.0.0 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](https://stackflow.so/docs/get-started/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](https://www.npmjs.com/package/@stackflow/plugin-basic-ui) `BottomSheet` is easy. ```tsx title='ActivityMenuSheetActivity.tsx' import { VStack } from "@seed-design/react"; import { useFlow, type StaticActivityComponentType } from "@stackflow/react/future"; import { IconHouseLine } from "@karrotmarket/react-monochrome-icon"; import { AppBar, AppBarBackButton, AppBarIconButton, AppBarLeft, AppBarMain, AppBarRight, } from "seed-design/ui/app-bar"; import { AppScreen, AppScreenContent } from "seed-design/ui/app-screen"; import { ActionButton } from "seed-design/ui/action-button"; declare module "@stackflow/config" { interface Register { ActivityMenuSheetActivity: {}; } } const ActivityMenuSheetActivity: StaticActivityComponentType<"ActivityMenuSheetActivity"> = () => { const { push } = useFlow(); return ( push("ActivityHome", {})}> push("ActivityMenuSheetSimple", {})} > ActivityMenuSheetSimple을 Push push("ActivityMenuSheetActivity", {})} > 지금 열린 이 Activity를 Push ); }; export default ActivityMenuSheetActivity; ``` ```tsx title='ActivityMenuSheetSimple.tsx' import { useActivityZIndexBase } from "@seed-design/stackflow"; import { useActivity, useFlow, type StaticActivityComponentType } from "@stackflow/react/future"; import { IconPencilLine, IconPlusLine, IconTrashcanLine, } from "@karrotmarket/react-monochrome-icon"; import { SwipeableMenuSheetContent, SwipeableMenuSheetGroup, SwipeableMenuSheetItem, SwipeableMenuSheetRoot, } from "seed-design/ui/swipeable-menu-sheet"; import { Snackbar, useSnackbarAdapter } from "seed-design/ui/snackbar"; declare module "@stackflow/config" { interface Register { ActivityMenuSheetSimple: {}; } } const ActivityMenuSheetSimple: StaticActivityComponentType<"ActivityMenuSheetSimple"> = () => { const { pop } = useFlow(); const activity = useActivity(); const snackbar = useSnackbarAdapter(); const handleAction = (action: string) => { snackbar.create({ render: () => , }); pop(); }; return ( !open && pop()}> handleAction("add")} label="추가" prefixIcon={} /> handleAction("edit")} label="수정" prefixIcon={} /> handleAction("delete")} tone="critical" label="삭제" prefixIcon={} /> ); }; export default ActivityMenuSheetSimple; ``` ### Usage ```tsx 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 ( {/* [!code highlight] */} !open && pop()}> handleAction("add")} label="Add" prefixIcon={} /> handleAction("edit")} label="Edit" prefixIcon={} /> handleAction("delete")} tone="critical" label="Delete" prefixIcon={} /> ); }; ``` 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()}`. - [Learn more about `useActivityZIndexBase`](#about-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](https://stackflow.so/docs/get-started/navigating-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`. **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](#making-a-swipeable-menu-sheet-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. ```tsx title='ActivityMenuSheetStep.tsx' import { 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, AppBarIconButton, AppBarMain, AppBarRight } from "seed-design/ui/app-bar"; import { AppScreen, AppScreenContent } from "seed-design/ui/app-screen"; import { SwipeableMenuSheetContent, SwipeableMenuSheetGroup, SwipeableMenuSheetItem, SwipeableMenuSheetRoot, SwipeableMenuSheetTrigger, } from "seed-design/ui/swipeable-menu-sheet"; import { IconHouseLine, IconPencilLine, IconPlusLine, IconTrashcanLine, } from "@karrotmarket/react-monochrome-icon"; import { Snackbar, useSnackbarAdapter } from "seed-design/ui/snackbar"; declare module "@stackflow/config" { interface Register { ActivityMenuSheetStep: { "menu-sheet"?: "open"; }; } } const ActivityMenuSheetStep: StaticActivityComponentType<"ActivityMenuSheetStep"> = () => { const [open, setOpen] = useState(false); const { push } = useFlow(); const { pushStep, popStep } = useStepFlow("ActivityMenuSheetStep"); const params = useActivityParams<"ActivityMenuSheetStep">(); const isOverlayOpen = params["menu-sheet"] === "open"; const snackbar = useSnackbarAdapter(); useEffect(() => setOpen(isOverlayOpen), [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) => { snackbar.create({ render: () => , }); setOpen(false); }; return ( push("ActivityHome", {})}> Menu Sheet 열기 handleAction("add")} label="추가" prefixIcon={} /> handleAction("edit")} label="수정" prefixIcon={} /> handleAction("delete")} tone="critical" label="삭제" prefixIcon={} /> { // 이 Menu Sheet는 Activity로 만들어지지 않았기 때문에, z-index 정리를 위해 // Menu Sheet를 먼저 닫고 다음 Activity를 push해야 합니다. setOpen(false); push("ActivityDetail", { title: "Menu Sheet에서 이동한 화면", body: "Menu Sheet를 닫고 이동했습니다.", }); }} label="Push" /> ); }; export default ActivityMenuSheetStep; ``` ### 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: { "menu-sheet"?: "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["menu-sheet"] === "open"; // [!code highlight] useEffect(() => { if (!isOverlayOpen) { setOpen(false); } }, [isOverlayOpen]); // [!code highlight] 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 ( Open {/* [!code highlight] */} handleAction("add")} label="Add" /> handleAction("edit")} label="Edit" /> { // [!code highlight] setOpen(false); // Close the Swipeable Menu Sheet before moving to another Activity. push("ActivityNext"); }} label="Push" /> handleAction("delete")} tone="critical" label="Delete" /> {/* [!code highlight] */} ); }; ``` 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 })}`. - [Learn more about `useActivityZIndexBase`](#about-useactivityzindexbase) ### `useStepOverlay` It is convenient to use `useStepOverlay` to generalize #2 and #3. For implementation examples of `useStepOverlay`, see 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(); const handleAction = (action: string) => { console.log("Selected action:", action); popStep(); }; return ( {/* [!code highlight] */} Open {/* [!code highlight] */} handleAction("add")} label="Add" /> handleAction("edit")} label="Edit" /> { // [!code highlight] setOpen(false); // Close the Swipeable Menu Sheet before moving to another Activity. push("ActivityNext"); }} label="Push" /> handleAction("delete")} tone="critical" label="Delete" /> {/* [!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 the entered Activities. - Elements that make up AppScreen (layer, appbar, dim, edge) have z-index calculated based on the `useActivityZIndexBase()` value. ``` ┌─ 2nd BottomSheet Activity │ └─ BottomSheet: 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 a BottomSheet on top of the current Activity in the Step pattern. ``` ┌─ BottomSheet displayed with activityOffset: 1 from 1st AppScreen Activity (e.g., Step pattern) │ └─ BottomSheet: 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 the entered Activities. - While the values returned by both hooks are the same, 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 `useActivityZIndexBase` hook in the `@stackflow/react-ui-core` package does not provide the `activityOffset` option. - The `useActivityZIndexBase` hook is provided starting from `@seed-design/stackflow@1.1.1`. If you were specifying `layerIndex` in the form `useActivity().zIndex * 5 + n`, you can change it to `useActivityZIndexBase() + n`. As you make the change, also review whether `+ n` is necessary.