Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 142x 32x 3x 5x 70x 70x 70x 70x 70x 5x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 9x 9x 1x 1x 70x 70x 70x 70x 7x 7x | <template>
<div ref="rootRef" class="space-header-menu">
<button
type="button"
class="btn space-header-button"
:aria-label="label"
:title="label"
aria-haspopup="menu"
:aria-expanded="open"
:data-testid="testId"
@click="toggle"
>
<font-awesome-icon :icon="icon" />
<span v-if="buttonText" class="space-header-button-text">{{ buttonText }}</span>
</button>
<div
v-if="open"
ref="panelRef"
class="space-header-menu-panel"
role="menu"
:style="{ left: `${panelShift}px` }"
:data-testid="`${testId}-menu`"
>
<button
v-for="option in options"
:key="option.value"
type="button"
class="space-header-menu-item"
role="menuitemradio"
:aria-checked="option.value === modelValue"
:data-testid="`${testId}-${option.value}`"
@click="choose(option.value)"
>
<font-awesome-icon v-if="option.icon" :icon="option.icon" class="space-header-menu-icon" />
<span class="flex-grow-1">{{ option.label }}</span>
<font-awesome-icon
v-if="option.value === modelValue"
icon="check"
class="space-header-menu-check"
/>
</button>
</div>
</div>
</template>
<script setup lang="ts">
// @implements UC-SPACE-017.1, UC-SPACE-018.1
/**
* A small menu of the space's header (light/dark mode, language): a button,
* and a list of choices with the current one checked. Closes on a choice, a
* click outside or Escape, like the app's account menu. The list opens under
* the button from its left edge, as a phone's narrow header would cut a list
* opening leftward; it moves left only as far as the screen's right edge asks.
*/
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
export interface SpaceHeaderMenuOption {
value: string
label: string
icon?: string
}
defineProps<{
modelValue: string
options: SpaceHeaderMenuOption[]
icon: string
/** The button's accessible name and tooltip */
label: string
/** Shown next to the icon (e.g. the language code) */
buttonText?: string
testId: string
}>()
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
const open = ref(false)
const rootRef = ref<HTMLElement | null>(null)
const panelRef = ref<HTMLElement | null>(null)
/** How far left of the button the list starts: 0, unless it would leave the screen */
const panelShift = ref(0)
/** Kept between the list and the screen's edges */
const SCREEN_MARGIN = 8
async function toggle() {
open.value = !open.value
Iif (!open.value) return
panelShift.value = 0
await nextTick()
const panel = panelRef.value?.getBoundingClientRect()
Iif (!panel) return
const overflow = panel.right - (document.documentElement.clientWidth - SCREEN_MARGIN)
if (overflow > 0) panelShift.value = -Math.max(0, Math.min(overflow, panel.left - SCREEN_MARGIN))
}
function choose(value: string) {
open.value = false
emit('update:modelValue', value)
}
function onClickOutside(event: MouseEvent) {
const target = event.target as Node | null
if (target && rootRef.value?.contains(target)) return
open.value = false
}
function onKeydown(event: KeyboardEvent) {
Eif (event.key === 'Escape') open.value = false
}
onMounted(() => {
document.addEventListener('click', onClickOutside)
document.addEventListener('keydown', onKeydown)
})
onBeforeUnmount(() => {
document.removeEventListener('click', onClickOutside)
document.removeEventListener('keydown', onKeydown)
})
</script>
<style scoped>
.space-header-menu {
position: relative;
}
/* On the header's main color, like its text */
.space-header-button {
display: inline-flex;
align-items: center;
gap: 0.35rem;
height: 38px;
color: var(--space-on-primary);
border: 1px solid color-mix(in srgb, var(--space-on-primary) 45%, transparent);
background: transparent;
}
.space-header-button:hover,
.space-header-button:focus-visible {
color: var(--space-on-primary);
background: color-mix(in srgb, var(--space-on-primary) 15%, transparent);
}
.space-header-button-text {
font-size: 0.8rem;
font-weight: 600;
text-transform: uppercase;
}
.space-header-menu-panel {
position: absolute;
top: calc(100% + 0.25rem);
z-index: 1150;
min-width: 180px;
padding: 0.25rem 0;
background: var(--space-background);
color: var(--space-text);
border: 1px solid color-mix(in srgb, var(--space-text) 18%, transparent);
border-radius: 0.375rem;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
}
.space-header-menu-item {
display: flex;
align-items: center;
gap: 0.6rem;
width: 100%;
border: none;
background: transparent;
color: inherit;
text-align: left;
cursor: pointer;
padding: 0.5rem 1rem;
font-size: 0.9rem;
white-space: nowrap;
}
.space-header-menu-item:hover,
.space-header-menu-item:focus-visible {
background: color-mix(in srgb, var(--space-text) 8%, transparent);
}
.space-header-menu-icon {
width: 1rem;
opacity: 0.8;
}
/* The text's color: the main color can be too dark on a dark background */
.space-header-menu-check {
color: inherit;
}
</style>
|