feat: migrate Toolbar with teleport-safe focus ownership

This commit is contained in:
maoyingjie
2026-10-23 10:50:42 +08:00
parent 9c93647ad2
commit b2525bb04d
33 changed files with 2168 additions and 30 deletions
+59 -3
View File
@@ -1,12 +1,14 @@
<script setup lang="ts">
import { useInheritedDisabled } from "../field/disabled";
import { computed, inject, ref, watch } from "vue";
import { computed, inject, onBeforeUnmount, ref, useAttrs, watch } from "vue";
import { Primitive } from "reka-ui";
import type { ButtonHTMLAttributes } from "vue";
import { buttonVariants, type ButtonVariants } from "@heroui-vue/styles";
import Spinner from "../spinner/Spinner.vue";
import { buttonGroupContextKey } from "../button-group/context";
import { toolbarKey } from "../toolbar/context";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const props = withDefaults(
defineProps<{
variant?: ButtonVariants["variant"];
@@ -32,6 +34,7 @@ const props = withDefaults(
},
);
const buttonGroupContext = inject(buttonGroupContextKey, undefined);
const toolbar = inject(toolbarKey, undefined);
const isButtonGroupChild = computed(() => props.__button_group_child === true);
const finalSize = computed(
() =>
@@ -69,6 +72,55 @@ const localDisabled = computed(() =>
);
const pending = computed(() => props.isPending || props.loading);
const disabled = useInheritedDisabled(() => localDisabled.value);
const root = ref<HTMLElement | undefined>();
let toolbarUnregister: (() => void) | undefined;
function setRoot(el: unknown) {
const node = el as { $el?: HTMLElement } | HTMLElement | null;
const next = !node
? undefined
: node instanceof HTMLElement
? node
: ((node.$el as HTMLElement) ?? undefined);
if (root.value === next) return;
root.value = next;
}
/** Toolbar owns focus only for DOM-contained buttons (not Teleport). */
const inToolbarDom = computed(() =>
Boolean(toolbar && root.value && toolbar.owns(root.value)),
);
watch(
() => [Boolean(toolbar), root.value, inToolbarDom.value] as const,
() => {
toolbarUnregister?.();
toolbarUnregister = undefined;
if (toolbar && inToolbarDom.value) {
toolbarUnregister = toolbar.register(() => root.value, disabled);
}
},
{ immediate: true, flush: "post" },
);
onBeforeUnmount(() => toolbarUnregister?.());
const toolbarTabIndex = computed(() =>
inToolbarDom.value ? toolbar!.tabIndex(root.value) : undefined,
);
/** Merge focus-owner attributes during rendering; preserve asChild attributes otherwise. */
function rootBind() {
const bound: Record<string, unknown> = { ...attrs };
const tab = toolbarTabIndex.value;
if (tab !== undefined) bound.tabindex = tab;
return bound;
}
function noteToolbarFocus(e: FocusEvent) {
void e;
if (inToolbarDom.value) toolbar!.noteFocus(root.value);
}
const hovered = ref(false),
pressed = ref(false),
focused = ref(false),
@@ -96,10 +148,11 @@ function focus(e: FocusEvent) {
</script>
<template>
<Primitive
:ref="setRoot"
@click.capture="guard"
as="button"
:as-child="asChild"
v-bind="$attrs"
v-bind="rootBind()"
:type="type"
:class="
buttonVariants({
@@ -128,7 +181,10 @@ function focus(e: FocusEvent) {
@keydown.space="pressed = !pending && !disabled"
@keydown.enter="pressed = !pending && !disabled"
@keyup="pressed = false"
@focus="focus"
@focus="
focus($event);
noteToolbarFocus($event);
"
@blur="
focused = false;
focusVisible = false;