feat: migrate button groups and toggle buttons with focus regressions

This commit is contained in:
maoyingjie
2026-10-23 09:57:41 +08:00
parent 99df0f1c91
commit 9c93647ad2
61 changed files with 4535 additions and 10 deletions
@@ -0,0 +1,104 @@
<script lang="ts">
import {
Comment,
Fragment,
Text,
cloneVNode,
computed,
defineComponent,
h,
inject,
isVNode,
provide,
useAttrs,
type PropType,
type VNode,
type VNodeChild,
} from "vue";
import { buttonGroupVariants, type ButtonVariants } from "@heroui-vue/styles";
import { buttonGroupContextKey, buttonGroupOrientationKey } from "./context";
function flattenChildren(nodes: VNodeChild[], out: VNode[] = []): VNode[] {
for (const child of nodes) {
if (Array.isArray(child)) {
flattenChildren(child, out);
continue;
}
if (!isVNode(child)) continue;
if (child.type === Comment || child.type === Text) continue;
if (child.type === Fragment && Array.isArray(child.children)) {
flattenChildren(child.children as VNodeChild[], out);
continue;
}
out.push(child);
}
return out;
}
export default defineComponent({
name: "ButtonGroup",
inheritAttrs: false,
props: {
size: {
type: String as PropType<ButtonVariants["size"]>,
default: undefined,
},
variant: {
type: String as PropType<ButtonVariants["variant"]>,
default: undefined,
},
orientation: {
type: String as PropType<"horizontal" | "vertical">,
default: undefined,
},
fullWidth: { type: Boolean, default: false },
isDisabled: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
as: { type: String, default: "div" },
},
setup(props, { attrs, slots }) {
const inheritedOrientation = inject(buttonGroupOrientationKey, undefined);
const orientation = computed(
() => props.orientation ?? inheritedOrientation?.value ?? "horizontal",
);
const isDisabled = computed(() => props.isDisabled || props.disabled);
const groupContext = computed(() => ({
slots: buttonGroupVariants({
fullWidth: props.fullWidth,
orientation: orientation.value,
}),
size: props.size,
variant: props.variant,
isDisabled: isDisabled.value,
fullWidth: props.fullWidth,
orientation: orientation.value,
}));
provide(buttonGroupContextKey, groupContext);
function render() {
const flat = flattenChildren((slots.default?.() ?? []) as VNodeChild[]);
// Only component children accept the marker; native nodes must not leak props.
const children = flat.map((child) =>
typeof child.type === "object" && child.type !== null
? cloneVNode(child, { __button_group_child: true })
: child,
);
return h(
props.as,
{
...attrs,
role: "group",
class: [groupContext.value.slots.base(), attrs.class],
"data-slot": "button-group",
"aria-disabled": isDisabled.value || undefined,
},
children,
);
}
return render;
},
});
</script>
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { computed, inject, useAttrs } from "vue";
import { Primitive } from "reka-ui";
import { buttonGroupContextKey } from "./context";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
/** Absorbs direct-child markers; not a public API. */
__button_group_child?: boolean;
}>();
void props;
const attrs = useAttrs();
const group = inject(buttonGroupContextKey, undefined);
const separatorClass = computed(() => group?.value.slots.separator());
</script>
<template>
<Primitive
as="span"
aria-hidden="true"
:class="[separatorClass, attrs.class]"
data-slot="button-group-separator"
v-bind="{ ...attrs, class: undefined }"
>
<slot />
</Primitive>
</template>
@@ -0,0 +1,27 @@
import type { ComputedRef, InjectionKey } from "vue";
import type { ButtonVariants, buttonGroupVariants } from "@heroui-vue/styles";
/** Marks direct children of ButtonGroup so nested Buttons do not inherit group props. */
export const BUTTON_GROUP_CHILD = "__button_group_child" as const;
export type ButtonGroupSlots = ReturnType<typeof buttonGroupVariants>;
export interface ButtonGroupContextValue {
slots: ButtonGroupSlots;
size?: ButtonVariants["size"];
variant?: ButtonVariants["variant"];
isDisabled?: boolean;
fullWidth?: boolean;
orientation: "horizontal" | "vertical";
}
export type ButtonGroupContext = ComputedRef<ButtonGroupContextValue>;
export const buttonGroupContextKey: InjectionKey<ButtonGroupContext> = Symbol(
"HeroUIVue.ButtonGroup",
);
/** Orientation inherited by ButtonGroup when nested in ToggleButtonGroup. */
export const buttonGroupOrientationKey: InjectionKey<
ComputedRef<"horizontal" | "vertical">
> = Symbol("HeroUIVue.ButtonGroupOrientation");