feat: initialize Vue component library and aligned documentation

This commit is contained in:
maoyingjie
2026-10-21 09:06:28 +08:00
commit 9d40dc0ecb
149 changed files with 13208 additions and 0 deletions
@@ -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>
+16
View File
@@ -0,0 +1,16 @@
<script setup lang="ts">
import { cardVariants, type CardVariants } from "@heroui-vue/styles";
withDefaults(
defineProps<{ variant?: CardVariants["variant"]; as?: string }>(),
{ variant: "default", as: "div" },
);
</script>
<template>
<component
:is="as"
:class="cardVariants({ variant }).base()"
data-slot="card"
>
<slot />
</component>
</template>
@@ -0,0 +1,8 @@
<script setup lang="ts">
withDefaults(defineProps<{ as?: string }>(), { as: "div" });
</script>
<template>
<component :is="as" class="hv-card__content" data-slot="card-content">
<slot />
</component>
</template>
@@ -0,0 +1,8 @@
<script setup lang="ts">
withDefaults(defineProps<{ as?: string }>(), { as: "p" });
</script>
<template>
<component :is="as" class="hv-card__description" data-slot="card-description">
<slot />
</component>
</template>
@@ -0,0 +1,8 @@
<script setup lang="ts">
withDefaults(defineProps<{ as?: string }>(), { as: "div" });
</script>
<template>
<component :is="as" class="hv-card__footer" data-slot="card-footer">
<slot />
</component>
</template>
@@ -0,0 +1,8 @@
<script setup lang="ts">
withDefaults(defineProps<{ as?: string }>(), { as: "div" });
</script>
<template>
<component :is="as" class="hv-card__header" data-slot="card-header">
<slot />
</component>
</template>
@@ -0,0 +1,8 @@
<script setup lang="ts">
withDefaults(defineProps<{ as?: string }>(), { as: "h3" });
</script>
<template>
<component :is="as" class="hv-card__title" data-slot="card-title">
<slot />
</component>
</template>
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { inject, onBeforeUnmount, useId, useAttrs } from "vue";
import { switchKey } from "../switch/context";
const ctx = inject(switchKey, undefined);
const attrs = useAttrs();
const id = String(attrs.id ?? useId());
if (ctx) ctx.descriptionIds.value = [...ctx.descriptionIds.value, id];
onBeforeUnmount(() => {
if (ctx)
ctx.descriptionIds.value = ctx.descriptionIds.value.filter(
(value) => value !== id,
);
});
</script>
<template>
<p :id="id" class="hv-description" data-slot="description"><slot /></p>
</template>
@@ -0,0 +1,73 @@
<script setup lang="ts">
import { inputVariants, type InputVariants } from "@heroui-vue/styles";
import {
computed,
ref,
onMounted,
onBeforeUnmount,
nextTick,
useAttrs,
} from "vue";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
modelValue?: string;
defaultValue?: string;
invalid?: boolean;
variant?: InputVariants["variant"];
fullWidth?: boolean;
type?: string;
}>();
const attrs = useAttrs();
const invalidState = computed(
() =>
props.invalid ||
attrs["aria-invalid"] === true ||
attrs["aria-invalid"] === "true",
);
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
const internal = ref(props.defaultValue ?? "");
const value = computed(() => props.modelValue ?? internal.value);
const element = ref<HTMLInputElement>();
function update(event: Event) {
const next = (event.target as HTMLInputElement).value;
internal.value = next;
emit("update:modelValue", next);
void nextTick(() => {
if (element.value && element.value.value !== value.value)
element.value.value = value.value;
});
}
function reset(event: Event) {
void nextTick(() => {
if (event.defaultPrevented) return;
internal.value = props.defaultValue ?? "";
if (element.value) element.value.value = value.value;
});
}
let form: HTMLFormElement | null = null;
onMounted(() => {
form = element.value?.form ?? null;
form?.addEventListener("reset", reset);
});
onBeforeUnmount(() => form?.removeEventListener("reset", reset));
defineExpose({ focus: () => element.value?.focus(), element });
</script>
<template>
<input
ref="element"
v-bind="$attrs"
:value="value"
:class="inputVariants({ variant, fullWidth })"
:type="type ?? 'text'"
:data-invalid="invalidState || undefined"
:aria-invalid="invalidState || undefined"
:aria-readonly="
$attrs.readonly === '' ||
$attrs.readonly === true ||
$attrs.readOnly === true ||
undefined
"
data-slot="input"
@input="update"
/>
</template>
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { inject, useAttrs } from "vue";
import { switchKey } from "../switch/context";
const ctx = inject(switchKey, undefined);
const attrs = useAttrs();
defineProps<{
isRequired?: boolean;
isDisabled?: boolean;
isInvalid?: boolean;
}>();
</script>
<template>
<component
:is="ctx && !attrs.for ? 'span' : 'label'"
class="hv-label"
:class="{
'hv-label--required': isRequired,
'hv-label--disabled': isDisabled,
'hv-label--invalid': isInvalid,
}"
data-slot="label"
>
<slot />
</component>
</template>
@@ -0,0 +1,24 @@
<script setup lang="ts">
import { inject, onBeforeUnmount, useId } from "vue";
import { switchKey } from "./context";
const ctx = inject(switchKey, undefined);
const id = useId();
if (ctx) ctx.errorIds.value = [...ctx.errorIds.value, id];
onBeforeUnmount(() => {
if (ctx)
ctx.errorIds.value = ctx.errorIds.value.filter((value) => value !== id);
});
</script>
<template>
<p
v-if="!ctx || ctx.invalid.value"
:id="id"
data-slot="field-error"
class="hv-field-error"
role="alert"
>
<slot :validation-errors="ctx?.error.value ? [ctx.error.value] : []">
{{ ctx?.error.value }}
</slot>
</p>
</template>
@@ -0,0 +1,201 @@
<script setup lang="ts">
import {
computed,
provide,
ref,
useId,
useAttrs,
onMounted,
onBeforeUnmount,
nextTick,
watchEffect,
watch,
} from "vue";
import { switchVariants, type SwitchVariants } from "@heroui-vue/styles";
import { Primitive } from "reka-ui";
import { switchKey } from "./context";
import SwitchContent from "./SwitchContent.vue";
import SwitchControl from "./SwitchControl.vue";
import SwitchThumb from "./SwitchThumb.vue";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
checked?: boolean;
isSelected?: boolean;
defaultChecked?: boolean;
defaultSelected?: boolean;
isDisabled?: boolean;
disabled?: boolean;
isReadOnly?: boolean;
isInvalid?: boolean;
isRequired?: boolean;
required?: boolean;
name?: string;
value?: string;
id?: string;
size?: SwitchVariants["size"];
asChild?: boolean;
validate?: (value: boolean) => string | string[] | true | null | undefined;
validationBehavior?: "native" | "aria";
}>(),
{
checked: undefined,
isSelected: undefined,
defaultChecked: undefined,
defaultSelected: undefined,
},
);
const emit = defineEmits<{
"update:checked": [value: boolean];
"update:isSelected": [value: boolean];
change: [value: boolean];
}>();
const attrs = useAttrs();
const rootAttrs = () =>
Object.fromEntries(
Object.entries(attrs).filter(
([key]) => key === "class" || key === "style" || key.startsWith("data-"),
),
);
const controlAttrs = () =>
Object.fromEntries(
Object.entries(attrs).filter(
([key]) => key !== "class" && key !== "style" && !key.startsWith("data-"),
),
);
const initial = props.defaultSelected ?? props.defaultChecked ?? false;
const internal = ref(initial);
const element = computed(
() => input.value?.previousElementSibling as HTMLElement | undefined,
);
const input = ref<HTMLInputElement>();
const hovered = ref(false),
pressed = ref(false),
focusVisible = ref(false);
const error = ref("");
const showError = ref(false);
const checked = computed(
() => props.isSelected ?? props.checked ?? internal.value,
);
const disabled = computed(() => props.isDisabled || props.disabled);
const readOnly = computed(() => props.isReadOnly);
const required = computed(() => props.isRequired || props.required);
const invalid = computed(
() => props.isInvalid || (showError.value && Boolean(error.value)),
);
const generated = useId();
const descriptionIds = ref<string[]>([]);
const errorIds = ref<string[]>([]);
function update(value: boolean) {
if (disabled.value || readOnly.value) return;
internal.value = value;
emit("update:checked", value);
emit("update:isSelected", value);
emit("change", value);
}
watch([disabled, readOnly], () => {
hovered.value = false;
pressed.value = false;
});
provide(switchKey, {
hovered,
pressed,
focusVisible,
descriptionIds,
errorIds,
error,
controlAttrs,
checked,
disabled,
readOnly,
required,
invalid,
id: computed(() => props.id ?? generated),
update,
});
watchEffect(() => {
const result = props.validate?.(checked.value);
error.value =
typeof result === "string"
? result
: Array.isArray(result)
? result.join(" ")
: required.value && !checked.value
? "请开启此选项。"
: "";
if (props.validationBehavior === "aria") showError.value = true;
input.value?.setCustomValidity(
props.validationBehavior === "aria" || disabled.value || readOnly.value
? ""
: error.value,
);
});
function onInvalid(event: Event) {
event.preventDefault();
showError.value = true;
element.value?.querySelector<HTMLButtonElement>("[role=switch]")?.focus();
}
function reset(event: Event) {
void nextTick(() => {
if (event.defaultPrevented) return;
internal.value = initial;
showError.value = false;
if (input.value) input.value.checked = checked.value;
});
}
let form: HTMLFormElement | null = null;
onMounted(() => {
form = input.value?.form ?? null;
form?.addEventListener("reset", reset);
});
onBeforeUnmount(() => form?.removeEventListener("reset", reset));
defineExpose({
focus: () =>
element.value?.querySelector<HTMLButtonElement>("[role=switch]")?.focus(),
});
</script>
<template>
<Primitive
as="div"
:as-child="asChild"
v-bind="rootAttrs()"
:class="switchVariants({ size }).base()"
:data-selected="checked"
:data-disabled="disabled || undefined"
:data-readonly="readOnly || undefined"
:data-invalid="invalid || undefined"
:data-required="required || undefined"
data-slot="switch"
>
<slot
:is-selected="checked"
:is-disabled="Boolean(disabled)"
:is-read-only="Boolean(readOnly)"
:is-invalid="Boolean(invalid)"
:is-required="Boolean(required)"
:state="{
isSelected: checked,
setSelected: update,
toggle: () => update(!checked),
}"
>
<SwitchContent>
<SwitchControl><SwitchThumb /></SwitchControl>
</SwitchContent>
</slot>
</Primitive>
<input
ref="input"
class="hv-switch__input"
type="checkbox"
tabindex="-1"
aria-hidden="true"
:name="name"
:value="value ?? 'on'"
:checked="checked"
:disabled="disabled"
:required="required && !readOnly && validationBehavior !== 'aria'"
@invalid="onInvalid"
@change="update(($event.target as HTMLInputElement).checked)"
/>
</template>
@@ -0,0 +1,71 @@
<script setup lang="ts">
import { inject } from "vue";
import { SwitchRoot } from "reka-ui";
import { switchKey } from "./context";
const ctx = inject(switchKey);
if (!ctx) throw new Error("SwitchContent must be inside Switch");
function guard(event: Event) {
if (ctx?.readOnly.value) {
event.preventDefault();
event.stopImmediatePropagation();
}
}
</script>
<template>
<SwitchRoot
:id="ctx.id.value"
v-bind="ctx.controlAttrs()"
class="hv-switch__content"
data-slot="switch-content"
:model-value="ctx.checked.value"
:disabled="ctx.disabled.value"
:aria-required="ctx.required.value || undefined"
:aria-describedby="
[
ctx.controlAttrs()['aria-describedby'],
...ctx.descriptionIds.value,
...(ctx.invalid.value ? ctx.errorIds.value : []),
]
.filter(Boolean)
.join(' ') || undefined
"
:aria-readonly="ctx.readOnly.value || undefined"
:aria-invalid="ctx.invalid.value || undefined"
:data-hovered="ctx.hovered.value || undefined"
:data-pressed="ctx.pressed.value || undefined"
:data-focus-visible="ctx.focusVisible.value || undefined"
@pointerenter="ctx.hovered.value = !ctx.disabled.value"
@pointerleave="
ctx.hovered.value = false;
ctx.pressed.value = false;
"
@pointerdown="
ctx.pressed.value = !ctx.disabled.value && !ctx.readOnly.value
"
@pointerup="ctx.pressed.value = false"
@focus="
ctx.focusVisible.value = ($event.target as HTMLElement).matches(
':focus-visible',
)
"
@blur="
ctx.focusVisible.value = false;
ctx.pressed.value = false;
"
@keydown.space="
ctx.pressed.value = !ctx.disabled.value && !ctx.readOnly.value
"
@keyup="ctx.pressed.value = false"
@click.capture="guard"
@keydown.enter.capture="guard"
@keydown.space.capture="guard"
@update:model-value="ctx.update"
>
<slot
:is-selected="ctx.checked.value"
:is-hovered="ctx.hovered.value"
:is-pressed="ctx.pressed.value"
:is-focus-visible="ctx.focusVisible.value"
/>
</SwitchRoot>
</template>
@@ -0,0 +1,16 @@
<script setup lang="ts">
import { inject } from "vue";
import { switchKey } from "./context";
const ctx = inject(switchKey);
if (!ctx) throw new Error("SwitchControl must be inside Switch");
</script>
<template>
<span class="hv-switch__control" data-slot="switch-control">
<slot
:is-selected="ctx.checked.value"
:is-hovered="ctx.hovered.value"
:is-pressed="ctx.pressed.value"
:is-focus-visible="ctx.focusVisible.value"
/>
</span>
</template>
@@ -0,0 +1,17 @@
<script setup lang="ts">
withDefaults(defineProps<{ orientation?: "horizontal" | "vertical" }>(), {
orientation: "vertical",
});
</script>
<template>
<div
role="group"
class="hv-switch-group"
:class="`hv-switch-group--${orientation}`"
data-slot="switch-group"
>
<div class="hv-switch-group__items" data-slot="switch-group-items">
<slot />
</div>
</div>
</template>
@@ -0,0 +1,3 @@
<template>
<span class="hv-switch__icon" data-slot="switch-icon"><slot /></span>
</template>
@@ -0,0 +1,3 @@
<template>
<span class="hv-switch__thumb" data-slot="switch-thumb"><slot /></span>
</template>
@@ -0,0 +1,19 @@
import type { ComputedRef, InjectionKey, Ref } from "vue";
export interface SwitchContext {
hovered: Ref<boolean>;
pressed: Ref<boolean>;
focusVisible: Ref<boolean>;
checked: ComputedRef<boolean>;
disabled: ComputedRef<boolean>;
readOnly: ComputedRef<boolean>;
required: ComputedRef<boolean>;
invalid: ComputedRef<boolean>;
descriptionIds: Ref<string[]>;
errorIds: Ref<string[]>;
error: Ref<string>;
controlAttrs: () => Record<string, unknown>;
id: ComputedRef<string>;
update: (value: boolean) => void;
}
export const switchKey: InjectionKey<SwitchContext> =
Symbol("HeroUIVue.Switch");