From 174255f7ec3cdc26b6003e22820cb4bf42ab8ff7 Mon Sep 17 00:00:00 2001 From: Doma Date: Fri, 12 May 2023 04:37:23 +0800 Subject: [PATCH] fix(Menu): fix invisible dropdown on mobile screens (#524) --- src/components/ui/Menu.tsx | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/components/ui/Menu.tsx b/src/components/ui/Menu.tsx index 237a4b55..27ae2a98 100644 --- a/src/components/ui/Menu.tsx +++ b/src/components/ui/Menu.tsx @@ -1,10 +1,17 @@ import Link from "next/link" -import { Fragment } from "react" +import React, { Fragment } from "react" +import { autoUpdate, shift, useFloating } from "@floating-ui/react" import { Menu as HeadlessUiMenu } from "@headlessui/react" import { cn } from "~/lib/utils" +/** + * A dropdown menu that is opened by clicking on the target element. + * + * Use Headless UI for **accessible** interactions, + * and Floating UI for positioning. + */ export function Menu({ target, dropdown, @@ -14,17 +21,26 @@ export function Menu({ dropdown: React.ReactNode placement?: "bottom-start" | "bottom-end" }>) { + const { refs, floatingStyles } = useFloating({ + placement, + middleware: [ + // Prevent overflowing viewport + shift({ padding: 20 }), + ], + whileElementsMounted: autoUpdate, + }) + return ( - {target} + + {React.cloneElement(target, { ref: refs.setReference })} + {dropdown}