feat: migrate Toolbar with teleport-safe focus ownership
This commit is contained in:
@@ -6,7 +6,9 @@ import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
isVNode,
|
||||
ref,
|
||||
provide,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
@@ -17,6 +19,7 @@ import {
|
||||
type ToggleButtonVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import { buttonGroupOrientationKey } from "../button-group/context";
|
||||
import { toolbarKey } from "../toolbar/context";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import {
|
||||
toggleButtonGroupKey,
|
||||
@@ -98,7 +101,7 @@ export default defineComponent({
|
||||
disallowEmptySelection: { type: Boolean, default: false },
|
||||
orientation: {
|
||||
type: String as PropType<"horizontal" | "vertical">,
|
||||
default: "horizontal",
|
||||
default: undefined,
|
||||
},
|
||||
dir: {
|
||||
type: String as PropType<"ltr" | "rtl">,
|
||||
@@ -125,10 +128,27 @@ export default defineComponent({
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
const isSingle = computed(() => props.selectionMode !== "multiple");
|
||||
const isDisabled = computed(() => props.isDisabled || props.disabled);
|
||||
const orientation = computed(() => props.orientation);
|
||||
const toolbar = inject(toolbarKey, undefined);
|
||||
const rootEl = ref<HTMLElement | 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 (rootEl.value === next) return;
|
||||
rootEl.value = next;
|
||||
}
|
||||
const inToolbarDom = computed(() =>
|
||||
Boolean(toolbar && rootEl.value && toolbar.owns(rootEl.value)),
|
||||
);
|
||||
const orientation = computed(
|
||||
() => props.orientation ?? toolbar?.orientation.value ?? "horizontal",
|
||||
);
|
||||
const config = useConfig();
|
||||
const dir = computed<"ltr" | "rtl">(
|
||||
() => props.dir ?? config?.dir.value ?? "ltr",
|
||||
() => props.dir ?? toolbar?.dir.value ?? config?.dir.value ?? "ltr",
|
||||
);
|
||||
|
||||
const selection = useToggleButtonSelection({
|
||||
@@ -166,10 +186,18 @@ export default defineComponent({
|
||||
change: selection.request,
|
||||
orientation,
|
||||
dir,
|
||||
tabIndex: roving.tabIndex,
|
||||
register: (key, el, disabled) => roving.register({ key, el, disabled }),
|
||||
keydown: roving.keydown,
|
||||
noteFocus: roving.noteFocus,
|
||||
// Focus ownership: defer only when this group is DOM-owned by a Toolbar.
|
||||
tabIndex: (key) => (inToolbarDom.value ? -1 : roving.tabIndex(key)),
|
||||
register: (key, el, disabled) =>
|
||||
inToolbarDom.value ? () => {} : roving.register({ key, el, disabled }),
|
||||
keydown: (event, key) => {
|
||||
if (inToolbarDom.value) return;
|
||||
roving.keydown(event, key);
|
||||
},
|
||||
noteFocus: (key) => {
|
||||
if (inToolbarDom.value) return;
|
||||
roving.noteFocus(key);
|
||||
},
|
||||
};
|
||||
|
||||
provide(toggleButtonGroupKey, groupContext);
|
||||
@@ -179,6 +207,7 @@ export default defineComponent({
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
ref: setRoot,
|
||||
...attrs,
|
||||
role: isSingle.value ? "radiogroup" : "toolbar",
|
||||
class: [slotsMap.value.base(), attrs.class],
|
||||
|
||||
Reference in New Issue
Block a user