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

155 lines
2.3 KiB
Vue
Raw Normal View History

<template>
2021-09-30 00:50:45 +09:00
<div
class="ziffeoms"
:class="{ disabled, checked }"
role="switch"
:aria-checked="checked"
:aria-disabled="disabled"
@click.prevent="toggle"
>
<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"
>
2021-11-19 19:36:12 +09:00
<span v-tooltip="checked ? $ts.itsOn : $ts.itsOff" class="button">
2021-09-30 00:50:45 +09:00
<span class="handle"></span>
</span>
<span class="label">
<span><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">
import { defineComponent } from 'vue';
export default defineComponent({
props: {
2021-09-30 00:50:45 +09:00
modelValue: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
}
},
computed: {
checked(): boolean {
2021-09-30 00:50:45 +09:00
return this.modelValue;
}
},
methods: {
toggle() {
if (this.disabled) return;
2021-09-30 00:50:45 +09:00
this.$emit('update:modelValue', !this.checked);
}
}
});
</script>
<style lang="scss" scoped>
2021-09-30 00:50:45 +09:00
.ziffeoms {
position: relative;
display: flex;
cursor: pointer;
transition: all 0.3s;
2021-09-30 00:50:45 +09:00
&:first-child {
margin-top: 0;
}
&:last-child {
margin-bottom: 0;
}
> * {
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;
display: inline-block;
flex-shrink: 0;
margin: 0;
width: 36px;
height: 26px;
background: var(--switchBg);
outline: none;
border-radius: 999px;
transition: inherit;
> .handle {
position: absolute;
top: 0;
bottom: 0;
left: 5px;
margin: auto 0;
border-radius: 100%;
transition: background-color 0.3s, transform 0.3s;
width: 16px;
height: 16px;
background-color: #fff;
}
2021-09-30 00:50:45 +09:00
}
2021-09-30 00:50:45 +09:00
> .label {
margin-left: 16px;
margin-top: 2px;
display: block;
cursor: pointer;
transition: inherit;
color: var(--fg);
2021-09-30 00:50:45 +09:00
> span {
display: block;
line-height: 20px;
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
&:hover {
> .button {
2021-09-30 00:50:45 +09:00
background-color: var(--accentedBg);
}
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 {
background-color: var(--accent);
border-color: var(--accent);
2021-09-30 00:50:45 +09:00
> .handle {
transform: translateX(10px);
}
}
}
}
</style>