fix: resolve close policy, handler merging and submenu panel props
- Menu forwards the raw close policy and the item resolves item > menu (boolean or function) > per-mode default, so action items close by default and an explicit single/multiple policy is no longer pre-empted. - Root popover chains caller handlers with the internal ones for keydown, escape, interact-outside, auto focus and animation end; callers run first and a prevented event stops the internal logic. - Submenu panels receive the nested popover's data/style/id/extra events, honour isKeyboardDismissDisabled without closing the parent level or a modal, and forward shouldCloseOnInteractOutside. - A standalone Menu provides its own content-attribute channel so the accessible name/description land on the real role=menu node.
This commit is contained in:
@@ -118,7 +118,20 @@ export default defineComponent({
|
||||
const resolvedSide = computed(() => props.side ?? position.value.side);
|
||||
const resolvedAlign = computed(() => props.align ?? position.value.align);
|
||||
|
||||
type Handler<Ev> =
|
||||
((event: Ev) => void) | ((event: Ev) => void)[] | undefined;
|
||||
/** Caller handlers first, then the internal one: Vue array semantics. */
|
||||
function chain<Ev extends Event>(
|
||||
name: string,
|
||||
own: (event: Ev) => void,
|
||||
): Array<(event: Ev) => void> {
|
||||
const user = (attrs as Record<string, Handler<Ev>>)[name];
|
||||
const list = Array.isArray(user) ? user : user ? [user] : [];
|
||||
return [...list, own];
|
||||
}
|
||||
|
||||
function escape(event: KeyboardEvent) {
|
||||
if (event.defaultPrevented) return;
|
||||
if (props.isKeyboardDismissDisabled) event.preventDefault();
|
||||
}
|
||||
/**
|
||||
@@ -207,14 +220,24 @@ export default defineComponent({
|
||||
collisionPadding:
|
||||
props.collisionPadding ?? props.containerPadding,
|
||||
loop: props.loop ?? contentAttrs?.loop.value,
|
||||
onAnimationend: onAnimationEnd,
|
||||
onOpenAutoFocus: (event: Event) => {
|
||||
onAnimationend: chain<AnimationEvent>(
|
||||
"onAnimationend",
|
||||
onAnimationEnd,
|
||||
),
|
||||
onOpenAutoFocus: chain<Event>("onOpenAutoFocus", (event) => {
|
||||
if (event.defaultPrevented) return;
|
||||
if (contentAttrs?.autoFocus.value === false)
|
||||
event.preventDefault();
|
||||
},
|
||||
onEscapeKeyDown: escape,
|
||||
onInteractOutside: interactOutside,
|
||||
onKeydown: onSurfaceKeydown,
|
||||
}),
|
||||
onEscapeKeyDown: chain<KeyboardEvent>(
|
||||
"onEscapeKeyDown",
|
||||
escape,
|
||||
),
|
||||
onInteractOutside: chain<Event>(
|
||||
"onInteractOutside",
|
||||
interactOutside as (event: Event) => void,
|
||||
),
|
||||
onKeydown: chain<KeyboardEvent>("onKeydown", onSurfaceKeydown),
|
||||
onVnodeMounted: (vnode) => {
|
||||
contentElement.value = menuSurfaceFromVNode(vnode);
|
||||
},
|
||||
|
||||
@@ -36,7 +36,8 @@ import { useConfig } from "../../providers/context";
|
||||
import { elementFromValue, menuSurfaceFromVNode } from "./dom";
|
||||
import DropdownPopover from "./DropdownPopover.vue";
|
||||
|
||||
const PANEL_PROPS = [
|
||||
/** Panel props that are translated instead of spread onto the element. */
|
||||
const CONTROL_PROPS = new Set([
|
||||
"placement",
|
||||
"offset",
|
||||
"crossOffset",
|
||||
@@ -52,7 +53,10 @@ const PANEL_PROPS = [
|
||||
"to",
|
||||
"isKeyboardDismissDisabled",
|
||||
"shouldCloseOnInteractOutside",
|
||||
] as const;
|
||||
"class",
|
||||
"onKeydown",
|
||||
"onInteractOutside",
|
||||
]);
|
||||
|
||||
type Handler =
|
||||
((event: Event) => void) | ((event: Event) => void)[] | undefined;
|
||||
@@ -84,7 +88,7 @@ function inheritedProps(props: Record<string, unknown> | null) {
|
||||
key.startsWith("data-") ||
|
||||
key.startsWith("aria-") ||
|
||||
/^on[A-Z]/.test(key) ||
|
||||
(PANEL_PROPS as readonly string[]).includes(key)
|
||||
CONTROL_PROPS.has(key)
|
||||
)
|
||||
out[key] = value;
|
||||
}
|
||||
@@ -127,6 +131,12 @@ export default defineComponent({
|
||||
true,
|
||||
);
|
||||
|
||||
// Panel configuration for the current render; event handlers read it later.
|
||||
const panelConfig: {
|
||||
dismissDisabled: boolean;
|
||||
shouldCloseOutside?: (event: Event) => boolean;
|
||||
} = { dismissDisabled: false };
|
||||
|
||||
/**
|
||||
* Reka labels the submenu surface with the id it generates for its own
|
||||
* trigger, but our trigger item keeps an instance id, so that reference
|
||||
@@ -154,15 +164,19 @@ export default defineComponent({
|
||||
|
||||
function handleEscape(event: KeyboardEvent) {
|
||||
if (event.defaultPrevented) return;
|
||||
if (
|
||||
event.key === "Escape" &&
|
||||
contentAttrs.escapeKeyBehavior.value === "clearSelection"
|
||||
) {
|
||||
if (event.key !== "Escape") return;
|
||||
if (panelConfig.dismissDisabled) {
|
||||
// Keep this level, the parent menu and any modal open.
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
if (contentAttrs.escapeKeyBehavior.value === "clearSelection") {
|
||||
event.preventDefault();
|
||||
contentAttrs.clearSelection();
|
||||
return;
|
||||
}
|
||||
if (event.key !== "Escape" || !subOpen.value) return;
|
||||
if (!subOpen.value) return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
// Only the menu level that owns the focused element closes.
|
||||
if (!target?.closest?.("[role='menu']")) return;
|
||||
@@ -175,6 +189,15 @@ export default defineComponent({
|
||||
elementFromValue(triggerRef.value)?.focus();
|
||||
}
|
||||
|
||||
function handleInteractOutside(event: {
|
||||
defaultPrevented?: boolean;
|
||||
preventDefault?: () => void;
|
||||
}) {
|
||||
if (event.defaultPrevented) return;
|
||||
if (panelConfig.shouldCloseOutside?.(event as unknown as Event) === false)
|
||||
event.preventDefault?.();
|
||||
}
|
||||
|
||||
return () => {
|
||||
const children = flatten(slots.default?.() ?? []);
|
||||
const triggerVNode = children[0] ?? null;
|
||||
@@ -187,40 +210,51 @@ export default defineComponent({
|
||||
? (popoverVNode.props as Record<string, unknown> | null)
|
||||
: null,
|
||||
);
|
||||
const {
|
||||
class: popoverClass,
|
||||
onKeydown: popoverKeydown,
|
||||
...restProps
|
||||
} = popoverProps;
|
||||
const domProps: Record<string, unknown> = {};
|
||||
for (const [key, value] of Object.entries(popoverProps)) {
|
||||
if (!CONTROL_PROPS.has(key)) domProps[key] = value;
|
||||
}
|
||||
|
||||
const placement = (restProps.placement as OverlayPlacement) ?? "end";
|
||||
const placement = (popoverProps.placement as OverlayPlacement) ?? "end";
|
||||
const position = resolvePlacement(placement, config?.dir.value ?? "ltr");
|
||||
|
||||
panelConfig.dismissDisabled =
|
||||
(popoverProps.isKeyboardDismissDisabled as boolean | undefined) ??
|
||||
false;
|
||||
panelConfig.shouldCloseOutside =
|
||||
popoverProps.shouldCloseOnInteractOutside as
|
||||
((event: Event) => boolean) | undefined;
|
||||
|
||||
// Reka fixes the submenu side/align to the parent convention, so only
|
||||
// the offsets/collision props can be forwarded.
|
||||
const panelProps: Record<string, unknown> = {
|
||||
sideOffset:
|
||||
(restProps.sideOffset as number | undefined) ??
|
||||
(restProps.offset as number | undefined),
|
||||
(popoverProps.sideOffset as number | undefined) ??
|
||||
(popoverProps.offset as number | undefined),
|
||||
alignOffset:
|
||||
(restProps.alignOffset as number | undefined) ??
|
||||
(restProps.crossOffset as number | undefined),
|
||||
(popoverProps.alignOffset as number | undefined) ??
|
||||
(popoverProps.crossOffset as number | undefined),
|
||||
avoidCollisions:
|
||||
(restProps.avoidCollisions as boolean | undefined) ??
|
||||
(restProps.shouldFlip as boolean | undefined),
|
||||
(popoverProps.avoidCollisions as boolean | undefined) ??
|
||||
(popoverProps.shouldFlip as boolean | undefined),
|
||||
collisionPadding:
|
||||
(restProps.collisionPadding as number | undefined) ??
|
||||
(restProps.containerPadding as number | undefined),
|
||||
loop: restProps.loop as boolean | undefined,
|
||||
(popoverProps.collisionPadding as number | undefined) ??
|
||||
(popoverProps.containerPadding as number | undefined),
|
||||
loop: popoverProps.loop as boolean | undefined,
|
||||
};
|
||||
const side = (restProps.side as string | undefined) ?? position.side;
|
||||
const align = (restProps.align as string | undefined) ?? position.align;
|
||||
const side = (popoverProps.side as string | undefined) ?? position.side;
|
||||
const align =
|
||||
(popoverProps.align as string | undefined) ?? position.align;
|
||||
|
||||
const keydownChain = [
|
||||
...asArray(popoverKeydown as Handler),
|
||||
...asArray(popoverProps.onKeydown as Handler),
|
||||
...asArray(attrs.onKeydown as Handler),
|
||||
handleEscape,
|
||||
];
|
||||
const interactChain = [
|
||||
...asArray(popoverProps.onInteractOutside as Handler),
|
||||
handleInteractOutside as (event: Event) => void,
|
||||
];
|
||||
const labelledBy = contentAttrs.labelledBy.value;
|
||||
|
||||
return h(
|
||||
@@ -258,28 +292,31 @@ export default defineComponent({
|
||||
),
|
||||
h(
|
||||
RekaDropdownMenuPortal as never,
|
||||
{ to: restProps.to as string | HTMLElement | undefined },
|
||||
{ to: popoverProps.to as string | HTMLElement | undefined },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
RekaDropdownMenuSubContent as never,
|
||||
{
|
||||
...domProps,
|
||||
...panelProps,
|
||||
side,
|
||||
align,
|
||||
"aria-label": contentAttrs.label.value,
|
||||
"aria-describedby": contentAttrs.describedBy.value,
|
||||
loop:
|
||||
(restProps.loop as boolean | undefined) ??
|
||||
contentAttrs.loop.value,
|
||||
class: [dropdownCtx?.slots.value.popover(), popoverClass],
|
||||
class: [
|
||||
dropdownCtx?.slots.value.popover(),
|
||||
popoverProps.class,
|
||||
],
|
||||
"data-slot": "dropdown-popover",
|
||||
"data-submenu-content": "true",
|
||||
"data-entering": entering.value || undefined,
|
||||
"data-exiting": exiting.value || undefined,
|
||||
onKeydown: keydownChain,
|
||||
onInteractOutside: interactChain,
|
||||
onAnimationend: onAnimationEnd,
|
||||
onOpenAutoFocus: (event: Event) => {
|
||||
if (event.defaultPrevented) return;
|
||||
if (contentAttrs.autoFocus.value === false)
|
||||
event.preventDefault();
|
||||
},
|
||||
|
||||
@@ -136,13 +136,13 @@ export default defineComponent({
|
||||
});
|
||||
|
||||
function shouldClose() {
|
||||
// Priority: item policy -> menu policy -> per-mode default.
|
||||
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;
|
||||
if (typeof menuPolicy === "boolean") return menuPolicy;
|
||||
// Default: action and radio items close, checkboxes stay open.
|
||||
return selectionMode.value !== "multiple";
|
||||
}
|
||||
|
||||
function activate() {
|
||||
|
||||
@@ -85,7 +85,19 @@ export default defineComponent({
|
||||
const attrs = useAttrs();
|
||||
const dropdownCtx = inject(dropdownContextKey, null);
|
||||
const outerClose = inject(menuCloseKey, null);
|
||||
const contentAttrs = inject(menuContentAttrsKey, null);
|
||||
// Outside a Dropdown surface the Menu owns its content, so it provides the
|
||||
// channel itself and keeps the name/description on the real menu node.
|
||||
const liveContentAttrs = inject(menuContentAttrsKey, null) ?? {
|
||||
label: ref<string | undefined>(),
|
||||
labelledBy: ref<string | undefined>(),
|
||||
describedBy: ref<string | undefined>(),
|
||||
loop: ref<boolean | undefined>(),
|
||||
autoFocus: ref<boolean | undefined>(),
|
||||
escapeKeyBehavior: ref<"clearSelection" | "none">("none"),
|
||||
clearSelection: () => {},
|
||||
};
|
||||
provide(menuContentAttrsKey, liveContentAttrs);
|
||||
const contentAttrs = liveContentAttrs;
|
||||
const outerMenuRoot = injectDropdownMenuRootContext(null);
|
||||
// Standalone Menu (no DropdownRoot/Popover) still supports items.
|
||||
const needsOwnRoot = computed(() => !outerMenuRoot);
|
||||
@@ -150,9 +162,9 @@ export default defineComponent({
|
||||
emitAction: (key: MenuKey) => emit("action", key),
|
||||
selection,
|
||||
selectionMode: computed(() => props.selectionMode),
|
||||
shouldCloseOnSelect: computed(
|
||||
() => props.shouldCloseOnSelect ?? props.selectionMode === "multiple",
|
||||
),
|
||||
// Unset stays undefined: the item resolves the per-mode default so an
|
||||
// explicit policy is never pre-empted by a guessed boolean.
|
||||
shouldCloseOnSelect: computed(() => props.shouldCloseOnSelect),
|
||||
itemCount,
|
||||
registerItem: () => {
|
||||
itemCount.value += 1;
|
||||
@@ -225,7 +237,9 @@ export default defineComponent({
|
||||
"data-slot": "dropdown-popover",
|
||||
sideOffset: 0,
|
||||
avoidCollisions: false,
|
||||
"aria-label": contentAttrs?.label.value,
|
||||
"aria-label": contentAttrs.label.value,
|
||||
"aria-labelledby": contentAttrs.labelledBy.value,
|
||||
"aria-describedby": contentAttrs.describedBy.value,
|
||||
"data-static": "true",
|
||||
// A static in-flow list never steals focus from the page.
|
||||
onOpenAutoFocus: (event: Event) => event.preventDefault(),
|
||||
|
||||
@@ -22,8 +22,14 @@ export interface MenuContextValue {
|
||||
selection: MenuSelectionOwner;
|
||||
/** Root selection mode for data-selection-mode styling. */
|
||||
selectionMode: ComputedRef<MenuSelectionMode>;
|
||||
/** Menu-level close policy for activation (official `shouldCloseOnSelect`). */
|
||||
shouldCloseOnSelect: ComputedRef<boolean | ((key: MenuKey) => boolean)>;
|
||||
/**
|
||||
* Menu-level close policy for activation (official `shouldCloseOnSelect`).
|
||||
* Undefined means "no explicit policy"; the item then applies the per-mode
|
||||
* default.
|
||||
*/
|
||||
shouldCloseOnSelect: ComputedRef<
|
||||
boolean | ((key: MenuKey) => boolean) | undefined
|
||||
>;
|
||||
/** Number of mounted items, used for the empty-state slot. */
|
||||
itemCount: Ref<number>;
|
||||
registerItem: () => () => void;
|
||||
|
||||
Reference in New Issue
Block a user