| @@ -1,6 +1,8 @@ |
| 1 | 1 | <script setup lang="ts"> |
| 2 | 2 | import { useData } from 'vitepress' |
| 3 | import { NuVerticalTransition } from '@nolebase/ui' |
| 3 | 4 | import { useStorage } from '@vueuse/core' |
| 5 | import { computed, onMounted, onUnmounted, ref } from 'vue' |
| 4 | 6 | |
| 5 | 7 | import { AccentColorStorageKey } from '../../constants' |
| 6 | 8 | import MenuTitle from './MenuTitle.vue' |
| @@ -57,44 +59,147 @@ const accentOptions = [ |
| 57 | 59 | ] |
| 58 | 60 | |
| 59 | 61 | const accentColor = useStorage(AccentColorStorageKey, 'ayanami') |
| 62 | |
| 63 | const open = ref(false) |
| 64 | const rootRef = ref<HTMLDivElement>() |
| 65 | |
| 66 | const selectedOption = computed( |
| 67 | () => accentOptions.find((o) => o.key === accentColor.value) ?? accentOptions[0] |
| 68 | ) |
| 69 | |
| 70 | function shadesFor(option: typeof accentOptions[number]) { |
| 71 | return 'lightShades' in option && !isDark.value ? option.lightShades! : option.shades |
| 72 | } |
| 73 | |
| 74 | function select(key: string) { |
| 75 | accentColor.value = key |
| 76 | open.value = false |
| 77 | } |
| 78 | |
| 79 | function onDocClick(e: MouseEvent) { |
| 80 | if (rootRef.value && !rootRef.value.contains(e.target as Node)) { |
| 81 | open.value = false |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | onMounted(() => document.addEventListener('click', onDocClick, true)) |
| 86 | onUnmounted(() => document.removeEventListener('click', onDocClick, true)) |
| 60 | 87 | </script> |
| 61 | 88 | |
| 62 | 89 | <template> |
| 63 | | <div> |
| 90 | <div ref="rootRef" class="accent-color"> |
| 64 | 91 | <MenuTitle title="Accent Color" aria-label="Accent Color" mb-2> |
| 65 | 92 | <template #icon> |
| 66 | 93 | <span i-lucide:swatch-book mr-1 aria-hidden="true" /> |
| 67 | 94 | </template> |
| 68 | 95 | </MenuTitle> |
| 69 | | <div class="accent-list"> |
| 96 | |
| 97 | <div class="accent-select" :class="{ 'accent-select--open': open }"> |
| 70 | 98 | <button |
| 71 | | v-for="option in accentOptions" |
| 72 | | :key="option.key" |
| 73 | | class="accent-btn" |
| 74 | | :class="{ 'accent-btn--selected': accentColor === option.key }" |
| 75 | | :aria-pressed="accentColor === option.key" |
| 76 | | :aria-label="option.label" |
| 77 | | @click="accentColor = option.key" |
| 99 | type="button" |
| 100 | class="accent-trigger" |
| 101 | aria-haspopup="listbox" |
| 102 | :aria-expanded="open" |
| 103 | :aria-label="`Accent Color: ${selectedOption.label}`" |
| 104 | @click="open = !open" |
| 78 | 105 | > |
| 79 | | <span class="accent-label">{{ option.label }}</span> |
| 106 | <span class="accent-label">{{ selectedOption.label }}</span> |
| 80 | 107 | <span class="accent-shades"> |
| 81 | 108 | <span |
| 82 | | v-for="(shade, i) in ('lightShades' in option && !isDark ? option.lightShades : option.shades)" |
| 109 | v-for="(shade, i) in shadesFor(selectedOption)" |
| 83 | 110 | :key="i" |
| 84 | 111 | class="accent-shade" |
| 85 | 112 | :style="{ backgroundColor: shade }" |
| 86 | 113 | /> |
| 87 | 114 | </span> |
| 115 | <span |
| 116 | class="accent-chevron i-lucide:chevron-down" |
| 117 | :class="{ 'accent-chevron--open': open }" |
| 118 | aria-hidden="true" |
| 119 | /> |
| 88 | 120 | </button> |
| 121 | |
| 122 | <NuVerticalTransition :duration="200"> |
| 123 | <div v-show="open" class="accent-options" role="listbox"> |
| 124 | <button |
| 125 | v-for="option in accentOptions" |
| 126 | :key="option.key" |
| 127 | type="button" |
| 128 | role="option" |
| 129 | class="accent-btn" |
| 130 | :class="{ 'accent-btn--selected': accentColor === option.key }" |
| 131 | :aria-selected="accentColor === option.key" |
| 132 | :aria-label="option.label" |
| 133 | @click="select(option.key)" |
| 134 | > |
| 135 | <span class="accent-label">{{ option.label }}</span> |
| 136 | <span class="accent-shades"> |
| 137 | <span |
| 138 | v-for="(shade, i) in shadesFor(option)" |
| 139 | :key="i" |
| 140 | class="accent-shade" |
| 141 | :style="{ backgroundColor: shade }" |
| 142 | /> |
| 143 | </span> |
| 144 | </button> |
| 145 | </div> |
| 146 | </NuVerticalTransition> |
| 89 | 147 | </div> |
| 90 | 148 | </div> |
| 91 | 149 | </template> |
| 92 | 150 | |
| 93 | 151 | <style scoped> |
| 94 | | .accent-list { |
| 152 | .accent-select { |
| 153 | position: relative; |
| 154 | } |
| 155 | |
| 156 | .accent-trigger { |
| 157 | display: flex; |
| 158 | align-items: center; |
| 159 | gap: 8px; |
| 160 | width: 100%; |
| 161 | padding: 7px 10px; |
| 162 | border-radius: 8px; |
| 163 | border: 1px solid var(--vp-c-divider); |
| 164 | background: var(--wk-c-menu-bg); |
| 165 | color: var(--vp-c-text-1); |
| 166 | font-size: 13px; |
| 167 | cursor: pointer; |
| 168 | transition: border-color 0.15s, background-color 0.15s; |
| 169 | } |
| 170 | |
| 171 | .accent-trigger:hover { |
| 172 | border-color: var(--vp-c-brand-1); |
| 173 | } |
| 174 | |
| 175 | .accent-select--open .accent-trigger { |
| 176 | border-color: var(--vp-c-brand-1); |
| 177 | } |
| 178 | |
| 179 | .accent-chevron { |
| 180 | flex-shrink: 0; |
| 181 | width: 16px; |
| 182 | height: 16px; |
| 183 | color: var(--vp-c-text-2); |
| 184 | transition: transform 0.2s ease; |
| 185 | } |
| 186 | |
| 187 | .accent-chevron--open { |
| 188 | transform: rotate(180deg); |
| 189 | } |
| 190 | |
| 191 | .accent-options { |
| 192 | margin-top: 6px; |
| 193 | max-height: 240px; |
| 194 | overflow-y: auto; |
| 95 | 195 | display: flex; |
| 96 | 196 | flex-direction: column; |
| 97 | 197 | gap: 2px; |
| 198 | padding: 4px; |
| 199 | border-radius: 8px; |
| 200 | border: 1px solid var(--vp-c-divider); |
| 201 | background: var(--vp-c-bg-elv); |
| 202 | box-shadow: var(--vp-shadow-2); |
| 98 | 203 | } |
| 99 | 204 | |
| 100 | 205 | .accent-btn { |
| @@ -141,5 +246,4 @@ const accentColor = useStorage(AccentColorStorageKey, 'ayanami') |
| 141 | 246 | width: 18px; |
| 142 | 247 | height: 100%; |
| 143 | 248 | } |
| 144 | | |
| 145 | 249 | </style> |