docs/.vitepress/theme/components/PreferenceRadio.vue

9d0bab29f196611c566b37a1a2acc996a0702f64
wotaku/docs/.vitepress/theme/components/PreferenceRadio.vue

97 lines · 2076 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}-${
21    props.options
22      .map((key) => key.toLowerCase())
23      .join('-')
24  }`
25)
26const name = key + (props.screenMenu ? '-screen-menu' : '')
27
28const selected = useLocalStorage(key, () => props.defaultOption)
29
30const optionsWithKeys = props.options.map((option) => ({
31  key: name + '-' + removeSpaces(option),
32  value: option
33}))
34
35function removeSpaces(str: string) {
36  return str.replace(/\s/g, '_')
37}
38</script>
39
40<template>
41  <p class="title">{{ text }}</p>
42  <div class="VPPreference" :class="{ 'screen-menu': screenMenu }">
43    <template v-for="option in optionsWithKeys" :key="option">
44      <input
45        type="radio"
46        :id="option.key"
47        :name="name"
48        :value="option.value"
49        v-model="selected"
50      />
51      <label :for="option.key">{{ option.value }}</label>
52    </template>
53  </div>
54</template>
55
56<style scoped>
57.VPPreference {
58  display: flex;
59  margin: 12px 0;
60  border: 1px solid var(--vp-c-border);
61  border-radius: 6px;
62  font-size: 14px;
63  color: var(--vp-c-text-1);
64}
65
66.VPPreference:first-child {
67  margin-top: 0;
68}
69
70.VPPreference:last-child {
71  margin-bottom: 0;
72}
73
74.VPPreference.screen-menu {
75  margin: 12px 0 0 12px;
76}
77
78.VPPreference input[type="radio"] {
79  pointer-events: none;
80  position: fixed;
81  opacity: 0;
82}
83
84.VPPreference label {
85  flex: 1;
86  margin: 2px;
87  padding: 4px 12px;
88  cursor: pointer;
89  border-radius: 4px;
90  text-align: center;
91}
92
93.VPPreference input[type="radio"]:checked + label {
94  background-color: var(--vp-c-default-soft);
95  color: var(--vp-c-brand-1);
96}
97</style>