feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -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");
|
||||
Reference in New Issue
Block a user