fix(Menu): fix invisible dropdown on mobile screens (#524)

This commit is contained in:
Doma 2023-05-12 04:37:23 +08:00 committed by GitHub
parent d2650a344f
commit 174255f7ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 7 deletions

View File

@ -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 (
<HeadlessUiMenu>
<HeadlessUiMenu.Button as={Fragment}>{target}</HeadlessUiMenu.Button>
<HeadlessUiMenu.Button as={Fragment}>
{React.cloneElement(target, { ref: refs.setReference })}
</HeadlessUiMenu.Button>
<HeadlessUiMenu.Items
ref={refs.setFloating}
className={cn(
"fixed z-10 mt-1 w-full outline-none text-gray-600 bg-white rounded-lg ring-1 ring-border shadow-md py-2 md:absolute md:w-max",
{
"bottom-start": "top-[100%] left-0",
"bottom-end": "top-[100%] right-0",
}[placement],
"absolute z-10 mt-1 w-max outline-none text-gray-600 bg-white rounded-lg ring-1 ring-border shadow-md py-2",
)}
style={floatingStyles}
>
{dropdown}
</HeadlessUiMenu.Items>