|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<script setup lang="ts"> |
| 7 | +import { computed } from 'vue' |
| 8 | +import { t } from '../../l10n.js' |
| 9 | +import { isMac } from '../../utils/platform.ts' |
| 10 | +
|
| 11 | +const props = withDefaults(defineProps<{ |
| 12 | + /** |
| 13 | + * Key name or symbol to display. |
| 14 | + * For common special keys (CTRL, ALT, SHIFT, Arrow keys, etc.) it will display the symbol on macOS and the localized name on Windows/Linux. |
| 15 | + */ |
| 16 | + symbol?: 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Control' | 'Alt' | 'Shift' | 'Enter' | 'Space' | 'Tab' | 'Delete' | 'Escape' | (string & {}) |
| 17 | + /** |
| 18 | + * Explicitly use macOS (true) or Windows/Linux (false) key symbols. |
| 19 | + * By default it uses the OS detected from the user agent. |
| 20 | + */ |
| 21 | + mac?: boolean | undefined |
| 22 | +}>(), { |
| 23 | + symbol: undefined, |
| 24 | + mac: isMac, |
| 25 | +}) |
| 26 | +
|
| 27 | +/** |
| 28 | + * Map of special key names to symbols or localized names: |
| 29 | + * - macOS uses symbols instead of names |
| 30 | + * - Windows/Linux uses localized names |
| 31 | + * - In ternary expressions, // TRANSLATORS comments only works for the second operand, but not for `else` |
| 32 | + */ |
| 33 | +const labels = computed(() => ({ |
| 34 | + ArrowUp: '↑', |
| 35 | + ArrowDown: '↓', |
| 36 | + ArrowLeft: '←', |
| 37 | + ArrowRight: '→', |
| 38 | + Control: !props.mac |
| 39 | + ? t('Ctrl') // TRANSLATORS: Ctrl key on keyboard (Windows/Linux) |
| 40 | + : '⌘', |
| 41 | + Alt: !props.mac |
| 42 | + ? t('Alt') // TRANSLATORS: Alt key on keyboard (Windows/Linux) |
| 43 | + : '⌥', |
| 44 | + Shift: !props.mac |
| 45 | + ? t('Shift') // TRANSLATORS: Shift key on keyboard |
| 46 | + : '⇧', |
| 47 | + Enter: !props.mac |
| 48 | + ? t('Enter') // TRANSLATORS: Enter key on keyboard |
| 49 | + : '⏎', |
| 50 | + Tab: !props.mac |
| 51 | + ? t('Tab') // TRANSLATORS: Tab key on keyboard |
| 52 | + : '⇥', |
| 53 | + Delete: !props.mac |
| 54 | + ? t('Delete') // TRANSLATORS: Delete key on keyboard |
| 55 | + : '⌫', |
| 56 | + Escape: !props.mac |
| 57 | + ? t('Escape') // TRANSLATORS: Escape key on keyboard |
| 58 | + : '⎋', |
| 59 | + Space: t('Space'), // TRANSLATORS: Space key on keyboard |
| 60 | +} as const)) |
| 61 | +
|
| 62 | +const label = computed(() => (props.symbol && labels.value[props.symbol]) || props.symbol) |
| 63 | +</script> |
| 64 | + |
| 65 | +<template> |
| 66 | + <kbd :class="$style.kbd"> |
| 67 | + <slot> |
| 68 | + {{ label }} |
| 69 | + </slot> |
| 70 | + </kbd> |
| 71 | +</template> |
| 72 | + |
| 73 | +<style lang="scss" module> |
| 74 | +.kbd { |
| 75 | + display: inline-flex; |
| 76 | + align-items: center; |
| 77 | + justify-content: center; |
| 78 | + min-width: var(--default-clickable-area); |
| 79 | + height: var(--default-clickable-area); |
| 80 | + padding-inline: calc(2 * var(--default-grid-baseline)) calc(2 * var(--default-grid-baseline)); |
| 81 | + border: 2px solid var(--color-primary-element-light); |
| 82 | + border-block-end-width: 4px; |
| 83 | + border-radius: var(--border-radius-element); |
| 84 | + box-shadow: none; /* Override server <kbd> styles */ |
| 85 | + font-family: var(--font-family); /* Design decision: looks better with the default font instead of mono */ |
| 86 | + line-height: 1; |
| 87 | + white-space: nowrap; |
| 88 | +
|
| 89 | + & + .kbd { |
| 90 | + margin-inline-start: calc(1 * var(--default-grid-baseline)); |
| 91 | + } |
| 92 | +} |
| 93 | +</style> |
| 94 | + |
| 95 | +<docs> |
| 96 | +Nextcloud-styled `<kbd>` element. It can be used with a single key symbol in the content or with the `symbol` prop (preferred). |
| 97 | + |
| 98 | +Subsequent `<NcKbd>` elements will have a small margin between them. |
| 99 | + |
| 100 | +```vue |
| 101 | +<template> |
| 102 | + <div> |
| 103 | + <NcKbd symbol="Control" /> |
| 104 | + <NcKbd symbol="F" /> |
| 105 | + </div> |
| 106 | +</template> |
| 107 | +``` |
| 108 | + |
| 109 | +### Special symbols |
| 110 | + |
| 111 | +It is recommended to use the `symbol` prop. It displays the appropriate symbol or localized name depending on the OS. |
| 112 | +OS detection is automatic but can be overridden via the `mac` prop. |
| 113 | + |
| 114 | +```vue |
| 115 | +<template> |
| 116 | + <table class="sample-table"> |
| 117 | + <tr> |
| 118 | + <th>symbol</th> |
| 119 | + <th>Auto</th> |
| 120 | + <th>macOS</th> |
| 121 | + <th>Windows/Linux</th> |
| 122 | + </tr> |
| 123 | + <tr> |
| 124 | + <th>ArrowUp</th> |
| 125 | + <td><NcKbd symbol="ArrowUp" /></td> |
| 126 | + <td><NcKbd symbol="ArrowUp" :mac="true" /></td> |
| 127 | + <td><NcKbd symbol="ArrowUp" :mac="false" /></td> |
| 128 | + </tr> |
| 129 | + <tr> |
| 130 | + <th>ArrowDown</th> |
| 131 | + <td><NcKbd symbol="ArrowDown" /></td> |
| 132 | + <td><NcKbd symbol="ArrowDown" :mac="true" /></td> |
| 133 | + <td><NcKbd symbol="ArrowDown" :mac="false" /></td> |
| 134 | + </tr> |
| 135 | + <tr> |
| 136 | + <th>ArrowLeft</th> |
| 137 | + <td><NcKbd symbol="ArrowLeft" /></td> |
| 138 | + <td><NcKbd symbol="ArrowLeft" :mac="true" /></td> |
| 139 | + <td><NcKbd symbol="ArrowLeft" :mac="false" /></td> |
| 140 | + </tr> |
| 141 | + <tr> |
| 142 | + <th>ArrowRight</th> |
| 143 | + <td><NcKbd symbol="ArrowRight" /></td> |
| 144 | + <td><NcKbd symbol="ArrowRight" :mac="true" /></td> |
| 145 | + <td><NcKbd symbol="ArrowRight" :mac="false" /></td> |
| 146 | + </tr> |
| 147 | + <tr> |
| 148 | + <th>Control</th> |
| 149 | + <td><NcKbd symbol="Control" /></td> |
| 150 | + <td><NcKbd symbol="Control" :mac="true" /></td> |
| 151 | + <td><NcKbd symbol="Control" :mac="false" /></td> |
| 152 | + </tr> |
| 153 | + <tr> |
| 154 | + <th>Alt</th> |
| 155 | + <td><NcKbd symbol="Alt" /></td> |
| 156 | + <td><NcKbd symbol="Alt" :mac="true" /></td> |
| 157 | + <td><NcKbd symbol="Alt" :mac="false" /></td> |
| 158 | + </tr> |
| 159 | + <tr> |
| 160 | + <th>Shift</th> |
| 161 | + <td><NcKbd symbol="Shift" /></td> |
| 162 | + <td><NcKbd symbol="Shift" :mac="true" /></td> |
| 163 | + <td><NcKbd symbol="Shift" :mac="false" /></td> |
| 164 | + </tr> |
| 165 | + <tr> |
| 166 | + <th>Enter</th> |
| 167 | + <td><NcKbd symbol="Enter" /></td> |
| 168 | + <td><NcKbd symbol="Enter" :mac="true" /></td> |
| 169 | + <td><NcKbd symbol="Enter" :mac="false" /></td> |
| 170 | + </tr> |
| 171 | + <tr> |
| 172 | + <th>Tab</th> |
| 173 | + <td><NcKbd symbol="Tab" /></td> |
| 174 | + <td><NcKbd symbol="Tab" :mac="true" /></td> |
| 175 | + <td><NcKbd symbol="Tab" :mac="false" /></td> |
| 176 | + </tr> |
| 177 | + <tr> |
| 178 | + <th>Delete</th> |
| 179 | + <td><NcKbd symbol="Delete" /></td> |
| 180 | + <td><NcKbd symbol="Delete" :mac="true" /></td> |
| 181 | + <td><NcKbd symbol="Delete" :mac="false" /></td> |
| 182 | + </tr> |
| 183 | + <tr> |
| 184 | + <th>Escape</th> |
| 185 | + <td><NcKbd symbol="Escape" /></td> |
| 186 | + <td><NcKbd symbol="Escape" :mac="true" /></td> |
| 187 | + <td><NcKbd symbol="Escape" :mac="false" /></td> |
| 188 | + </tr> |
| 189 | + <tr> |
| 190 | + <th>Space</th> |
| 191 | + <td><NcKbd symbol="Space" /></td> |
| 192 | + <td><NcKbd symbol="Space" :mac="true" /></td> |
| 193 | + <td><NcKbd symbol="Space" :mac="false" /></td> |
| 194 | + </tr> |
| 195 | + </table> |
| 196 | +</template> |
| 197 | + |
| 198 | +<style scoped> |
| 199 | +.sample-table { |
| 200 | + border-collapse: collapse; |
| 201 | + |
| 202 | + th, |
| 203 | + td { |
| 204 | + padding-inline: calc(2 * var(--default-grid-baseline)); |
| 205 | + padding-block: calc(1 * var(--default-grid-baseline)); |
| 206 | + } |
| 207 | + |
| 208 | + tr:first-child { |
| 209 | + border-block-end: 2px solid var(--color-border); |
| 210 | + |
| 211 | + th { |
| 212 | + font-weight: bold; |
| 213 | + } |
| 214 | + } |
| 215 | +} |
| 216 | +</style> |
| 217 | +``` |
| 218 | + |
| 219 | +### Custom content |
| 220 | + |
| 221 | +In a special case you might want to use a custom content. |
| 222 | + |
| 223 | +```vue |
| 224 | +<template> |
| 225 | + <NcKbd aria-label="Eject"> |
| 226 | + <IconEject :size="15" /> |
| 227 | + </NcKbd> |
| 228 | +</template> |
| 229 | + |
| 230 | +<script> |
| 231 | +import IconEject from 'vue-material-design-icons/Eject.vue' |
| 232 | + |
| 233 | +export default { |
| 234 | + components: { IconEject } |
| 235 | +} |
| 236 | +</script> |
| 237 | +``` |
| 238 | +</docs> |
0 commit comments