hippofish/packages/client/src/components/form/switch.vue

137 lines
2.3 KiB
Vue
Raw Normal View History

<template>
2023-05-15 22:19:33 +02:00
<label class="ziffeomt">
2023-04-08 02:01:42 +02:00
<input
type="checkbox"
2023-05-15 22:19:33 +02:00
:checked="modelValue"
2023-04-08 02:01:42 +02:00
:disabled="disabled"
2023-05-15 22:19:33 +02:00
v-on:change="(x) => toggle(x)"
2023-04-08 02:01:42 +02:00
/>
2023-05-15 22:19:33 +02:00
<div
2023-04-08 02:01:42 +02:00
class="button"
>
<div class="knob"></div>
2023-05-15 22:19:33 +02:00
</div>
2023-04-08 02:01:42 +02:00
<span class="label">
<!-- TODO: 無名slotの方は廃止 -->
2023-05-15 22:19:33 +02:00
<span><slot name="label"></slot><slot></slot></span>
2023-04-08 02:01:42 +02:00
<p class="caption"><slot name="caption"></slot></p>
</span>
2023-05-15 22:19:33 +02:00
</label>
</template>
<script lang="ts" setup>
2023-05-15 22:19:33 +02:00
import { Ref } from "vue";
const props = defineProps<{
modelValue: boolean | Ref<boolean>;
disabled?: boolean;
}>();
2021-12-27 14:59:14 +01:00
const emit = defineEmits<{
2023-04-08 02:01:42 +02:00
(ev: "update:modelValue", v: boolean): void;
}>();
2021-12-27 14:59:14 +01:00
2023-05-15 22:19:33 +02:00
function toggle(x) {
if (props.disabled) return;
2023-05-15 22:19:33 +02:00
emit("update:modelValue", x.target.checked);
}
</script>
<style lang="scss" scoped>
2022-06-26 09:38:27 +02:00
.ziffeomt {
2021-09-29 17:50:45 +02:00
position: relative;
display: flex;
2021-12-27 14:59:14 +01:00
transition: all 0.2s ease;
2021-09-29 17:50:45 +02:00
> * {
user-select: none;
}
> input {
position: absolute;
2023-05-15 22:19:33 +02:00
width: 32px;
height: 23px;
2021-09-29 17:50:45 +02:00
opacity: 0;
margin: 0;
}
2021-09-29 17:50:45 +02:00
> .button {
position: relative;
2021-12-25 17:42:50 +01:00
display: inline-flex;
2021-09-29 17:50:45 +02:00
flex-shrink: 0;
margin: 0;
2021-12-25 17:42:50 +01:00
box-sizing: border-box;
2022-06-26 09:38:27 +02:00
width: 32px;
2021-12-25 17:42:50 +01:00
height: 23px;
2021-09-29 17:50:45 +02:00
outline: none;
2022-06-26 09:38:27 +02:00
background: var(--swutchOffBg);
background-clip: content-box;
border: solid 1px var(--swutchOffBg);
border-radius: 999px;
2021-09-29 17:50:45 +02:00
transition: inherit;
2022-06-27 17:20:51 +02:00
user-select: none;
2023-05-15 22:19:33 +02:00
pointer-events: none;
2021-09-29 17:50:45 +02:00
2022-06-26 09:38:27 +02:00
> .knob {
position: absolute;
top: 3px;
left: 3px;
width: 15px;
height: 15px;
background: var(--swutchOffFg);
border-radius: 999px;
2021-12-27 14:59:14 +01:00
transition: all 0.2s ease;
2021-12-25 17:42:50 +01:00
}
}
&:hover {
> .button {
border-color: var(--inputBorderHover) !important;
}
2021-09-29 17:50:45 +02:00
}
2023-02-06 21:52:00 +01:00
&:focus-within > .button {
outline: auto;
}
2021-09-29 17:50:45 +02:00
> .label {
2022-01-04 07:36:14 +01:00
margin-left: 12px;
2021-09-29 17:50:45 +02:00
margin-top: 2px;
display: block;
transition: inherit;
color: var(--fg);
2021-09-29 17:50:45 +02:00
> span {
display: block;
line-height: 20px;
2021-12-25 17:42:50 +01:00
cursor: pointer;
2021-09-29 17:50:45 +02:00
transition: inherit;
}
2021-11-28 12:07:37 +01:00
> .caption {
margin: 8px 0 0 0;
2021-09-29 17:50:45 +02:00
color: var(--fgTransparentWeak);
2021-11-28 12:07:37 +01:00
font-size: 0.85em;
&:empty {
display: none;
}
}
2021-09-29 17:50:45 +02:00
}
2023-05-15 22:19:33 +02:00
> input:disabled ~ * {
2021-09-29 17:50:45 +02:00
opacity: 0.6;
cursor: not-allowed;
}
2023-05-15 22:19:33 +02:00
> input:checked ~ .button {
background-color: var(--swutchOnBg) !important;
border-color: var(--swutchOnBg) !important;
2023-05-15 22:19:33 +02:00
> .knob {
left: 12px;
background: var(--swutchOnFg);
}
}
}
</style>