1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-18 00:29:00 +09:00
MisskeyIO/packages/client/src/components/form/switch.vue

143 lines
2.4 KiB
Vue
Raw Normal View History

<template>
2021-09-30 00:50:45 +09:00
<div
2022-06-26 16:38:27 +09:00
class="ziffeomt"
2021-09-30 00:50:45 +09:00
:class="{ disabled, checked }"
>
<input
ref="input"
2021-11-19 19:36:12 +09:00
type="checkbox"
2021-09-30 00:50:45 +09:00
:disabled="disabled"
@keydown.enter="toggle"
>
2022-06-26 16:38:27 +09:00
<span ref="button" v-tooltip="checked ? $ts.itsOn : $ts.itsOff" class="button" @click.prevent="toggle">
<div class="knob"></div>
2021-09-30 00:50:45 +09:00
</span>
<span class="label">
2021-12-30 21:47:48 +09:00
<!-- TODO: 無名slotの方は廃止 -->
<span @click="toggle"><slot name="label"></slot><slot></slot></span>
2021-11-28 20:07:37 +09:00
<p class="caption"><slot name="caption"></slot></p>
2021-09-30 00:50:45 +09:00
</span>
</div>
</template>
<script lang="ts" setup>
import { toRefs, Ref } from 'vue';
2021-12-27 22:59:14 +09:00
import * as os from '@/os';
const props = defineProps<{
modelValue: boolean | Ref<boolean>;
disabled?: boolean;
}>();
2021-12-27 22:59:14 +09:00
const emit = defineEmits<{
(ev: 'update:modelValue', v: boolean): void;
}>();
2021-12-27 22:59:14 +09:00
let button = $ref<HTMLElement>();
const checked = toRefs(props).modelValue;
const toggle = () => {
if (props.disabled) return;
emit('update:modelValue', !checked.value);
2021-12-27 22:59:14 +09:00
if (!checked.value) {
2022-06-26 16:38:27 +09:00
}
};
</script>
<style lang="scss" scoped>
2022-06-26 16:38:27 +09:00
.ziffeomt {
2021-09-30 00:50:45 +09:00
position: relative;
display: flex;
2021-12-27 22:59:14 +09:00
transition: all 0.2s ease;
2021-09-30 00:50:45 +09:00
> * {
user-select: none;
}
> input {
position: absolute;
width: 0;
height: 0;
opacity: 0;
margin: 0;
}
2021-09-30 00:50:45 +09:00
> .button {
position: relative;
2021-12-26 01:42:50 +09:00
display: inline-flex;
2021-09-30 00:50:45 +09:00
flex-shrink: 0;
margin: 0;
2021-12-26 01:42:50 +09:00
box-sizing: border-box;
2022-06-26 16:38:27 +09:00
width: 32px;
2021-12-26 01:42:50 +09:00
height: 23px;
2021-09-30 00:50:45 +09:00
outline: none;
2022-06-26 16:38:27 +09:00
background: var(--swutchOffBg);
background-clip: content-box;
border: solid 1px var(--swutchOffBg);
border-radius: 999px;
2021-12-26 01:42:50 +09:00
cursor: pointer;
2021-09-30 00:50:45 +09:00
transition: inherit;
2022-06-26 16:38:27 +09:00
> .knob {
position: absolute;
top: 3px;
left: 3px;
width: 15px;
height: 15px;
background: var(--swutchOffFg);
border-radius: 999px;
2021-12-27 22:59:14 +09:00
transition: all 0.2s ease;
2021-12-26 01:42:50 +09:00
}
}
&:hover {
> .button {
border-color: var(--inputBorderHover) !important;
}
2021-09-30 00:50:45 +09:00
}
2021-09-30 00:50:45 +09:00
> .label {
2022-01-04 15:36:14 +09:00
margin-left: 12px;
2021-09-30 00:50:45 +09:00
margin-top: 2px;
display: block;
transition: inherit;
color: var(--fg);
2021-09-30 00:50:45 +09:00
> span {
display: block;
line-height: 20px;
2021-12-26 01:42:50 +09:00
cursor: pointer;
2021-09-30 00:50:45 +09:00
transition: inherit;
}
2021-11-28 20:07:37 +09:00
> .caption {
margin: 8px 0 0 0;
2021-09-30 00:50:45 +09:00
color: var(--fgTransparentWeak);
2021-11-28 20:07:37 +09:00
font-size: 0.85em;
&:empty {
display: none;
}
}
2021-09-30 00:50:45 +09:00
}
2021-09-30 00:50:45 +09:00
&.disabled {
opacity: 0.6;
cursor: not-allowed;
}
&.checked {
> .button {
2022-06-26 16:38:27 +09:00
background-color: var(--swutchOnBg) !important;
border-color: var(--swutchOnBg) !important;
2022-06-26 16:38:27 +09:00
> .knob {
left: 12px;
background: var(--swutchOnFg);
}
}
}
}
</style>