docs/.vitepress/theme/components/PreferenceRadio.vue
89 lines · 2016 bytes
1<!--
2 All Rights Reserved
3
4 Copyright (c) 2025 taskylizard
5
6 All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
7-->
8<script setup lang="ts">
9import { useLocalStorage } from '@vueuse/core'
10
11const props = defineProps<{
12 text: string
13 group: string
14 options: string[]
15 defaultOption: string
16 screenMenu?: boolean
17}>()
18
19const key = removeSpaces(
20 `preference-${props.group}-${props.options
21 .map((key) => key.toLowerCase())
22 .join('-')}`
23)
24const name = key + (props.screenMenu ? '-screen-menu' : '')
25
26const selected = useLocalStorage(key, () => props.defaultOption)
27
28const optionsWithKeys = props.options.map((option) => ({
29 key: name + '-' + removeSpaces(option),
30 value: option
31}))
32
33function removeSpaces(str: string) {
34 return str.replace(/\s/g, '_')
35}
36</script>
37
38<template>
39 <p class="title">{{ text }}</p>
40 <div class="VPPreference" :class="{ 'screen-menu': screenMenu }">
41 <template v-for="option in optionsWithKeys" :key="option">
42 <input type="radio" :id="option.key" :name="name" :value="option.value" v-model="selected" />
43 <label :for="option.key">{{ option.value }}</label>
44 </template>
45 </div>
46</template>
47
48<style scoped>
49.VPPreference {
50 display: flex;
51 margin: 12px 0;
52 border: 1px solid var(--vp-c-border);
53 border-radius: 6px;
54 font-size: 14px;
55 color: var(--vp-c-text-1);
56}
57
58.VPPreference:first-child {
59 margin-top: 0;
60}
61
62.VPPreference:last-child {
63 margin-bottom: 0;
64}
65
66.VPPreference.screen-menu {
67 margin: 12px 0 0 12px;
68}
69
70.VPPreference input[type='radio'] {
71 pointer-events: none;
72 position: fixed;
73 opacity: 0;
74}
75
76.VPPreference label {
77 flex: 1;
78 margin: 2px;
79 padding: 4px 12px;
80 cursor: pointer;
81 border-radius: 4px;
82 text-align: center;
83}
84
85.VPPreference input[type='radio']:checked+label {
86 background-color: var(--vp-c-default-soft);
87 color: var(--vp-c-brand-1);
88}
89</style>