Commit 8c7248d233
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/extrepo/RatingFilter.vue +144 −21
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <script setup lang="ts"> |
| 2 | import { NuInputHorizontalRadioGroup } from '@nolebase/ui' | |
| 3 | import { computed } from 'vue' | |
| 2 | import { NuInputHighlight, NuVerticalTransition } from '@nolebase/ui' | |
| 3 | import { computed, onMounted, onUnmounted, ref } from 'vue' | |
| 4 | 4 | import MenuTitle from './MenuTitle.vue' |
| 5 | 5 | import type { RatingFilter } from './types' |
| 6 | 6 | |
| @@ -15,36 +15,159 @@ const emit = defineEmits<{ | ||
| 15 | 15 | |
| 16 | 16 | const options = computed(() => { |
| 17 | 17 | const base = [ |
| 18 | { value: 'all', title: 'All', ariaLabel: 'All', text: 'All', name: 'Rating' }, | |
| 19 | { value: 'safe', title: 'Safe', ariaLabel: 'Safe', text: 'Safe', name: 'Rating' } | |
| 18 | { value: 'all' as RatingFilter, label: 'All' }, | |
| 19 | { value: 'safe' as RatingFilter, label: 'Safe' } | |
| 20 | 20 | ] |
| 21 | 21 | if (props.hasSuggestive) { |
| 22 | base.push({ value: 'suggestive', title: 'Mature', ariaLabel: 'Mature', text: 'Mature', name: 'Rating' }) | |
| 22 | base.push({ value: 'suggestive' as RatingFilter, label: 'Mature' }) | |
| 23 | 23 | } |
| 24 | 24 | base.push({ |
| 25 | value: 'nsfw', | |
| 26 | title: props.hasSuggestive ? 'Adult' : 'NSFW', | |
| 27 | ariaLabel: 'NSFW', | |
| 28 | text: props.hasSuggestive ? 'Adult' : 'NSFW', | |
| 29 | name: 'Rating' | |
| 25 | value: 'nsfw' as RatingFilter, | |
| 26 | label: props.hasSuggestive ? 'Adult' : 'NSFW' | |
| 30 | 27 | }) |
| 31 | 28 | return base |
| 32 | 29 | }) |
| 33 | 30 | |
| 34 | const rating = computed({ | |
| 35 | get: () => props.modelValue, | |
| 36 | set: (value: RatingFilter) => emit('update:modelValue', value) | |
| 37 | }) | |
| 31 | const rootRef = ref<HTMLElement>() | |
| 32 | const open = ref(false) | |
| 33 | ||
| 34 | const currentLabel = computed(() => options.value.find(o => o.value === props.modelValue)?.label ?? 'All') | |
| 35 | ||
| 36 | function select(value: RatingFilter) { | |
| 37 | emit('update:modelValue', value) | |
| 38 | open.value = false | |
| 39 | } | |
| 40 | ||
| 41 | function onDocClick(e: MouseEvent) { | |
| 42 | if (rootRef.value && !rootRef.value.contains(e.target as Node)) open.value = false | |
| 43 | } | |
| 44 | ||
| 45 | onMounted(() => document.addEventListener('click', onDocClick, true)) | |
| 46 | onUnmounted(() => document.removeEventListener('click', onDocClick, true)) | |
| 38 | 47 | </script> |
| 39 | 48 | |
| 40 | 49 | <template> |
| 41 | <div class="ext-rating" role="radiogroup"> | |
| 50 | <div ref="rootRef" class="ext-rating-select" :class="{ 'ext-rating-select--open': open }"> | |
| 42 | 51 | <MenuTitle icon="i-lucide:shield" title="Rating" /> |
| 43 | <NuInputHorizontalRadioGroup | |
| 44 | v-model="rating" | |
| 45 | bg="$vp-nolebase-enhanced-readabilities-menu-background-color" | |
| 46 | text="sm $vp-nolebase-enhanced-readabilities-menu-text-color" | |
| 47 | :options="options" | |
| 48 | /> | |
| 52 | <NuInputHighlight class="rounded-md"> | |
| 53 | <button | |
| 54 | type="button" | |
| 55 | class="ext-rating-trigger" | |
| 56 | aria-haspopup="listbox" | |
| 57 | :aria-expanded="open" | |
| 58 | @click="open = !open" | |
| 59 | > | |
| 60 | <span class="ext-rating-label">{{ currentLabel }}</span> | |
| 61 | <span class="ext-rating-chevron i-lucide:chevron-down" :class="{ 'ext-rating-chevron--open': open }" /> | |
| 62 | </button> | |
| 63 | ||
| 64 | <NuVerticalTransition :duration="200"> | |
| 65 | <div v-show="open" class="ext-rating-options" role="listbox"> | |
| 66 | <button | |
| 67 | v-for="option in options" | |
| 68 | :key="option.value" | |
| 69 | type="button" | |
| 70 | role="option" | |
| 71 | class="ext-rating-option" | |
| 72 | :class="{ 'ext-rating-option--selected': modelValue === option.value }" | |
| 73 | :aria-selected="modelValue === option.value" | |
| 74 | @click="select(option.value)" | |
| 75 | > | |
| 76 | <span class="ext-rating-option-label">{{ option.label }}</span> | |
| 77 | </button> | |
| 78 | </div> | |
| 79 | </NuVerticalTransition> | |
| 80 | </NuInputHighlight> | |
| 49 | 81 | </div> |
| 50 | 82 | </template> |
| 83 | ||
| 84 | <style scoped> | |
| 85 | .ext-rating-select { | |
| 86 | position: relative; | |
| 87 | } | |
| 88 | ||
| 89 | .ext-rating-trigger { | |
| 90 | display: flex; | |
| 91 | align-items: center; | |
| 92 | gap: 8px; | |
| 93 | width: 100%; | |
| 94 | min-height: 44px; | |
| 95 | padding: 7px 10px; | |
| 96 | border-radius: 8px; | |
| 97 | border: 2px solid var(--vp-c-divider); | |
| 98 | background: var(--wk-c-menu-bg); | |
| 99 | color: var(--vp-c-text-1); | |
| 100 | font-size: 13px; | |
| 101 | cursor: pointer; | |
| 102 | transition: border-color 0.15s, background-color 0.15s; | |
| 103 | } | |
| 104 | ||
| 105 | .ext-rating-trigger:hover, | |
| 106 | .ext-rating-select--open .ext-rating-trigger { | |
| 107 | border-color: var(--vp-c-brand-1); | |
| 108 | } | |
| 109 | ||
| 110 | .ext-rating-label { | |
| 111 | flex: 1; | |
| 112 | text-align: left; | |
| 113 | } | |
| 114 | ||
| 115 | .ext-rating-chevron { | |
| 116 | flex-shrink: 0; | |
| 117 | color: var(--vp-c-text-2); | |
| 118 | transition: transform 0.2s ease; | |
| 119 | } | |
| 120 | ||
| 121 | .ext-rating-chevron--open { | |
| 122 | transform: rotate(180deg); | |
| 123 | } | |
| 124 | ||
| 125 | .ext-rating-options { | |
| 126 | margin-top: 6px; | |
| 127 | max-height: 240px; | |
| 128 | overflow-y: auto; | |
| 129 | overscroll-behavior: contain; | |
| 130 | display: flex; | |
| 131 | flex-direction: column; | |
| 132 | gap: 2px; | |
| 133 | padding: 4px; | |
| 134 | border-radius: 8px; | |
| 135 | border: 1px solid var(--vp-c-divider); | |
| 136 | background: var(--vp-c-bg-elv); | |
| 137 | box-shadow: var(--vp-shadow-2, 0 4px 16px rgba(0, 0, 0, 0.2)); | |
| 138 | } | |
| 139 | ||
| 140 | .ext-rating-option { | |
| 141 | display: flex; | |
| 142 | align-items: center; | |
| 143 | gap: 8px; | |
| 144 | padding: 5px 8px; | |
| 145 | border-radius: 8px; | |
| 146 | border: none; | |
| 147 | background: transparent; | |
| 148 | cursor: pointer; | |
| 149 | color: var(--vp-c-text-1); | |
| 150 | font-size: 13px; | |
| 151 | text-align: left; | |
| 152 | width: 100%; | |
| 153 | transition: background-color 0.15s; | |
| 154 | } | |
| 155 | ||
| 156 | .ext-rating-option-label { | |
| 157 | flex: 1; | |
| 158 | min-width: 0; | |
| 159 | } | |
| 160 | ||
| 161 | .ext-rating-option:hover { | |
| 162 | background-color: var(--vp-c-bg-soft); | |
| 163 | } | |
| 164 | ||
| 165 | .ext-rating-option--selected { | |
| 166 | background-color: var(--vp-c-brand-soft); | |
| 167 | font-weight: 600; | |
| 168 | } | |
| 169 | ||
| 170 | .ext-rating-option--selected:hover { | |
| 171 | background-color: var(--vp-c-brand-soft); | |
| 172 | } | |
| 173 | </style> | |