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:
@@ -1,12 +1,47 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h } from "vue";
|
||||
import { defineComponent, h, type PropType } from "vue";
|
||||
import MenuItem from "../menu-item/MenuItem.vue";
|
||||
import type { MenuItemVariants } from "@heroui-vue/styles";
|
||||
import type { MenuKey } from "../menu/context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DropdownItem",
|
||||
inheritAttrs: false,
|
||||
setup(_, { attrs, slots }) {
|
||||
return () => h(MenuItem as never, attrs, slots);
|
||||
props: {
|
||||
id: { type: [String, Number] as PropType<MenuKey>, default: undefined },
|
||||
textValue: { type: String, default: undefined },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
disabled: { type: Boolean, default: undefined },
|
||||
variant: {
|
||||
type: String as PropType<MenuItemVariants["variant"]>,
|
||||
default: "default",
|
||||
},
|
||||
closeOnSelect: { type: Boolean, default: undefined },
|
||||
hasSubmenu: { type: Boolean, default: undefined },
|
||||
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,
|
||||
},
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
return () =>
|
||||
h(
|
||||
MenuItem as never,
|
||||
{
|
||||
...props,
|
||||
...attrs,
|
||||
onSelect: (key: MenuKey) => emit("select", key),
|
||||
},
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -1,12 +1,55 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h } from "vue";
|
||||
import { defineComponent, h, type PropType } from "vue";
|
||||
import Menu from "../menu/Menu.vue";
|
||||
import type { MenuKey, MenuSelectionMode } from "../menu/context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DropdownMenu",
|
||||
inheritAttrs: false,
|
||||
setup(_, { attrs, slots }) {
|
||||
return () => h(Menu as never, attrs, slots);
|
||||
props: {
|
||||
selectionMode: {
|
||||
type: String as PropType<MenuSelectionMode>,
|
||||
default: "none",
|
||||
},
|
||||
selectedKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultSelectedKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
disabledKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
disallowEmptySelection: { type: Boolean, default: false },
|
||||
shouldCloseOnSelect: {
|
||||
type: [Boolean, Function] as PropType<
|
||||
boolean | ((key: MenuKey) => boolean)
|
||||
>,
|
||||
default: undefined,
|
||||
},
|
||||
},
|
||||
emits: {
|
||||
"update:selectedKeys": (_keys: MenuKey[]) => true,
|
||||
selectionChange: (_keys: MenuKey[]) => true,
|
||||
action: (_key: MenuKey) => true,
|
||||
},
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
return () =>
|
||||
h(
|
||||
Menu as never,
|
||||
{
|
||||
...props,
|
||||
...attrs,
|
||||
"onUpdate:selectedKeys": (keys: MenuKey[]) =>
|
||||
emit("update:selectedKeys", keys),
|
||||
onSelectionChange: (keys: MenuKey[]) => emit("selectionChange", keys),
|
||||
onAction: (key: MenuKey) => emit("action", key),
|
||||
},
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -8,20 +8,26 @@ import {
|
||||
provide,
|
||||
ref,
|
||||
useAttrs,
|
||||
watchEffect,
|
||||
type PropType,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
import {
|
||||
DropdownMenuContent as RekaDropdownMenuContent,
|
||||
DropdownMenuPortal as RekaDropdownMenuPortal,
|
||||
injectDropdownMenuRootContext,
|
||||
} from "reka-ui";
|
||||
import { dropdownVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
dropdownContextKey,
|
||||
menuCloseKey,
|
||||
menuContentAttrsKey,
|
||||
submenuContentKey,
|
||||
} from "../menu/context";
|
||||
import { useOverlayAnimation } from "../overlay/animation";
|
||||
import { resolvePlacement, type OverlayPlacement } from "../overlay/placement";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import { surfaceContextKey } from "../surface/context";
|
||||
import { menuSurfaceFromVNode } from "./dom";
|
||||
|
||||
/**
|
||||
@@ -32,9 +38,35 @@ export default defineComponent({
|
||||
name: "DropdownPopover",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
/** Placement relative to the trigger (official prop name). */
|
||||
placement: {
|
||||
type: String as PropType<OverlayPlacement>,
|
||||
default: "bottom",
|
||||
},
|
||||
/** Distance from the trigger (official `offset`). */
|
||||
offset: { type: Number, default: 0 },
|
||||
/** Cross-axis distance (official `crossOffset`). */
|
||||
crossOffset: { type: Number, default: 0 },
|
||||
/** Flip on collision (official `shouldFlip`). */
|
||||
shouldFlip: { type: Boolean, default: true },
|
||||
/** Viewport padding kept free (official `containerPadding`). */
|
||||
containerPadding: { type: Number, default: 12 },
|
||||
/** Ignore Escape while true (official `isKeyboardDismissDisabled`). */
|
||||
isKeyboardDismissDisabled: { type: Boolean, default: false },
|
||||
/** Return false to keep the menu open for an outside interaction. */
|
||||
shouldCloseOnInteractOutside: {
|
||||
type: Function as PropType<(event: Event) => boolean>,
|
||||
default: undefined,
|
||||
},
|
||||
/** Portal target; defaults to `document.body`. */
|
||||
to: {
|
||||
type: [String, Object] as PropType<string | HTMLElement>,
|
||||
default: undefined,
|
||||
},
|
||||
// Low-level positioning overrides, kept for direct control.
|
||||
side: { type: String as PropType<string>, default: undefined },
|
||||
sideOffset: { type: Number, default: undefined },
|
||||
align: { type: String as PropType<string>, default: undefined },
|
||||
sideOffset: { type: Number, default: undefined },
|
||||
alignOffset: { type: Number, default: undefined },
|
||||
avoidCollisions: { type: Boolean, default: undefined },
|
||||
collisionPadding: {
|
||||
@@ -48,11 +80,30 @@ export default defineComponent({
|
||||
const dropdownCtx = inject(dropdownContextKey, null);
|
||||
const inSubmenu = inject(submenuContentKey, false);
|
||||
const rootCtx = injectDropdownMenuRootContext(null);
|
||||
const config = useConfig();
|
||||
|
||||
provide(menuCloseKey, () => {
|
||||
rootCtx?.onOpenChange(false);
|
||||
});
|
||||
|
||||
// Surface resets to the overlay default, like the fixed upstream popover,
|
||||
// so nested fields do not inherit a surrounding surface variant.
|
||||
provide(surfaceContextKey, { variant: computed(() => "default" as const) });
|
||||
|
||||
// Label/description attributes belong on the real `role="menu"` element.
|
||||
const contentAttrs = inSubmenu
|
||||
? (inject(menuContentAttrsKey, null) ?? 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: () => {},
|
||||
};
|
||||
if (!inSubmenu && contentAttrs) provide(menuContentAttrsKey, contentAttrs);
|
||||
|
||||
const contentElement = ref<HTMLElement | null>(null);
|
||||
const isOpen = computed(() => rootCtx?.open.value ?? false);
|
||||
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
||||
@@ -61,40 +112,123 @@ export default defineComponent({
|
||||
true,
|
||||
);
|
||||
|
||||
const position = computed(() =>
|
||||
resolvePlacement(props.placement, config?.dir.value ?? "ltr"),
|
||||
);
|
||||
const resolvedSide = computed(() => props.side ?? position.value.side);
|
||||
const resolvedAlign = computed(() => props.align ?? position.value.align);
|
||||
|
||||
function escape(event: KeyboardEvent) {
|
||||
if (props.isKeyboardDismissDisabled) event.preventDefault();
|
||||
}
|
||||
/**
|
||||
* `escapeKeyBehavior="clearSelection"` clears the selection and keeps the
|
||||
* overlay open; the dismissable layer respects `defaultPrevented`.
|
||||
*/
|
||||
function onSurfaceKeydown(event: KeyboardEvent) {
|
||||
if (event.defaultPrevented) return;
|
||||
if (event.key !== "Escape") return;
|
||||
if (contentAttrs?.escapeKeyBehavior.value !== "clearSelection") return;
|
||||
event.preventDefault();
|
||||
contentAttrs.clearSelection();
|
||||
}
|
||||
function interactOutside(event: {
|
||||
defaultPrevented?: boolean;
|
||||
preventDefault?: () => void;
|
||||
}) {
|
||||
if (
|
||||
props.shouldCloseOnInteractOutside?.(event as unknown as Event) ===
|
||||
false
|
||||
)
|
||||
event.preventDefault?.();
|
||||
}
|
||||
|
||||
watchEffect(() => {
|
||||
if (inSubmenu || !contentAttrs) return;
|
||||
contentAttrs.label.value = attrs["aria-label"] as string | undefined;
|
||||
contentAttrs.labelledBy.value = attrs["aria-labelledby"] as
|
||||
string | undefined;
|
||||
contentAttrs.describedBy.value = attrs["aria-describedby"] as
|
||||
string | undefined;
|
||||
});
|
||||
|
||||
function labelProps(): Record<string, unknown> {
|
||||
const out: Record<string, unknown> = {};
|
||||
const label = (contentAttrs?.label.value ??
|
||||
(attrs["aria-label"] as string | undefined)) as string | undefined;
|
||||
const labelledBy = (contentAttrs?.labelledBy.value ??
|
||||
(attrs["aria-labelledby"] as string | undefined)) as string | undefined;
|
||||
const describedBy = (contentAttrs?.describedBy.value ??
|
||||
(attrs["aria-describedby"] as string | undefined)) as
|
||||
string | undefined;
|
||||
if (label) {
|
||||
out["aria-label"] = label;
|
||||
// aria-labelledby would win over aria-label; drop the trigger default.
|
||||
out["aria-labelledby"] = labelledBy;
|
||||
} else if (labelledBy) {
|
||||
out["aria-labelledby"] = labelledBy;
|
||||
}
|
||||
if (describedBy) out["aria-describedby"] = describedBy;
|
||||
return out;
|
||||
}
|
||||
|
||||
return () => {
|
||||
const scope = {
|
||||
isEntering: entering.value,
|
||||
isExiting: exiting.value,
|
||||
placement: props.placement,
|
||||
};
|
||||
if (inSubmenu) {
|
||||
// Reka SubContent already positions and dismisses this level;
|
||||
// the submenu trigger forwards attrs onto that element.
|
||||
return h(Fragment, null, slots.default?.() as never);
|
||||
return h(Fragment, null, slots.default?.(scope) as never);
|
||||
}
|
||||
return h(
|
||||
RekaDropdownMenuContent as never,
|
||||
RekaDropdownMenuPortal as never,
|
||||
{ to: props.to },
|
||||
{
|
||||
...attrs,
|
||||
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,
|
||||
alignOffset: props.alignOffset,
|
||||
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;
|
||||
},
|
||||
default: () =>
|
||||
h(
|
||||
RekaDropdownMenuContent as never,
|
||||
{
|
||||
...attrs,
|
||||
...labelProps(),
|
||||
class: dropdownCtx?.slots.value.popover({
|
||||
class: attrs.class as never,
|
||||
}),
|
||||
"data-slot": "dropdown-popover",
|
||||
"data-entering": entering.value || undefined,
|
||||
"data-exiting": exiting.value || undefined,
|
||||
side: resolvedSide.value,
|
||||
align: resolvedAlign.value,
|
||||
sideOffset: props.sideOffset ?? props.offset,
|
||||
alignOffset: props.alignOffset ?? props.crossOffset,
|
||||
avoidCollisions: props.avoidCollisions ?? props.shouldFlip,
|
||||
collisionPadding:
|
||||
props.collisionPadding ?? props.containerPadding,
|
||||
loop: props.loop ?? contentAttrs?.loop.value,
|
||||
onAnimationend: onAnimationEnd,
|
||||
onOpenAutoFocus: (event: Event) => {
|
||||
if (contentAttrs?.autoFocus.value === false)
|
||||
event.preventDefault();
|
||||
},
|
||||
onEscapeKeyDown: escape,
|
||||
onInteractOutside: interactOutside,
|
||||
onKeydown: onSurfaceKeydown,
|
||||
onVnodeMounted: (vnode) => {
|
||||
contentElement.value = menuSurfaceFromVNode(vnode);
|
||||
},
|
||||
onVnodeUpdated: (vnode) => {
|
||||
const element = menuSurfaceFromVNode(vnode);
|
||||
if (element) contentElement.value = element;
|
||||
},
|
||||
onVnodeUnmounted: () => {
|
||||
contentElement.value = null;
|
||||
},
|
||||
},
|
||||
{ default: () => slots.default?.(scope) },
|
||||
),
|
||||
},
|
||||
slots,
|
||||
);
|
||||
};
|
||||
},
|
||||
|
||||
@@ -42,7 +42,10 @@ export default defineComponent({
|
||||
const isOpen = computed(() =>
|
||||
props.open !== undefined ? props.open : uncontrolledOpen.value,
|
||||
);
|
||||
// The trigger suppresses the base trigger's own toggle while it decides.
|
||||
const suppressOpenRequests = ref(false);
|
||||
function setOpen(value: boolean) {
|
||||
if (suppressOpenRequests.value) return;
|
||||
if (props.open === undefined) uncontrolledOpen.value = value;
|
||||
emit("update:open", value);
|
||||
}
|
||||
@@ -53,6 +56,7 @@ export default defineComponent({
|
||||
longPressDuration: computed(() => props.longPressDuration),
|
||||
open: isOpen,
|
||||
setOpen,
|
||||
suppressOpenRequests,
|
||||
});
|
||||
|
||||
return () =>
|
||||
|
||||
@@ -1,12 +1,47 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h } from "vue";
|
||||
import { defineComponent, h, type PropType } from "vue";
|
||||
import MenuSection from "../menu-section/MenuSection.vue";
|
||||
import type { MenuKey, MenuSelectionMode } from "../menu/context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DropdownSection",
|
||||
inheritAttrs: false,
|
||||
setup(_, { attrs, slots }) {
|
||||
return () => h(MenuSection as never, attrs, slots);
|
||||
props: {
|
||||
selectionMode: {
|
||||
type: String as PropType<MenuSelectionMode>,
|
||||
default: undefined,
|
||||
},
|
||||
selectedKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultSelectedKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
disabledKeys: {
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
disallowEmptySelection: { type: Boolean, default: false },
|
||||
},
|
||||
emits: {
|
||||
"update:selectedKeys": (_keys: MenuKey[]) => true,
|
||||
selectionChange: (_keys: MenuKey[]) => true,
|
||||
},
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
return () =>
|
||||
h(
|
||||
MenuSection as never,
|
||||
{
|
||||
...props,
|
||||
...attrs,
|
||||
onUpdateSelectedKeys: (keys: MenuKey[]) =>
|
||||
emit("update:selectedKeys", keys),
|
||||
onSelectionChange: (keys: MenuKey[]) => emit("selectionChange", keys),
|
||||
},
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
import {
|
||||
Fragment,
|
||||
cloneVNode,
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
@@ -14,20 +13,50 @@ import {
|
||||
useAttrs,
|
||||
useId,
|
||||
watch,
|
||||
type PropType,
|
||||
type Ref,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import {
|
||||
DropdownMenuPortal as RekaDropdownMenuPortal,
|
||||
DropdownMenuSub as RekaDropdownMenuSub,
|
||||
DropdownMenuSubContent as RekaDropdownMenuSubContent,
|
||||
DropdownMenuSubTrigger as RekaDropdownMenuSubTrigger,
|
||||
} from "reka-ui";
|
||||
import { dropdownVariants } from "@heroui-vue/styles";
|
||||
import { dropdownContextKey, submenuContentKey } from "../menu/context";
|
||||
import {
|
||||
dropdownContextKey,
|
||||
menuContentAttrsKey,
|
||||
submenuContentKey,
|
||||
submenuOpenKey,
|
||||
} from "../menu/context";
|
||||
import { useOverlayAnimation } from "../overlay/animation";
|
||||
import { resolvePlacement, type OverlayPlacement } from "../overlay/placement";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import { elementFromValue, menuSurfaceFromVNode } from "./dom";
|
||||
import DropdownPopover from "./DropdownPopover.vue";
|
||||
|
||||
const PANEL_PROPS = [
|
||||
"placement",
|
||||
"offset",
|
||||
"crossOffset",
|
||||
"shouldFlip",
|
||||
"containerPadding",
|
||||
"side",
|
||||
"align",
|
||||
"sideOffset",
|
||||
"alignOffset",
|
||||
"avoidCollisions",
|
||||
"collisionPadding",
|
||||
"loop",
|
||||
"to",
|
||||
"isKeyboardDismissDisabled",
|
||||
"shouldCloseOnInteractOutside",
|
||||
] as const;
|
||||
|
||||
type Handler =
|
||||
((event: Event) => void) | ((event: Event) => void)[] | undefined;
|
||||
|
||||
function flatten(nodes: VNodeChild[], out: VNodeChild[] = []): VNodeChild[] {
|
||||
for (const n of nodes) {
|
||||
if (Array.isArray(n)) flatten(n, out);
|
||||
@@ -36,8 +65,13 @@ function flatten(nodes: VNodeChild[], out: VNodeChild[] = []): VNodeChild[] {
|
||||
return out;
|
||||
}
|
||||
|
||||
function asArray(handler: Handler): Array<(event: Event) => void> {
|
||||
if (!handler) return [];
|
||||
return Array.isArray(handler) ? handler : [handler];
|
||||
}
|
||||
|
||||
/** Attributes a nested `<DropdownPopover>` may pass to the submenu surface. */
|
||||
function inheritableAttrs(props: Record<string, unknown> | null) {
|
||||
function inheritedProps(props: Record<string, unknown> | null) {
|
||||
const out: Record<string, unknown> = {};
|
||||
if (!props) return out;
|
||||
for (const [key, value] of Object.entries(props)) {
|
||||
@@ -49,7 +83,8 @@ function inheritableAttrs(props: Record<string, unknown> | null) {
|
||||
key === "role" ||
|
||||
key.startsWith("data-") ||
|
||||
key.startsWith("aria-") ||
|
||||
/^on[A-Z]/.test(key)
|
||||
/^on[A-Z]/.test(key) ||
|
||||
(PANEL_PROPS as readonly string[]).includes(key)
|
||||
)
|
||||
out[key] = value;
|
||||
}
|
||||
@@ -68,12 +103,24 @@ export default defineComponent({
|
||||
setup(_, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const dropdownCtx = inject(dropdownContextKey, null);
|
||||
const config = useConfig();
|
||||
provide(submenuContentKey, true);
|
||||
|
||||
const subOpen = ref(false);
|
||||
const triggerRef = ref<unknown>(null);
|
||||
const contentElement = ref<HTMLElement | null>(null);
|
||||
const fallbackId = useId();
|
||||
const contentAttrs = {
|
||||
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, contentAttrs);
|
||||
provide(submenuOpenKey, subOpen);
|
||||
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
||||
subOpen as Ref<boolean>,
|
||||
contentElement,
|
||||
@@ -82,20 +129,17 @@ export default defineComponent({
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* trigger, but our trigger item keeps an instance id, so that reference
|
||||
* dangles. Point it at the real trigger element of this instance.
|
||||
*/
|
||||
function repairLabel() {
|
||||
function repairLabel(explicit?: string) {
|
||||
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 (explicit) {
|
||||
content.setAttribute("aria-labelledby", explicit);
|
||||
return;
|
||||
}
|
||||
if (!trigger.id) trigger.id = fallbackId;
|
||||
content.setAttribute("aria-labelledby", trigger.id);
|
||||
}
|
||||
@@ -109,6 +153,15 @@ export default defineComponent({
|
||||
onMounted(repairLabel as () => void);
|
||||
|
||||
function handleEscape(event: KeyboardEvent) {
|
||||
if (event.defaultPrevented) return;
|
||||
if (
|
||||
event.key === "Escape" &&
|
||||
contentAttrs.escapeKeyBehavior.value === "clearSelection"
|
||||
) {
|
||||
event.preventDefault();
|
||||
contentAttrs.clearSelection();
|
||||
return;
|
||||
}
|
||||
if (event.key !== "Escape" || !subOpen.value) return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
// Only the menu level that owns the focused element closes.
|
||||
@@ -129,11 +182,46 @@ export default defineComponent({
|
||||
const popoverVNode = contentVNodes.find(
|
||||
(node) => isVNode(node) && node.type === DropdownPopover,
|
||||
);
|
||||
const popoverAttrs = inheritableAttrs(
|
||||
const popoverProps = inheritedProps(
|
||||
isVNode(popoverVNode)
|
||||
? (popoverVNode.props as Record<string, unknown> | null)
|
||||
: null,
|
||||
);
|
||||
const {
|
||||
class: popoverClass,
|
||||
onKeydown: popoverKeydown,
|
||||
...restProps
|
||||
} = popoverProps;
|
||||
|
||||
const placement = (restProps.placement as OverlayPlacement) ?? "end";
|
||||
const position = resolvePlacement(placement, config?.dir.value ?? "ltr");
|
||||
|
||||
// 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),
|
||||
alignOffset:
|
||||
(restProps.alignOffset as number | undefined) ??
|
||||
(restProps.crossOffset as number | undefined),
|
||||
avoidCollisions:
|
||||
(restProps.avoidCollisions as boolean | undefined) ??
|
||||
(restProps.shouldFlip as boolean | undefined),
|
||||
collisionPadding:
|
||||
(restProps.collisionPadding as number | undefined) ??
|
||||
(restProps.containerPadding as number | undefined),
|
||||
loop: restProps.loop as boolean | undefined,
|
||||
};
|
||||
const side = (restProps.side as string | undefined) ?? position.side;
|
||||
const align = (restProps.align as string | undefined) ?? position.align;
|
||||
|
||||
const keydownChain = [
|
||||
...asArray(popoverKeydown as Handler),
|
||||
...asArray(attrs.onKeydown as Handler),
|
||||
handleEscape,
|
||||
];
|
||||
const labelledBy = contentAttrs.labelledBy.value;
|
||||
|
||||
return h(
|
||||
RekaDropdownMenuSub as never,
|
||||
@@ -169,39 +257,55 @@ export default defineComponent({
|
||||
},
|
||||
),
|
||||
h(
|
||||
RekaDropdownMenuSubContent as never,
|
||||
{
|
||||
...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;
|
||||
},
|
||||
},
|
||||
RekaDropdownMenuPortal as never,
|
||||
{ to: restProps.to as string | HTMLElement | undefined },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
Fragment,
|
||||
null,
|
||||
// Nested DropdownPopover is a style shell inside SubContent.
|
||||
contentVNodes as never,
|
||||
RekaDropdownMenuSubContent as never,
|
||||
{
|
||||
...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],
|
||||
"data-slot": "dropdown-popover",
|
||||
"data-submenu-content": "true",
|
||||
"data-entering": entering.value || undefined,
|
||||
"data-exiting": exiting.value || undefined,
|
||||
onKeydown: keydownChain,
|
||||
onAnimationend: onAnimationEnd,
|
||||
onOpenAutoFocus: (event: Event) => {
|
||||
if (contentAttrs.autoFocus.value === false)
|
||||
event.preventDefault();
|
||||
},
|
||||
onVnodeMounted: (vnode) => {
|
||||
contentElement.value = menuSurfaceFromVNode(vnode);
|
||||
void nextTick(() => repairLabel(labelledBy));
|
||||
},
|
||||
onVnodeUpdated: (vnode) => {
|
||||
const element = menuSurfaceFromVNode(vnode);
|
||||
if (!element) return;
|
||||
contentElement.value = element;
|
||||
void nextTick(() => repairLabel(labelledBy));
|
||||
},
|
||||
onVnodeUnmounted: () => {
|
||||
contentElement.value = null;
|
||||
},
|
||||
},
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
Fragment,
|
||||
null,
|
||||
// Nested DropdownPopover is a style shell inside SubContent.
|
||||
contentVNodes as never,
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
|
||||
@@ -17,6 +17,11 @@ const LONG_PRESS_MOVE_PX = 10;
|
||||
|
||||
const SELECTION_KEYS = new Set(["Enter", " ", "Spacebar"]);
|
||||
|
||||
type Handler =
|
||||
| ((event: KeyboardEvent) => void)
|
||||
| ((event: KeyboardEvent) => void)
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* Trigger. In `longPress` mode this component owns the press lifecycle and
|
||||
* cancels Reka's own click/keydown activation through its documented
|
||||
@@ -79,11 +84,37 @@ export default defineComponent({
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
function onKeydown(event: KeyboardEvent) {
|
||||
/**
|
||||
* Long-press keyboard activation. The base trigger toggles on keydown, so
|
||||
* for Enter/Space the caller's own keydown handlers run first (on the
|
||||
* capture phase of the trigger region, with native Vue array semantics),
|
||||
* then this component either cancels or opens exactly once while the base
|
||||
* trigger's toggle is suspended — a cancelled keydown never opens the menu.
|
||||
*/
|
||||
function suspendBaseToggle() {
|
||||
const ctx = dropdownCtx;
|
||||
if (!ctx) return;
|
||||
ctx.suppressOpenRequests.value = true;
|
||||
setTimeout(() => {
|
||||
ctx.suppressOpenRequests.value = false;
|
||||
}, 0);
|
||||
}
|
||||
function decideKeydown(event: KeyboardEvent) {
|
||||
if (!isLongPress()) return;
|
||||
if (!SELECTION_KEYS.has(event.key)) return;
|
||||
if (event.defaultPrevented) return;
|
||||
event.preventDefault();
|
||||
dropdownCtx?.setOpen(!(dropdownCtx?.open.value ?? false));
|
||||
const ctx = dropdownCtx;
|
||||
// Open first, then suspend: the base trigger toggles later in the same
|
||||
// event and must not undo this decision.
|
||||
ctx?.setOpen(!(ctx.open.value ?? false));
|
||||
suspendBaseToggle();
|
||||
}
|
||||
function keydownChain() {
|
||||
if (!isLongPress()) return undefined;
|
||||
const user = (attrs as { onKeydown?: Handler }).onKeydown;
|
||||
const userHandlers = Array.isArray(user) ? user : user ? [user] : [];
|
||||
return [...userHandlers, decideKeydown];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,15 +151,18 @@ export default defineComponent({
|
||||
onPointercancelCapture: clearPress,
|
||||
onPointerleaveCapture: clearPress,
|
||||
onClickCapture: onActivation,
|
||||
onKeydownCapture: onKeydown,
|
||||
onKeydownCapture: keydownChain(),
|
||||
onContextmenuCapture: clearPress,
|
||||
};
|
||||
|
||||
return () => {
|
||||
const longPress = isLongPress();
|
||||
const triggerAttrs: Record<string, unknown> = { ...attrs };
|
||||
if (longPress) delete triggerAttrs.onKeydown;
|
||||
const trigger = h(
|
||||
RekaDropdownMenuTrigger as never,
|
||||
{
|
||||
...attrs,
|
||||
...triggerAttrs,
|
||||
ref: triggerRef,
|
||||
as: props.as,
|
||||
asChild: props.asChild ?? true,
|
||||
@@ -137,10 +171,15 @@ export default defineComponent({
|
||||
}),
|
||||
"data-slot": "dropdown-trigger",
|
||||
},
|
||||
slots,
|
||||
{
|
||||
default: () =>
|
||||
slots.default?.({
|
||||
isOpen: dropdownCtx?.open.value ?? false,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!isLongPress()) return trigger;
|
||||
if (!longPress) return trigger;
|
||||
|
||||
return h(
|
||||
"span",
|
||||
|
||||
Reference in New Issue
Block a user