feat: migrate button groups and toggle buttons with focus regressions
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
|
||||
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix; Vue state adapters. */
|
||||
/* ==========================================================================
|
||||
ButtonGroup - Groups related buttons together
|
||||
========================================================================== */
|
||||
|
||||
/* Base button group styles */
|
||||
.hv-button-group {
|
||||
@apply inline-flex h-auto items-center justify-center gap-0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Orientation variants
|
||||
========================================================================== */
|
||||
|
||||
.hv-button-group--horizontal {
|
||||
@apply flex-row;
|
||||
}
|
||||
|
||||
.hv-button-group--vertical {
|
||||
@apply flex-col;
|
||||
}
|
||||
|
||||
/* Remove border radius from all buttons in group */
|
||||
.hv-button-group .hv-button {
|
||||
@apply rounded-none;
|
||||
}
|
||||
|
||||
/* Apply border radius to first button (start edge) */
|
||||
.hv-button-group--horizontal .hv-button:first-child {
|
||||
@apply rounded-s-3xl;
|
||||
}
|
||||
|
||||
/* Apply border radius to last button (end edge) */
|
||||
.hv-button-group--horizontal .hv-button:last-child {
|
||||
@apply rounded-e-3xl;
|
||||
}
|
||||
|
||||
/* If only one button, apply both rounded edges */
|
||||
.hv-button-group--horizontal .hv-button:first-child:last-child {
|
||||
@apply rounded-3xl;
|
||||
}
|
||||
|
||||
/* Apply border radius to first button (top edge) in vertical */
|
||||
.hv-button-group--vertical .hv-button:first-child {
|
||||
@apply rounded-t-3xl;
|
||||
}
|
||||
|
||||
/* Apply border radius to last button (bottom edge) in vertical */
|
||||
.hv-button-group--vertical .hv-button:last-child {
|
||||
@apply rounded-b-3xl;
|
||||
}
|
||||
|
||||
/* If only one button in vertical, apply all rounded edges */
|
||||
.hv-button-group--vertical .hv-button:first-child:last-child {
|
||||
@apply rounded-3xl;
|
||||
}
|
||||
|
||||
/* Remove scale transform on active/pressed state for buttons in group */
|
||||
.hv-button-group .hv-button:active,
|
||||
.hv-button-group .hv-button[data-pressed="true"] {
|
||||
transform: none;
|
||||
}
|
||||
|
||||
/* Raise focused button so its outward focus ring + gap shows above adjacent buttons */
|
||||
.hv-button-group .hv-button:focus-visible:not([data-focused]),
|
||||
.hv-button-group .hv-button[data-focus-visible="true"] {
|
||||
@apply z-10;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Separator (compound component)
|
||||
========================================================================== */
|
||||
|
||||
.hv-button-group__separator {
|
||||
@apply rounded-sm bg-current opacity-15;
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition: opacity 150ms var(--ease-smooth);
|
||||
@apply motion-reduce:transition-none;
|
||||
|
||||
/* Horizontal orientation - vertical divider */
|
||||
.hv-button-group--horizontal & {
|
||||
inset-inline-start: -1px;
|
||||
top: 25%;
|
||||
width: 1px;
|
||||
height: 50%;
|
||||
}
|
||||
|
||||
/* Vertical orientation - horizontal divider */
|
||||
.hv-button-group--vertical & {
|
||||
inset-inline-start: 25%;
|
||||
top: -1px;
|
||||
width: 50%;
|
||||
height: 1px;
|
||||
}
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Outline button border handling
|
||||
========================================================================== */
|
||||
|
||||
/* Remove adjacent borders from outline buttons in horizontal group */
|
||||
/* First button: remove end border (right in LTR, left in RTL) */
|
||||
.hv-button-group--horizontal .hv-button--outline:first-child {
|
||||
@apply border-e-0;
|
||||
}
|
||||
|
||||
/* Last button: remove start border (left in LTR, right in RTL) */
|
||||
.hv-button-group--horizontal .hv-button--outline:last-child {
|
||||
@apply border-s-0;
|
||||
}
|
||||
|
||||
/* Middle buttons: remove both inline borders */
|
||||
.hv-button-group--horizontal
|
||||
.hv-button--outline:not(:first-child):not(:last-child) {
|
||||
@apply border-x-0;
|
||||
}
|
||||
|
||||
/* Remove adjacent borders from outline buttons in vertical group */
|
||||
/* First button: remove bottom border */
|
||||
.hv-button-group--vertical .hv-button--outline:first-child {
|
||||
@apply border-b-0;
|
||||
}
|
||||
|
||||
/* Last button: remove top border */
|
||||
.hv-button-group--vertical .hv-button--outline:last-child {
|
||||
@apply border-t-0;
|
||||
}
|
||||
|
||||
/* Middle buttons: remove both block borders */
|
||||
.hv-button-group--vertical
|
||||
.hv-button--outline:not(:first-child):not(:last-child) {
|
||||
@apply border-y-0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Full width modifier
|
||||
========================================================================== */
|
||||
|
||||
.hv-button-group--full-width {
|
||||
@apply w-full;
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
|
||||
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix; Vue state adapters. */
|
||||
/* ==========================================================================
|
||||
ToggleButtonGroup - Groups multiple ToggleButtons into a unified control
|
||||
========================================================================== */
|
||||
|
||||
/* Base styles */
|
||||
.hv-toggle-button-group {
|
||||
@apply inline-flex h-auto w-fit items-center justify-center gap-0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Orientation variants
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button-group--horizontal {
|
||||
@apply flex-row;
|
||||
}
|
||||
|
||||
.hv-toggle-button-group--vertical {
|
||||
@apply flex-col;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Full width modifier
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button-group--full-width {
|
||||
@apply w-full;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Attached mode (default) - buttons connected with dividers
|
||||
========================================================================== */
|
||||
|
||||
/* Remove border radius from all toggle buttons in attached group */
|
||||
.hv-toggle-button-group .hv-toggle-button {
|
||||
@apply rounded-none;
|
||||
}
|
||||
|
||||
/* Apply border radius to first button (start edge) */
|
||||
.hv-toggle-button-group--horizontal .hv-toggle-button:first-child {
|
||||
@apply rounded-s-3xl;
|
||||
}
|
||||
|
||||
/* Apply border radius to last button (end edge) */
|
||||
.hv-toggle-button-group--horizontal .hv-toggle-button:last-child {
|
||||
@apply rounded-e-3xl;
|
||||
}
|
||||
|
||||
/* If only one button, apply both rounded edges */
|
||||
.hv-toggle-button-group--horizontal .hv-toggle-button:first-child:last-child {
|
||||
@apply rounded-3xl;
|
||||
}
|
||||
|
||||
/* Apply border radius to first button (top edge) in vertical */
|
||||
.hv-toggle-button-group--vertical .hv-toggle-button:first-child {
|
||||
@apply rounded-t-3xl;
|
||||
}
|
||||
|
||||
/* Apply border radius to last button (bottom edge) in vertical */
|
||||
.hv-toggle-button-group--vertical .hv-toggle-button:last-child {
|
||||
@apply rounded-b-3xl;
|
||||
}
|
||||
|
||||
/* If only one button in vertical, apply all rounded edges */
|
||||
.hv-toggle-button-group--vertical .hv-toggle-button:first-child:last-child {
|
||||
@apply rounded-3xl;
|
||||
}
|
||||
|
||||
/* Remove scale transform on active/pressed state for buttons in group */
|
||||
.hv-toggle-button-group .hv-toggle-button:active,
|
||||
.hv-toggle-button-group .hv-toggle-button[data-pressed="true"] {
|
||||
transform: none;
|
||||
}
|
||||
|
||||
/* Override focus ring to use inset style so it stays within button bounds */
|
||||
.hv-toggle-button-group .hv-toggle-button:focus-visible:not([data-focused]),
|
||||
.hv-toggle-button-group .hv-toggle-button[data-focus-visible="true"] {
|
||||
--tw-ring-offset-width: 0px;
|
||||
@apply ring-inset;
|
||||
}
|
||||
|
||||
/* Full width: stretch each toggle button */
|
||||
.hv-toggle-button-group--full-width .hv-toggle-button {
|
||||
@apply flex-1;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Separator (compound component)
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button-group__separator {
|
||||
@apply rounded-sm bg-current opacity-15;
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition: opacity 150ms var(--ease-smooth);
|
||||
@apply motion-reduce:transition-none;
|
||||
|
||||
/* Horizontal orientation - vertical divider */
|
||||
.hv-toggle-button-group--horizontal & {
|
||||
inset-inline-start: -1px;
|
||||
top: 25%;
|
||||
width: 1px;
|
||||
height: 50%;
|
||||
}
|
||||
|
||||
/* Vertical orientation - horizontal divider */
|
||||
.hv-toggle-button-group--vertical & {
|
||||
inset-inline-start: 25%;
|
||||
top: -1px;
|
||||
width: 50%;
|
||||
height: 1px;
|
||||
}
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Detached mode - buttons separated with gaps
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button-group--detached {
|
||||
@apply gap-1;
|
||||
}
|
||||
|
||||
/* Restore border radius in detached mode */
|
||||
.hv-toggle-button-group--detached .hv-toggle-button {
|
||||
@apply rounded-3xl;
|
||||
}
|
||||
|
||||
/* Hide separators in detached mode */
|
||||
.hv-toggle-button-group--detached .hv-toggle-button-group__separator {
|
||||
display: none;
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
/* Adapted from HeroUI v3, ce90ea3. Copyright 2025 NextUI Inc.
|
||||
* See third-party/HEROUI-LICENSE. Modified: hv- class prefix; Vue state adapters. */
|
||||
/* ==========================================================================
|
||||
ToggleButton - Interactive toggle control for on/off states
|
||||
========================================================================== */
|
||||
|
||||
/* Base styles */
|
||||
.hv-toggle-button {
|
||||
@apply relative isolate inline-flex h-10 w-fit origin-center items-center justify-center gap-2 rounded-3xl px-4 text-sm font-medium whitespace-nowrap outline-none select-none no-highlight md:h-9;
|
||||
|
||||
/**
|
||||
* Transitions
|
||||
* CRITICAL: motion-reduce must be AFTER transition for correct override specificity
|
||||
*/
|
||||
transition:
|
||||
transform 250ms var(--ease-smooth),
|
||||
background-color 100ms var(--ease-out),
|
||||
box-shadow 100ms var(--ease-out);
|
||||
@apply transform-gpu motion-reduce:transition-none;
|
||||
|
||||
/* Cursor */
|
||||
cursor: var(--cursor-interactive);
|
||||
|
||||
/* Default tokens (unselected state) */
|
||||
--toggle-button-bg: var(--default);
|
||||
--toggle-button-bg-hover: var(--default-hover);
|
||||
--toggle-button-bg-pressed: var(--default-hover);
|
||||
--toggle-button-fg: currentColor;
|
||||
|
||||
/* Selected state tokens */
|
||||
--toggle-button-bg-selected: var(--accent-soft);
|
||||
--toggle-button-bg-selected-hover: var(--accent-soft-hover);
|
||||
--toggle-button-bg-selected-pressed: var(--accent-soft-hover);
|
||||
--toggle-button-fg-selected: var(--accent-soft-foreground);
|
||||
|
||||
background-color: var(--toggle-button-bg);
|
||||
color: var(--toggle-button-fg);
|
||||
|
||||
/* Focus state */
|
||||
&:focus-visible:not([data-focused]),
|
||||
&[data-focus-visible="true"] {
|
||||
@apply status-focused;
|
||||
}
|
||||
|
||||
/* Disabled state */
|
||||
&:disabled,
|
||||
&[aria-disabled="true"] {
|
||||
@apply status-disabled;
|
||||
}
|
||||
|
||||
/* Hover */
|
||||
@media (hover: hover) {
|
||||
&:hover,
|
||||
&[data-hovered="true"] {
|
||||
background-color: var(--toggle-button-bg-hover);
|
||||
}
|
||||
}
|
||||
|
||||
/* Active / pressed */
|
||||
&:active,
|
||||
&[data-pressed="true"] {
|
||||
background-color: var(--toggle-button-bg-pressed);
|
||||
transform: scale(0.97);
|
||||
}
|
||||
|
||||
/* Selected state */
|
||||
&[data-selected="true"] {
|
||||
background-color: var(--toggle-button-bg-selected);
|
||||
color: var(--toggle-button-fg-selected);
|
||||
|
||||
@media (hover: hover) {
|
||||
&:hover,
|
||||
&[data-hovered="true"] {
|
||||
background-color: var(--toggle-button-bg-selected-hover);
|
||||
}
|
||||
}
|
||||
|
||||
&:active,
|
||||
&[data-pressed="true"] {
|
||||
background-color: var(--toggle-button-bg-selected-pressed);
|
||||
}
|
||||
}
|
||||
|
||||
/* SVG icon styling */
|
||||
& svg {
|
||||
@apply pointer-events-none -mx-0.5 my-0.5 size-5 shrink-0 self-center sm:my-1 sm:size-4;
|
||||
}
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Size variants
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button--sm {
|
||||
@apply h-9 px-3 md:h-8;
|
||||
|
||||
& svg {
|
||||
@apply size-4;
|
||||
}
|
||||
|
||||
&:active,
|
||||
&[data-pressed="true"] {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
}
|
||||
|
||||
.hv-toggle-button--md {
|
||||
/* No styles as this is the default size */
|
||||
}
|
||||
|
||||
.hv-toggle-button--lg {
|
||||
@apply h-11 text-base md:h-10;
|
||||
|
||||
&:active,
|
||||
&[data-pressed="true"] {
|
||||
transform: scale(0.96);
|
||||
}
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Variant styles
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button--default {
|
||||
--toggle-button-bg: var(--default);
|
||||
--toggle-button-bg-hover: var(--default-hover);
|
||||
--toggle-button-bg-pressed: var(--default-hover);
|
||||
}
|
||||
|
||||
.hv-toggle-button--ghost {
|
||||
--toggle-button-bg: transparent;
|
||||
--toggle-button-bg-hover: var(--default);
|
||||
--toggle-button-bg-pressed: var(--default);
|
||||
--toggle-button-fg: var(--default-foreground);
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Icon-only modifier
|
||||
========================================================================== */
|
||||
|
||||
.hv-toggle-button--icon-only {
|
||||
@apply w-10 p-0 md:w-9;
|
||||
}
|
||||
|
||||
.hv-toggle-button--icon-only.hv-toggle-button--sm {
|
||||
@apply w-9 md:w-8;
|
||||
}
|
||||
|
||||
.hv-toggle-button--icon-only.hv-toggle-button--lg {
|
||||
@apply w-11 md:w-10;
|
||||
}
|
||||
@@ -130,3 +130,6 @@
|
||||
|
||||
@import "./components/scroll-shadow.css";
|
||||
@import "./components/tabs.css";
|
||||
@import "./components/button-group.css" layer(components);
|
||||
@import "./components/toggle-button.css" layer(components);
|
||||
@import "./components/toggle-button-group.css" layer(components);
|
||||
|
||||
@@ -1056,3 +1056,95 @@ export type TabsVariants = Omit<
|
||||
VariantProps<typeof tabsVariants>,
|
||||
TabsRenderPropsKeys
|
||||
>;
|
||||
|
||||
export const buttonGroupVariants = tv({
|
||||
defaultVariants: {
|
||||
fullWidth: false,
|
||||
orientation: "horizontal",
|
||||
},
|
||||
slots: {
|
||||
base: "hv-button-group",
|
||||
separator: "hv-button-group__separator",
|
||||
},
|
||||
variants: {
|
||||
fullWidth: {
|
||||
false: {},
|
||||
true: {
|
||||
base: "hv-button-group--full-width",
|
||||
},
|
||||
},
|
||||
orientation: {
|
||||
horizontal: {
|
||||
base: "hv-button-group--horizontal",
|
||||
},
|
||||
vertical: {
|
||||
base: "hv-button-group--vertical",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export type ButtonGroupVariants = VariantProps<typeof buttonGroupVariants>;
|
||||
|
||||
export const toggleButtonVariants = tv({
|
||||
base: "hv-toggle-button",
|
||||
defaultVariants: {
|
||||
isIconOnly: false,
|
||||
size: "md",
|
||||
variant: "default",
|
||||
},
|
||||
variants: {
|
||||
isIconOnly: {
|
||||
true: "hv-toggle-button--icon-only",
|
||||
},
|
||||
size: {
|
||||
lg: "hv-toggle-button--lg",
|
||||
md: "hv-toggle-button--md",
|
||||
sm: "hv-toggle-button--sm",
|
||||
},
|
||||
variant: {
|
||||
default: "hv-toggle-button--default",
|
||||
ghost: "hv-toggle-button--ghost",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export type ToggleButtonVariants = VariantProps<typeof toggleButtonVariants>;
|
||||
|
||||
export const toggleButtonGroupVariants = tv({
|
||||
defaultVariants: {
|
||||
fullWidth: false,
|
||||
isDetached: false,
|
||||
orientation: "horizontal",
|
||||
},
|
||||
slots: {
|
||||
base: "hv-toggle-button-group",
|
||||
separator: "hv-toggle-button-group__separator",
|
||||
},
|
||||
variants: {
|
||||
fullWidth: {
|
||||
false: {},
|
||||
true: {
|
||||
base: "hv-toggle-button-group--full-width",
|
||||
},
|
||||
},
|
||||
isDetached: {
|
||||
false: {},
|
||||
true: {
|
||||
base: "hv-toggle-button-group--detached",
|
||||
},
|
||||
},
|
||||
orientation: {
|
||||
horizontal: {
|
||||
base: "hv-toggle-button-group--horizontal",
|
||||
},
|
||||
vertical: {
|
||||
base: "hv-toggle-button-group--vertical",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export type ToggleButtonGroupVariants = VariantProps<
|
||||
typeof toggleButtonGroupVariants
|
||||
>;
|
||||
|
||||
@@ -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");
|
||||
@@ -1,10 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import { useInheritedDisabled } from "../field/disabled";
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { computed, inject, ref, 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";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
@@ -18,11 +19,56 @@ const props = withDefaults(
|
||||
disabled?: boolean;
|
||||
loading?: boolean;
|
||||
type?: ButtonHTMLAttributes["type"];
|
||||
/** Marks a direct ButtonGroup child (see BUTTON_GROUP_CHILD). */
|
||||
__button_group_child?: boolean;
|
||||
}>(),
|
||||
{ type: "button" },
|
||||
{
|
||||
type: "button",
|
||||
variant: undefined,
|
||||
size: undefined,
|
||||
fullWidth: undefined,
|
||||
isDisabled: undefined,
|
||||
disabled: undefined,
|
||||
},
|
||||
);
|
||||
const buttonGroupContext = inject(buttonGroupContextKey, undefined);
|
||||
const isButtonGroupChild = computed(() => props.__button_group_child === true);
|
||||
const finalSize = computed(
|
||||
() =>
|
||||
props.size ??
|
||||
(isButtonGroupChild.value ? buttonGroupContext?.value.size : undefined),
|
||||
);
|
||||
const finalVariant = computed(
|
||||
() =>
|
||||
props.variant ??
|
||||
(isButtonGroupChild.value ? buttonGroupContext?.value.variant : undefined),
|
||||
);
|
||||
const finalFullWidth = computed(
|
||||
() =>
|
||||
props.fullWidth ??
|
||||
(isButtonGroupChild.value
|
||||
? buttonGroupContext?.value.fullWidth
|
||||
: undefined),
|
||||
);
|
||||
const groupDisabled = computed(() =>
|
||||
isButtonGroupChild.value
|
||||
? Boolean(buttonGroupContext?.value.isDisabled)
|
||||
: false,
|
||||
);
|
||||
/**
|
||||
* Upstream nullish precedence: explicit child props override ButtonGroup defaults.
|
||||
* Fieldset/native constraints still force disabled through useInheritedDisabled.
|
||||
*/
|
||||
const explicitDisabled = computed(
|
||||
() => props.isDisabled ?? props.disabled ?? undefined,
|
||||
);
|
||||
const localDisabled = computed(() =>
|
||||
explicitDisabled.value === undefined
|
||||
? groupDisabled.value
|
||||
: Boolean(explicitDisabled.value),
|
||||
);
|
||||
const pending = computed(() => props.isPending || props.loading);
|
||||
const disabled = useInheritedDisabled(() => props.isDisabled || props.disabled);
|
||||
const disabled = useInheritedDisabled(() => localDisabled.value);
|
||||
const hovered = ref(false),
|
||||
pressed = ref(false),
|
||||
focused = ref(false),
|
||||
@@ -55,7 +101,14 @@ function focus(e: FocusEvent) {
|
||||
:as-child="asChild"
|
||||
v-bind="$attrs"
|
||||
:type="type"
|
||||
:class="buttonVariants({ variant, size, fullWidth, isIconOnly })"
|
||||
:class="
|
||||
buttonVariants({
|
||||
variant: finalVariant,
|
||||
size: finalSize,
|
||||
fullWidth: finalFullWidth,
|
||||
isIconOnly,
|
||||
})
|
||||
"
|
||||
:disabled="disabled"
|
||||
:aria-disabled="pending || disabled || undefined"
|
||||
:aria-busy="pending || undefined"
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
Comment,
|
||||
Fragment,
|
||||
Text,
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
isVNode,
|
||||
provide,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import {
|
||||
toggleButtonGroupVariants,
|
||||
type ToggleButtonVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import { buttonGroupOrientationKey } from "../button-group/context";
|
||||
import { useConfig } from "../../providers/context";
|
||||
import {
|
||||
toggleButtonGroupKey,
|
||||
type ToggleButtonGroupKey,
|
||||
type ToggleButtonGroupContextValue,
|
||||
} from "./context";
|
||||
import {
|
||||
useToggleButtonSelection,
|
||||
useToggleRovingFocus,
|
||||
type ToggleButtonGroupSelection,
|
||||
} from "./useToggleButtonSelection";
|
||||
|
||||
function flattenChildren(
|
||||
nodes: VNodeChild[],
|
||||
out: VNodeChild[] = [],
|
||||
): VNodeChild[] {
|
||||
for (const child of nodes) {
|
||||
if (Array.isArray(child)) {
|
||||
flattenChildren(child, out);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
isVNode(child) &&
|
||||
child.type === Fragment &&
|
||||
Array.isArray(child.children)
|
||||
) {
|
||||
flattenChildren(child.children as VNodeChild[], out);
|
||||
continue;
|
||||
}
|
||||
out.push(child);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "ToggleButtonGroup",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
selectionMode: {
|
||||
type: String as PropType<"single" | "multiple">,
|
||||
default: "single",
|
||||
},
|
||||
modelValue: {
|
||||
type: [
|
||||
String,
|
||||
Number,
|
||||
Array,
|
||||
null,
|
||||
] as PropType<ToggleButtonGroupSelection>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultValue: {
|
||||
type: [
|
||||
String,
|
||||
Number,
|
||||
Array,
|
||||
null,
|
||||
] as PropType<ToggleButtonGroupSelection>,
|
||||
default: undefined,
|
||||
},
|
||||
selectedKeys: {
|
||||
type: [
|
||||
String,
|
||||
Number,
|
||||
Array,
|
||||
null,
|
||||
] as PropType<ToggleButtonGroupSelection>,
|
||||
default: undefined,
|
||||
},
|
||||
defaultSelectedKeys: {
|
||||
type: [
|
||||
String,
|
||||
Number,
|
||||
Array,
|
||||
null,
|
||||
] as PropType<ToggleButtonGroupSelection>,
|
||||
default: undefined,
|
||||
},
|
||||
disallowEmptySelection: { type: Boolean, default: false },
|
||||
orientation: {
|
||||
type: String as PropType<"horizontal" | "vertical">,
|
||||
default: "horizontal",
|
||||
},
|
||||
dir: {
|
||||
type: String as PropType<"ltr" | "rtl">,
|
||||
default: undefined,
|
||||
},
|
||||
size: {
|
||||
type: String as PropType<ToggleButtonVariants["size"]>,
|
||||
default: undefined,
|
||||
},
|
||||
isDetached: { type: Boolean, default: false },
|
||||
fullWidth: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
disabled: { type: Boolean, default: false },
|
||||
as: { type: String, default: "div" },
|
||||
},
|
||||
emits: {
|
||||
"update:modelValue": (
|
||||
_value: ToggleButtonGroupKey[] | ToggleButtonGroupKey | null,
|
||||
) => true,
|
||||
selectionChange: (
|
||||
_value: ToggleButtonGroupKey[] | ToggleButtonGroupKey | null,
|
||||
) => true,
|
||||
},
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
const isSingle = computed(() => props.selectionMode !== "multiple");
|
||||
const isDisabled = computed(() => props.isDisabled || props.disabled);
|
||||
const orientation = computed(() => props.orientation);
|
||||
const config = useConfig();
|
||||
const dir = computed<"ltr" | "rtl">(
|
||||
() => props.dir ?? config?.dir.value ?? "ltr",
|
||||
);
|
||||
|
||||
const selection = useToggleButtonSelection({
|
||||
isSingle: () => isSingle.value,
|
||||
read: () => props.selectedKeys ?? props.modelValue,
|
||||
defaults: () => props.defaultSelectedKeys ?? props.defaultValue,
|
||||
keys: ["modelValue", "model-value", "selectedKeys", "selected-keys"],
|
||||
disallowEmptySelection: () => props.disallowEmptySelection,
|
||||
emit: (value) => {
|
||||
emit("update:modelValue", value);
|
||||
emit("selectionChange", value);
|
||||
},
|
||||
});
|
||||
|
||||
const roving = useToggleRovingFocus({
|
||||
orientation: () => orientation.value,
|
||||
dir: () => dir.value,
|
||||
});
|
||||
|
||||
const slotsMap = computed(() =>
|
||||
toggleButtonGroupVariants({
|
||||
fullWidth: props.fullWidth,
|
||||
isDetached: props.isDetached,
|
||||
orientation: orientation.value,
|
||||
}),
|
||||
);
|
||||
const size = computed(() => props.size);
|
||||
|
||||
const groupContext: ToggleButtonGroupContextValue = {
|
||||
slots: slotsMap,
|
||||
size,
|
||||
isDisabled,
|
||||
isSingle,
|
||||
selected: selection.selected,
|
||||
change: selection.request,
|
||||
orientation,
|
||||
dir,
|
||||
tabIndex: roving.tabIndex,
|
||||
register: (key, el, disabled) => roving.register({ key, el, disabled }),
|
||||
keydown: roving.keydown,
|
||||
noteFocus: roving.noteFocus,
|
||||
};
|
||||
|
||||
provide(toggleButtonGroupKey, groupContext);
|
||||
provide(buttonGroupOrientationKey, orientation);
|
||||
|
||||
function render() {
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
role: isSingle.value ? "radiogroup" : "toolbar",
|
||||
class: [slotsMap.value.base(), attrs.class],
|
||||
"data-slot": "toggle-button-group",
|
||||
"aria-disabled": isDisabled.value || undefined,
|
||||
"aria-orientation": orientation.value,
|
||||
dir: dir.value,
|
||||
},
|
||||
flattenChildren((slots.default?.() ?? []) as VNodeChild[]),
|
||||
);
|
||||
}
|
||||
|
||||
return render;
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,26 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, useAttrs } from "vue";
|
||||
import { Primitive } from "reka-ui";
|
||||
import { toggleButtonGroupKey } from "./context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = defineProps<{
|
||||
/** Absorbs group child markers; not a public API. */
|
||||
__button_group_child?: boolean;
|
||||
}>();
|
||||
void props;
|
||||
const attrs = useAttrs();
|
||||
const group = inject(toggleButtonGroupKey, undefined);
|
||||
const separatorClass = computed(() => group?.slots.value.separator());
|
||||
</script>
|
||||
<template>
|
||||
<Primitive
|
||||
as="span"
|
||||
aria-hidden="true"
|
||||
:class="[separatorClass, attrs.class]"
|
||||
data-slot="toggle-button-group-separator"
|
||||
v-bind="{ ...attrs, class: undefined }"
|
||||
>
|
||||
<slot />
|
||||
</Primitive>
|
||||
</template>
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { ComputedRef, InjectionKey } from "vue";
|
||||
import type {
|
||||
ToggleButtonVariants,
|
||||
toggleButtonGroupVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
|
||||
export type ToggleButtonGroupKey = string | number;
|
||||
|
||||
export interface ToggleButtonGroupContextValue {
|
||||
slots: ComputedRef<ReturnType<typeof toggleButtonGroupVariants>>;
|
||||
size: ComputedRef<ToggleButtonVariants["size"] | undefined>;
|
||||
isDisabled: ComputedRef<boolean>;
|
||||
isSingle: ComputedRef<boolean>;
|
||||
selected: (key: ToggleButtonGroupKey) => boolean;
|
||||
change: (key: ToggleButtonGroupKey) => void;
|
||||
orientation: ComputedRef<"horizontal" | "vertical">;
|
||||
dir: ComputedRef<"ltr" | "rtl">;
|
||||
/** Roving tabindex for the active member key. */
|
||||
tabIndex: (key: ToggleButtonGroupKey) => number;
|
||||
/** Register a member for keyboard order; returns unregister. */
|
||||
register: (
|
||||
key: ToggleButtonGroupKey,
|
||||
el: () => HTMLElement | undefined,
|
||||
disabled: ComputedRef<boolean>,
|
||||
) => () => void;
|
||||
/** Arrow-key navigation within the group. */
|
||||
keydown: (event: KeyboardEvent, key: ToggleButtonGroupKey) => void;
|
||||
/** Sync the single Tab entry after a real focus, without moving focus. */
|
||||
noteFocus: (key: ToggleButtonGroupKey) => void;
|
||||
}
|
||||
|
||||
export const toggleButtonGroupKey: InjectionKey<ToggleButtonGroupContextValue> =
|
||||
Symbol("HeroUIVue.ToggleButtonGroup");
|
||||
@@ -0,0 +1,264 @@
|
||||
import {
|
||||
computed,
|
||||
getCurrentInstance,
|
||||
onBeforeUpdate,
|
||||
ref,
|
||||
watch,
|
||||
type ComputedRef,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
import type { ToggleButtonGroupKey } from "./context";
|
||||
|
||||
export type ToggleButtonGroupSelection =
|
||||
ToggleButtonGroupKey | ToggleButtonGroupKey[] | null | undefined;
|
||||
|
||||
function toArray(value: ToggleButtonGroupSelection): ToggleButtonGroupKey[] {
|
||||
if (value == null) return [];
|
||||
return Array.isArray(value) ? [...value] : [value];
|
||||
}
|
||||
|
||||
function sameKey(a: ToggleButtonGroupKey, b: ToggleButtonGroupKey): boolean {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
function normalize(
|
||||
raw: ToggleButtonGroupSelection,
|
||||
single: boolean,
|
||||
): ToggleButtonGroupKey[] | ToggleButtonGroupKey | null {
|
||||
const list = toArray(raw);
|
||||
return single ? (list[0] ?? null) : list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controlled/uncontrolled selection for ToggleButtonGroup.
|
||||
* Single mode stores one key or null; multiple mode stores an array.
|
||||
* Mode switches collapse internal state so single mode never keeps >1 key.
|
||||
*/
|
||||
export function useToggleButtonSelection(options: {
|
||||
isSingle: () => boolean;
|
||||
read: () => ToggleButtonGroupSelection;
|
||||
defaults: () => ToggleButtonGroupSelection;
|
||||
keys: string[];
|
||||
disallowEmptySelection: () => boolean;
|
||||
emit: (value: ToggleButtonGroupKey[] | ToggleButtonGroupKey | null) => void;
|
||||
}) {
|
||||
const instance = getCurrentInstance();
|
||||
const copy = <T>(value: T): T =>
|
||||
(Array.isArray(value) ? [...value] : value) as T;
|
||||
|
||||
const initial = copy(normalize(options.defaults(), options.isSingle())) as
|
||||
ToggleButtonGroupKey[] | ToggleButtonGroupKey | null;
|
||||
const internal = ref(initial) as Ref<
|
||||
ToggleButtonGroupKey[] | ToggleButtonGroupKey | null
|
||||
>;
|
||||
|
||||
const controlled = () =>
|
||||
options.keys.some((key) => Object.hasOwn(instance?.vnode.props ?? {}, key));
|
||||
const isControlled = ref(controlled());
|
||||
onBeforeUpdate(() => {
|
||||
isControlled.value = controlled();
|
||||
});
|
||||
|
||||
const value = computed(() => {
|
||||
const external = normalize(options.read(), options.isSingle());
|
||||
return isControlled.value ? external : internal.value;
|
||||
});
|
||||
|
||||
// Reconcile uncontrolled internal shape when selectionMode flips.
|
||||
watch(
|
||||
() => options.isSingle(),
|
||||
(single) => {
|
||||
if (controlled()) return;
|
||||
const list = toArray(internal.value);
|
||||
const next = single ? (list[0] ?? null) : list;
|
||||
internal.value = copy(next);
|
||||
},
|
||||
);
|
||||
|
||||
function selected(key: ToggleButtonGroupKey): boolean {
|
||||
return toArray(value.value).some((item) => sameKey(item, key));
|
||||
}
|
||||
|
||||
function request(key: ToggleButtonGroupKey) {
|
||||
const single = options.isSingle();
|
||||
const current = toArray(value.value);
|
||||
let next: ToggleButtonGroupKey[];
|
||||
|
||||
if (single) {
|
||||
const already = current.some((item) => sameKey(item, key));
|
||||
if (already && options.disallowEmptySelection()) return;
|
||||
next = already ? [] : [key];
|
||||
} else {
|
||||
const index = current.findIndex((item) => sameKey(item, key));
|
||||
if (index >= 0) {
|
||||
if (current.length === 1 && options.disallowEmptySelection()) return;
|
||||
next = current.filter((_, i) => i !== index);
|
||||
} else {
|
||||
next = [...current, key];
|
||||
}
|
||||
}
|
||||
|
||||
const payload = single ? (next[0] ?? null) : next;
|
||||
if (!controlled()) internal.value = copy(payload);
|
||||
options.emit(copy(payload));
|
||||
}
|
||||
|
||||
return { value, selected, request };
|
||||
}
|
||||
|
||||
export interface ToggleMember {
|
||||
key: ToggleButtonGroupKey;
|
||||
el: () => HTMLElement | undefined;
|
||||
disabled: ComputedRef<boolean>;
|
||||
}
|
||||
|
||||
function domOrder(members: ToggleMember[]): ToggleMember[] {
|
||||
const placed = members.map((m) => ({ m, el: m.el() }));
|
||||
const withEl = placed.filter(
|
||||
(p): p is { m: ToggleMember; el: HTMLElement } => !!p.el,
|
||||
);
|
||||
const without = placed.filter((p) => !p.el).map((p) => p.m);
|
||||
withEl.sort((a, b) => {
|
||||
const pos = a.el.compareDocumentPosition(b.el);
|
||||
if (pos & Node.DOCUMENT_POSITION_FOLLOWING) return -1;
|
||||
if (pos & Node.DOCUMENT_POSITION_PRECEDING) return 1;
|
||||
return 0;
|
||||
});
|
||||
return [...withEl.map((p) => p.m), ...without];
|
||||
}
|
||||
|
||||
/**
|
||||
* Roving tabindex with a single Tab stop per group.
|
||||
* Navigation follows live DOM order; real focus updates the Tab entry.
|
||||
* Arrow keys follow orientation + dir; cancelled events are ignored.
|
||||
*/
|
||||
export function useToggleRovingFocus(options: {
|
||||
orientation: () => "horizontal" | "vertical";
|
||||
dir: () => "ltr" | "rtl";
|
||||
}) {
|
||||
const members = new Map<ToggleButtonGroupKey, ToggleMember>();
|
||||
const version = ref(0);
|
||||
const active = ref<ToggleButtonGroupKey | null>(null);
|
||||
const stops = new Map<ToggleButtonGroupKey, () => void>();
|
||||
|
||||
function bump() {
|
||||
version.value++;
|
||||
}
|
||||
|
||||
function register(member: ToggleMember) {
|
||||
members.set(member.key, member);
|
||||
bump();
|
||||
const stop = watch(
|
||||
member.disabled,
|
||||
() => {
|
||||
if (member.disabled.value && active.value === member.key) {
|
||||
active.value = ordered()[0]?.key ?? null;
|
||||
}
|
||||
bump();
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
stops.set(member.key, stop);
|
||||
if (
|
||||
active.value == null ||
|
||||
!members.has(active.value) ||
|
||||
members.get(active.value)?.disabled.value
|
||||
) {
|
||||
active.value = ordered()[0]?.key ?? member.key;
|
||||
}
|
||||
return () => {
|
||||
stop();
|
||||
stops.delete(member.key);
|
||||
members.delete(member.key);
|
||||
if (active.value === member.key) {
|
||||
active.value = ordered()[0]?.key ?? null;
|
||||
}
|
||||
bump();
|
||||
};
|
||||
}
|
||||
|
||||
function ordered(): ToggleMember[] {
|
||||
void version.value;
|
||||
return domOrder([...members.values()].filter((m) => !m.disabled.value));
|
||||
}
|
||||
|
||||
/** Sync Tab entry to a real focus target without re-calling focus(). */
|
||||
function noteFocus(key: ToggleButtonGroupKey) {
|
||||
const member = members.get(key);
|
||||
if (!member || member.disabled.value) return;
|
||||
if (active.value === key) return;
|
||||
active.value = key;
|
||||
bump();
|
||||
}
|
||||
|
||||
function tabIndex(key: ToggleButtonGroupKey): number {
|
||||
const list = ordered();
|
||||
if (!list.length) return 0;
|
||||
const current = active.value ?? list[0]!.key;
|
||||
return sameKey(key, current) ? 0 : -1;
|
||||
}
|
||||
|
||||
function focusKey(key: ToggleButtonGroupKey) {
|
||||
active.value = key;
|
||||
bump();
|
||||
members.get(key)?.el()?.focus();
|
||||
}
|
||||
|
||||
function keydown(event: KeyboardEvent, key: ToggleButtonGroupKey) {
|
||||
if (event.defaultPrevented) return;
|
||||
const list = ordered();
|
||||
if (!list.length) return;
|
||||
const index = list.findIndex((m) => sameKey(m.key, key));
|
||||
if (index < 0) return;
|
||||
|
||||
const horizontal = options.orientation() === "horizontal";
|
||||
const rtl = options.dir() === "rtl";
|
||||
let nextIndex = index;
|
||||
|
||||
if (horizontal) {
|
||||
switch (event.key) {
|
||||
case "ArrowRight":
|
||||
nextIndex = rtl
|
||||
? (index - 1 + list.length) % list.length
|
||||
: (index + 1) % list.length;
|
||||
break;
|
||||
case "ArrowLeft":
|
||||
nextIndex = rtl
|
||||
? (index + 1) % list.length
|
||||
: (index - 1 + list.length) % list.length;
|
||||
break;
|
||||
case "Home":
|
||||
nextIndex = 0;
|
||||
break;
|
||||
case "End":
|
||||
nextIndex = list.length - 1;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
switch (event.key) {
|
||||
case "ArrowDown":
|
||||
nextIndex = (index + 1) % list.length;
|
||||
break;
|
||||
case "ArrowUp":
|
||||
nextIndex = (index - 1 + list.length) % list.length;
|
||||
break;
|
||||
case "Home":
|
||||
nextIndex = 0;
|
||||
break;
|
||||
case "End":
|
||||
nextIndex = list.length - 1;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
const next = list[nextIndex]!;
|
||||
focusKey(next.key);
|
||||
}
|
||||
|
||||
return { register, tabIndex, keydown, noteFocus, active, version };
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, inject, onBeforeUnmount, ref, useAttrs, watch } from "vue";
|
||||
import { Primitive } from "reka-ui";
|
||||
import {
|
||||
toggleButtonVariants,
|
||||
type ToggleButtonVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import {
|
||||
toggleButtonGroupKey,
|
||||
type ToggleButtonGroupKey,
|
||||
} from "../toggle-button-group/context";
|
||||
|
||||
defineOptions({ inheritAttrs: false });
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** Controlled selected state (standalone). */
|
||||
modelValue?: boolean | null;
|
||||
isSelected?: boolean | null;
|
||||
defaultValue?: boolean;
|
||||
defaultSelected?: boolean;
|
||||
isDisabled?: boolean;
|
||||
disabled?: boolean;
|
||||
isIconOnly?: boolean;
|
||||
size?: ToggleButtonVariants["size"];
|
||||
variant?: ToggleButtonVariants["variant"];
|
||||
/** Group member key (upstream `id` in selectedKeys). Not used as DOM id. */
|
||||
id?: ToggleButtonGroupKey;
|
||||
value?: ToggleButtonGroupKey;
|
||||
asChild?: boolean;
|
||||
/** Absorbs ButtonGroup-style child markers; not a public API. */
|
||||
__button_group_child?: boolean;
|
||||
}>(),
|
||||
{
|
||||
modelValue: undefined,
|
||||
isSelected: undefined,
|
||||
variant: undefined,
|
||||
size: undefined,
|
||||
isDisabled: undefined,
|
||||
disabled: undefined,
|
||||
},
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
"update:modelValue": [value: boolean];
|
||||
change: [value: boolean];
|
||||
}>();
|
||||
|
||||
const attrs = useAttrs();
|
||||
const group = inject(toggleButtonGroupKey, undefined);
|
||||
const groupKey = computed<ToggleButtonGroupKey | null>(() => {
|
||||
const key = props.value ?? props.id;
|
||||
return key === undefined || key === null ? null : key;
|
||||
});
|
||||
const inGroup = computed(() => Boolean(group) && groupKey.value !== null);
|
||||
|
||||
const initial = props.defaultSelected ?? props.defaultValue ?? false;
|
||||
const local = ref(Boolean(initial));
|
||||
const controlled = computed(
|
||||
() => props.modelValue !== undefined || props.isSelected !== undefined,
|
||||
);
|
||||
const standaloneSelected = computed(() =>
|
||||
controlled.value
|
||||
? Boolean(props.isSelected ?? props.modelValue)
|
||||
: local.value,
|
||||
);
|
||||
|
||||
const selected = computed(() =>
|
||||
inGroup.value ? group!.selected(groupKey.value!) : standaloneSelected.value,
|
||||
);
|
||||
|
||||
const groupDisabled = computed(() =>
|
||||
inGroup.value ? group!.isDisabled.value : false,
|
||||
);
|
||||
const disabled = computed(() =>
|
||||
Boolean(props.isDisabled || props.disabled || groupDisabled.value),
|
||||
);
|
||||
|
||||
const isSingleInGroup = computed(
|
||||
() => Boolean(group) && group!.isSingle.value && groupKey.value !== null,
|
||||
);
|
||||
|
||||
function request(e: MouseEvent) {
|
||||
if (e.defaultPrevented || disabled.value) return;
|
||||
if (inGroup.value) {
|
||||
group!.change(groupKey.value!);
|
||||
return;
|
||||
}
|
||||
const next = !selected.value;
|
||||
if (!controlled.value) local.value = next;
|
||||
emit("update:modelValue", next);
|
||||
emit("change", next);
|
||||
}
|
||||
|
||||
const hovered = ref(false);
|
||||
const pressed = ref(false);
|
||||
const focused = ref(false);
|
||||
const focusVisible = ref(false);
|
||||
const root = ref<HTMLElement | undefined>();
|
||||
let unregister: (() => void) | undefined;
|
||||
|
||||
function setRoot(el: unknown) {
|
||||
const node = el as { $el?: HTMLElement } | HTMLElement | null;
|
||||
if (!node) {
|
||||
root.value = undefined;
|
||||
return;
|
||||
}
|
||||
root.value =
|
||||
node instanceof HTMLElement
|
||||
? node
|
||||
: ((node.$el as HTMLElement) ?? undefined);
|
||||
}
|
||||
|
||||
watch(
|
||||
disabled,
|
||||
(isDisabled) => {
|
||||
if (isDisabled) {
|
||||
hovered.value = false;
|
||||
pressed.value = false;
|
||||
}
|
||||
},
|
||||
{ flush: "sync" },
|
||||
);
|
||||
|
||||
function bindRegistration() {
|
||||
unregister?.();
|
||||
unregister = undefined;
|
||||
if (!inGroup.value || !group || groupKey.value === null) return;
|
||||
unregister = group.register(groupKey.value, () => root.value, disabled);
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [inGroup.value, groupKey.value] as const,
|
||||
() => {
|
||||
bindRegistration();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onBeforeUnmount(() => unregister?.());
|
||||
|
||||
function guard(e: MouseEvent) {
|
||||
if (disabled.value) {
|
||||
e.preventDefault();
|
||||
e.stopImmediatePropagation();
|
||||
}
|
||||
}
|
||||
function focus(e: FocusEvent) {
|
||||
focused.value = true;
|
||||
focusVisible.value = (e.target as HTMLElement).matches(":focus-visible");
|
||||
if (inGroup.value && group && groupKey.value !== null) {
|
||||
group.noteFocus(groupKey.value);
|
||||
}
|
||||
}
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
if (e.defaultPrevented) return;
|
||||
if (!inGroup.value || !group || groupKey.value === null) return;
|
||||
group.keydown(e, groupKey.value);
|
||||
}
|
||||
|
||||
const finalSize = computed(() => props.size ?? group?.size.value ?? "md");
|
||||
const finalVariant = computed(() => props.variant ?? "default");
|
||||
|
||||
/** Independent buttons keep the public `id` as DOM id; group keys stay logical. */
|
||||
const domId = computed(() =>
|
||||
inGroup.value ? (attrs.id as string | undefined) : props.id,
|
||||
);
|
||||
const tabIndex = computed(() =>
|
||||
inGroup.value && group && groupKey.value !== null
|
||||
? group.tabIndex(groupKey.value)
|
||||
: undefined,
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<Primitive
|
||||
:ref="setRoot"
|
||||
as="button"
|
||||
:as-child="asChild"
|
||||
v-bind="$attrs"
|
||||
:id="domId"
|
||||
type="button"
|
||||
:class="
|
||||
toggleButtonVariants({
|
||||
isIconOnly,
|
||||
size: finalSize,
|
||||
variant: finalVariant,
|
||||
})
|
||||
"
|
||||
:disabled="disabled"
|
||||
:role="isSingleInGroup ? 'radio' : 'button'"
|
||||
:aria-checked="isSingleInGroup ? String(selected) : undefined"
|
||||
:aria-pressed="isSingleInGroup ? undefined : String(selected)"
|
||||
:aria-disabled="disabled || undefined"
|
||||
:tabindex="tabIndex"
|
||||
:data-selected="selected || undefined"
|
||||
:data-hovered="(!disabled && hovered) || undefined"
|
||||
:data-pressed="(!disabled && pressed) || undefined"
|
||||
:data-focused="focused || undefined"
|
||||
:data-focus-visible="focusVisible || undefined"
|
||||
:data-disabled="disabled || undefined"
|
||||
data-slot="toggle-button"
|
||||
@click.capture="guard"
|
||||
@click="request"
|
||||
@pointerenter="hovered = !disabled"
|
||||
@pointerleave="
|
||||
hovered = false;
|
||||
pressed = false;
|
||||
"
|
||||
@pointerdown="pressed = !disabled"
|
||||
@pointerup="pressed = false"
|
||||
@keydown.space="pressed = !disabled"
|
||||
@keydown.enter="pressed = !disabled"
|
||||
@keydown="onKeydown"
|
||||
@keyup="pressed = false"
|
||||
@focus="focus"
|
||||
@blur="
|
||||
focused = false;
|
||||
focusVisible = false;
|
||||
pressed = false;
|
||||
"
|
||||
>
|
||||
<slot
|
||||
:is-selected="Boolean(selected)"
|
||||
:is-pressed="pressed"
|
||||
:is-hovered="hovered"
|
||||
:is-focused="focused"
|
||||
:is-focus-visible="focusVisible"
|
||||
:is-disabled="Boolean(disabled)"
|
||||
/>
|
||||
</Primitive>
|
||||
</template>
|
||||
@@ -499,3 +499,31 @@ export type {
|
||||
TabsIndicatorProps,
|
||||
TabsSeparatorProps,
|
||||
} from "./types";
|
||||
|
||||
export { default as ButtonGroup } from "./components/button-group/ButtonGroup.vue";
|
||||
export { default as ButtonGroupSeparator } from "./components/button-group/ButtonGroupSeparator.vue";
|
||||
export type { ButtonGroupContextValue } from "./components/button-group/context";
|
||||
export {
|
||||
buttonGroupVariants,
|
||||
toggleButtonVariants,
|
||||
toggleButtonGroupVariants,
|
||||
type ButtonGroupVariants,
|
||||
type ToggleButtonVariants,
|
||||
type ToggleButtonGroupVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
|
||||
export { default as ToggleButton } from "./components/toggle-button/ToggleButton.vue";
|
||||
export { default as ToggleButtonGroup } from "./components/toggle-button-group/ToggleButtonGroup.vue";
|
||||
export { default as ToggleButtonGroupSeparator } from "./components/toggle-button-group/ToggleButtonGroupSeparator.vue";
|
||||
export type {
|
||||
ToggleButtonGroupKey,
|
||||
ToggleButtonGroupContextValue,
|
||||
} from "./components/toggle-button-group/context";
|
||||
|
||||
export type {
|
||||
ButtonGroupProps,
|
||||
ButtonGroupSeparatorProps,
|
||||
ToggleButtonProps,
|
||||
ToggleButtonGroupProps,
|
||||
ToggleButtonGroupSeparatorProps,
|
||||
} from "./types";
|
||||
|
||||
@@ -374,3 +374,21 @@ export type TabsIndicatorProps = InstanceType<typeof TabsIndicator>["$props"];
|
||||
|
||||
import type TabsSeparator from "./components/tabs/TabsSeparator.vue";
|
||||
export type TabsSeparatorProps = InstanceType<typeof TabsSeparator>["$props"];
|
||||
|
||||
import type ButtonGroup from "./components/button-group/ButtonGroup.vue";
|
||||
import type ButtonGroupSeparator from "./components/button-group/ButtonGroupSeparator.vue";
|
||||
import type ToggleButton from "./components/toggle-button/ToggleButton.vue";
|
||||
import type ToggleButtonGroup from "./components/toggle-button-group/ToggleButtonGroup.vue";
|
||||
import type ToggleButtonGroupSeparator from "./components/toggle-button-group/ToggleButtonGroupSeparator.vue";
|
||||
|
||||
export type ButtonGroupProps = InstanceType<typeof ButtonGroup>["$props"];
|
||||
export type ButtonGroupSeparatorProps = InstanceType<
|
||||
typeof ButtonGroupSeparator
|
||||
>["$props"];
|
||||
export type ToggleButtonProps = InstanceType<typeof ToggleButton>["$props"];
|
||||
export type ToggleButtonGroupProps = InstanceType<
|
||||
typeof ToggleButtonGroup
|
||||
>["$props"];
|
||||
export type ToggleButtonGroupSeparatorProps = InstanceType<
|
||||
typeof ToggleButtonGroupSeparator
|
||||
>["$props"];
|
||||
|
||||
Reference in New Issue
Block a user