fix: portal dropdown surfaces and close the review capability gaps
- Root and submenu surfaces render through the dropdown portal with the overlay z-index, so clipping/isolation ancestors no longer cut the menu; panel height is capped by the floating available height and the animation origin maps the real transform origin. - Standalone Menu is a static in-flow list: no scroll lock, no outside pointer blocking, no floating placement and no focus stealing. - Selection ownership is isolated per level: section disabled keys add to the menu list, and a nested Menu resets the section owner. - closeOnSelect resolves item > menu policy > per-mode default for selection items; actions and radio items close, checkboxes stay open. - Business keys no longer become DOM ids (instance ids + data-key), and submenu surfaces label themselves from the real trigger. - Long-press keydown runs the caller's handlers first and only opens when not cancelled; submenu panels merge caller keydown with the internal Escape. - Link items render a real anchor, aria labels land on the role=menu node, submenu panels forward placement/offset/collision props and caller attrs, and the popover provides the default Surface variant. - Wrapper parts declare real props/emits, plus the missing official capability props (disallowEmptySelection, escapeKeyBehavior, shouldFocusWrap, autoFocus, shouldCloseOnSelect, close, empty slot, isKeyboardDismissDisabled, shouldCloseOnInteractOutside, placement/offset family, scoped slots). - Ship an independent consumer fixture for browser-level verification.
This commit is contained in:
@@ -4,9 +4,12 @@ import {
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
provide,
|
||||
ref,
|
||||
useAttrs,
|
||||
useId,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { DropdownMenuItem as RekaMenuItem } from "reka-ui";
|
||||
@@ -22,10 +25,20 @@ type NativeClickHandler =
|
||||
((e: MouseEvent) => void) | ((e: MouseEvent) => void)[] | undefined;
|
||||
type NativeHandler<Ev> = ((e: Ev) => void) | ((e: Ev) => void)[] | undefined;
|
||||
|
||||
const LINK_ATTRS = [
|
||||
"href",
|
||||
"target",
|
||||
"rel",
|
||||
"download",
|
||||
"hrefLang",
|
||||
"referrerPolicy",
|
||||
] as const;
|
||||
|
||||
export default defineComponent({
|
||||
name: "MenuItem",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
/** Business key used by selection and actions. Never becomes a DOM id. */
|
||||
id: { type: [String, Number] as PropType<MenuKey>, default: undefined },
|
||||
textValue: { type: String, default: undefined },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
@@ -34,7 +47,7 @@ export default defineComponent({
|
||||
type: String as PropType<MenuItemVariants["variant"]>,
|
||||
default: "default",
|
||||
},
|
||||
/** Set false to keep the menu open after an action item. */
|
||||
/** Explicit close policy for this item (official `shouldCloseOnSelect`). */
|
||||
closeOnSelect: { type: Boolean, default: undefined },
|
||||
/**
|
||||
* Render this item as a submenu trigger. `DropdownSubmenuTrigger` sets it
|
||||
@@ -42,6 +55,16 @@ export default defineComponent({
|
||||
* submenu (upstream derives it from the collection structure).
|
||||
*/
|
||||
hasSubmenu: { type: Boolean, default: undefined },
|
||||
/** Render a link menu item instead of a button-like item. */
|
||||
href: { type: String, default: undefined },
|
||||
target: { type: String, default: undefined },
|
||||
rel: { type: String, default: undefined },
|
||||
download: {
|
||||
type: [String, Boolean] as PropType<string | boolean>,
|
||||
default: undefined,
|
||||
},
|
||||
hrefLang: { type: String, default: undefined },
|
||||
referrerPolicy: { type: String, default: undefined },
|
||||
},
|
||||
emits: {
|
||||
select: (_key: MenuKey) => true,
|
||||
@@ -55,6 +78,7 @@ export default defineComponent({
|
||||
const isFocused = ref(false);
|
||||
const isHovered = ref(false);
|
||||
const isPressed = ref(false);
|
||||
const domId = useId();
|
||||
const key = computed<MenuKey>(
|
||||
() => props.id ?? (attrs.id as MenuKey | undefined) ?? "",
|
||||
);
|
||||
@@ -80,6 +104,15 @@ export default defineComponent({
|
||||
if (selectionMode.value === "single") return "menuitemradio";
|
||||
return "menuitem";
|
||||
});
|
||||
const linkProps = computed(() => {
|
||||
if (!props.href) return null;
|
||||
const out: Record<string, unknown> = { as: "a", href: props.href };
|
||||
for (const name of LINK_ATTRS) {
|
||||
if (name === "href") continue;
|
||||
if (props[name] !== undefined) out[name] = props[name];
|
||||
}
|
||||
return out;
|
||||
});
|
||||
|
||||
provide(menuItemContextKey, {
|
||||
slots: slotsComputed,
|
||||
@@ -93,6 +126,25 @@ export default defineComponent({
|
||||
},
|
||||
});
|
||||
|
||||
let unregister: (() => void) | undefined;
|
||||
onMounted(() => {
|
||||
unregister = menuCtx?.registerItem();
|
||||
});
|
||||
onUnmounted(() => {
|
||||
unregister?.();
|
||||
unregister = undefined;
|
||||
});
|
||||
|
||||
function shouldClose() {
|
||||
if (props.closeOnSelect !== undefined) return props.closeOnSelect;
|
||||
const menuPolicy = menuCtx?.shouldCloseOnSelect.value;
|
||||
if (typeof menuPolicy === "function") return menuPolicy(key.value);
|
||||
// Default per mode: action and radio items close, checkboxes stay open.
|
||||
if (selectionMode.value !== "none")
|
||||
return selectionMode.value === "single";
|
||||
return menuPolicy ?? true;
|
||||
}
|
||||
|
||||
function activate() {
|
||||
if (isDisabled.value) return;
|
||||
emit("select", key.value);
|
||||
@@ -100,12 +152,11 @@ export default defineComponent({
|
||||
const mode = selectionMode.value;
|
||||
if (mode !== "none") {
|
||||
owner?.proposeSelection(key.value);
|
||||
if (mode === "single") menuCtx?.closeLevel();
|
||||
if (shouldClose()) menuCtx?.closeLevel();
|
||||
return;
|
||||
}
|
||||
menuCtx?.emitAction(key.value);
|
||||
if (props.closeOnSelect === false) return;
|
||||
menuCtx?.closeLevel();
|
||||
if (shouldClose()) menuCtx?.closeLevel();
|
||||
}
|
||||
|
||||
/** Keep the caller's own handler(s) in front of the internal tracker. */
|
||||
@@ -129,8 +180,8 @@ export default defineComponent({
|
||||
const rest: Record<string, unknown> = {
|
||||
...(attrs as Record<string, unknown>),
|
||||
};
|
||||
delete rest.onClick;
|
||||
for (const name of [
|
||||
"onClick",
|
||||
"onFocus",
|
||||
"onBlur",
|
||||
"onPointerDown",
|
||||
@@ -150,17 +201,22 @@ export default defineComponent({
|
||||
: []
|
||||
) as Array<(e: MouseEvent) => void>;
|
||||
clickChain.push((event: MouseEvent) => {
|
||||
if (event.defaultPrevented || isDisabled.value) return;
|
||||
if (event.defaultPrevented) return;
|
||||
if (isDisabled.value) {
|
||||
// A disabled link must not navigate either.
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
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,
|
||||
...linkProps.value,
|
||||
id: domId,
|
||||
class: slotsComputed.value.item({ class: attrs.class as never }),
|
||||
role: roleAttr,
|
||||
@@ -180,6 +236,7 @@ export default defineComponent({
|
||||
"data-pressed": isPressed.value || undefined,
|
||||
"data-has-submenu": hasSubmenu.value || undefined,
|
||||
"data-selection-mode": selectionMode.value,
|
||||
"data-key": String(key.value),
|
||||
disabled: isDisabled.value,
|
||||
textValue: props.textValue,
|
||||
onClick: clickChain,
|
||||
|
||||
Reference in New Issue
Block a user