53b4639b0e
- 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.
197 lines
5.8 KiB
Vue
197 lines
5.8 KiB
Vue
<script lang="ts">
|
|
import {
|
|
defineComponent,
|
|
h,
|
|
inject,
|
|
nextTick,
|
|
onBeforeUnmount,
|
|
onMounted,
|
|
ref,
|
|
useAttrs,
|
|
} from "vue";
|
|
import { DropdownMenuTrigger as RekaDropdownMenuTrigger } from "reka-ui";
|
|
import { dropdownContextKey } from "../menu/context";
|
|
import { elementFromValue } from "./dom";
|
|
|
|
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
|
|
* `event.defaultPrevented` guard, so the real trigger element decides when the
|
|
* menu opens instead of a late bubble interception.
|
|
*/
|
|
export default defineComponent({
|
|
name: "DropdownTrigger",
|
|
inheritAttrs: false,
|
|
props: {
|
|
as: { type: String, default: undefined },
|
|
asChild: { type: Boolean, default: undefined },
|
|
},
|
|
setup(props, { slots }) {
|
|
const attrs = useAttrs();
|
|
const dropdownCtx = inject(dropdownContextKey, null);
|
|
const triggerRef = ref<unknown>(null);
|
|
|
|
const isLongPress = () =>
|
|
(dropdownCtx?.triggerMode.value ?? "press") === "longPress";
|
|
|
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
let startX = 0;
|
|
let startY = 0;
|
|
|
|
function clearPress() {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
timer = null;
|
|
}
|
|
}
|
|
|
|
function onPointerdown(event: PointerEvent) {
|
|
if (event.isPrimary === false) return;
|
|
if (event.pointerType === "mouse" && event.button !== 0) return;
|
|
clearPress();
|
|
startX = event.clientX;
|
|
startY = event.clientY;
|
|
timer = setTimeout(() => {
|
|
timer = null;
|
|
dropdownCtx?.setOpen(true);
|
|
}, dropdownCtx?.longPressDuration.value ?? 500);
|
|
}
|
|
|
|
function onPointermove(event: PointerEvent) {
|
|
if (!timer) return;
|
|
if (
|
|
Math.hypot(event.clientX - startX, event.clientY - startY) <=
|
|
LONG_PRESS_MOVE_PX
|
|
)
|
|
return;
|
|
clearPress();
|
|
}
|
|
|
|
function onActivation(event: MouseEvent | KeyboardEvent) {
|
|
if (!isLongPress()) return;
|
|
// Short press never opens; after a long press the closing click/keyup
|
|
// must not toggle the menu back. Cancelling here also stops Reka's own
|
|
// trigger handler for this event.
|
|
event.preventDefault();
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
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];
|
|
}
|
|
|
|
/**
|
|
* Upstream's `Dropdown.Trigger` is a real `<button>`, so a custom trigger
|
|
* (e.g. an Avatar) stays keyboard operable. With `asChild` the element is
|
|
* whatever the caller supplied, so restore button semantics when it is not
|
|
* natively focusable.
|
|
*/
|
|
function ensureFocusable() {
|
|
const el = elementFromValue(triggerRef.value);
|
|
if (!el) return;
|
|
const tag = el.tagName;
|
|
const role = el.getAttribute("role");
|
|
if (
|
|
tag === "BUTTON" ||
|
|
(tag === "A" && el.hasAttribute("href")) ||
|
|
el.hasAttribute("tabindex") ||
|
|
role === "button" ||
|
|
role === "link" ||
|
|
role === "menuitem"
|
|
)
|
|
return;
|
|
if (!role) el.setAttribute("role", "button");
|
|
el.setAttribute("tabindex", "0");
|
|
}
|
|
|
|
onMounted(() => void nextTick(ensureFocusable));
|
|
onBeforeUnmount(clearPress);
|
|
|
|
const guardHandlers = {
|
|
onPointerdownCapture: onPointerdown,
|
|
onPointermoveCapture: onPointermove,
|
|
onPointerupCapture: clearPress,
|
|
onPointercancelCapture: clearPress,
|
|
onPointerleaveCapture: clearPress,
|
|
onClickCapture: onActivation,
|
|
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,
|
|
{
|
|
...triggerAttrs,
|
|
ref: triggerRef,
|
|
as: props.as,
|
|
asChild: props.asChild ?? true,
|
|
class: dropdownCtx?.slots.value.trigger({
|
|
class: attrs.class as never,
|
|
}),
|
|
"data-slot": "dropdown-trigger",
|
|
},
|
|
{
|
|
default: () =>
|
|
slots.default?.({
|
|
isOpen: dropdownCtx?.open.value ?? false,
|
|
}),
|
|
},
|
|
);
|
|
|
|
if (!longPress) return trigger;
|
|
|
|
return h(
|
|
"span",
|
|
{
|
|
...guardHandlers,
|
|
style: "display: contents",
|
|
"data-slot": "dropdown-trigger-region",
|
|
},
|
|
[trigger],
|
|
);
|
|
};
|
|
},
|
|
});
|
|
</script>
|