feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from "vue";
|
||||
import { Primitive } from "reka-ui";
|
||||
import type { ButtonHTMLAttributes } from "vue";
|
||||
import { buttonVariants, type ButtonVariants } from "@heroui-vue/styles";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
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"];
|
||||
}>(),
|
||||
{ type: "button" },
|
||||
);
|
||||
const pending = computed(() => props.isPending || props.loading);
|
||||
const disabled = computed(() => props.isDisabled || props.disabled);
|
||||
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) {
|
||||
e.preventDefault();
|
||||
e.stopImmediatePropagation();
|
||||
}
|
||||
}
|
||||
function focus(e: FocusEvent) {
|
||||
focused.value = true;
|
||||
focusVisible.value = (e.target as HTMLElement).matches(":focus-visible");
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<Primitive
|
||||
as="button"
|
||||
:as-child="asChild"
|
||||
v-bind="$attrs"
|
||||
:type="type"
|
||||
:class="buttonVariants({ variant, size, fullWidth, 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"
|
||||
@click.capture="guard"
|
||||
@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"
|
||||
@blur="
|
||||
focused = false;
|
||||
focusVisible = false;
|
||||
pressed = false;
|
||||
"
|
||||
>
|
||||
<span v-if="loading && !asChild" class="hv-spinner" aria-hidden="true" />
|
||||
<slot
|
||||
:is-pending="Boolean(pending)"
|
||||
:is-disabled="Boolean(disabled)"
|
||||
:is-hovered="hovered"
|
||||
:is-pressed="pressed"
|
||||
:is-focused="focused"
|
||||
:is-focus-visible="focusVisible"
|
||||
/>
|
||||
</Primitive>
|
||||
</template>
|
||||
Reference in New Issue
Block a user