docs/.vitepress/theme/components/settings/SettingsFlyout.vue

9c6588973e856dbc27ebb283f0d963c792faa8df
wotaku/docs/.vitepress/theme/components/settings/SettingsFlyout.vue history · blame · raw

86 lines · 1666 bytes

 1<script setup lang="ts">
 2import { onMounted, onUnmounted, ref } from 'vue'
 3
 4const open = ref(false)
 5const el = ref<HTMLElement>()
 6
 7function toggle() {
 8  open.value = !open.value
 9}
10
11function onDocClick(e: MouseEvent) {
12  if (el.value && !el.value.contains(e.target as Node)) {
13    open.value = false
14  }
15}
16
17onMounted(() => document.addEventListener('click', onDocClick, true))
18onUnmounted(() => document.removeEventListener('click', onDocClick, true))
19</script>
20
21<template>
22  <div ref="el" class="VPFlyout">
23    <button
24      type="button"
25      class="button"
26      aria-haspopup="true"
27      :aria-expanded="open"
28      aria-label="Enhanced Readability"
29      @click="toggle"
30    >
31      <span class="text">
32        <span class="i-lucide:settings option-icon" />
33      </span>
34    </button>
35
36    <div v-show="open" class="menu">
37      <div class="VPMenu">
38        <slot />
39      </div>
40    </div>
41  </div>
42</template>
43
44<style scoped>
45.VPFlyout {
46  position: relative;
47}
48
49.button {
50  display: flex;
51  align-items: center;
52  padding: 0 12px;
53  height: var(--vp-nav-height);
54  color: var(--vp-c-text-1);
55  transition: color 0.5s;
56}
57
58.text {
59  display: flex;
60  align-items: center;
61  line-height: var(--vp-nav-height);
62}
63
64.option-icon {
65  font-size: 16px;
66}
67
68.menu {
69  position: absolute;
70  top: calc(var(--vp-nav-height) / 2 + 20px);
71  right: 0;
72  z-index: 100;
73}
74
75.VPMenu {
76  border-radius: 12px;
77  padding: 12px;
78  min-width: 128px;
79  border: 1px solid var(--vp-c-divider);
80  background-color: var(--vp-c-bg-elv);
81  box-shadow: var(--vp-shadow-3);
82  transition: background-color 0.5s;
83  max-height: calc(100vh - var(--vp-nav-height));
84  overflow-y: auto;
85}
86</style>