docs/.vitepress/theme/components/extrepo/SettingsMenu.vue

ba4f23d3bab9e2bb3b8efdb22317c8cc8f353ec5
wotaku/docs/.vitepress/theme/components/extrepo/SettingsMenu.vue history · blame · raw

311 lines · 8606 bytes

  1<script setup lang="ts">
  2import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue'
  3import BrokenFilter from './BrokenFilter.vue'
  4import FilterSelect, { type FilterOption } from './FilterSelect.vue'
  5import { contentTypeLabel, langFlagClass, langLabel } from './helpers'
  6import type { RatingFilter as RatingFilterValue } from './types'
  7
  8const props = defineProps<{
  9  langFilter: string
 10  ratingFilter: RatingFilterValue
 11  availableLangs: string[]
 12  hasMultiLanguage: boolean
 13  hasRatings: boolean
 14  hasMixed: boolean
 15  hasSuggestive: boolean
 16  contentTypeFilter: string
 17  availableContentTypes: string[]
 18  hasContentTypes: boolean
 19  showBroken: boolean
 20  hasBroken: boolean
 21}>()
 22
 23defineEmits<{
 24  'update:langFilter': [value: string]
 25  'update:ratingFilter': [value: RatingFilterValue]
 26  'update:contentTypeFilter': [value: string]
 27  'update:showBroken': [value: boolean]
 28}>()
 29
 30const rootRef = ref<HTMLElement>()
 31const btnRef = ref<HTMLElement>()
 32const menuRef = ref<HTMLElement>()
 33const open = ref(false)
 34const menuStyle = ref<Record<string, string>>({})
 35let raf = 0
 36
 37function updatePosition() {
 38  if (!btnRef.value) return
 39  const viewportMargin = 12
 40  const menuWidth = menuRef.value?.offsetWidth ?? 260
 41  const btnRect = btnRef.value.getBoundingClientRect()
 42  const maxLeft = Math.max(
 43    viewportMargin,
 44    window.innerWidth - viewportMargin - menuWidth
 45  )
 46  const left = Math.min(
 47    Math.max(btnRect.right - menuWidth, viewportMargin),
 48    maxLeft
 49  )
 50  const maxTop = Math.max(viewportMargin, window.innerHeight - viewportMargin)
 51  const top = Math.min(btnRect.bottom + 6, maxTop)
 52  menuStyle.value = { top: `${top}px`, left: `${left}px` }
 53}
 54
 55function toggle() {
 56  open.value = !open.value
 57  if (open.value) void nextTick(updatePosition)
 58}
 59
 60function onDocClick(e: MouseEvent) {
 61  const target = e.target as Node
 62  if (
 63    rootRef.value && !rootRef.value.contains(target) &&
 64    menuRef.value && !menuRef.value.contains(target)
 65  ) {
 66    open.value = false
 67  }
 68}
 69
 70function onReposition() {
 71  if (!open.value || raf) return
 72  raf = requestAnimationFrame(() => {
 73    raf = 0
 74    updatePosition()
 75  })
 76}
 77
 78onMounted(() => {
 79  document.addEventListener('click', onDocClick, true)
 80  window.addEventListener('resize', onReposition, { passive: true })
 81  window.addEventListener('scroll', onReposition, {
 82    capture: true,
 83    passive: true
 84  })
 85})
 86
 87onUnmounted(() => {
 88  document.removeEventListener('click', onDocClick, true)
 89  window.removeEventListener('resize', onReposition)
 90  window.removeEventListener('scroll', onReposition, true)
 91  if (raf) cancelAnimationFrame(raf)
 92})
 93
 94const langOptions = computed<FilterOption[]>(() => [
 95  { value: '', label: 'All languages' },
 96  ...(props.hasMultiLanguage
 97    ? [{ value: 'all', label: 'Multi-language', flag: langFlagClass('all') }]
 98    : []),
 99  ...props.availableLangs.map(lang => ({
100    value: lang,
101    label: langLabel(lang),
102    flag: langFlagClass(lang)
103  }))
104])
105const langTrigger = computed(() =>
106  props.langFilter ? langLabel(props.langFilter) : 'All languages'
107)
108
109const ratingOptions = computed<FilterOption[]>(() => {
110  const opts: FilterOption[] = [
111    { value: 'all', label: 'All' },
112    { value: 'safe', label: 'Safe' }
113  ]
114  if (props.hasMixed) opts.push({ value: 'mixed', label: 'Mixed' })
115  if (props.hasSuggestive) {
116    opts.push({ value: 'suggestive', label: 'Mature' })
117  }
118  opts.push({
119    value: 'nsfw',
120    label: props.hasMixed || props.hasSuggestive ? 'Adult' : 'NSFW'
121  })
122  return opts
123})
124
125const typeOptions = computed<FilterOption[]>(() => [
126  { value: '', label: 'All types' },
127  ...props.availableContentTypes.map(t => ({
128    value: t,
129    label: contentTypeLabel(t)
130  }))
131])
132const typeTrigger = computed(() =>
133  props.contentTypeFilter
134    ? contentTypeLabel(props.contentTypeFilter)
135    : 'All types'
136)
137</script>
138
139<template>
140  <div ref="rootRef" class="ext-settings">
141    <button
142      ref="btnRef"
143      type="button"
144      class="ext-settings-btn"
145      aria-haspopup="true"
146      :aria-expanded="open"
147      aria-label="Filters"
148      @click="toggle"
149    >
150      <span class="i-lucide:settings" />
151    </button>
152
153    <Teleport to="body">
154      <div
155        v-show="open"
156        ref="menuRef"
157        class="ext-settings-menu VPMenu"
158        :class="{
159          'ext-settings-menu--wide': hasMixed || hasSuggestive || hasContentTypes
160        }"
161        :style="menuStyle"
162      >
163        <FilterSelect
164          v-if="availableLangs.length > 0"
165          :model-value="langFilter"
166          :options="langOptions"
167          :trigger-label="langTrigger"
168          title="Language"
169          icon="i-lucide:languages"
170          help-text="Filters extensions by translated language (not original language)."
171          @update:model-value="$emit('update:langFilter', $event)"
172        />
173        <FilterSelect
174          v-if="hasRatings"
175          :model-value="ratingFilter"
176          :options="ratingOptions"
177          title="Rating"
178          icon="i-lucide:shield"
179          help-text="Choose the highest content rating to include."
180          @update:model-value="$emit('update:ratingFilter', $event as RatingFilterValue)"
181        >
182          <template #help-extra>
183            <div class="rating-help-options">
184              <div class="rating-help-option">
185                <strong>All</strong>
186                <span>Shows entries at every rating level</span>
187              </div>
188              <div class="rating-help-option">
189                <strong>Safe</strong>
190                <span>Only entries with no mature content</span>
191              </div>
192              <div v-if="hasMixed" class="rating-help-option">
193                <strong>Mixed</strong>
194                <span>Includes mixed content</span>
195              </div>
196              <div v-if="hasSuggestive" class="rating-help-option">
197                <strong>Mature</strong>
198                <span>Includes mature content</span>
199              </div>
200              <div class="rating-help-option">
201                <strong>{{ hasMixed || hasSuggestive ? 'Adult' : 'NSFW' }}</strong>
202                <span>Includes explicit adult content</span>
203              </div>
204            </div>
205          </template>
206        </FilterSelect>
207        <FilterSelect
208          v-if="hasContentTypes"
209          :model-value="contentTypeFilter"
210          :options="typeOptions"
211          :trigger-label="typeTrigger"
212          title="Type"
213          icon="i-lucide:tags"
214          help-text="Filters extensions by type."
215          @update:model-value="$emit('update:contentTypeFilter', $event)"
216        />
217        <BrokenFilter
218          v-if="hasBroken"
219          :model-value="showBroken"
220          @update:model-value="$emit('update:showBroken', $event)"
221        />
222      </div>
223    </Teleport>
224  </div>
225</template>
226
227<style scoped>
228.ext-settings {
229  position: relative;
230  flex-shrink: 0;
231}
232.ext-settings-btn {
233  display: flex;
234  align-items: center;
235  justify-content: center;
236  width: 30px;
237  height: 30px;
238  padding: 0;
239  border: none;
240  border-radius: 0;
241  background: transparent;
242  color: var(--vp-c-text-1);
243  opacity: 0.55;
244  cursor: pointer;
245  transition: opacity 0.25s, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
246}
247.ext-settings-btn:hover,
248.ext-settings-btn[aria-expanded="true"] {
249  color: var(--vp-c-text-1);
250  opacity: 1;
251  background: transparent;
252}
253.ext-settings-btn:active {
254  transform: scale(0.94);
255}
256.ext-settings-btn span {
257  font-size: 15px;
258}
259html.effects-disabled .ext-settings-btn {
260  transition: none;
261}
262html.effects-disabled .ext-settings-btn:active {
263  transform: none;
264}
265.ext-settings-menu {
266  --vp-nolebase-enhanced-readabilities-menu-background-color: var(
267    --wk-c-menu-bg
268  );
269  --vp-nolebase-enhanced-readabilities-menu-text-color: var(--vp-c-text-1);
270  position: fixed;
271  z-index: 100;
272  width: 260px;
273  max-width: calc(100vw - 24px);
274  max-height: calc(100vh - 24px);
275  overflow-y: auto;
276  padding: 12px;
277  border-radius: 12px;
278  border: 1px solid var(--vp-c-divider);
279  background-color: var(--vp-c-bg-elv);
280  box-shadow: var(--vp-shadow-3, 0 4px 16px rgba(0, 0, 0, 0.3));
281}
282.ext-settings-menu > * + * {
283  margin-top: 14px;
284}
285.ext-settings-menu--wide {
286  width: 320px;
287}
288.rating-help-options {
289  display: flex;
290  flex-direction: column;
291  gap: 5px;
292}
293.rating-help-option {
294  display: flex;
295  flex-direction: column;
296  gap: 4px;
297  background: var(--wk-c-menu-bg);
298  border-radius: 12px;
299  padding: 10px 12px;
300}
301.rating-help-option strong {
302  font-size: 0.875rem;
303  font-weight: 600;
304  color: var(--vp-c-text-1);
305}
306.rating-help-option span {
307  font-size: 0.875rem;
308  color: var(--vp-c-text-2);
309  line-height: 1.4;
310}
311</style>