-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathNcKbd.vue
More file actions
244 lines (220 loc) · 6.08 KB
/
NcKbd.vue
File metadata and controls
244 lines (220 loc) · 6.08 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup lang="ts">
import type { Slot } from 'vue'
import { computed } from 'vue'
import { t } from '../../l10n.ts'
import { isMac } from '../../utils/platform.ts'
const {
symbol = undefined,
mac = isMac,
} = defineProps<{
/**
* Key name or symbol to display.
* For common special keys (CTRL, ALT, SHIFT, Arrow keys, etc.) it will display the symbol on macOS and the localized name on Windows/Linux.
*/
symbol?: 'ArrowUp' | 'ArrowDown' | 'ArrowLeft' | 'ArrowRight' | 'Control' | 'Alt' | 'Shift' | 'Enter' | 'Space' | 'Tab' | 'Delete' | 'Escape' | (string & {})
/**
* Explicitly use macOS (true) or Windows/Linux (false) key symbols.
* By default it uses the OS detected from the user agent.
*/
mac?: boolean | undefined
}>()
defineSlots<{
default?: Slot
}>()
/**
* Map of special key names to symbols or localized names:
* - macOS uses symbols instead of names
* - Windows/Linux uses localized names
* - In ternary expressions, // TRANSLATORS comments only works for the second operand, but not for `else`
*/
const labels = computed(() => ({
ArrowUp: '↑',
ArrowDown: '↓',
ArrowLeft: '←',
ArrowRight: '→',
Control: !mac
? t('Ctrl') // TRANSLATORS: Ctrl key on keyboard (Windows/Linux)
: '⌘',
Alt: !mac
? t('Alt') // TRANSLATORS: Alt key on keyboard (Windows/Linux)
: '⌥',
Shift: !mac
? t('Shift') // TRANSLATORS: Shift key on keyboard
: '⇧',
Enter: !mac
? t('Enter') // TRANSLATORS: Enter key on keyboard
: '⏎',
Tab: !mac
? t('Tab') // TRANSLATORS: Tab key on keyboard
: '⇥',
Delete: !mac
? t('Delete') // TRANSLATORS: Delete key on keyboard
: '⌫',
Escape: !mac
? t('Escape') // TRANSLATORS: Escape key on keyboard
: '⎋',
Space: t('Space'), // TRANSLATORS: Space key on keyboard
} as const))
const label = computed(() => (symbol && labels.value[symbol]) || symbol)
</script>
<template>
<kbd :class="$style.kbd">
<slot>
{{ label }}
</slot>
</kbd>
</template>
<style lang="scss" module>
.kbd {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: var(--default-clickable-area);
height: var(--default-clickable-area);
padding-inline: calc(2 * var(--default-grid-baseline)) calc(2 * var(--default-grid-baseline));
border: 2px solid var(--color-primary-element-light);
border-block-end-width: 4px;
border-radius: var(--border-radius-element);
box-shadow: none; /* Override server <kbd> styles */
font-family: var(--font-family); /* Design decision: looks better with the default font instead of mono */
line-height: 1;
white-space: nowrap;
& + .kbd {
margin-inline-start: calc(1 * var(--default-grid-baseline));
}
}
</style>
<docs>
Nextcloud-styled `<kbd>` element. It can be used with a single key symbol in the content or with the `symbol` prop (preferred).
Subsequent `<NcKbd>` elements will have a small margin between them.
```vue
<template>
<div>
<NcKbd symbol="Control" />
<NcKbd symbol="F" />
</div>
</template>
```
### Special symbols
It is recommended to use the `symbol` prop. It displays the appropriate symbol or localized name depending on the OS.
OS detection is automatic but can be overridden via the `mac` prop.
```vue
<template>
<table class="sample-table">
<tr>
<th>symbol</th>
<th>Auto</th>
<th>macOS</th>
<th>Windows/Linux</th>
</tr>
<tr>
<th>ArrowUp</th>
<td><NcKbd symbol="ArrowUp" /></td>
<td><NcKbd symbol="ArrowUp" :mac="true" /></td>
<td><NcKbd symbol="ArrowUp" :mac="false" /></td>
</tr>
<tr>
<th>ArrowDown</th>
<td><NcKbd symbol="ArrowDown" /></td>
<td><NcKbd symbol="ArrowDown" :mac="true" /></td>
<td><NcKbd symbol="ArrowDown" :mac="false" /></td>
</tr>
<tr>
<th>ArrowLeft</th>
<td><NcKbd symbol="ArrowLeft" /></td>
<td><NcKbd symbol="ArrowLeft" :mac="true" /></td>
<td><NcKbd symbol="ArrowLeft" :mac="false" /></td>
</tr>
<tr>
<th>ArrowRight</th>
<td><NcKbd symbol="ArrowRight" /></td>
<td><NcKbd symbol="ArrowRight" :mac="true" /></td>
<td><NcKbd symbol="ArrowRight" :mac="false" /></td>
</tr>
<tr>
<th>Control</th>
<td><NcKbd symbol="Control" /></td>
<td><NcKbd symbol="Control" :mac="true" /></td>
<td><NcKbd symbol="Control" :mac="false" /></td>
</tr>
<tr>
<th>Alt</th>
<td><NcKbd symbol="Alt" /></td>
<td><NcKbd symbol="Alt" :mac="true" /></td>
<td><NcKbd symbol="Alt" :mac="false" /></td>
</tr>
<tr>
<th>Shift</th>
<td><NcKbd symbol="Shift" /></td>
<td><NcKbd symbol="Shift" :mac="true" /></td>
<td><NcKbd symbol="Shift" :mac="false" /></td>
</tr>
<tr>
<th>Enter</th>
<td><NcKbd symbol="Enter" /></td>
<td><NcKbd symbol="Enter" :mac="true" /></td>
<td><NcKbd symbol="Enter" :mac="false" /></td>
</tr>
<tr>
<th>Tab</th>
<td><NcKbd symbol="Tab" /></td>
<td><NcKbd symbol="Tab" :mac="true" /></td>
<td><NcKbd symbol="Tab" :mac="false" /></td>
</tr>
<tr>
<th>Delete</th>
<td><NcKbd symbol="Delete" /></td>
<td><NcKbd symbol="Delete" :mac="true" /></td>
<td><NcKbd symbol="Delete" :mac="false" /></td>
</tr>
<tr>
<th>Escape</th>
<td><NcKbd symbol="Escape" /></td>
<td><NcKbd symbol="Escape" :mac="true" /></td>
<td><NcKbd symbol="Escape" :mac="false" /></td>
</tr>
<tr>
<th>Space</th>
<td><NcKbd symbol="Space" /></td>
<td><NcKbd symbol="Space" :mac="true" /></td>
<td><NcKbd symbol="Space" :mac="false" /></td>
</tr>
</table>
</template>
<style scoped>
.sample-table {
border-collapse: collapse;
th,
td {
padding-inline: calc(2 * var(--default-grid-baseline));
padding-block: calc(1 * var(--default-grid-baseline));
}
tr:first-child {
border-block-end: 2px solid var(--color-border);
th {
font-weight: var(--font-weight-heading, bold);
}
}
}
</style>
```
### Custom content
In a special case you might want to use a custom content.
```vue
<template>
<NcKbd aria-label="Eject">
<IconEject :size="15" />
</NcKbd>
</template>
<script>
import IconEject from 'vue-material-design-icons/Eject.vue'
export default {
components: { IconEject }
}
</script>
```
</docs>