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
@@ -20,6 +20,7 @@ import {
type NativeClickHandler =
((e: MouseEvent) => void) | ((e: MouseEvent) => void)[] | undefined;
type NativeHandler<Ev> = ((e: Ev) => void) | ((e: Ev) => void)[] | undefined;
export default defineComponent({
name: "MenuItem",
@@ -35,6 +36,12 @@ export default defineComponent({
},
/** Set false to keep the menu open after an action item. */
closeOnSelect: { type: Boolean, default: undefined },
/**
* Render this item as a submenu trigger. `DropdownSubmenuTrigger` sets it
* so the item no longer depends on a rendered indicator to know it has a
* submenu (upstream derives it from the collection structure).
*/
hasSubmenu: { type: Boolean, default: undefined },
},
emits: {
select: (_key: MenuKey) => true,
@@ -44,11 +51,17 @@ export default defineComponent({
const menuCtx = inject(menuContextKey, null);
const sectionCtx = inject(menuSectionContextKey, null);
const owner = sectionCtx?.selection ?? menuCtx?.selection;
const hasSubmenu = ref(false);
const indicatorSubmenu = ref(false);
const isFocused = ref(false);
const isHovered = ref(false);
const isPressed = ref(false);
const key = computed<MenuKey>(
() => props.id ?? (attrs.id as MenuKey | undefined) ?? "",
);
const hasSubmenu = computed(
() => props.hasSubmenu ?? indicatorSubmenu.value,
);
const isDisabled = computed(
() =>
props.isDisabled ||
@@ -72,9 +85,11 @@ export default defineComponent({
slots: slotsComputed,
isDisabled,
isSelected,
hasSubmenu: computed(() => hasSubmenu.value),
isFocused,
isPressed,
hasSubmenu,
setHasSubmenu(value: boolean) {
hasSubmenu.value = value;
indicatorSubmenu.value = value;
},
});
@@ -93,6 +108,16 @@ export default defineComponent({
menuCtx?.closeLevel();
}
/** Keep the caller's own handler(s) in front of the internal tracker. */
function withOwn<Ev extends Event>(
name: string,
own: (event: Ev) => void,
): NativeHandler<Ev> {
const user = (attrs as Record<string, NativeHandler<Ev>>)[name];
if (!user) return own;
return Array.isArray(user) ? [...user, own] : [user, own];
}
return () => {
// Hand the user handlers and our activation to Vue as one DOM `onClick`
// array so browser/Vue listener semantics stay intact: capture order,
@@ -105,6 +130,17 @@ export default defineComponent({
...(attrs as Record<string, unknown>),
};
delete rest.onClick;
for (const name of [
"onFocus",
"onBlur",
"onPointerDown",
"onPointerUp",
"onPointerCancel",
"onPointerEnter",
"onPointerLeave",
]) {
delete rest[name];
}
const clickChain: Array<(e: MouseEvent) => void> = (
Array.isArray(userOnClick)
@@ -118,12 +154,16 @@ export default defineComponent({
activate();
});
const roleAttr = (attrs.role as string | undefined) ?? role.value;
const domId = props.id ?? (attrs.id as MenuKey | undefined);
return h(
RekaMenuItem as never,
{
...rest,
class: [slotsComputed.value.item(), attrs.class],
role: (attrs.role as string | undefined) ?? role.value,
id: domId,
class: slotsComputed.value.item({ class: attrs.class as never }),
role: roleAttr,
"aria-checked":
selectionMode.value === "none"
? undefined
@@ -133,16 +173,52 @@ export default defineComponent({
"aria-disabled": isDisabled.value || undefined,
"data-slot": "menu-item",
"data-selected": isSelected.value || undefined,
// RAC's render-state hooks that the ported CSS and the official
// custom-styles demo rely on.
"data-focused": isFocused.value || undefined,
"data-hovered": isHovered.value || undefined,
"data-pressed": isPressed.value || undefined,
"data-has-submenu": hasSubmenu.value || undefined,
"data-selection-mode": selectionMode.value,
disabled: isDisabled.value,
textValue: props.textValue,
onClick: clickChain,
onFocus: withOwn<FocusEvent>("onFocus", () => {
isFocused.value = true;
}),
onBlur: withOwn<FocusEvent>("onBlur", () => {
isFocused.value = false;
isPressed.value = false;
}),
onPointerDown: withOwn<PointerEvent>("onPointerDown", () => {
isPressed.value = true;
}),
onPointerUp: withOwn<PointerEvent>("onPointerUp", () => {
isPressed.value = false;
}),
onPointerCancel: withOwn<PointerEvent>("onPointerCancel", () => {
isPressed.value = false;
}),
onPointerEnter: withOwn<PointerEvent>("onPointerEnter", () => {
isHovered.value = true;
}),
onPointerLeave: withOwn<PointerEvent>("onPointerLeave", () => {
isHovered.value = false;
isPressed.value = false;
}),
onSelect: (itemSelectEvent?: { preventDefault?: () => void }) => {
itemSelectEvent?.preventDefault?.();
},
},
slots,
{
default: () =>
slots.default?.({
isSelected: isSelected.value,
isFocused: isFocused.value,
isDisabled: isDisabled.value,
isPressed: isPressed.value,
}),
},
);
};
},
@@ -58,21 +58,29 @@ export default defineComponent({
const attrs = useAttrs();
const item = inject(menuItemContextKey, null);
return () => {
const isSelected = item?.isSelected.value ?? false;
const state = {
isSelected: item?.isSelected.value ?? false,
// Menu items have no indeterminate state; kept for the upstream
// render-prop contract.
isIndeterminate: false,
isFocused: item?.isFocused.value ?? false,
isDisabled: item?.isDisabled.value ?? false,
isPressed: item?.isPressed.value ?? false,
};
const content = slots.default
? slots.default({ isSelected })
? slots.default(state)
: props.type === "dot"
? DotIcon()
: CheckmarkIcon(isSelected);
: CheckmarkIcon(state.isSelected);
return h(
props.as,
{
...attrs,
"aria-hidden": "true",
class: [item?.slots.value.indicator(), attrs.class],
class: item?.slots.value.indicator({ class: attrs.class as never }),
"data-slot": "menu-item-indicator",
"data-type": props.type,
"data-visible": isSelected || undefined,
"data-visible": state.isSelected || undefined,
},
content,
);
@@ -9,6 +9,9 @@ import {
} from "vue";
import { menuItemContextKey } from "../menu/context";
/** Ported from HeroUI `IconChevronRight` (icons.tsx) so the default
* submenu affordance keeps the baseline artwork without adding an icon
* dependency to the component package. */
function ChevronIcon() {
return h(
"svg",
@@ -16,14 +19,19 @@ function ChevronIcon() {
"aria-hidden": "true",
fill: "none",
height: "16",
stroke: "currentColor",
"stroke-linecap": "round",
"stroke-linejoin": "round",
"stroke-width": "2",
viewBox: "0 0 24 24",
role: "presentation",
viewBox: "0 0 16 16",
width: "16",
xmlns: "http://www.w3.org/2000/svg",
},
[h("path", { d: "m9 18 6-6-6-6" })],
[
h("path", {
"clip-rule": "evenodd",
d: "M5.47 2.97a.75.75 0 0 1 1.06 0l4.5 4.5a.75.75 0 0 1 0 1.06l-4.5 4.5a.75.75 0 1 1-1.06-1.06L9.44 8 5.47 4.03a.75.75 0 0 1 0-1.06Z",
fill: "currentColor",
"fill-rule": "evenodd",
}),
],
);
}
@@ -44,7 +52,9 @@ export default defineComponent({
{
...attrs,
"aria-hidden": "true",
class: [item.slots.value.submenuIndicator(), attrs.class],
class: item.slots.value.submenuIndicator({
class: attrs.class as never,
}),
"data-slot": "submenu-indicator",
},
slots.default ? slots.default() : ChevronIcon(),