@thekestongroup/ui 0.6.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +0 -1
- package/dist/components/Checkbox/Checkbox.vue.d.ts +2 -0
- package/dist/components/CheckboxGroup/CheckboxGroup.vue.d.ts +45 -0
- package/dist/components/Chip/Chip.vue.d.ts +0 -1
- package/dist/components/Header/Header.vue.d.ts +0 -1
- package/dist/components/InputDate/InputDate.vue.d.ts +1 -1
- package/dist/components/InputTextArea/InputTextArea.vue.d.ts +49 -0
- package/dist/components/LanguageSwitcher/LanguageSwitcher.vue.d.ts +24 -0
- package/dist/components/Notification/NotificationPane.vue.d.ts +1 -4
- package/dist/components/PageHeader/PageHeader.vue.d.ts +8 -3
- package/dist/components/Select/Select.vue.d.ts +0 -1
- package/dist/components/SettingsRow/SettingsRow.vue.d.ts +28 -0
- package/dist/components/Sidebar/Sidebar.vue.d.ts +0 -1
- package/dist/components/Slideover/Slideover.vue.d.ts +0 -1
- package/dist/components/Spinner/Spinner.vue.d.ts +0 -1
- package/dist/components/Steps/Steps.vue.d.ts +20 -0
- package/dist/components/Switch/Switch.vue.d.ts +2 -0
- package/dist/components/Table/Table.vue.d.ts +21 -1
- package/dist/i18n/en.d.ts +146 -0
- package/dist/i18n/index.d.ts +32 -0
- package/dist/index.d.ts +12 -0
- package/dist/keston-ui.js +2288 -1718
- package/package.json +4 -2
- package/src/components/Age/Age.vue +4 -2
- package/src/components/AppShell/AppShell.vue +4 -2
- package/src/components/AvatarGroup/AvatarGroup.vue +4 -2
- package/src/components/Breadcrumb/Breadcrumb.vue +3 -2
- package/src/components/Calendar/Calendar.vue +8 -6
- package/src/components/Card/Card.vue +3 -1
- package/src/components/Checkbox/Checkbox.stories.ts +3 -0
- package/src/components/Checkbox/Checkbox.vue +3 -1
- package/src/components/CheckboxGroup/CheckboxGroup.stories.ts +48 -0
- package/src/components/CheckboxGroup/CheckboxGroup.vue +96 -0
- package/src/components/Chip/Chip.vue +3 -2
- package/src/components/FormField/FormField.vue +3 -1
- package/src/components/Header/Header.vue +6 -5
- package/src/components/Input/Input.vue +4 -2
- package/src/components/InputCode/InputCode.vue +4 -2
- package/src/components/InputDate/InputDate.vue +4 -2
- package/src/components/InputPassword/InputPassword.vue +4 -2
- package/src/components/InputRichText/InputRichText.vue +6 -4
- package/src/components/InputTextArea/InputTextArea.stories.ts +53 -0
- package/src/components/InputTextArea/InputTextArea.vue +89 -0
- package/src/components/LanguageSwitcher/LanguageSwitcher.stories.ts +21 -0
- package/src/components/LanguageSwitcher/LanguageSwitcher.vue +57 -0
- package/src/components/Link/Link.vue +1 -0
- package/src/components/Notification/Notification.vue +3 -1
- package/src/components/Notification/NotificationPane.vue +10 -8
- package/src/components/Notification/Toaster.vue +9 -7
- package/src/components/PageHeader/PageHeader.stories.ts +29 -1
- package/src/components/PageHeader/PageHeader.vue +108 -22
- package/src/components/Progress/Progress.vue +3 -1
- package/src/components/RadioGroup/RadioGroup.vue +3 -1
- package/src/components/Select/Select.vue +6 -5
- package/src/components/SettingsRow/SettingsRow.stories.ts +59 -0
- package/src/components/SettingsRow/SettingsRow.vue +39 -0
- package/src/components/Sidebar/Sidebar.vue +5 -4
- package/src/components/Slideover/Slideover.vue +8 -7
- package/src/components/Spinner/Spinner.vue +4 -3
- package/src/components/StatTile/StatTile.vue +3 -1
- package/src/components/Steps/Steps.stories.ts +38 -0
- package/src/components/Steps/Steps.vue +38 -0
- package/src/components/SummaryList/SummaryList.vue +5 -4
- package/src/components/Switch/Switch.vue +3 -1
- package/src/components/Table/Table.stories.ts +34 -0
- package/src/components/Table/Table.vue +98 -8
- package/src/i18n/en.ts +127 -0
- package/src/i18n/i18n.test.ts +56 -0
- package/src/i18n/index.ts +89 -0
- package/src/index.ts +14 -0
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
<script setup lang="ts" generic="Row extends Record<string, any>">
|
|
2
|
+
import { useI18n } from 'vue-i18n'
|
|
2
3
|
import { computed, onBeforeUnmount, onMounted, ref, useId, type Component, type HTMLAttributes } from 'vue'
|
|
3
4
|
import { ArrowDown, ArrowUp, ChevronsUpDown } from '@lucide/vue'
|
|
4
5
|
import { cn } from '../../lib/cn'
|
|
5
6
|
import EmptyState from '../EmptyState/EmptyState.vue'
|
|
7
|
+
import Checkbox from '../Checkbox/Checkbox.vue'
|
|
6
8
|
import Spinner from '../Spinner/Spinner.vue'
|
|
7
9
|
import { nextSort, sortRows, type TableColumn, type TableSort } from './table'
|
|
8
10
|
|
|
@@ -44,6 +46,14 @@ export interface TableProps<Row = Record<string, any>> {
|
|
|
44
46
|
loading?: boolean
|
|
45
47
|
/** md (default) 12px above and below each cell, sm 8px for dense tables. */
|
|
46
48
|
size?: 'sm' | 'md'
|
|
49
|
+
/**
|
|
50
|
+
* For a table that fills a `Card` with `padding="none"` and has a title
|
|
51
|
+
* above it (so the content lines up with the card's padding): the first and
|
|
52
|
+
* last columns have 24px at the table's outer edges (from sm), and there is
|
|
53
|
+
* the same space beneath the last row. The row lines still run edge to
|
|
54
|
+
* edge. Leave it off for a table that is the whole card.
|
|
55
|
+
*/
|
|
56
|
+
inset?: boolean
|
|
47
57
|
/** Keep the header row in view while the table scrolls. Give the table a max height (`class="max-h-96"`). */
|
|
48
58
|
stickyHeader?: boolean
|
|
49
59
|
/** Don't sort the rows: just update `sort`, e.g. to sort on the server. */
|
|
@@ -56,6 +66,15 @@ export interface TableProps<Row = Record<string, any>> {
|
|
|
56
66
|
rowLink?: (row: Row, index: number) => unknown
|
|
57
67
|
/** Component for row links, e.g. RouterLink. It receives `to`. Default a plain `<a href>`. */
|
|
58
68
|
linkComponent?: string | Component
|
|
69
|
+
/**
|
|
70
|
+
* A checkbox at the start of each row (and "select all" in the header), for
|
|
71
|
+
* acting on several rows. `true` for every row, or a function saying which
|
|
72
|
+
* rows can be ticked. The whole cell is the click target. Ticked rows'
|
|
73
|
+
* keys (`rowKey`) are in `v-model:selected`.
|
|
74
|
+
*/
|
|
75
|
+
selectable?: boolean | ((row: Row) => boolean)
|
|
76
|
+
/** Describes a row for its checkbox, e.g. its name: "Select Jo Bloggs". Default "Select row 2". */
|
|
77
|
+
rowLabel?: (row: Row, index: number) => string
|
|
59
78
|
class?: HTMLAttributes['class']
|
|
60
79
|
ui?: {
|
|
61
80
|
table?: HTMLAttributes['class']
|
|
@@ -64,13 +83,15 @@ export interface TableProps<Row = Record<string, any>> {
|
|
|
64
83
|
}
|
|
65
84
|
}
|
|
66
85
|
|
|
86
|
+
const { t } = useI18n()
|
|
67
87
|
const props = withDefaults(defineProps<TableProps<Row>>(), {
|
|
68
|
-
emptyText: 'Nothing to show.',
|
|
69
88
|
size: 'md',
|
|
70
89
|
linkComponent: 'a'
|
|
71
90
|
})
|
|
72
91
|
|
|
73
92
|
const sort = defineModel<TableSort | null>('sort', { default: null })
|
|
93
|
+
/** Ticked rows' keys (see `selectable`). */
|
|
94
|
+
const selected = defineModel<(string | number)[]>('selected', { default: () => [] })
|
|
74
95
|
|
|
75
96
|
defineSlots<
|
|
76
97
|
{
|
|
@@ -86,6 +107,37 @@ defineSlots<
|
|
|
86
107
|
|
|
87
108
|
const captionId = `${useId()}-caption`
|
|
88
109
|
|
|
110
|
+
// ── Selecting rows ──────────────────────────────────────────────────────────
|
|
111
|
+
const canSelect = (row: Row) => (typeof props.selectable === 'function' ? props.selectable(row) : !!props.selectable)
|
|
112
|
+
const selectableKeys = computed(() => shownRows.value.flatMap((row, i) => (canSelect(row) ? [keyOf(row, i)] : [])))
|
|
113
|
+
const isSelected = (row: Row, index: number) => selected.value.includes(keyOf(row, index))
|
|
114
|
+
function setRow(row: Row, index: number, on: boolean) {
|
|
115
|
+
const key = keyOf(row, index)
|
|
116
|
+
selected.value = on ? [...selected.value.filter((k) => k !== key), key] : selected.value.filter((k) => k !== key)
|
|
117
|
+
}
|
|
118
|
+
/** Header box: ticked when every row is, a dash when some are. */
|
|
119
|
+
const allState = computed<boolean | 'indeterminate'>(() => {
|
|
120
|
+
const n = selectableKeys.value.filter((k) => selected.value.includes(k)).length
|
|
121
|
+
return n === 0 ? false : n === selectableKeys.value.length ? true : 'indeterminate'
|
|
122
|
+
})
|
|
123
|
+
function setAll(on: boolean) {
|
|
124
|
+
const keys = selectableKeys.value
|
|
125
|
+
selected.value = on ? [...new Set([...selected.value, ...keys])] : selected.value.filter((k) => !keys.includes(k))
|
|
126
|
+
}
|
|
127
|
+
// The checkbox cell is above a row's link, so clicking anywhere in it ticks
|
|
128
|
+
// the box rather than opening the row.
|
|
129
|
+
const selectCell = 'relative z-1 w-0 cursor-pointer'
|
|
130
|
+
/**
|
|
131
|
+
* Pressing anywhere in the cell ticks the box: the cell's padding and the gap
|
|
132
|
+
* around the box, not only the box. The box and its (hidden) label do their
|
|
133
|
+
* own ticking, so they're left alone, or it would tick twice.
|
|
134
|
+
*/
|
|
135
|
+
function onCellPress(event: MouseEvent, toggle: () => void) {
|
|
136
|
+
if ((event.target as HTMLElement).closest('button, label, .sr-only')) return
|
|
137
|
+
toggle()
|
|
138
|
+
}
|
|
139
|
+
const colspan = computed(() => props.columns.length + (props.selectable ? 1 : 0))
|
|
140
|
+
|
|
89
141
|
const shownRows = computed(() => (props.manualSort ? props.rows : sortRows(props.rows, props.columns, sort.value)))
|
|
90
142
|
|
|
91
143
|
function keyOf(row: Row, index: number) {
|
|
@@ -124,7 +176,11 @@ const linkedRowClass = [
|
|
|
124
176
|
'[&_:is(a,button,input,select,textarea,label,[tabindex]):not([data-row-link])]:z-1'
|
|
125
177
|
]
|
|
126
178
|
|
|
127
|
-
const cellPadding = computed(() =>
|
|
179
|
+
const cellPadding = computed(() => {
|
|
180
|
+
const base = props.size === 'sm' ? 'px-4 py-2' : 'px-4 py-3'
|
|
181
|
+
return props.inset ? `${base} sm:first:pl-6 sm:last:pr-6` : base
|
|
182
|
+
})
|
|
183
|
+
|
|
128
184
|
|
|
129
185
|
function columnClasses(column: TableColumn<Row>) {
|
|
130
186
|
return cn(column.align === 'end' && 'text-right', column.nowrap !== false && 'whitespace-nowrap', column.class)
|
|
@@ -164,18 +220,33 @@ onBeforeUnmount(() => observer?.disconnect())
|
|
|
164
220
|
:tabindex="scrolls ? 0 : undefined"
|
|
165
221
|
:role="scrolls ? 'region' : undefined"
|
|
166
222
|
:aria-labelledby="scrolls && (caption || $slots.caption) ? captionId : undefined"
|
|
167
|
-
:aria-label="scrolls && !caption && !$slots.caption ? '
|
|
223
|
+
:aria-label="scrolls && !caption && !$slots.caption ? t('ui.table.table') : undefined"
|
|
168
224
|
>
|
|
169
225
|
<table
|
|
170
226
|
:class="cn('w-full border-separate border-spacing-0 text-left text-sm text-black', ui?.table)"
|
|
171
227
|
:aria-busy="loading || undefined"
|
|
172
228
|
>
|
|
173
|
-
<caption v-if="caption || $slots.caption" :id="captionId" :class="showCaption ? 'px-4 pt-4 pb-2 text-left text-base font-bold' : 'sr-only'">
|
|
229
|
+
<caption v-if="caption || $slots.caption" :id="captionId" :class="showCaption ? cn('px-4 pt-4 pb-2 text-left text-base font-bold', inset && 'sm:px-6') : 'sr-only'">
|
|
174
230
|
<slot name="caption">{{ caption }}</slot>
|
|
175
231
|
</caption>
|
|
176
232
|
|
|
177
233
|
<thead :class="cn(stickyHeader && 'sticky top-0 z-10 bg-white', ui?.header)">
|
|
178
234
|
<tr>
|
|
235
|
+
<th
|
|
236
|
+
v-if="selectable"
|
|
237
|
+
scope="col"
|
|
238
|
+
:class="cn('border-b border-black/15', cellPadding, selectableKeys.length && selectCell)"
|
|
239
|
+
@click="onCellPress($event, () => selectableKeys.length && setAll(allState !== true))"
|
|
240
|
+
>
|
|
241
|
+
<Checkbox
|
|
242
|
+
v-if="selectableKeys.length"
|
|
243
|
+
:model-value="allState"
|
|
244
|
+
:label="t('ui.table.selectAll')"
|
|
245
|
+
hide-label
|
|
246
|
+
@update:model-value="setAll($event === true)"
|
|
247
|
+
/>
|
|
248
|
+
<span v-else class="sr-only">{{ t('ui.table.select') }}</span>
|
|
249
|
+
</th>
|
|
179
250
|
<th
|
|
180
251
|
v-for="column in columns"
|
|
181
252
|
:key="column.key"
|
|
@@ -211,13 +282,13 @@ onBeforeUnmount(() => observer?.disconnect())
|
|
|
211
282
|
|
|
212
283
|
<tbody :class="cn(loading && shownRows.length && 'opacity-50 transition-opacity')">
|
|
213
284
|
<tr v-if="!shownRows.length">
|
|
214
|
-
<td :colspan="
|
|
285
|
+
<td :colspan="colspan" class="px-4 py-8 text-center text-black/65">
|
|
215
286
|
<span v-if="loading" class="inline-flex items-center gap-2">
|
|
216
287
|
<Spinner size="sm" />
|
|
217
288
|
Loading…
|
|
218
289
|
</span>
|
|
219
290
|
<slot v-else name="empty">
|
|
220
|
-
<EmptyState size="sm" :description="emptyText" class="p-0" />
|
|
291
|
+
<EmptyState size="sm" :description="emptyText ?? t('ui.table.empty')" class="p-0" />
|
|
221
292
|
</slot>
|
|
222
293
|
</td>
|
|
223
294
|
</tr>
|
|
@@ -226,6 +297,19 @@ onBeforeUnmount(() => observer?.disconnect())
|
|
|
226
297
|
:key="keyOf(row, index)"
|
|
227
298
|
:class="cn('group/row', linkFor(row, index) && linkedRowClass, ui?.row)"
|
|
228
299
|
>
|
|
300
|
+
<td
|
|
301
|
+
v-if="selectable"
|
|
302
|
+
:class="cn('border-b border-black/15 align-middle', !inset && 'group-last/row:border-b-0', cellPadding, canSelect(row) && selectCell)"
|
|
303
|
+
@click="canSelect(row) && onCellPress($event, () => setRow(row, index, !isSelected(row, index)))"
|
|
304
|
+
>
|
|
305
|
+
<Checkbox
|
|
306
|
+
v-if="canSelect(row)"
|
|
307
|
+
:model-value="isSelected(row, index)"
|
|
308
|
+
:label="rowLabel ? rowLabel(row, index) : t('ui.table.selectRow', { n: index + 1 })"
|
|
309
|
+
hide-label
|
|
310
|
+
@update:model-value="setRow(row, index, $event === true)"
|
|
311
|
+
/>
|
|
312
|
+
</td>
|
|
229
313
|
<component
|
|
230
314
|
:is="column.rowHeader ? 'th' : 'td'"
|
|
231
315
|
v-for="column in columns"
|
|
@@ -233,7 +317,7 @@ onBeforeUnmount(() => observer?.disconnect())
|
|
|
233
317
|
:scope="column.rowHeader ? 'row' : undefined"
|
|
234
318
|
:class="
|
|
235
319
|
cn(
|
|
236
|
-
'border-b border-black/15 align-middle group-last/row:border-b-0',
|
|
320
|
+
'border-b border-black/15 align-middle', !inset && 'group-last/row:border-b-0',
|
|
237
321
|
column.rowHeader ? 'font-bold' : 'font-normal',
|
|
238
322
|
column.align === 'end' && 'tabular-nums',
|
|
239
323
|
cellPadding,
|
|
@@ -257,6 +341,11 @@ onBeforeUnmount(() => observer?.disconnect())
|
|
|
257
341
|
</slot>
|
|
258
342
|
</component>
|
|
259
343
|
</tr>
|
|
344
|
+
<!-- inset: the last row keeps its line, then the space below it is its
|
|
345
|
+
own part, so a hovered last row isn't taller than the others. -->
|
|
346
|
+
<tr v-if="inset && shownRows.length && !$slots.footer" aria-hidden="true">
|
|
347
|
+
<td :colspan="colspan" class="h-2 p-0 sm:h-4"></td>
|
|
348
|
+
</tr>
|
|
260
349
|
</tbody>
|
|
261
350
|
|
|
262
351
|
<!-- Footer cells get the same padding and a top border, so totals line up. -->
|
|
@@ -265,7 +354,8 @@ onBeforeUnmount(() => observer?.disconnect())
|
|
|
265
354
|
:class="
|
|
266
355
|
cn(
|
|
267
356
|
'font-bold *:*:border-t *:*:border-black/15',
|
|
268
|
-
size === 'sm' ? '*:*:px-4 *:*:py-2' : '*:*:px-4 *:*:py-3'
|
|
357
|
+
size === 'sm' ? '*:*:px-4 *:*:py-2' : '*:*:px-4 *:*:py-3',
|
|
358
|
+
inset && '*:*:sm:first:pl-6 *:*:sm:last:pr-6'
|
|
269
359
|
)
|
|
270
360
|
"
|
|
271
361
|
>
|
package/src/i18n/en.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keston UI's own words in English: what its components say and announce
|
|
3
|
+
* (button names, screen reader text, empty and error messages). Apps add
|
|
4
|
+
* these to their vue-i18n messages (see `createKestonI18n`) and keep their
|
|
5
|
+
* own under other keys.
|
|
6
|
+
*
|
|
7
|
+
* To add a language, copy this object, translate the values and give it to
|
|
8
|
+
* `createKestonI18n` under its code, e.g. `{ en, cy }`. Keep the keys and
|
|
9
|
+
* the `{placeholders}` as they are.
|
|
10
|
+
*/
|
|
11
|
+
export const en = {
|
|
12
|
+
ui: {
|
|
13
|
+
age: {
|
|
14
|
+
born: 'Born {date}',
|
|
15
|
+
birthdayToday: 'Birthday today',
|
|
16
|
+
birthdayTomorrow: 'Birthday tomorrow',
|
|
17
|
+
birthdayInDays: 'Birthday in {days} days'
|
|
18
|
+
},
|
|
19
|
+
appShell: {
|
|
20
|
+
menu: 'Menu',
|
|
21
|
+
expandSidebar: 'Expand sidebar',
|
|
22
|
+
collapseSidebar: 'Collapse sidebar'
|
|
23
|
+
},
|
|
24
|
+
avatar: {
|
|
25
|
+
account: 'Account: {name}',
|
|
26
|
+
accountWithInitials: '{initials}, account: {name}'
|
|
27
|
+
},
|
|
28
|
+
avatarGroup: {
|
|
29
|
+
people: 'no people | 1 person | {count} people',
|
|
30
|
+
more: '{count} more: {names}'
|
|
31
|
+
},
|
|
32
|
+
breadcrumb: { label: 'Breadcrumb' },
|
|
33
|
+
calendar: {
|
|
34
|
+
previousMonth: 'Previous month',
|
|
35
|
+
nextMonth: 'Next month',
|
|
36
|
+
previousYear: 'Previous year',
|
|
37
|
+
nextYear: 'Next year',
|
|
38
|
+
previousYears: 'Previous years',
|
|
39
|
+
nextYears: 'Next years',
|
|
40
|
+
chooseMonth: 'Choose month',
|
|
41
|
+
chooseYear: 'Choose year'
|
|
42
|
+
},
|
|
43
|
+
chip: { remove: 'Remove' },
|
|
44
|
+
formField: { about: 'About {label}' },
|
|
45
|
+
header: {
|
|
46
|
+
menu: 'Menu',
|
|
47
|
+
signIn: 'Sign in',
|
|
48
|
+
account: 'Account: {name}',
|
|
49
|
+
accountWithInitials: '{initials}, account: {name}'
|
|
50
|
+
},
|
|
51
|
+
input: {
|
|
52
|
+
maxLength: 'Must be {count} characters or fewer',
|
|
53
|
+
minLength: 'Must be at least {count} characters'
|
|
54
|
+
},
|
|
55
|
+
inputCode: { character: 'Character {n} of {total}' },
|
|
56
|
+
inputDate: {
|
|
57
|
+
openCalendar: 'Open calendar',
|
|
58
|
+
enterDate: 'Enter a date like {example}'
|
|
59
|
+
},
|
|
60
|
+
inputPassword: {
|
|
61
|
+
capsLock: 'Caps Lock is on',
|
|
62
|
+
show: 'Show password'
|
|
63
|
+
},
|
|
64
|
+
inputRichText: {
|
|
65
|
+
formattingFor: 'Formatting for {label}',
|
|
66
|
+
bold: 'Bold',
|
|
67
|
+
italic: 'Italic',
|
|
68
|
+
underline: 'Underline'
|
|
69
|
+
},
|
|
70
|
+
languageSwitcher: { label: 'Language' },
|
|
71
|
+
notifications: {
|
|
72
|
+
title: 'Notifications',
|
|
73
|
+
region: 'Notification',
|
|
74
|
+
hotkeyRegion: 'Notifications ({hotkey})',
|
|
75
|
+
notifications: 'notifications',
|
|
76
|
+
empty: 'You’re all caught up.',
|
|
77
|
+
withUnread: '{label}, {count} unread',
|
|
78
|
+
dismiss: 'Dismiss: {title}',
|
|
79
|
+
close: 'Close: {title}',
|
|
80
|
+
clearAll: 'Clear all',
|
|
81
|
+
more: '+{count} more'
|
|
82
|
+
},
|
|
83
|
+
pageHeader: {
|
|
84
|
+
actions: '{title} actions',
|
|
85
|
+
moreActions: 'More actions'
|
|
86
|
+
},
|
|
87
|
+
progress: { stepOf: 'Step {current} of {total}' },
|
|
88
|
+
select: {
|
|
89
|
+
choose: 'Choose…',
|
|
90
|
+
search: 'Search…',
|
|
91
|
+
noMatches: 'No matches',
|
|
92
|
+
showOptions: 'Show options',
|
|
93
|
+
remove: 'Remove {name}'
|
|
94
|
+
},
|
|
95
|
+
sidebar: {
|
|
96
|
+
main: 'Main',
|
|
97
|
+
backFrom: 'Back from {name}',
|
|
98
|
+
expand: 'Expand sidebar',
|
|
99
|
+
collapse: 'Collapse sidebar'
|
|
100
|
+
},
|
|
101
|
+
slideover: {
|
|
102
|
+
close: 'Close',
|
|
103
|
+
cancel: 'Cancel',
|
|
104
|
+
previous: 'Previous',
|
|
105
|
+
next: 'Next',
|
|
106
|
+
finish: 'Finish',
|
|
107
|
+
steps: 'Steps',
|
|
108
|
+
stepOf: 'Step {current} of {total}: {name}'
|
|
109
|
+
},
|
|
110
|
+
spinner: { loading: 'Loading' },
|
|
111
|
+
statTile: { up: 'Up', down: 'Down', flat: 'No change' },
|
|
112
|
+
steps: {
|
|
113
|
+
stepOf: 'Step {current} of {total}',
|
|
114
|
+
stepOfNamed: 'Step {current} of {total}: {name}'
|
|
115
|
+
},
|
|
116
|
+
summaryList: { notSet: 'Not set' },
|
|
117
|
+
table: {
|
|
118
|
+
table: 'Table',
|
|
119
|
+
empty: 'Nothing to show.',
|
|
120
|
+
selectAll: 'Select all rows',
|
|
121
|
+
selectRow: 'Select row {n}',
|
|
122
|
+
select: 'Select'
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export type UiMessages = typeof en
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { createKestonI18n, en } from './index'
|
|
3
|
+
|
|
4
|
+
// Tests run in Node: stand in for the browser's storage and <html>.
|
|
5
|
+
const store = new Map<string, string>()
|
|
6
|
+
const html = { lang: '' }
|
|
7
|
+
vi.stubGlobal('localStorage', {
|
|
8
|
+
getItem: (k: string) => store.get(k) ?? null,
|
|
9
|
+
setItem: (k: string, v: string) => void store.set(k, v),
|
|
10
|
+
clear: () => store.clear()
|
|
11
|
+
})
|
|
12
|
+
vi.stubGlobal('document', { documentElement: html })
|
|
13
|
+
|
|
14
|
+
describe('createKestonI18n', () => {
|
|
15
|
+
beforeEach(() => store.clear())
|
|
16
|
+
|
|
17
|
+
it('has Keston UI’s words and the app’s own', () => {
|
|
18
|
+
const i18n = createKestonI18n({ messages: { en: { users: { title: 'Users' } } } })
|
|
19
|
+
expect(i18n.global.t('ui.slideover.close')).toBe('Close')
|
|
20
|
+
expect(i18n.global.t('users.title')).toBe('Users')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('falls back to English for a language that lacks a word', () => {
|
|
24
|
+
const i18n = createKestonI18n({ locale: 'cy', messages: { cy: { users: { title: 'Defnyddwyr' } } } })
|
|
25
|
+
expect(i18n.global.t('users.title')).toBe('Defnyddwyr')
|
|
26
|
+
expect(i18n.global.t('ui.slideover.close')).toBe('Close')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('remembers the language and sets <html lang>', async () => {
|
|
30
|
+
const i18n = createKestonI18n({ messages: { cy: {} } })
|
|
31
|
+
i18n.global.locale.value = 'cy'
|
|
32
|
+
await Promise.resolve()
|
|
33
|
+
expect(localStorage.getItem('keston-locale')).toBe('cy')
|
|
34
|
+
expect(html.lang).toBe('cy')
|
|
35
|
+
expect(createKestonI18n({ messages: { cy: {} } }).global.locale.value).toBe('cy')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('ignores a saved language the app no longer has', () => {
|
|
39
|
+
localStorage.setItem('keston-locale', 'fr')
|
|
40
|
+
expect(createKestonI18n().global.locale.value).toBe('en')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('has a working message for every key', () => {
|
|
44
|
+
const i18n = createKestonI18n()
|
|
45
|
+
const leaves = (node: Record<string, unknown>, path = ''): string[] =>
|
|
46
|
+
Object.entries(node).flatMap(([k, v]) =>
|
|
47
|
+
typeof v === 'object' && v ? leaves(v as Record<string, unknown>, `${path}${k}.`) : [`${path}${k}`]
|
|
48
|
+
)
|
|
49
|
+
const keys = leaves(en)
|
|
50
|
+
expect(keys.length).toBeGreaterThan(50)
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
const text = i18n.global.t(key, { count: 2, n: 2, current: 1, total: 3, name: 'X', label: 'X', title: 'X', names: 'X', hotkey: 'X', example: 'X', initials: 'X', date: 'X', days: 2 }, 2)
|
|
53
|
+
expect(text, key).not.toBe(key)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { watch } from 'vue'
|
|
2
|
+
import { createI18n } from 'vue-i18n'
|
|
3
|
+
import { en } from './en'
|
|
4
|
+
|
|
5
|
+
export { en } from './en'
|
|
6
|
+
export type { UiMessages } from './en'
|
|
7
|
+
|
|
8
|
+
/** Words by language code, then by key (nested objects are fine). */
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
export type Messages = Record<string, Record<string, any>>
|
|
11
|
+
|
|
12
|
+
/** Keston UI's own words, by language. Today: English. */
|
|
13
|
+
export const uiMessages: Messages = { en }
|
|
14
|
+
|
|
15
|
+
export interface KestonI18nOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The app's own words, by language code: `{ en: { users: { title: 'Users' } } }`.
|
|
18
|
+
* Keston UI's words are added to each language (the app's win if a key
|
|
19
|
+
* is repeated), so only languages that Keston UI also has — or that the
|
|
20
|
+
* app lists here — are available.
|
|
21
|
+
*/
|
|
22
|
+
messages?: Messages
|
|
23
|
+
/** The language to start in. Default: the one saved last time, else the browser's, else `fallbackLocale`. */
|
|
24
|
+
locale?: string
|
|
25
|
+
/** Used for any word a language is missing. Default `en`. */
|
|
26
|
+
fallbackLocale?: string
|
|
27
|
+
/** Where the chosen language is remembered (localStorage). Default `keston-locale`. */
|
|
28
|
+
storageKey?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Sets up vue-i18n the way every Keston app should: Keston UI's words plus
|
|
33
|
+
* the app's own, English as the fallback, the language remembered between
|
|
34
|
+
* visits, and `<html lang>` kept in step. Install it with `app.use(i18n)`.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const i18n = createKestonI18n({ messages: { en } })
|
|
38
|
+
* createApp(App).use(i18n).mount('#app')
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function createKestonI18n(options: KestonI18nOptions = {}) {
|
|
42
|
+
const { messages = {}, fallbackLocale = 'en', storageKey = 'keston-locale' } = options
|
|
43
|
+
|
|
44
|
+
const codes = [...new Set([...Object.keys(uiMessages), ...Object.keys(messages)])]
|
|
45
|
+
const merged: Messages = {}
|
|
46
|
+
for (const code of codes) merged[code] = { ...uiMessages[code], ...messages[code] }
|
|
47
|
+
|
|
48
|
+
const i18n = createI18n({
|
|
49
|
+
legacy: false,
|
|
50
|
+
locale: options.locale ?? initialLocale(codes, fallbackLocale, storageKey),
|
|
51
|
+
fallbackLocale,
|
|
52
|
+
messages: merged
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// Remember the choice, and tell the browser (screen readers pick the
|
|
56
|
+
// voice by it, browsers offer translation by it).
|
|
57
|
+
watch(
|
|
58
|
+
i18n.global.locale,
|
|
59
|
+
(locale) => {
|
|
60
|
+
try {
|
|
61
|
+
localStorage.setItem(storageKey, locale)
|
|
62
|
+
} catch {
|
|
63
|
+
// Private windows can refuse storage; the choice just isn't remembered.
|
|
64
|
+
}
|
|
65
|
+
if (typeof document !== 'undefined') document.documentElement.lang = locale
|
|
66
|
+
},
|
|
67
|
+
{ immediate: true }
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return i18n
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Saved choice, else the browser's languages in order, else the fallback. */
|
|
74
|
+
function initialLocale(codes: string[], fallback: string, storageKey: string): string {
|
|
75
|
+
try {
|
|
76
|
+
const saved = localStorage.getItem(storageKey)
|
|
77
|
+
if (saved && codes.includes(saved)) return saved
|
|
78
|
+
} catch {
|
|
79
|
+
// No storage: carry on.
|
|
80
|
+
}
|
|
81
|
+
if (typeof navigator !== 'undefined') {
|
|
82
|
+
for (const wanted of navigator.languages ?? []) {
|
|
83
|
+
const exact = codes.find((c) => c.toLowerCase() === wanted.toLowerCase())
|
|
84
|
+
const base = codes.find((c) => c.toLowerCase() === wanted.split('-')[0]!.toLowerCase())
|
|
85
|
+
if (exact ?? base) return (exact ?? base)!
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return fallback
|
|
89
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,8 @@ export { default as Header } from './components/Header/Header.vue'
|
|
|
12
12
|
export type { HeaderProps, HeaderUser, HeaderUserAction } from './components/Header/Header.vue'
|
|
13
13
|
export { default as PageHeader } from './components/PageHeader/PageHeader.vue'
|
|
14
14
|
export type { PageHeaderAction, PageHeaderProps } from './components/PageHeader/PageHeader.vue'
|
|
15
|
+
export { default as SettingsRow } from './components/SettingsRow/SettingsRow.vue'
|
|
16
|
+
export type { SettingsRowProps } from './components/SettingsRow/SettingsRow.vue'
|
|
15
17
|
|
|
16
18
|
// Element
|
|
17
19
|
export { default as Avatar } from './components/Avatar/Avatar.vue'
|
|
@@ -58,6 +60,12 @@ export { default as Switch } from './components/Switch/Switch.vue'
|
|
|
58
60
|
export type { SwitchProps } from './components/Switch/Switch.vue'
|
|
59
61
|
export { default as Calendar } from './components/Calendar/Calendar.vue'
|
|
60
62
|
export type { CalendarProps } from './components/Calendar/Calendar.vue'
|
|
63
|
+
export { default as InputTextArea } from './components/InputTextArea/InputTextArea.vue'
|
|
64
|
+
export type { InputTextAreaProps } from './components/InputTextArea/InputTextArea.vue'
|
|
65
|
+
export { default as CheckboxGroup } from './components/CheckboxGroup/CheckboxGroup.vue'
|
|
66
|
+
export type { CheckboxGroupProps, CheckboxOption, CheckboxGroupValue } from './components/CheckboxGroup/CheckboxGroup.vue'
|
|
67
|
+
export { default as LanguageSwitcher } from './components/LanguageSwitcher/LanguageSwitcher.vue'
|
|
68
|
+
export type { LanguageSwitcherProps } from './components/LanguageSwitcher/LanguageSwitcher.vue'
|
|
61
69
|
|
|
62
70
|
// Data
|
|
63
71
|
export { default as Table } from './components/Table/Table.vue'
|
|
@@ -85,6 +93,8 @@ export type { SidebarProps, SidebarItem, SidebarSection } from './components/Sid
|
|
|
85
93
|
export { sidebarTrail } from './components/Sidebar/sidebar'
|
|
86
94
|
export { default as Link } from './components/Link/Link.vue'
|
|
87
95
|
export type { LinkProps } from './components/Link/Link.vue'
|
|
96
|
+
export { default as Steps } from './components/Steps/Steps.vue'
|
|
97
|
+
export type { StepsProps } from './components/Steps/Steps.vue'
|
|
88
98
|
|
|
89
99
|
// Overlay
|
|
90
100
|
export { default as Popover } from './components/Popover/Popover.vue'
|
|
@@ -115,6 +125,10 @@ export type {
|
|
|
115
125
|
ToastOptions
|
|
116
126
|
} from './components/Notification/notifications'
|
|
117
127
|
|
|
128
|
+
// Languages
|
|
129
|
+
export { createKestonI18n, uiMessages, en } from './i18n'
|
|
130
|
+
export type { KestonI18nOptions, Messages, UiMessages } from './i18n'
|
|
131
|
+
|
|
118
132
|
// Helpers
|
|
119
133
|
export { age, daysToBirthday, formatDate, formatDateTime, toDate } from './lib/dates'
|
|
120
134
|
export type { DateInput } from './lib/dates'
|