fix: align Dropdown/Menu with the HeroUI baseline after fidelity audit

- menu-item.css keys disabled styling off the presence attribute Reka emits.
- Dropdown surfaces drive the ported enter/exit animations and use data-side
  for placement-specific motion.
- One role=menu per popup; items render their key as DOM id; submenu surfaces
  point aria-labelledby at the real trigger; MenuSection names its group from a
  nested Header.
- Custom (asChild) triggers stay keyboard focusable; library Button/Avatar keep
  their own layout instead of the custom-trigger rules.
- Items emit data-focused/data-hovered/data-pressed, expose the upstream render
  props to slots, and merge caller classes through tailwind-variants.
- Submenu triggers declare hasSubmenu explicitly and forward class/ARIA; the
  default submenu indicator uses the baseline chevron artwork.
- Demos restore Kbd slot="keyboard" (27x), IconSquarePlus, and remove the added
  comment, stray space and duplicated indicator SVGs.
- Document the page after the official structure and API tables, including
  renamed props, Vue extensions and remaining gaps.
This commit is contained in:
maoyingjie
2026-10-24 22:14:10 +08:00
parent 64aaf5e53f
commit 043a5418f3
27 changed files with 1124 additions and 254 deletions
@@ -1,22 +1,28 @@
<script lang="ts">
import {
Fragment,
computed,
defineComponent,
h,
inject,
provide,
ref,
useAttrs,
type PropType,
type Ref,
} from "vue";
import {
DropdownMenuContent as RekaDropdownMenuContent,
injectDropdownMenuRootContext,
} from "reka-ui";
import { dropdownVariants } from "@heroui-vue/styles";
import {
dropdownContextKey,
menuCloseKey,
submenuContentKey,
} from "../menu/context";
import { useOverlayAnimation } from "../overlay/animation";
import { menuSurfaceFromVNode } from "./dom";
/**
* Dropdown popover / menu content shell. Provides close for Menu items.
@@ -47,19 +53,28 @@ export default defineComponent({
rootCtx?.onOpenChange(false);
});
const contentElement = ref<HTMLElement | null>(null);
const isOpen = computed(() => rootCtx?.open.value ?? false);
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
isOpen as Ref<boolean>,
contentElement,
true,
);
return () => {
const cls = [dropdownCtx?.slots.value.popover(), attrs.class];
if (inSubmenu) {
// Reka SubContent already positions and dismisses this level;
// keep a lightweight shell so Menu styling still applies.
// the submenu trigger forwards attrs onto that element.
return h(Fragment, null, slots.default?.() as never);
}
return h(
RekaDropdownMenuContent as never,
{
...attrs,
class: cls,
class: dropdownCtx?.slots.value.popover({ class: attrs.class }),
"data-slot": "dropdown-popover",
"data-entering": entering.value || undefined,
"data-exiting": exiting.value || undefined,
side: props.side,
sideOffset: props.sideOffset,
align: props.align,
@@ -67,6 +82,17 @@ export default defineComponent({
avoidCollisions: props.avoidCollisions,
collisionPadding: props.collisionPadding,
loop: props.loop,
onAnimationend: onAnimationEnd,
onVnodeMounted: (vnode) => {
contentElement.value = menuSurfaceFromVNode(vnode);
},
onVnodeUpdated: (vnode) => {
const element = menuSurfaceFromVNode(vnode);
if (element) contentElement.value = element;
},
onVnodeUnmounted: () => {
contentElement.value = null;
},
},
slots,
);
@@ -2,12 +2,19 @@
import {
Fragment,
cloneVNode,
computed,
defineComponent,
h,
inject,
isVNode,
nextTick,
onMounted,
provide,
ref,
useAttrs,
useId,
watch,
type Ref,
type VNodeChild,
} from "vue";
import {
@@ -15,7 +22,11 @@ import {
DropdownMenuSubContent as RekaDropdownMenuSubContent,
DropdownMenuSubTrigger as RekaDropdownMenuSubTrigger,
} from "reka-ui";
import { dropdownVariants } from "@heroui-vue/styles";
import { dropdownContextKey, submenuContentKey } from "../menu/context";
import { useOverlayAnimation } from "../overlay/animation";
import { elementFromValue, menuSurfaceFromVNode } from "./dom";
import DropdownPopover from "./DropdownPopover.vue";
function flatten(nodes: VNodeChild[], out: VNodeChild[] = []): VNodeChild[] {
for (const n of nodes) {
@@ -25,10 +36,24 @@ function flatten(nodes: VNodeChild[], out: VNodeChild[] = []): VNodeChild[] {
return out;
}
function resolveElement(value: unknown): HTMLElement | null {
if (value instanceof Element) return value as HTMLElement;
const el = (value as { $el?: unknown } | null)?.$el;
return el instanceof Element ? (el as HTMLElement) : null;
/** Attributes a nested `<DropdownPopover>` may pass to the submenu surface. */
function inheritableAttrs(props: Record<string, unknown> | null) {
const out: Record<string, unknown> = {};
if (!props) return out;
for (const [key, value] of Object.entries(props)) {
if (key === "ref" || key === "key") continue;
if (
key === "class" ||
key === "style" ||
key === "id" ||
key === "role" ||
key.startsWith("data-") ||
key.startsWith("aria-") ||
/^on[A-Z]/.test(key)
)
out[key] = value;
}
return out;
}
/**
@@ -47,6 +72,41 @@ export default defineComponent({
const subOpen = ref(false);
const triggerRef = ref<unknown>(null);
const contentElement = ref<HTMLElement | null>(null);
const fallbackId = useId();
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
subOpen as Ref<boolean>,
contentElement,
true,
);
/**
* Reka labels the submenu surface with the id it generates for its own
* trigger, but our trigger item keeps the caller's `id` (the selection
* key), so that reference dangles. Point it at the real trigger element.
*/
function repairLabel() {
const content = contentElement.value;
const trigger = elementFromValue(triggerRef.value);
if (!content || !trigger) return;
const ids = (content.getAttribute("aria-labelledby") ?? "")
.split(/\s+/)
.filter(Boolean);
const resolves = ids.some(
(id) => content.ownerDocument.getElementById(id) !== null,
);
if (resolves) return;
if (!trigger.id) trigger.id = fallbackId;
content.setAttribute("aria-labelledby", trigger.id);
}
watch(
contentElement,
(element) => {
if (element) void nextTick(repairLabel);
},
{ flush: "post" },
);
onMounted(repairLabel as () => void);
function handleEscape(event: KeyboardEvent) {
if (event.key !== "Escape" || !subOpen.value) return;
@@ -59,13 +119,21 @@ export default defineComponent({
event.preventDefault();
event.stopPropagation();
subOpen.value = false;
resolveElement(triggerRef.value)?.focus();
elementFromValue(triggerRef.value)?.focus();
}
return () => {
const children = flatten(slots.default?.() ?? []);
const triggerVNode = children[0] ?? null;
const contentVNodes = children.slice(1);
const popoverVNode = contentVNodes.find(
(node) => isVNode(node) && node.type === DropdownPopover,
);
const popoverAttrs = inheritableAttrs(
isVNode(popoverVNode)
? (popoverVNode.props as Record<string, unknown> | null)
: null,
);
return h(
RekaDropdownMenuSub as never,
@@ -88,7 +156,11 @@ export default defineComponent({
default: () =>
triggerVNode
? cloneVNode(triggerVNode as never, {
...attrs,
ref: triggerRef as never,
// The item is a submenu trigger regardless of whether
// the caller renders an indicator.
hasSubmenu: true,
// Escape with focus on the trigger (pointer-opened
// submenu) must also close only this level.
onKeydown: handleEscape,
@@ -99,10 +171,29 @@ export default defineComponent({
h(
RekaDropdownMenuSubContent as never,
{
class: [dropdownCtx?.slots.value.popover()],
...popoverAttrs,
class: [dropdownCtx?.slots.value.popover(), popoverAttrs.class],
"data-slot": "dropdown-popover",
"data-submenu-content": "true",
"data-entering": entering.value || undefined,
"data-exiting": exiting.value || undefined,
onKeydown: handleEscape,
onAnimationend: onAnimationEnd,
onVnodeMounted: (vnode) => {
contentElement.value = menuSurfaceFromVNode(vnode);
void nextTick(repairLabel);
},
onVnodeUpdated: (vnode) => {
const element = menuSurfaceFromVNode(vnode);
if (!element) return;
contentElement.value = element;
// Reka re-applies its own trigger id on re-render, so the
// association is restored after every patch.
void nextTick(repairLabel);
},
onVnodeUnmounted: () => {
contentElement.value = null;
},
},
{
default: () =>
@@ -1,7 +1,17 @@
<script lang="ts">
import { defineComponent, h, inject, onBeforeUnmount, useAttrs } from "vue";
import {
defineComponent,
h,
inject,
nextTick,
onBeforeUnmount,
onMounted,
ref,
useAttrs,
} from "vue";
import { DropdownMenuTrigger as RekaDropdownMenuTrigger } from "reka-ui";
import { dropdownContextKey } from "../menu/context";
import { elementFromValue } from "./dom";
const LONG_PRESS_MOVE_PX = 10;
@@ -23,6 +33,7 @@ export default defineComponent({
setup(props, { slots }) {
const attrs = useAttrs();
const dropdownCtx = inject(dropdownContextKey, null);
const triggerRef = ref<unknown>(null);
const isLongPress = () =>
(dropdownCtx?.triggerMode.value ?? "press") === "longPress";
@@ -75,6 +86,33 @@ export default defineComponent({
dropdownCtx?.setOpen(!(dropdownCtx?.open.value ?? false));
}
/**
* Upstream's `Dropdown.Trigger` is a real `<button>`, so a custom trigger
* (e.g. an Avatar) stays keyboard operable. With `asChild` the element is
* whatever the caller supplied, so restore button semantics when it is not
* natively focusable.
*/
function ensureFocusable() {
const el = elementFromValue(triggerRef.value);
if (!el) return;
const tag = el.tagName;
const role = el.getAttribute("role");
if (
tag === "BUTTON" ||
(tag === "A" && el.hasAttribute("href")) ||
el.hasAttribute("tabindex") ||
role === "button" ||
role === "link" ||
role === "menuitem"
)
return;
if (!role) el.setAttribute("role", "button");
el.setAttribute("tabindex", "0");
}
onMounted(() => void nextTick(ensureFocusable));
onBeforeUnmount(clearPress);
const guardHandlers = {
onPointerdownCapture: onPointerdown,
onPointermoveCapture: onPointermove,
@@ -86,16 +124,17 @@ export default defineComponent({
onContextmenuCapture: clearPress,
};
onBeforeUnmount(clearPress);
return () => {
const trigger = h(
RekaDropdownMenuTrigger as never,
{
...attrs,
ref: triggerRef,
as: props.as,
asChild: props.asChild ?? true,
class: [dropdownCtx?.slots.value.trigger(), attrs.class],
class: dropdownCtx?.slots.value.trigger({
class: attrs.class as never,
}),
"data-slot": "dropdown-trigger",
},
slots,
@@ -0,0 +1,36 @@
import type { VNode } from "vue";
/** Resolve a component/ref handle to its DOM element. */
export function elementFromValue(value: unknown): HTMLElement | null {
if (value instanceof Element) return value as HTMLElement;
const el = (value as { $el?: unknown } | null)?.$el;
return el instanceof Element ? (el as HTMLElement) : null;
}
/**
* Resolve the element a vnode mounted to. Reka's content components expose the
* positioned element through `$el`; fragments (text nodes) resolve to null.
*/
export function elementFromVNode(
vnode: VNode | null | undefined,
): HTMLElement | null {
if (!vnode) return null;
const el = vnode.el;
if (el instanceof Element) return el as HTMLElement;
const proxyEl = (vnode.component?.proxy as { $el?: unknown } | null)?.$el;
return proxyEl instanceof Element ? (proxyEl as HTMLElement) : null;
}
/**
* Reka renders a positioning wrapper around the menu surface; the popover
* styles and animations live on the inner `[data-reka-menu-content]` node.
*/
export function menuSurfaceFromVNode(
vnode: VNode | null | undefined,
): HTMLElement | null {
const el = elementFromVNode(vnode);
if (!el) return null;
if (el.matches("[data-reka-menu-content]")) return el;
const inner = el.querySelector("[data-reka-menu-content]");
return inner instanceof Element ? (inner as HTMLElement) : el;
}