|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, shallowRef, useAttrs } from 'vue' |
| 3 | +import { useControllableState } from '../hooks/useControllableState.ts' |
| 4 | +import { Primitive } from '../primitive/index.ts' |
| 5 | +import { composeEventHandlers } from '../utils/composeEventHandlers.ts' |
| 6 | +import { isFunction } from '../utils/is.ts' |
| 7 | +import { type SwitchEmits, type SwitchProps, getState, provideSwitchContext } from './Switch.ts' |
| 8 | +import BubbleInput from './BubbleInput.vue' |
| 9 | +
|
| 10 | +defineOptions({ |
| 11 | + name: 'OkuSwitch', |
| 12 | + inheritAttrs: false, |
| 13 | +}) |
| 14 | +
|
| 15 | +const props = withDefaults(defineProps<SwitchProps>(), { |
| 16 | + as: 'button', |
| 17 | + checked: undefined, |
| 18 | + defaultChecked: false, |
| 19 | + value: 'on', |
| 20 | +}) |
| 21 | +const emit = defineEmits<SwitchEmits>() |
| 22 | +const attrs = useAttrs() |
| 23 | +
|
| 24 | +const buttonEl = shallowRef<HTMLButtonElement>() |
| 25 | +let hasConsumerStoppedPropagation: boolean |
| 26 | +
|
| 27 | +// We set this to true by default so that events bubble to forms without JS (SSR) |
| 28 | +const isFormControl = computed(() => buttonEl.value ? Boolean(buttonEl.value.closest('form')) : true) |
| 29 | +
|
| 30 | +const checked = useControllableState(props, v => emit('update:checked', v), 'checked', props.defaultChecked) |
| 31 | +
|
| 32 | +provideSwitchContext({ |
| 33 | + checked, |
| 34 | + disabled() { |
| 35 | + return props.disabled |
| 36 | + }, |
| 37 | +}) |
| 38 | +
|
| 39 | +type CliclEvent = Event & { _stopPropagation: Event['stopPropagation'], _isPropagationStopped: boolean, isPropagationStopped: () => boolean } |
| 40 | +const onClick = composeEventHandlers<CliclEvent>((event) => { |
| 41 | + event._stopPropagation = event.stopPropagation |
| 42 | + event._isPropagationStopped = false |
| 43 | + event.stopPropagation = function stopPropagation() { |
| 44 | + this._isPropagationStopped = true |
| 45 | + event._stopPropagation() |
| 46 | + } |
| 47 | + event.isPropagationStopped = function isPropagationStopped() { |
| 48 | + return this._isPropagationStopped |
| 49 | + } |
| 50 | + isFunction(attrs.onClick) && attrs.onClick(event) |
| 51 | +}, (event) => { |
| 52 | + checked.value = !checked.value |
| 53 | +
|
| 54 | + if (isFormControl.value) { |
| 55 | + hasConsumerStoppedPropagation = event.isPropagationStopped() |
| 56 | + // if switch is in a form, stop propagation from the button so that we only propagate |
| 57 | + // one click event (from the input). We propagate changes from an input so that native |
| 58 | + // form validation works and form events reflect switch updates. |
| 59 | + if (!hasConsumerStoppedPropagation) |
| 60 | + event.stopPropagation() |
| 61 | + } |
| 62 | +}) |
| 63 | +
|
| 64 | +defineExpose({ |
| 65 | + $el: buttonEl, |
| 66 | +}) |
| 67 | +</script> |
| 68 | + |
| 69 | +<template> |
| 70 | + <Primitive |
| 71 | + :ref="(el: any) => buttonEl = el?.$el" |
| 72 | + :as="as" |
| 73 | + :as-child="asChild" |
| 74 | + type="button" |
| 75 | + role="switch" |
| 76 | + :aria-checked="checked" |
| 77 | + :aria-required="required" |
| 78 | + :data-state="getState(checked)" |
| 79 | + :data-disabled="disabled ? '' : undefined" |
| 80 | + :disabled="disabled" |
| 81 | + :value="value" |
| 82 | + v-bind="{ |
| 83 | + ...attrs, |
| 84 | + onClick, |
| 85 | + }" |
| 86 | + > |
| 87 | + <slot /> |
| 88 | + </Primitive> |
| 89 | + <BubbleInput |
| 90 | + v-if="isFormControl" |
| 91 | + :control="buttonEl" |
| 92 | + :bubbles="!hasConsumerStoppedPropagation" |
| 93 | + :name="name" |
| 94 | + :value="value" |
| 95 | + :checked="checked" |
| 96 | + :required="required" |
| 97 | + :disabled="disabled" |
| 98 | + :style="{ |
| 99 | + // We transform because the input is absolutely positioned but we have |
| 100 | + // rendered it **after** the button. This pulls it back to sit on top |
| 101 | + // of the button. |
| 102 | + transform: 'translateX(-100%)', |
| 103 | + }" |
| 104 | + /> |
| 105 | +</template> |
0 commit comments