docs/.vitepress/theme/components/PreferenceRadio.vue
103 lines · 2364 bytes
1<!--
2 Copyright (c) 2024 taskylizard
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15-->
16<script setup lang="ts">
17import { useLocalStorage } from '@vueuse/core'
18
19const props = defineProps<{
20 text: string
21 group: string
22 options: string[]
23 defaultOption: string
24 screenMenu?: boolean
25}>()
26
27const key = removeSpaces(
28 `preference-${props.group}-${props.options
29 .map((key) => key.toLowerCase())
30 .join('-')}`
31)
32const name = key + (props.screenMenu ? '-screen-menu' : '')
33
34const selected = useLocalStorage(key, () => props.defaultOption)
35
36const optionsWithKeys = props.options.map((option) => ({
37 key: name + '-' + removeSpaces(option),
38 value: option
39}))
40
41function removeSpaces(str: string) {
42 return str.replace(/\s/g, '_')
43}
44</script>
45
46<template>
47 <p class="title">{{ text }}</p>
48 <div class="VPPreference" :class="{ 'screen-menu': screenMenu }">
49 <template v-for="option in optionsWithKeys" :key="option">
50 <input
51 type="radio"
52 :id="option.key"
53 :name="name"
54 :value="option.value"
55 v-model="selected"
56 />
57 <label :for="option.key">{{ option.value }}</label>
58 </template>
59 </div>
60</template>
61
62<style scoped>
63.VPPreference {
64 display: flex;
65 margin: 12px 0;
66 border: 1px solid var(--vp-c-border);
67 border-radius: 6px;
68 font-size: 14px;
69 color: var(--vp-c-text-1);
70}
71
72.VPPreference:first-child {
73 margin-top: 0;
74}
75
76.VPPreference:last-child {
77 margin-bottom: 0;
78}
79
80.VPPreference.screen-menu {
81 margin: 12px 0 0 12px;
82}
83
84.VPPreference input[type='radio'] {
85 pointer-events: none;
86 position: fixed;
87 opacity: 0;
88}
89
90.VPPreference label {
91 flex: 1;
92 margin: 2px;
93 padding: 4px 12px;
94 cursor: pointer;
95 border-radius: 4px;
96 text-align: center;
97}
98
99.VPPreference input[type='radio']:checked + label {
100 background-color: var(--vp-c-default-soft);
101 color: var(--vp-c-brand-1);
102}
103</style>