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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -42,6 +42,8 @@ export default defineComponent({
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
/** Clear the last selection when the user deselects it. */
|
||||
disallowEmptySelection: { type: Boolean, default: false },
|
||||
},
|
||||
emits: {
|
||||
"update:selectedKeys": (_keys: MenuKey[]) => true,
|
||||
@@ -68,6 +70,7 @@ export default defineComponent({
|
||||
selectionMode: effectiveMode,
|
||||
selectedKeys: acceptedKeys,
|
||||
disabledKeys: computed(() => props.disabledKeys ?? []),
|
||||
disallowEmptySelection: computed(() => props.disallowEmptySelection),
|
||||
onSelectionChange(next) {
|
||||
if (!isControlled.value) internal.value = next;
|
||||
emit("update:selectedKeys", next);
|
||||
@@ -121,11 +124,11 @@ export default defineComponent({
|
||||
? sectionSelection
|
||||
: (menuCtx?.selection ?? sectionSelection)
|
||||
).isItemSelected(key),
|
||||
// Disabled keys are additive: the section list never overrides the
|
||||
// menu list, whichever side owns the selection.
|
||||
isItemDisabled: (key: MenuKey) =>
|
||||
(ownsSelection.value
|
||||
? sectionSelection
|
||||
: (menuCtx?.selection ?? sectionSelection)
|
||||
).isItemDisabled(key),
|
||||
(props.disabledKeys ?? []).some((item) => item === key) ||
|
||||
(menuCtx?.selection.isItemDisabled(key) ?? false),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -8,10 +8,12 @@ import {
|
||||
ref,
|
||||
useAttrs,
|
||||
watch,
|
||||
watchEffect,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import {
|
||||
DropdownMenuContent as RekaDropdownMenuContent,
|
||||
DropdownMenuPortal as RekaDropdownMenuPortal,
|
||||
DropdownMenuRoot as RekaDropdownMenuRoot,
|
||||
injectDropdownMenuRootContext,
|
||||
} from "reka-ui";
|
||||
@@ -19,7 +21,10 @@ import { dropdownVariants, menuVariants } from "@heroui-vue/styles";
|
||||
import {
|
||||
dropdownContextKey,
|
||||
menuCloseKey,
|
||||
menuContentAttrsKey,
|
||||
menuContextKey,
|
||||
menuSectionContextKey,
|
||||
submenuOpenKey,
|
||||
type MenuKey,
|
||||
type MenuSelectionMode,
|
||||
} from "./context";
|
||||
@@ -27,7 +32,7 @@ import { useMenuSelection } from "./useMenuSelection";
|
||||
|
||||
/**
|
||||
* Menu list: selection/action context + menu styles.
|
||||
* Standalone use provides its own Reka menu root/content; inside
|
||||
* Standalone use renders the same list without a modal overlay; inside
|
||||
* DropdownPopover the surrounding Reka content is reused.
|
||||
*/
|
||||
export default defineComponent({
|
||||
@@ -50,18 +55,39 @@ export default defineComponent({
|
||||
type: Array as PropType<ReadonlyArray<MenuKey>>,
|
||||
default: () => [],
|
||||
},
|
||||
/** Clear the last selection when the user deselects it. */
|
||||
disallowEmptySelection: { type: Boolean, default: false },
|
||||
/** Wrap keyboard focus at the ends (official `shouldFocusWrap`). */
|
||||
shouldFocusWrap: { type: Boolean, default: undefined },
|
||||
/** Focus the menu when it opens (official `autoFocus`). */
|
||||
autoFocus: { type: Boolean, default: true },
|
||||
/** Escape clears the selection instead of closing (official name). */
|
||||
escapeKeyBehavior: {
|
||||
type: String as PropType<"clearSelection" | "none">,
|
||||
default: "none",
|
||||
},
|
||||
/** Menu-level close policy for activation (official prop name). */
|
||||
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,
|
||||
/** Fired when this menu level closes (official `onClose`). */
|
||||
close: () => true,
|
||||
},
|
||||
setup(props, { slots, emit }) {
|
||||
const attrs = useAttrs();
|
||||
const dropdownCtx = inject(dropdownContextKey, null);
|
||||
const outerClose = inject(menuCloseKey, null);
|
||||
const contentAttrs = inject(menuContentAttrsKey, null);
|
||||
const outerMenuRoot = injectDropdownMenuRootContext(null);
|
||||
// Standalone Menu (no DropdownRoot/Popover) must still support items.
|
||||
// Standalone Menu (no DropdownRoot/Popover) still supports items.
|
||||
const needsOwnRoot = computed(() => !outerMenuRoot);
|
||||
|
||||
const internal = ref<MenuKey[]>([...(props.defaultSelectedKeys ?? [])]);
|
||||
@@ -74,6 +100,7 @@ export default defineComponent({
|
||||
selectionMode: computed(() => props.selectionMode),
|
||||
selectedKeys: acceptedKeys,
|
||||
disabledKeys: computed(() => props.disabledKeys ?? []),
|
||||
disallowEmptySelection: computed(() => props.disallowEmptySelection),
|
||||
onSelectionChange(next) {
|
||||
if (!isControlled.value) internal.value = next;
|
||||
emit("update:selectedKeys", next);
|
||||
@@ -88,6 +115,33 @@ export default defineComponent({
|
||||
},
|
||||
);
|
||||
|
||||
function clearSelection() {
|
||||
if (props.disallowEmptySelection) return;
|
||||
if (acceptedKeys.value.length === 0) return;
|
||||
if (!isControlled.value) internal.value = [];
|
||||
emit("update:selectedKeys", []);
|
||||
emit("selectionChange", []);
|
||||
}
|
||||
|
||||
// Label/description/focus attributes for the surrounding surface.
|
||||
watchEffect(() => {
|
||||
if (!contentAttrs) return;
|
||||
contentAttrs.loop.value = props.shouldFocusWrap;
|
||||
contentAttrs.autoFocus.value = props.autoFocus;
|
||||
contentAttrs.escapeKeyBehavior.value = props.escapeKeyBehavior;
|
||||
});
|
||||
if (contentAttrs) contentAttrs.clearSelection = clearSelection;
|
||||
|
||||
// `close` fires when this menu level leaves the open state, whatever the
|
||||
// cause (item activation, Escape, outside interaction).
|
||||
const submenuOpen = inject(submenuOpenKey, null);
|
||||
const levelOpen = submenuOpen ?? dropdownCtx?.open ?? null;
|
||||
if (levelOpen)
|
||||
watch(levelOpen, (value, previous) => {
|
||||
if (previous && !value) emit("close");
|
||||
});
|
||||
|
||||
const itemCount = ref(0);
|
||||
provide(menuContextKey, {
|
||||
closeLevel: () => {
|
||||
if (outerClose) outerClose();
|
||||
@@ -96,46 +150,89 @@ export default defineComponent({
|
||||
emitAction: (key: MenuKey) => emit("action", key),
|
||||
selection,
|
||||
selectionMode: computed(() => props.selectionMode),
|
||||
shouldCloseOnSelect: computed(
|
||||
() => props.shouldCloseOnSelect ?? props.selectionMode === "multiple",
|
||||
),
|
||||
itemCount,
|
||||
registerItem: () => {
|
||||
itemCount.value += 1;
|
||||
return () => {
|
||||
itemCount.value = Math.max(0, itemCount.value - 1);
|
||||
};
|
||||
},
|
||||
});
|
||||
// A Menu opened inside a MenuSection owns its own scope; without this a
|
||||
// nested list would keep the outer section's selection owner.
|
||||
provide(menuSectionContextKey, null);
|
||||
|
||||
// The accessible name belongs on the real `role="menu"` node, which lives
|
||||
// on the surrounding content element rather than on this list.
|
||||
watchEffect(() => {
|
||||
if (!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;
|
||||
});
|
||||
|
||||
const listNode = () => {
|
||||
// The surrounding Reka content already renders `role="menu"`; upstream
|
||||
// also styles the list itself, so it stays a plain element here.
|
||||
function listProps() {
|
||||
const userRole = attrs.role as string | undefined;
|
||||
const rest: Record<string, unknown> = { ...attrs };
|
||||
delete rest.role;
|
||||
return h(
|
||||
"div",
|
||||
{
|
||||
...rest,
|
||||
...(userRole ? { role: userRole } : {}),
|
||||
class: menuVariants({
|
||||
class: [dropdownCtx?.slots.value.menu(), attrs.class],
|
||||
}),
|
||||
"data-selection-mode": props.selectionMode,
|
||||
"data-slot": dropdownCtx ? "dropdown-menu" : "menu",
|
||||
},
|
||||
slots,
|
||||
);
|
||||
};
|
||||
for (const key of [
|
||||
"role",
|
||||
"aria-label",
|
||||
"aria-labelledby",
|
||||
"aria-describedby",
|
||||
])
|
||||
delete rest[key];
|
||||
return {
|
||||
...rest,
|
||||
...(userRole ? { role: userRole } : {}),
|
||||
class: menuVariants({
|
||||
class: [dropdownCtx?.slots.value.menu(), attrs.class],
|
||||
}),
|
||||
"data-selection-mode": props.selectionMode,
|
||||
"data-slot": dropdownCtx ? "dropdown-menu" : "menu",
|
||||
};
|
||||
}
|
||||
|
||||
const listNode = () =>
|
||||
h("div", listProps(), [
|
||||
slots.default?.(),
|
||||
itemCount.value === 0 ? slots.empty?.() : undefined,
|
||||
]);
|
||||
|
||||
return () => {
|
||||
if (!needsOwnRoot.value) return listNode();
|
||||
// Public standalone Menu: own Reka root + content so items get menu
|
||||
// semantics and keyboard navigation without private context.
|
||||
// Standalone Menu: keep the base menu semantics and keyboard model, but
|
||||
// without modal behaviour (no scroll lock, no outside pointer blocking)
|
||||
// and without stealing focus.
|
||||
return h(
|
||||
RekaDropdownMenuRoot as never,
|
||||
{ open: true },
|
||||
{ open: true, modal: false },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
RekaDropdownMenuContent as never,
|
||||
RekaDropdownMenuPortal as never,
|
||||
{ disabled: true },
|
||||
{
|
||||
class: dropdownVariants().popover(),
|
||||
"data-slot": "dropdown-popover",
|
||||
sideOffset: 0,
|
||||
default: () =>
|
||||
h(
|
||||
RekaDropdownMenuContent as never,
|
||||
{
|
||||
class: dropdownVariants().popover(),
|
||||
"data-slot": "dropdown-popover",
|
||||
sideOffset: 0,
|
||||
avoidCollisions: false,
|
||||
"aria-label": contentAttrs?.label.value,
|
||||
"data-static": "true",
|
||||
// A static in-flow list never steals focus from the page.
|
||||
onOpenAutoFocus: (event: Event) => event.preventDefault(),
|
||||
},
|
||||
{ default: listNode },
|
||||
),
|
||||
},
|
||||
{ default: listNode },
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -22,6 +22,11 @@ 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)>;
|
||||
/** Number of mounted items, used for the empty-state slot. */
|
||||
itemCount: Ref<number>;
|
||||
registerItem: () => () => void;
|
||||
}
|
||||
|
||||
export const menuContextKey: InjectionKey<MenuContextValue> = Symbol("hv-menu");
|
||||
@@ -30,7 +35,8 @@ export interface MenuSectionContextValue {
|
||||
selection: MenuSelectionOwner;
|
||||
}
|
||||
|
||||
export const menuSectionContextKey: InjectionKey<MenuSectionContextValue> =
|
||||
/** `null` resets the owner so a nested Menu does not inherit a section. */
|
||||
export const menuSectionContextKey: InjectionKey<MenuSectionContextValue | null> =
|
||||
Symbol("hv-menu-section");
|
||||
|
||||
/** MenuSection exposes this so a nested Header can name the group. */
|
||||
@@ -43,6 +49,25 @@ export const menuSectionLabelKey: InjectionKey<MenuSectionLabelValue> = Symbol(
|
||||
"hv-menu-section-label",
|
||||
);
|
||||
|
||||
/** Label/description attributes that belong on the real `role="menu"` node. */
|
||||
export interface MenuContentAttrsValue {
|
||||
label: Ref<string | undefined>;
|
||||
labelledBy: Ref<string | undefined>;
|
||||
describedBy: Ref<string | undefined>;
|
||||
/** Focus wrap while navigating with the keyboard (official `shouldFocusWrap`). */
|
||||
loop: Ref<boolean | undefined>;
|
||||
/** Focus the menu when it opens (official `autoFocus`). */
|
||||
autoFocus: Ref<boolean | undefined>;
|
||||
/** Escape behaviour of the owning menu (official `escapeKeyBehavior`). */
|
||||
escapeKeyBehavior: Ref<"clearSelection" | "none">;
|
||||
/** Clear the selection for the owning menu (no-op when disallowed). */
|
||||
clearSelection: () => void;
|
||||
}
|
||||
|
||||
export const menuContentAttrsKey: InjectionKey<MenuContentAttrsValue> = Symbol(
|
||||
"hv-menu-content-attrs",
|
||||
);
|
||||
|
||||
export interface MenuItemContextValue {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
slots: ComputedRef<any>;
|
||||
@@ -60,6 +85,11 @@ export const menuItemContextKey: InjectionKey<MenuItemContextValue> =
|
||||
/** Provided by DropdownPopover / submenu content to close this menu level. */
|
||||
export const menuCloseKey: InjectionKey<() => void> = Symbol("hv-menu-close");
|
||||
|
||||
/** Open state of the enclosing submenu level, when any. */
|
||||
export const submenuOpenKey: InjectionKey<Ref<boolean>> = Symbol(
|
||||
"hv-dropdown-submenu-open",
|
||||
);
|
||||
|
||||
/** True when a DropdownPopover nests inside DropdownSubmenuTrigger. */
|
||||
export const submenuContentKey: InjectionKey<boolean> = Symbol(
|
||||
"hv-dropdown-submenu-content",
|
||||
@@ -70,6 +100,8 @@ export interface DropdownTriggerModeValue {
|
||||
longPressDuration: ComputedRef<number>;
|
||||
open: ComputedRef<boolean>;
|
||||
setOpen: (value: boolean) => void;
|
||||
/** While true, open requests coming from the base trigger are ignored. */
|
||||
suppressOpenRequests: Ref<boolean>;
|
||||
}
|
||||
|
||||
export interface DropdownContextValue extends DropdownTriggerModeValue {
|
||||
|
||||
@@ -11,6 +11,8 @@ export interface UseMenuSelectionOptions {
|
||||
Ref<ReadonlyArray<MenuKey>> | ComputedRef<ReadonlyArray<MenuKey>>;
|
||||
disabledKeys?:
|
||||
Ref<ReadonlyArray<MenuKey>> | ComputedRef<ReadonlyArray<MenuKey>>;
|
||||
/** Keep the last selected key when the user tries to clear it. */
|
||||
disallowEmptySelection?: Ref<boolean> | ComputedRef<boolean>;
|
||||
/** Accepted-value update. Return the accepted keys (may reject the proposal). */
|
||||
onSelectionChange: (keys: MenuKey[]) => void;
|
||||
}
|
||||
@@ -33,12 +35,14 @@ export function useMenuSelection(
|
||||
const current = options.selectedKeys.value;
|
||||
const has = isItemSelected(key);
|
||||
if (mode === "single") {
|
||||
if (has && options.disallowEmptySelection?.value) return;
|
||||
options.onSelectionChange(has ? [] : [key]);
|
||||
return;
|
||||
}
|
||||
options.onSelectionChange(
|
||||
has ? current.filter((k) => k !== key) : [...current, key],
|
||||
);
|
||||
const next = has ? current.filter((k) => k !== key) : [...current, key];
|
||||
if (has && next.length === 0 && options.disallowEmptySelection?.value)
|
||||
return;
|
||||
options.onSelectionChange(next);
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user