koel/resources/assets/js/components/ui/VolumeSlider.vue

114 lines
2.4 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-10-13 15:18:47 +00:00
<span id="volume" class="volume" :class="level">
<span
v-show="level === 'muted'"
v-koel-tooltip.top
2022-07-15 07:23:55 +00:00
role="button"
tabindex="0"
title="Unmute"
@click="unmute"
>
2023-11-10 13:16:06 +00:00
<Icon :icon="faVolumeMute" fixed-width />
</span>
<span
v-show="level !== 'muted'"
v-koel-tooltip.top
2022-07-15 07:23:55 +00:00
role="button"
tabindex="0"
title="Mute"
@click="mute"
>
2023-11-10 13:16:06 +00:00
<Icon :icon="level === 'discreet' ? faVolumeLow : faVolumeHigh" fixed-width />
</span>
2022-07-15 07:23:55 +00:00
2022-04-15 14:24:30 +00:00
<input
2024-03-18 17:51:51 +00:00
ref="inputEl"
class="plyr__volume"
2022-04-15 14:24:30 +00:00
max="10"
role="slider"
2022-04-15 14:24:30 +00:00
step="0.1"
title="Volume"
type="range"
@input="setVolume"
2022-04-15 14:24:30 +00:00
>
</span>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-07-15 07:23:55 +00:00
import { faVolumeHigh, faVolumeLow, faVolumeMute } from '@fortawesome/free-solid-svg-icons'
2024-03-18 17:51:51 +00:00
import { computed, onMounted, ref } from 'vue'
import { socketService, volumeManager } from '@/services'
import { preferenceStore } from '@/stores'
2024-03-18 17:51:51 +00:00
import { watchThrottled } from '@vueuse/core'
2022-04-15 14:24:30 +00:00
2024-03-18 17:51:51 +00:00
const inputEl = ref<HTMLInputElement>()
const volume = volumeManager.volume
2022-04-15 14:24:30 +00:00
const level = computed(() => {
if (volume.value === 0) return 'muted'
if (volume.value < 3) return 'discreet'
return 'loud'
})
2022-04-15 14:24:30 +00:00
const mute = () => volumeManager.mute()
const unmute = () => volumeManager.unmute()
2022-12-02 16:17:37 +00:00
const setVolume = (e: Event) => volumeManager.set(parseFloat((e.target as HTMLInputElement).value))
2022-07-15 07:23:55 +00:00
2024-03-18 17:51:51 +00:00
watchThrottled(volumeManager.volume, volume => {
preferenceStore.volume = volume
socketService.broadcast('SOCKET_VOLUME_CHANGED', volume)
}, { throttle: 1_000 })
onMounted(() => volumeManager.init(inputEl.value!, preferenceStore.volume))
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss">
#volume {
position: relative;
2022-10-13 15:18:47 +00:00
display: flex;
align-items: center;
2022-04-15 14:24:30 +00:00
[type=range] {
2022-07-15 07:23:55 +00:00
margin: 0 0 0 8px;
2022-10-13 15:18:47 +00:00
width: 120px;
height: 4px;
2022-04-15 14:24:30 +00:00
border-radius: 4px;
2022-10-13 15:18:47 +00:00
position: relative;
// increase click area
&::before {
position: absolute;
content: ' ';
left: 0;
right: 0;
top: -12px;
bottom: -12px;
}
2022-04-15 14:24:30 +00:00
2022-10-13 15:18:47 +00:00
&::-webkit-slider-thumb {
background: var(--color-text-secondary);
}
&:hover {
&::-webkit-slider-thumb {
background: var(--color-text-primary);
}
}
2022-04-15 14:24:30 +00:00
}
2022-10-13 15:18:47 +00:00
&.muted {
[type=range] {
&::-webkit-slider-thumb {
background: transparent;
box-shadow: none;
}
}
2022-04-15 14:24:30 +00:00
}
@media only screen and (max-width: 768px) {
display: none !important;
}
}
</style>