refactor: rewrite Checkbox and Switch without reka-ui
Both become plain role="checkbox"/role="switch" buttons driven by useVModel, matching the existing Input.vue controlled-value pattern. Classes and data-state/aria-checked contract are unchanged.
This commit is contained in:
parent
2341ceebe0
commit
8175db2cde
2 changed files with 71 additions and 43 deletions
|
|
@ -1,34 +1,50 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { CheckIcon } from '@lucide/vue';
|
import { CheckIcon } from '@lucide/vue'
|
||||||
|
import type { HTMLAttributes } from 'vue'
|
||||||
|
import { useVModel } from '@vueuse/core'
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
import type { CheckboxRootEmits, CheckboxRootProps } from "reka-ui"
|
const props = defineProps<{
|
||||||
import type { HTMLAttributes } from "vue"
|
modelValue?: boolean
|
||||||
import { reactiveOmit } from "@vueuse/core"
|
defaultValue?: boolean
|
||||||
import { CheckboxIndicator, CheckboxRoot, useForwardPropsEmits } from "reka-ui"
|
disabled?: boolean
|
||||||
import { cn } from "@/lib/utils"
|
required?: boolean
|
||||||
|
class?: HTMLAttributes['class']
|
||||||
|
}>()
|
||||||
|
|
||||||
const props = defineProps<CheckboxRootProps & { class?: HTMLAttributes["class"] }>()
|
const emit = defineEmits<{ 'update:modelValue': [value: boolean] }>()
|
||||||
const emits = defineEmits<CheckboxRootEmits>()
|
|
||||||
|
|
||||||
const delegatedProps = reactiveOmit(props, "class")
|
const checked = useVModel(props, 'modelValue', emit, {
|
||||||
|
passive: true,
|
||||||
|
defaultValue: props.defaultValue ?? false,
|
||||||
|
})
|
||||||
|
|
||||||
const forwarded = useForwardPropsEmits(delegatedProps, emits)
|
function toggle() {
|
||||||
|
if (props.disabled) return
|
||||||
|
checked.value = !checked.value
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<CheckboxRoot
|
<button
|
||||||
v-slot="slotProps"
|
type="button"
|
||||||
|
role="checkbox"
|
||||||
data-slot="checkbox"
|
data-slot="checkbox"
|
||||||
v-bind="forwarded"
|
:aria-checked="checked"
|
||||||
|
:aria-required="required"
|
||||||
|
:data-state="checked ? 'checked' : 'unchecked'"
|
||||||
|
:disabled="disabled"
|
||||||
:class="cn('border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary aria-invalid:aria-checked:border-primary aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 flex size-4 items-center justify-center rounded-[4px] border transition-colors group-has-disabled/field:opacity-50 focus-visible:ring-3 aria-invalid:ring-3 peer relative shrink-0 outline-none after:absolute after:-inset-x-3 after:-inset-y-2 disabled:cursor-not-allowed disabled:opacity-50', props.class)"
|
:class="cn('border-input dark:bg-input/30 data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary data-checked:border-primary aria-invalid:aria-checked:border-primary aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 flex size-4 items-center justify-center rounded-[4px] border transition-colors group-has-disabled/field:opacity-50 focus-visible:ring-3 aria-invalid:ring-3 peer relative shrink-0 outline-none after:absolute after:-inset-x-3 after:-inset-y-2 disabled:cursor-not-allowed disabled:opacity-50', props.class)"
|
||||||
|
@click="toggle"
|
||||||
>
|
>
|
||||||
<CheckboxIndicator
|
<span
|
||||||
|
v-if="checked"
|
||||||
data-slot="checkbox-indicator"
|
data-slot="checkbox-indicator"
|
||||||
class="[&>svg]:size-3.5 grid place-content-center text-current transition-none"
|
class="[&>svg]:size-3.5 grid place-content-center text-current transition-none"
|
||||||
>
|
>
|
||||||
<slot v-bind="slotProps">
|
<slot>
|
||||||
<CheckIcon />
|
<CheckIcon />
|
||||||
</slot>
|
</slot>
|
||||||
</CheckboxIndicator>
|
</span>
|
||||||
</CheckboxRoot>
|
</button>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,56 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { SwitchRootEmits, SwitchRootProps } from "reka-ui"
|
import type { HTMLAttributes } from 'vue'
|
||||||
import type { HTMLAttributes } from "vue"
|
import { useVModel } from '@vueuse/core'
|
||||||
import { reactiveOmit } from "@vueuse/core"
|
import { cn } from '@/lib/utils'
|
||||||
import {
|
|
||||||
SwitchRoot,
|
|
||||||
SwitchThumb,
|
|
||||||
useForwardPropsEmits,
|
|
||||||
} from "reka-ui"
|
|
||||||
import { cn } from "@/lib/utils"
|
|
||||||
|
|
||||||
const props = withDefaults(defineProps<SwitchRootProps & {
|
const props = withDefaults(
|
||||||
class?: HTMLAttributes["class"]
|
defineProps<{
|
||||||
size?: "sm" | "default"
|
modelValue?: boolean
|
||||||
}>(), {
|
defaultValue?: boolean
|
||||||
size: "default",
|
disabled?: boolean
|
||||||
|
required?: boolean
|
||||||
|
class?: HTMLAttributes['class']
|
||||||
|
size?: 'sm' | 'default'
|
||||||
|
}>(),
|
||||||
|
{ size: 'default' },
|
||||||
|
)
|
||||||
|
|
||||||
|
const emit = defineEmits<{ 'update:modelValue': [value: boolean] }>()
|
||||||
|
|
||||||
|
const checked = useVModel(props, 'modelValue', emit, {
|
||||||
|
passive: true,
|
||||||
|
defaultValue: props.defaultValue ?? false,
|
||||||
})
|
})
|
||||||
|
|
||||||
const emits = defineEmits<SwitchRootEmits>()
|
function toggle() {
|
||||||
|
if (props.disabled) return
|
||||||
const delegatedProps = reactiveOmit(props, "class", "size")
|
checked.value = !checked.value
|
||||||
|
}
|
||||||
const forwarded = useForwardPropsEmits(delegatedProps, emits)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<SwitchRoot
|
<button
|
||||||
v-slot="slotProps"
|
type="button"
|
||||||
|
role="switch"
|
||||||
data-slot="switch"
|
data-slot="switch"
|
||||||
:data-size="size"
|
:data-size="size"
|
||||||
v-bind="forwarded"
|
:aria-checked="checked"
|
||||||
|
:aria-required="required"
|
||||||
|
:data-state="checked ? 'checked' : 'unchecked'"
|
||||||
|
:data-disabled="disabled ? '' : undefined"
|
||||||
|
:disabled="disabled"
|
||||||
:class="cn(
|
:class="cn(
|
||||||
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 dark:data-unchecked:bg-input/80 shrink-0 rounded-full border border-transparent focus-visible:ring-3 aria-invalid:ring-3 data-[size=default]:h-[18.4px] data-[size=default]:w-8 data-[size=sm]:h-3.5 data-[size=sm]:w-6 peer group/switch relative inline-flex items-center transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 data-disabled:cursor-not-allowed data-disabled:opacity-50',
|
'data-checked:bg-primary data-unchecked:bg-input focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 dark:data-unchecked:bg-input/80 shrink-0 rounded-full border border-transparent focus-visible:ring-3 aria-invalid:ring-3 data-[size=default]:h-[18.4px] data-[size=default]:w-8 data-[size=sm]:h-3.5 data-[size=sm]:w-6 peer group/switch relative inline-flex items-center transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 data-disabled:cursor-not-allowed data-disabled:opacity-50',
|
||||||
props.class,
|
props.class,
|
||||||
)"
|
)"
|
||||||
|
@click="toggle"
|
||||||
>
|
>
|
||||||
<SwitchThumb
|
<span
|
||||||
data-slot="switch-thumb"
|
data-slot="switch-thumb"
|
||||||
|
:data-state="checked ? 'checked' : 'unchecked'"
|
||||||
class="bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 pointer-events-none block ring-0 transition-transform"
|
class="bg-background dark:data-unchecked:bg-foreground dark:data-checked:bg-primary-foreground rounded-full group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 pointer-events-none block ring-0 transition-transform"
|
||||||
>
|
>
|
||||||
<slot name="thumb" v-bind="slotProps" />
|
<slot name="thumb" />
|
||||||
</SwitchThumb>
|
</span>
|
||||||
</SwitchRoot>
|
</button>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue