210 lines
5.9 KiB
Vue
210 lines
5.9 KiB
Vue
<script setup lang="ts">
|
|
import { useInheritedDisabled } from "../field/disabled";
|
|
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"];
|
|
size?: ButtonVariants["size"];
|
|
fullWidth?: boolean;
|
|
isDisabled?: boolean;
|
|
isPending?: boolean;
|
|
isIconOnly?: boolean;
|
|
asChild?: boolean;
|
|
disabled?: boolean;
|
|
loading?: boolean;
|
|
type?: ButtonHTMLAttributes["type"];
|
|
/** Marks a direct ButtonGroup child (see BUTTON_GROUP_CHILD). */
|
|
__button_group_child?: boolean;
|
|
}>(),
|
|
{
|
|
type: "button",
|
|
variant: undefined,
|
|
size: undefined,
|
|
fullWidth: undefined,
|
|
isDisabled: undefined,
|
|
disabled: undefined,
|
|
},
|
|
);
|
|
const buttonGroupContext = inject(buttonGroupContextKey, undefined);
|
|
const toolbar = inject(toolbarKey, 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(() => 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),
|
|
focusVisible = ref(false);
|
|
watch(
|
|
[pending, disabled],
|
|
([isPending, isDisabled]) => {
|
|
if (isPending || isDisabled) {
|
|
hovered.value = false;
|
|
pressed.value = false;
|
|
}
|
|
},
|
|
{ flush: "sync" },
|
|
);
|
|
function guard(e: MouseEvent) {
|
|
if (pending.value || disabled.value) {
|
|
e.preventDefault();
|
|
e.stopImmediatePropagation();
|
|
}
|
|
}
|
|
function focus(e: FocusEvent) {
|
|
focused.value = true;
|
|
focusVisible.value = (e.target as HTMLElement).matches(":focus-visible");
|
|
}
|
|
</script>
|
|
<template>
|
|
<Primitive
|
|
:ref="setRoot"
|
|
@click.capture="guard"
|
|
as="button"
|
|
:as-child="asChild"
|
|
v-bind="rootBind()"
|
|
:type="type"
|
|
:class="
|
|
buttonVariants({
|
|
variant: finalVariant,
|
|
size: finalSize,
|
|
fullWidth: finalFullWidth,
|
|
isIconOnly,
|
|
})
|
|
"
|
|
:disabled="disabled"
|
|
:aria-disabled="pending || disabled || undefined"
|
|
:aria-busy="pending || undefined"
|
|
:data-pending="pending || undefined"
|
|
:data-hovered="(!pending && !disabled && hovered) || undefined"
|
|
:data-pressed="(!pending && !disabled && pressed) || undefined"
|
|
:data-focused="focused || undefined"
|
|
:data-focus-visible="focusVisible || undefined"
|
|
data-slot="button"
|
|
@pointerenter="hovered = !pending && !disabled"
|
|
@pointerleave="
|
|
hovered = false;
|
|
pressed = false;
|
|
"
|
|
@pointerdown="pressed = !pending && !disabled"
|
|
@pointerup="pressed = false"
|
|
@keydown.space="pressed = !pending && !disabled"
|
|
@keydown.enter="pressed = !pending && !disabled"
|
|
@keyup="pressed = false"
|
|
@focus="
|
|
focus($event);
|
|
noteToolbarFocus($event);
|
|
"
|
|
@blur="
|
|
focused = false;
|
|
focusVisible = false;
|
|
pressed = false;
|
|
"
|
|
>
|
|
<Spinner
|
|
v-if="loading && !asChild"
|
|
aria-hidden="true"
|
|
color="current"
|
|
size="sm"
|
|
/>
|
|
<slot
|
|
:is-pending="Boolean(pending)"
|
|
:is-disabled="Boolean(disabled)"
|
|
:is-hovered="hovered"
|
|
:is-pressed="pressed"
|
|
:is-focused="focused"
|
|
:is-focus-visible="focusVisible"
|
|
/>
|
|
</Primitive>
|
|
</template>
|