Placement dropdown menus
Five dropdown menus that keep the same panel and vary only where it anchors: bottom, top, left, right and centered. The panel is always positioned against its own trigger, never the viewport, so many instances work side by side. Each is self-contained (no radix), sized, token-driven, and closes on outside click, Escape or item select.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/dropdown-menu-placementDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/dropdown-menu-placement.tsx"use client"
import * as React from "react"
import {
Bell,
ChevronDown,
CreditCard,
Layers,
LogOut,
Settings,
User,
} from "lucide-react"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Placement dropdown family: 5 placement directions. The panel is the same clear
popover surface in every variant - the whole difference is WHERE it attaches
relative to the trigger: bottom, top, left, right and bottom-center. The panel is
always positioned relative to ITS OWN trigger (absolute inside a relative
wrapper), never pinned to the viewport; that is what lets dozens of examples work
correctly side by side on one page. NO radix or portal. It closes on an outside
click, on Escape (focus returns to the trigger) and on an item selection. The
arrow keys move between items. Color comes ONLY from tokens, via alpha
color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const panelWidth: Record<StyledSize, string> = {
sm: "w-48",
md: "w-56",
lg: "w-64",
xl: "w-72",
}
const itemHeight: Record<StyledSize, string> = {
sm: "min-h-8 py-1.5",
md: "min-h-9 py-2",
lg: "min-h-10 py-2.5",
xl: "min-h-11 py-3",
}
export interface StyledMenuItem {
label: React.ReactNode
icon?: React.ReactNode
description?: React.ReactNode
onSelect?: () => void
}
interface StyledMenuProps {
className?: string
size?: StyledSize
label?: React.ReactNode
items?: StyledMenuItem[]
}
const triggerBtn =
"inline-flex h-9 shrink-0 select-none items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-border bg-secondary px-4 text-sm font-medium text-secondary-foreground outline-none transition-colors hover:bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)] focus-visible:ring-[3px] focus-visible:ring-ring/50 [&>svg]:size-4 [&>svg]:shrink-0 [&>i]:text-base [&>i]:leading-none"
const panelBase =
"rounded-xl border border-border bg-popover p-1.5 text-sm text-popover-foreground shadow-lg outline-none"
const itemBase =
"flex w-full cursor-default select-none items-center gap-2.5 rounded-md px-3 text-left text-sm text-popover-foreground outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:bg-accent focus-visible:text-accent-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const fade = { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } }
interface Placement {
/* Konumlandirici wrapper sinifi - panelin trigger'a gore yeri. */
anchor: string
/* The panel's transform origin. */
origin: string
/* Giris/cikis kaymasinin yonu. */
offset: { x?: number; y?: number }
/* Trigger ikonunun acikken donme yonu. */
chevron: string
}
const spring = { type: "spring" as const, stiffness: 340, damping: 26 }
function useMenuState() {
const [open, setOpen] = React.useState(false)
const wrapperRef = React.useRef<HTMLDivElement>(null)
const triggerRef = React.useRef<HTMLButtonElement>(null)
React.useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false)
triggerRef.current?.focus()
}
}
const onPointer = (e: PointerEvent) => {
const node = wrapperRef.current
if (node && e.target instanceof Node && !node.contains(e.target)) {
setOpen(false)
}
}
window.addEventListener("keydown", onKey)
window.addEventListener("pointerdown", onPointer)
return () => {
window.removeEventListener("keydown", onKey)
window.removeEventListener("pointerdown", onPointer)
}
}, [open])
return { open, setOpen, wrapperRef, triggerRef }
}
function useArrowNav(panelRef: React.RefObject<HTMLDivElement | null>) {
return React.useCallback(
(e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return
const panel = panelRef.current
if (!panel) return
const nodes = Array.from(
panel.querySelectorAll<HTMLButtonElement>(
'[role="menuitem"],[role="menuitemcheckbox"]'
)
)
if (nodes.length === 0) return
e.preventDefault()
const current = nodes.indexOf(document.activeElement as HTMLButtonElement)
const next =
e.key === "ArrowDown"
? (current + 1) % nodes.length
: current <= 0
? nodes.length - 1
: current - 1
nodes[next]?.focus()
},
[panelRef]
)
}
/* Shared shell: the positioner (absolute) wrapper is built from the placement supplied from outside; the animation is carried by the inner motion panel, so placement and transform never get tangled. */
function MenuShell({
size = "md",
label = "Options",
className,
placement,
children,
}: {
size?: StyledSize
label?: React.ReactNode
className?: string
placement: Placement
children: (close: () => void) => React.ReactNode
}) {
const { open, setOpen, wrapperRef, triggerRef } = useMenuState()
const panelRef = React.useRef<HTMLDivElement>(null)
const onArrow = useArrowNav(panelRef)
const reduce = useReducedMotion()
const id = React.useId()
const panelId = `${id}-menu`
const spec = reduce
? fade
: {
initial: { opacity: 0, scale: 0.96, ...placement.offset },
animate: { opacity: 1, scale: 1, x: 0, y: 0 },
exit: { opacity: 0, scale: 0.96, ...placement.offset },
}
return (
<div
ref={wrapperRef}
data-slot="styled-dropdown-menu"
className={cn("relative inline-flex", className)}
>
<button
ref={triggerRef}
type="button"
data-slot="styled-dropdown-menu-trigger"
aria-haspopup="menu"
aria-expanded={open}
aria-controls={open ? panelId : undefined}
className={triggerBtn}
onClick={() => setOpen(!open)}
>
{label}
<ChevronDown
className={cn("transition-transform duration-200", open && placement.chevron)}
/>
</button>
<AnimatePresence>
{open ? (
<div className={cn("absolute z-50", placement.anchor, panelWidth[size])}>
<motion.div
ref={panelRef}
id={panelId}
role="menu"
data-slot="styled-dropdown-menu-content"
className={cn(panelBase, placement.origin)}
initial={spec.initial}
animate={spec.animate}
exit={spec.exit}
transition={reduce ? { duration: 0.16 } : spring}
onKeyDown={onArrow}
>
{children(() => setOpen(false))}
</motion.div>
</div>
) : null}
</AnimatePresence>
</div>
)
}
function MenuList({
items,
size,
close,
}: {
items: StyledMenuItem[]
size: StyledSize
close: () => void
}) {
return (
<div className="flex flex-col">
{items.map((item, i) => (
<button
key={`${i}`}
type="button"
role="menuitem"
tabIndex={0}
className={cn(itemBase, itemHeight[size])}
onClick={() => {
item.onSelect?.()
close()
}}
>
{item.icon ? (
<span className="flex shrink-0 items-center text-muted-foreground">
{item.icon}
</span>
) : null}
{item.label}
</button>
))}
</div>
)
}
const accountItems: StyledMenuItem[] = [
{ label: "Profile", icon: <User /> },
{ label: "Billing", icon: <CreditCard /> },
{ label: "Settings", icon: <Settings /> },
{ label: "Sign out", icon: <LogOut /> },
]
const workspaceItems: StyledMenuItem[] = [
{ label: "Overview", icon: <Layers /> },
{ label: "Members", icon: <User /> },
{ label: "Notifications", icon: <Bell /> },
{ label: "Settings", icon: <Settings /> },
]
/* Bottom: the default direction - the panel hangs below the trigger, aligned to its left edge. */
export function BottomMenu({ className, size = "md", label = "Bottom", items }: StyledMenuProps) {
return (
<MenuShell
size={size}
label={label}
className={className}
placement={{
anchor: "left-0 top-full mt-1",
origin: "origin-top-left",
offset: { y: -6 },
chevron: "rotate-180",
}}
>
{(close) => <MenuList items={items ?? accountItems} size={size} close={close} />}
</MenuShell>
)
}
/* Top: the panel opens ABOVE the trigger - for triggers near the bottom of the page. */
export function TopMenu({ className, size = "md", label = "Top", items }: StyledMenuProps) {
return (
<MenuShell
size={size}
label={label}
className={className}
placement={{
anchor: "bottom-full left-0 mb-1",
origin: "origin-bottom-left",
offset: { y: 6 },
chevron: "rotate-180",
}}
>
{(close) => <MenuList items={items ?? accountItems} size={size} close={close} />}
</MenuShell>
)
}
/* Left: the panel opens to the LEFT of the trigger, top edges aligned. */
export function LeftMenu({ className, size = "md", label = "Left", items }: StyledMenuProps) {
return (
<MenuShell
size={size}
label={label}
className={className}
placement={{
anchor: "right-full top-0 mr-1",
origin: "origin-top-right",
offset: { x: 6 },
chevron: "rotate-90",
}}
>
{(close) => <MenuList items={items ?? workspaceItems} size={size} close={close} />}
</MenuShell>
)
}
/* Right: the panel opens to the RIGHT of the trigger, top edges aligned. */
export function RightMenu({ className, size = "md", label = "Right", items }: StyledMenuProps) {
return (
<MenuShell
size={size}
label={label}
className={className}
placement={{
anchor: "left-full top-0 ml-1",
origin: "origin-top-left",
offset: { x: -6 },
chevron: "-rotate-90",
}}
>
{(close) => <MenuList items={items ?? workspaceItems} size={size} close={close} />}
</MenuShell>
)
}
/* Center: the panel is CENTRED HORIZONTALLY under the trigger (left-1/2 plus -translate-x-1/2 live on the positioner, so they do not clash with the motion transform). */
export function CenterMenu({ className, size = "md", label = "Center", items }: StyledMenuProps) {
return (
<MenuShell
size={size}
label={label}
className={className}
placement={{
anchor: "left-1/2 top-full mt-1 -translate-x-1/2",
origin: "origin-top",
offset: { y: -6 },
chevron: "rotate-180",
}}
>
{(close) => <MenuList items={items ?? accountItems} size={size} close={close} />}
</MenuShell>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Bottom
The default: below the trigger, aligned to its left edge.
import { BottomMenu } from "@/components/ui/dropdown-menu-placement"
<BottomMenu />Top
Above the trigger, for controls near the end of a page.
import { TopMenu } from "@/components/ui/dropdown-menu-placement"
<TopMenu />Left
To the left of the trigger, top edges aligned.
import { LeftMenu } from "@/components/ui/dropdown-menu-placement"
<LeftMenu />Right
To the right of the trigger, top edges aligned.
import { RightMenu } from "@/components/ui/dropdown-menu-placement"
<RightMenu />Center
Below the trigger and horizontally centered on it.
import { CenterMenu } from "@/components/ui/dropdown-menu-placement"
<CenterMenu />ai2 Placement dropdown menus: 5 styled variations on the token system
The ai2 Placement dropdown menus are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around click-triggered menus that anchor the panel in a chosen direction. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion fades and scales the panel in from the anchored edge. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the offset and scale are skipped and the panel fades in.
What is in the ai2 Placement dropdown menus?
5 exports in one file: Bottom, Top, Left, Right and Center. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion fades and scales the panel in from the anchored edge.
- Reduced-motion aware: Under prefers-reduced-motion, the offset and scale are skipped and the panel fades in.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Placement dropdown menus sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.