|
| 1 | + <!-- |
| 2 | + - @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> |
| 3 | + - |
| 4 | + - @author John Molakvoæ <skjnldsv@protonmail.com> |
| 5 | + - |
| 6 | + - @license GNU AGPL version 3 or any later version |
| 7 | + - |
| 8 | + - This program is free software: you can redistribute it and/or modify |
| 9 | + - it under the terms of the GNU Affero General Public License as |
| 10 | + - published by the Free Software Foundation, either version 3 of the |
| 11 | + - License, or (at your option) any later version. |
| 12 | + - |
| 13 | + - This program is distributed in the hope that it will be useful, |
| 14 | + - but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + - GNU Affero General Public License for more details. |
| 17 | + - |
| 18 | + - You should have received a copy of the GNU Affero General Public License |
| 19 | + - along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + - |
| 21 | + --> |
| 22 | +<template> |
| 23 | + <div :id="id" |
| 24 | + v-click-outside="clickOutsideConfig" |
| 25 | + :class="{ 'header-menu--opened': opened }" |
| 26 | + class="header-menu"> |
| 27 | + <a class="header-menu__trigger" |
| 28 | + href="#" |
| 29 | + :aria-label="ariaLabel" |
| 30 | + :aria-controls="`header-menu-${id}`" |
| 31 | + :aria-expanded="opened" |
| 32 | + aria-haspopup="menu" |
| 33 | + @click.prevent="toggleMenu"> |
| 34 | + <slot name="trigger" /> |
| 35 | + </a> |
| 36 | + <div v-show="opened" class="header-menu__carret" /> |
| 37 | + <div v-show="opened" |
| 38 | + :id="`header-menu-${id}`" |
| 39 | + class="header-menu__wrapper" |
| 40 | + role="menu"> |
| 41 | + <div class="header-menu__content"> |
| 42 | + <slot /> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | +</template> |
| 47 | + |
| 48 | +<script> |
| 49 | +import { directive as ClickOutside } from 'v-click-outside' |
| 50 | +import excludeClickOutsideClasses from '@nextcloud/vue/dist/Mixins/excludeClickOutsideClasses' |
| 51 | +
|
| 52 | +export default { |
| 53 | + name: 'HeaderMenu', |
| 54 | +
|
| 55 | + directives: { |
| 56 | + ClickOutside, |
| 57 | + }, |
| 58 | +
|
| 59 | + mixins: [ |
| 60 | + excludeClickOutsideClasses, |
| 61 | + ], |
| 62 | +
|
| 63 | + props: { |
| 64 | + id: { |
| 65 | + type: String, |
| 66 | + required: true, |
| 67 | + }, |
| 68 | + ariaLabel: { |
| 69 | + type: String, |
| 70 | + default: '', |
| 71 | + }, |
| 72 | + open: { |
| 73 | + type: Boolean, |
| 74 | + default: false, |
| 75 | + }, |
| 76 | + }, |
| 77 | +
|
| 78 | + data() { |
| 79 | + return { |
| 80 | + opened: this.open, |
| 81 | + clickOutsideConfig: { |
| 82 | + handler: this.closeMenu, |
| 83 | + middleware: this.clickOutsideMiddleware, |
| 84 | + }, |
| 85 | + } |
| 86 | + }, |
| 87 | +
|
| 88 | + watch: { |
| 89 | + open(newVal) { |
| 90 | + this.opened = newVal |
| 91 | + this.$nextTick(() => { |
| 92 | + if (this.opened) { |
| 93 | + this.openMenu() |
| 94 | + } else { |
| 95 | + this.closeMenu() |
| 96 | + } |
| 97 | + }) |
| 98 | + }, |
| 99 | + }, |
| 100 | +
|
| 101 | + mounted() { |
| 102 | + document.addEventListener('keydown', this.onKeyDown) |
| 103 | + }, |
| 104 | + beforeDestroy() { |
| 105 | + document.removeEventListener('keydown', this.onKeyDown) |
| 106 | + }, |
| 107 | +
|
| 108 | + methods: { |
| 109 | + /** |
| 110 | + * Toggle the current menu open state |
| 111 | + */ |
| 112 | + toggleMenu() { |
| 113 | + // Toggling current state |
| 114 | + if (!this.opened) { |
| 115 | + this.openMenu() |
| 116 | + } else { |
| 117 | + this.closeMenu() |
| 118 | + } |
| 119 | + }, |
| 120 | +
|
| 121 | + /** |
| 122 | + * Close the current menu |
| 123 | + */ |
| 124 | + closeMenu() { |
| 125 | + if (!this.opened) { |
| 126 | + return |
| 127 | + } |
| 128 | +
|
| 129 | + this.opened = false |
| 130 | + this.$emit('close') |
| 131 | + this.$emit('update:open', false) |
| 132 | + }, |
| 133 | +
|
| 134 | + /** |
| 135 | + * Open the current menu |
| 136 | + */ |
| 137 | + openMenu() { |
| 138 | + if (this.opened) { |
| 139 | + return |
| 140 | + } |
| 141 | +
|
| 142 | + this.opened = true |
| 143 | + this.$emit('open') |
| 144 | + this.$emit('update:open', true) |
| 145 | + }, |
| 146 | +
|
| 147 | + onKeyDown(event) { |
| 148 | + // If opened and escape pressed, close |
| 149 | + if (event.key === 'Escape' && this.opened) { |
| 150 | + event.preventDefault() |
| 151 | +
|
| 152 | + /** user cancelled the menu by pressing escape */ |
| 153 | + this.$emit('cancel') |
| 154 | +
|
| 155 | + /** we do NOT fire a close event to differentiate cancel and close */ |
| 156 | + this.opened = false |
| 157 | + this.$emit('update:open', false) |
| 158 | + } |
| 159 | + }, |
| 160 | + }, |
| 161 | +} |
| 162 | +</script> |
| 163 | + |
| 164 | +<style lang="scss" scoped> |
| 165 | +.header-menu { |
| 166 | + &__trigger { |
| 167 | + display: flex; |
| 168 | + align-items: center; |
| 169 | + justify-content: center; |
| 170 | + width: 50px; |
| 171 | + height: 44px; |
| 172 | + margin: 2px 0; |
| 173 | + padding: 0; |
| 174 | + cursor: pointer; |
| 175 | + opacity: .85; |
| 176 | + } |
| 177 | +
|
| 178 | + &--opened &__trigger, |
| 179 | + &__trigger:hover, |
| 180 | + &__trigger:focus, |
| 181 | + &__trigger:active { |
| 182 | + opacity: 1; |
| 183 | + } |
| 184 | +
|
| 185 | + &__trigger:focus-visible { |
| 186 | + outline: none; |
| 187 | + } |
| 188 | +
|
| 189 | + &__wrapper { |
| 190 | + position: fixed; |
| 191 | + z-index: 2000; |
| 192 | + top: 50px; |
| 193 | + right: 0; |
| 194 | + box-sizing: border-box; |
| 195 | + margin: 0; |
| 196 | + border-radius: 0 0 var(--border-radius) var(--border-radius); |
| 197 | + background-color: var(--color-main-background); |
| 198 | +
|
| 199 | + filter: drop-shadow(0 1px 5px var(--color-box-shadow)); |
| 200 | + } |
| 201 | +
|
| 202 | + &__carret { |
| 203 | + position: absolute; |
| 204 | + z-index: 2001; // Because __wrapper is 2000. |
| 205 | + left: calc(50% - 10px); |
| 206 | + bottom: 0; |
| 207 | + width: 0; |
| 208 | + height: 0; |
| 209 | + content: ' '; |
| 210 | + pointer-events: none; |
| 211 | + border: 10px solid transparent; |
| 212 | + border-bottom-color: var(--color-main-background); |
| 213 | + } |
| 214 | +
|
| 215 | + &__content { |
| 216 | + overflow: auto; |
| 217 | + width: 350px; |
| 218 | + max-width: 100vw; |
| 219 | + min-height: calc(44px * 1.5); |
| 220 | + max-height: calc(100vh - 50px * 2); |
| 221 | + } |
| 222 | +} |
| 223 | +
|
| 224 | +</style> |
0 commit comments