|
| 1 | +<script setup lang="ts"> |
| 2 | +import { onKeyStroke, onClickOutside } from '@vueuse/core' |
| 3 | +
|
| 4 | +const { settings } = useSettings() |
| 5 | +
|
| 6 | +const isOpen = ref(false) |
| 7 | +const menuRef = useTemplateRef('menuRef') |
| 8 | +const triggerRef = useTemplateRef('triggerRef') |
| 9 | +
|
| 10 | +function toggle() { |
| 11 | + isOpen.value = !isOpen.value |
| 12 | +} |
| 13 | +
|
| 14 | +function close() { |
| 15 | + isOpen.value = false |
| 16 | +} |
| 17 | +
|
| 18 | +// Close on click outside |
| 19 | +onClickOutside(menuRef, close, { ignore: [triggerRef] }) |
| 20 | +
|
| 21 | +// Close on Escape |
| 22 | +onKeyStroke('Escape', () => { |
| 23 | + if (isOpen.value) { |
| 24 | + close() |
| 25 | + triggerRef.value?.focus() |
| 26 | + } |
| 27 | +}) |
| 28 | +
|
| 29 | +// Open with comma key (global shortcut) |
| 30 | +onKeyStroke(',', e => { |
| 31 | + // Don't trigger if user is typing in an input |
| 32 | + const target = e.target as HTMLElement |
| 33 | + if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable) { |
| 34 | + return |
| 35 | + } |
| 36 | + e.preventDefault() |
| 37 | + toggle() |
| 38 | +}) |
| 39 | +</script> |
| 40 | + |
| 41 | +<template> |
| 42 | + <div class="relative flex items-center"> |
| 43 | + <button |
| 44 | + ref="triggerRef" |
| 45 | + type="button" |
| 46 | + class="link-subtle font-mono text-sm inline-flex items-center justify-center gap-2" |
| 47 | + :aria-expanded="isOpen" |
| 48 | + aria-haspopup="menu" |
| 49 | + aria-label="Settings" |
| 50 | + aria-keyshortcuts="," |
| 51 | + @click="toggle" |
| 52 | + > |
| 53 | + <span class="i-carbon-settings w-4 h-4 sm:hidden" aria-hidden="true" /> |
| 54 | + <span class="hidden sm:inline">settings</span> |
| 55 | + <kbd |
| 56 | + class="hidden sm:inline-flex items-center justify-center w-5 h-5 text-xs bg-bg-muted border border-border rounded" |
| 57 | + aria-hidden="true" |
| 58 | + > |
| 59 | + , |
| 60 | + </kbd> |
| 61 | + </button> |
| 62 | + |
| 63 | + <!-- Settings popover --> |
| 64 | + <Transition |
| 65 | + enter-active-class="transition-opacity transition-transform duration-100 ease-out motion-reduce:transition-none" |
| 66 | + enter-from-class="opacity-0 scale-95" |
| 67 | + enter-to-class="opacity-100 scale-100" |
| 68 | + leave-active-class="transition-opacity transition-transform duration-75 ease-in motion-reduce:transition-none" |
| 69 | + leave-from-class="opacity-100 scale-100" |
| 70 | + leave-to-class="opacity-0 scale-95" |
| 71 | + > |
| 72 | + <div |
| 73 | + v-if="isOpen" |
| 74 | + ref="menuRef" |
| 75 | + role="menu" |
| 76 | + class="absolute right-0 top-full mt-2 w-64 bg-bg-elevated border border-border rounded-lg shadow-lg z-50 overflow-hidden" |
| 77 | + > |
| 78 | + <div class="px-3 py-2 border-b border-border"> |
| 79 | + <h2 class="text-xs text-fg-subtle uppercase tracking-wider">Settings</h2> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div class="p-2 space-y-1"> |
| 83 | + <!-- Relative dates toggle --> |
| 84 | + <div |
| 85 | + class="flex items-center justify-between gap-3 px-2 py-2 rounded-md hover:bg-bg-muted transition-[background-color] duration-150 cursor-pointer" |
| 86 | + @click="settings.relativeDates = !settings.relativeDates" |
| 87 | + > |
| 88 | + <label |
| 89 | + :id="`settings-relative-dates-label`" |
| 90 | + class="text-sm text-fg cursor-pointer select-none" |
| 91 | + > |
| 92 | + Relative dates |
| 93 | + </label> |
| 94 | + <button |
| 95 | + type="button" |
| 96 | + role="switch" |
| 97 | + :aria-checked="settings.relativeDates" |
| 98 | + aria-labelledby="settings-relative-dates-label" |
| 99 | + class="relative inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-[background-color] duration-200 ease-in-out motion-reduce:transition-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 focus-visible:ring-offset-2 focus-visible:ring-offset-bg" |
| 100 | + :class="settings.relativeDates ? 'bg-fg' : 'bg-bg-subtle'" |
| 101 | + @click.stop="settings.relativeDates = !settings.relativeDates" |
| 102 | + > |
| 103 | + <span |
| 104 | + aria-hidden="true" |
| 105 | + class="pointer-events-none inline-block h-4 w-4 rounded-full shadow-sm ring-0 transition-transform duration-200 ease-in-out motion-reduce:transition-none" |
| 106 | + :class=" |
| 107 | + settings.relativeDates ? 'translate-x-4 bg-bg' : 'translate-x-0 bg-fg-muted' |
| 108 | + " |
| 109 | + /> |
| 110 | + </button> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + </Transition> |
| 115 | + </div> |
| 116 | +</template> |
0 commit comments