audiobookshelf/client/components/ui/ToggleSwitch.vue

49 lines
1.3 KiB
Vue
Raw Normal View History

2021-08-27 14:01:47 +02:00
<template>
<div>
<button :aria-labelledby="labeledBy" role="checkbox" class="border rounded-full border-black-100 flex items-center cursor-pointer w-10 justify-start" :aria-checked="toggleValue" :class="className" @click="clickToggle">
<span class="rounded-full border w-5 h-5 border-black-50 shadow transform transition-transform duration-100" :class="switchClassName"></span>
</button>
2021-08-27 14:01:47 +02:00
</div>
</template>
<script>
export default {
props: {
value: Boolean,
onColor: {
type: String,
default: 'success'
},
offColor: {
type: String,
default: 'primary'
},
disabled: Boolean,
labeledBy: String
2021-08-27 14:01:47 +02:00
},
computed: {
toggleValue: {
get() {
return this.value
},
set(val) {
this.$emit('input', val)
}
},
className() {
if (this.disabled) return this.toggleValue ? `bg-${this.onColor} cursor-not-allowed` : `bg-${this.offColor} cursor-not-allowed`
return this.toggleValue ? `bg-${this.onColor}` : `bg-${this.offColor}`
},
switchClassName() {
var bgColor = this.disabled ? 'bg-gray-300' : 'bg-white'
return this.toggleValue ? 'translate-x-5 ' + bgColor : bgColor
2021-08-27 14:01:47 +02:00
}
},
methods: {
clickToggle() {
if (this.disabled) return
2021-08-27 14:01:47 +02:00
this.toggleValue = !this.toggleValue
}
}
}
</script>