72 lines
2.1 KiB
Vue
72 lines
2.1 KiB
Vue
<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>
|