gamebrary/src/components/ToggleSwitch.vue

114 lines
1.8 KiB
Vue
Raw Normal View History

2018-11-25 03:38:06 +00:00
<template lang="html">
2019-11-08 20:34:06 +00:00
<span class="toggle-switch">
<p v-if="label">{{ label }}</p>
2018-11-25 03:47:57 +00:00
2019-11-08 20:34:06 +00:00
<input
:id="id"
v-model="localValue"
2019-12-02 18:46:28 +00:00
@change="$emit('change')"
2019-11-08 20:34:06 +00:00
type="checkbox"
>
2018-11-25 03:38:06 +00:00
2019-11-08 20:34:06 +00:00
<label :for="id" />
</span>
2018-11-25 03:38:06 +00:00
</template>
<script>
export default {
2019-11-08 19:56:03 +00:00
props: {
2019-11-08 20:34:06 +00:00
value: {
type: Boolean,
default: false,
},
id: {
type: String,
default: '',
},
label: {
type: String,
default: '',
},
2019-11-08 19:56:03 +00:00
},
data() {
return {
localValue: null,
};
},
watch: {
value(value) {
if (value) {
this.localValue = JSON.parse(JSON.stringify(this.value));
}
2018-11-25 03:38:06 +00:00
},
2019-11-08 19:56:03 +00:00
localValue(value) {
this.$emit('input', value);
2018-11-25 03:38:06 +00:00
},
2019-11-08 19:56:03 +00:00
},
2018-11-25 03:38:06 +00:00
2019-11-08 19:56:03 +00:00
mounted() {
if (this.value !== undefined) {
this.localValue = JSON.parse(JSON.stringify(this.value));
}
},
2018-11-25 03:38:06 +00:00
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
2019-09-10 20:02:16 +00:00
@import "~styles/styles";
2018-11-25 03:38:06 +00:00
.toggle-switch {
2019-11-08 20:34:06 +00:00
display: inline-flex;
2018-11-25 03:38:06 +00:00
2019-11-08 20:34:06 +00:00
p {
margin: 0;
font-size: 12px;
display: flex;
align-items: center;
font-weight: bold;
}
2018-11-25 03:47:57 +00:00
2019-11-08 20:34:06 +00:00
input[type=checkbox]{
height: 0;
width: 0;
2019-12-02 18:46:28 +00:00
margin: 0;
padding: 0;
2019-11-08 20:34:06 +00:00
visibility: hidden;
2018-11-25 03:38:06 +00:00
2019-11-08 20:34:06 +00:00
&:checked + label {
background: var(--success-background);
}
2018-11-25 03:38:06 +00:00
2019-11-08 20:34:06 +00:00
&:checked + label:after {
left: calc(100% - 3px);
transform: translateX(-100%);
2018-11-25 03:38:06 +00:00
}
2019-11-08 20:34:06 +00:00
}
2018-11-25 03:38:06 +00:00
2019-11-08 20:34:06 +00:00
label {
cursor: pointer;
text-indent: -9999px;
width: 34px;
height: 20px;
background: grey;
display: block;
border-radius: 100px;
position: relative;
2018-11-25 03:38:06 +00:00
2019-11-08 20:34:06 +00:00
&:after {
content: '';
position: absolute;
top: 3px;
left: 3px;
width: 14px;
height: 14px;
background: #fff;
border-radius: 90px;
transition: 0.3s;
2018-11-25 03:38:06 +00:00
}
2019-11-08 20:34:06 +00:00
}
2018-11-25 03:38:06 +00:00
}
</style>