@pienter/ui 0.5.0 → 0.7.1

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.
Files changed (31) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/CONVENTIONS.md +45 -0
  3. package/README.md +30 -0
  4. package/components/display/record-details/RecordDetails.vue +61 -0
  5. package/components/display/record-details/record-details.css +37 -0
  6. package/components/display/record-details/types.ts +8 -0
  7. package/components/form/block-editor/BlockEditor.vue +454 -0
  8. package/components/form/block-editor/block-editor.css +149 -0
  9. package/components/form/block-editor/types.ts +15 -0
  10. package/components/form/combobox/Combobox.vue +21 -34
  11. package/components/form/number-field/NumberField.vue +0 -1
  12. package/components/form/record-form/RecordFields.vue +128 -0
  13. package/components/form/record-form/RecordForm.vue +116 -0
  14. package/components/form/record-form/fields.ts +20 -0
  15. package/components/form/record-form/record-form.css +15 -0
  16. package/components/form/record-form/types.ts +28 -0
  17. package/components/form/text-input/text-input.css +2 -0
  18. package/components/layout/index/Index.vue +353 -0
  19. package/components/layout/index/index.css +114 -0
  20. package/components/layout/index/useIndex.ts +390 -0
  21. package/components/layout/table/table.css +2 -1
  22. package/components/navigation/breadcrumb/Breadcrumb.vue +24 -5
  23. package/components/navigation/breadcrumb/breadcrumb.css +15 -0
  24. package/components/navigation/sidebar/Sidebar.vue +11 -8
  25. package/components/navigation/tabs/Tabs.vue +6 -0
  26. package/composables/useMenu.ts +20 -27
  27. package/package.json +12 -2
  28. package/styles/0-settings/colors.css +10 -0
  29. package/utils/a11y/focus.ts +9 -3
  30. package/utils/cms/index.ts +283 -0
  31. package/utils/cms/schema.json +126 -0
@@ -0,0 +1,114 @@
1
+ @layer components {
2
+ .pui-index {
3
+ display: grid;
4
+ min-inline-size: 0;
5
+ gap: var(--space-s);
6
+ color: var(--text-clr-base);
7
+ }
8
+
9
+ .pui-index__header {
10
+ display: grid;
11
+ grid-template-columns: minmax(0, 1fr) auto;
12
+ align-items: center;
13
+ justify-content: space-between;
14
+ gap: var(--space-s);
15
+ padding-block-end: var(--space-s);
16
+ border-block-end: var(--stroke-sm) solid var(--border-clr-subtle);
17
+ }
18
+
19
+ .pui-index__heading {
20
+ display: grid;
21
+ gap: var(--space-3xs);
22
+ min-inline-size: 0;
23
+ }
24
+
25
+ .pui-index__title {
26
+ margin: 0;
27
+ font-family: var(--ff-display);
28
+ font-size: var(--step-2);
29
+ font-weight: var(--fw-display);
30
+ letter-spacing: var(--ls-display);
31
+ }
32
+
33
+ .pui-index__description {
34
+ margin: 0;
35
+ font-size: var(--step--1);
36
+ color: var(--text-clr-muted);
37
+ }
38
+
39
+ .pui-index__actions,
40
+ .pui-index__selection {
41
+ display: flex;
42
+ flex-wrap: wrap;
43
+ align-items: center;
44
+ gap: var(--space-xs);
45
+ }
46
+
47
+ .pui-index__body {
48
+ min-inline-size: 0;
49
+ background: var(--bg-clr-surface);
50
+ border: var(--stroke-sm) solid var(--border-clr-subtle);
51
+ border-radius: var(--radius-md);
52
+ }
53
+
54
+ .pui-index__selection,
55
+ .pui-index__error {
56
+ padding: var(--space-s);
57
+ }
58
+
59
+ .pui-index__toolbar {
60
+ display: flex;
61
+ flex-wrap: wrap;
62
+ align-items: end;
63
+ gap: var(--space-s);
64
+ padding: var(--space-s);
65
+ border-block-end: var(--stroke-sm) solid var(--border-clr-subtle);
66
+ }
67
+
68
+ .pui-index__search {
69
+ flex: 0 1 calc(var(--space-sidebar-width) * 1.5);
70
+ min-inline-size: min(100%, var(--space-sidebar-width));
71
+
72
+ @media (max-width: 48em) {
73
+ flex-grow: 1;
74
+ }
75
+ }
76
+
77
+ .pui-index__sort {
78
+ flex: 0 1 auto;
79
+ min-inline-size: 0;
80
+ max-inline-size: 100%;
81
+ }
82
+
83
+ .pui-index__results {
84
+ min-inline-size: 0;
85
+ }
86
+
87
+ .pui-index__table {
88
+ --pui-table-cell-padding-block: var(--space-2xs);
89
+ border: 0;
90
+ border-radius: var(--radius-md) var(--radius-md) 0 0;
91
+ }
92
+
93
+ .pui-index__page-size > .pui-field {
94
+ display: flex;
95
+ align-items: center;
96
+ gap: var(--space-xs);
97
+
98
+ & > .pui-field__label {
99
+ white-space: nowrap;
100
+ }
101
+ }
102
+
103
+ .pui-index__pagination {
104
+ display: flex;
105
+ flex-wrap: wrap;
106
+ align-items: center;
107
+ justify-content: end;
108
+ gap: var(--space-s);
109
+
110
+ & > .pui-pagination-nav {
111
+ flex: 1;
112
+ }
113
+ }
114
+ }
@@ -0,0 +1,390 @@
1
+ import { computed, inject, onScopeDispose, ref, shallowRef, watch } from 'vue';
2
+ import {
3
+ routeLocationKey,
4
+ routerKey,
5
+ isNavigationFailure,
6
+ NavigationFailureType,
7
+ type LocationQuery,
8
+ } from 'vue-router';
9
+ import {
10
+ cloneRecord,
11
+ decodeListQuery,
12
+ encodeListQuery,
13
+ normalizeListQuery,
14
+ type ApiError,
15
+ type ListLoader,
16
+ type ListQuery,
17
+ type QueryOptions,
18
+ } from '../../../utils/cms/index.js';
19
+
20
+ interface Options<T> {
21
+ load: ListLoader<T>;
22
+ queryOptions: QueryOptions;
23
+ syncQuery: boolean;
24
+ searchDebounce: number;
25
+ }
26
+
27
+ function isListParameter(key: string): boolean {
28
+ return (
29
+ key === 'filter' ||
30
+ key.startsWith('filter[') ||
31
+ ['page', 'page_size', 'search', 'sort'].some(
32
+ (name) => key === name || key.startsWith(`${name}[`),
33
+ )
34
+ );
35
+ }
36
+
37
+ function routeParams(values: LocationQuery): URLSearchParams {
38
+ const params = new URLSearchParams();
39
+ for (const [key, valuesForKey] of Object.entries(values)) {
40
+ if (!isListParameter(key)) continue;
41
+ for (const value of Array.isArray(valuesForKey)
42
+ ? valuesForKey
43
+ : [valuesForKey])
44
+ params.append(key, value ?? '');
45
+ }
46
+ params.sort();
47
+ return params;
48
+ }
49
+
50
+ function apiError(cause: unknown): ApiError {
51
+ const candidate =
52
+ cause && typeof cause === 'object' && 'error' in cause
53
+ ? cause.error
54
+ : cause;
55
+ if (
56
+ candidate &&
57
+ typeof candidate === 'object' &&
58
+ 'code' in candidate &&
59
+ typeof candidate.code === 'string' &&
60
+ 'message' in candidate &&
61
+ typeof candidate.message === 'string'
62
+ )
63
+ return candidate as ApiError;
64
+ return {
65
+ code: 'load_failed',
66
+ message:
67
+ cause instanceof Error
68
+ ? cause.message
69
+ : 'The list could not be loaded.',
70
+ };
71
+ }
72
+
73
+ interface Work {
74
+ promise: Promise<void>;
75
+ finish: () => void;
76
+ }
77
+
78
+ export function useIndex<T>(props: Options<T>) {
79
+ const route = inject(routeLocationKey, undefined);
80
+ const router = inject(routerKey, undefined);
81
+ const defaults = () =>
82
+ normalizeListQuery(
83
+ {
84
+ page: 1,
85
+ page_size: props.queryOptions.defaultPageSize ?? 20,
86
+ search: '',
87
+ sort: null,
88
+ filters: {},
89
+ },
90
+ props.queryOptions,
91
+ );
92
+ const query = shallowRef<ListQuery>(defaults());
93
+ const search = ref(query.value.search);
94
+ const rows = shallowRef<T[]>([]);
95
+ const meta = shallowRef({
96
+ page: query.value.page,
97
+ page_size: query.value.page_size,
98
+ total: 0,
99
+ });
100
+ const loading = ref(false);
101
+ const error = shallowRef<ApiError | null>(null);
102
+ const errors = computed(() => (error.value ? [error.value.message] : []));
103
+ const invalidQuery = ref(false);
104
+ let request = 0;
105
+ let navigation = 0;
106
+ let controller: AbortController | undefined;
107
+ let searchTimer: ReturnType<typeof setTimeout> | undefined;
108
+ let disposed = false;
109
+ let activeWork: Work | undefined;
110
+ let pendingUrl: { path: string; query: string; work: Work } | undefined;
111
+
112
+ function startWork(): Work {
113
+ activeWork?.finish();
114
+ cancelRequest();
115
+ let finish!: () => void;
116
+ const promise = new Promise<void>((resolve) => {
117
+ finish = resolve;
118
+ });
119
+ activeWork = { promise, finish };
120
+ return activeWork;
121
+ }
122
+
123
+ function fail(cause: unknown) {
124
+ error.value = apiError(cause);
125
+ invalidQuery.value = error.value.code === 'invalid_query';
126
+ }
127
+
128
+ function cancelSearch() {
129
+ clearTimeout(searchTimer);
130
+ searchTimer = undefined;
131
+ }
132
+
133
+ function cancelRequest() {
134
+ request++;
135
+ controller?.abort();
136
+ loading.value = false;
137
+ }
138
+
139
+ function fetchRows(work = startWork()): Promise<void> {
140
+ cancelRequest();
141
+ if (disposed || invalidQuery.value) {
142
+ work.finish();
143
+ return work.promise;
144
+ }
145
+ const current = request;
146
+ controller = new AbortController();
147
+ const { signal } = controller;
148
+ loading.value = true;
149
+ error.value = null;
150
+ void (async () => {
151
+ try {
152
+ const response = await props.load(cloneRecord(query.value), {
153
+ signal,
154
+ });
155
+ if (current !== request) return;
156
+ rows.value = response.data;
157
+ meta.value = response.meta;
158
+ } catch (cause) {
159
+ if (current === request) fail(cause);
160
+ } finally {
161
+ if (current === request) loading.value = false;
162
+ work.finish();
163
+ }
164
+ })();
165
+ return work.promise;
166
+ }
167
+
168
+ function accept(next: ListQuery, work: Work) {
169
+ query.value = next;
170
+ search.value = next.search;
171
+ invalidQuery.value = false;
172
+ return fetchRows(work);
173
+ }
174
+
175
+ const urlQuery = computed(() =>
176
+ props.syncQuery && route ? routeParams(route.query).toString() : '',
177
+ );
178
+ watch(
179
+ [
180
+ urlQuery,
181
+ () => (props.syncQuery ? route?.path : ''),
182
+ () => props.syncQuery,
183
+ () => props.queryOptions,
184
+ () => props.load,
185
+ ],
186
+ () => {
187
+ const matchesPending =
188
+ pendingUrl?.path === route?.path &&
189
+ pendingUrl?.query === urlQuery.value;
190
+ const preserveSearch = searchTimer !== undefined && matchesPending;
191
+ const work = preserveSearch
192
+ ? activeWork!
193
+ : matchesPending
194
+ ? pendingUrl!.work
195
+ : startWork();
196
+ pendingUrl = undefined;
197
+ navigation++;
198
+ if (!preserveSearch) cancelSearch();
199
+ cancelRequest();
200
+ if (props.syncQuery && (!route || !router))
201
+ throw new Error(
202
+ 'Index sync-query requires an installed Vue Router.',
203
+ );
204
+ try {
205
+ const next = props.syncQuery
206
+ ? decodeListQuery(
207
+ new URLSearchParams(urlQuery.value),
208
+ props.queryOptions,
209
+ )
210
+ : normalizeListQuery(query.value, props.queryOptions);
211
+ if (preserveSearch) {
212
+ query.value = next;
213
+ invalidQuery.value = false;
214
+ loading.value = true;
215
+ } else void accept(next, work);
216
+ } catch (cause) {
217
+ fail(cause);
218
+ work.finish();
219
+ }
220
+ },
221
+ { immediate: true, deep: true },
222
+ );
223
+
224
+ function commit(next: ListQuery, replace = false): Promise<void> {
225
+ if (disposed) return Promise.resolve();
226
+ cancelSearch();
227
+ const work = startWork();
228
+ let normalized: ListQuery;
229
+ try {
230
+ normalized = normalizeListQuery(next, props.queryOptions);
231
+ } catch (cause) {
232
+ fail(cause);
233
+ work.finish();
234
+ return work.promise;
235
+ }
236
+ if (!props.syncQuery || !route || !router)
237
+ return accept(normalized, work);
238
+ const values = { ...route.query };
239
+ for (const key of Object.keys(values))
240
+ if (isListParameter(key)) delete values[key];
241
+ for (const [key, value] of encodeListQuery(normalized))
242
+ values[key] = value;
243
+ query.value = normalized;
244
+ search.value = normalized.search;
245
+ const attempt = ++navigation;
246
+ const path = route.path;
247
+ pendingUrl = { path, query: routeParams(values).toString(), work };
248
+ loading.value = true;
249
+ void (async () => {
250
+ try {
251
+ const failure = await router[replace ? 'replace' : 'push']({
252
+ query: values,
253
+ hash: route.hash,
254
+ });
255
+ if (disposed || attempt !== navigation || route.path !== path)
256
+ return;
257
+ if (
258
+ isNavigationFailure(
259
+ failure,
260
+ NavigationFailureType.duplicated,
261
+ )
262
+ ) {
263
+ pendingUrl = undefined;
264
+ if (searchTimer !== undefined) {
265
+ query.value = normalized;
266
+ invalidQuery.value = false;
267
+ } else void accept(normalized, work);
268
+ } else if (failure)
269
+ failNavigation(
270
+ new Error('The list URL could not be updated.'),
271
+ work,
272
+ );
273
+ } catch (cause) {
274
+ if (disposed || attempt !== navigation || route.path !== path)
275
+ return;
276
+ failNavigation(cause, work);
277
+ }
278
+ })();
279
+ return work.promise;
280
+ }
281
+
282
+ function failNavigation(cause: unknown, work: Work) {
283
+ pendingUrl = undefined;
284
+ const preserveSearch = searchTimer !== undefined;
285
+ cancelRequest();
286
+ try {
287
+ query.value = decodeListQuery(
288
+ routeParams(route!.query),
289
+ props.queryOptions,
290
+ );
291
+ if (!preserveSearch) search.value = query.value.search;
292
+ invalidQuery.value = false;
293
+ } catch {
294
+ invalidQuery.value = true;
295
+ }
296
+ error.value = apiError(cause);
297
+ loading.value = preserveSearch;
298
+ work.finish();
299
+ }
300
+
301
+ function updateCriteria(patch: Partial<Omit<ListQuery, 'page'>>) {
302
+ void commit({
303
+ ...query.value,
304
+ search: search.value,
305
+ ...patch,
306
+ page: 1,
307
+ });
308
+ }
309
+
310
+ function setSearch(value: string) {
311
+ if (disposed) return;
312
+ cancelSearch();
313
+ startWork();
314
+ search.value = value;
315
+ loading.value = true;
316
+ searchTimer = setTimeout(
317
+ () => {
318
+ void commit({ ...query.value, search: value, page: 1 }, true);
319
+ },
320
+ Math.max(0, props.searchDebounce),
321
+ );
322
+ }
323
+
324
+ function reload(): Promise<void> {
325
+ cancelSearch();
326
+ if (pendingUrl || search.value !== query.value.search)
327
+ return commit(
328
+ {
329
+ ...query.value,
330
+ search: search.value,
331
+ page:
332
+ search.value !== query.value.search
333
+ ? 1
334
+ : query.value.page,
335
+ },
336
+ true,
337
+ );
338
+ return fetchRows();
339
+ }
340
+
341
+ function reset(): Promise<void> {
342
+ return commit(defaults(), true);
343
+ }
344
+ function setPage(page: number) {
345
+ void commit({
346
+ ...query.value,
347
+ search: search.value,
348
+ page: search.value !== query.value.search ? 1 : page,
349
+ });
350
+ }
351
+ function setFilters(filters: ListQuery['filters']) {
352
+ updateCriteria({ filters: { ...filters } });
353
+ }
354
+
355
+ function setFilter(name: string, value: string | undefined) {
356
+ if (value !== undefined) {
357
+ setFilters({ ...query.value.filters, [name]: value });
358
+ return;
359
+ }
360
+ const filters = { ...query.value.filters };
361
+ delete filters[name];
362
+ setFilters(filters);
363
+ }
364
+
365
+ onScopeDispose(() => {
366
+ disposed = true;
367
+ navigation++;
368
+ cancelSearch();
369
+ cancelRequest();
370
+ activeWork?.finish();
371
+ });
372
+
373
+ return {
374
+ query,
375
+ search,
376
+ rows,
377
+ meta,
378
+ loading,
379
+ error,
380
+ errors,
381
+ invalidQuery,
382
+ updateCriteria,
383
+ setSearch,
384
+ setPage,
385
+ setFilters,
386
+ setFilter,
387
+ reload,
388
+ reset,
389
+ };
390
+ }
@@ -66,7 +66,8 @@
66
66
  align-items: center;
67
67
  gap: var(--space-2xs);
68
68
  min-width: 0;
69
- padding: var(--space-xs) var(--space-s);
69
+ padding-block: var(--pui-table-cell-padding-block, var(--space-xs));
70
+ padding-inline: var(--space-s);
70
71
  text-align: left;
71
72
 
72
73
  &[data-head] {
@@ -1,6 +1,6 @@
1
1
  <template>
2
- <nav aria-label="Breadcrumb">
3
- <ol class="pui-breadcrumb">
2
+ <nav aria-label="Breadcrumb" class="pui-breadcrumb">
3
+ <ol class="pui-breadcrumb__trail">
4
4
  <template v-for="(item, index) in items" :key="index">
5
5
  <li
6
6
  v-if="index > 0"
@@ -13,15 +13,25 @@
13
13
  v-if="index < items.length - 1"
14
14
  class="pui-breadcrumb__item"
15
15
  >
16
- <a :href="item.href" class="pui-breadcrumb__link">{{
17
- item.label
18
- }}</a>
16
+ <slot
17
+ name="link"
18
+ :item="item"
19
+ :index="index"
20
+ link-class="pui-breadcrumb__link"
21
+ >
22
+ <a :href="item.href" class="pui-breadcrumb__link">{{
23
+ item.label
24
+ }}</a>
25
+ </slot>
19
26
  </li>
20
27
  <li v-else class="pui-breadcrumb__item" aria-current="page">
21
28
  {{ item.label }}
22
29
  </li>
23
30
  </template>
24
31
  </ol>
32
+ <div v-if="$slots.actions" class="pui-breadcrumb__actions">
33
+ <slot name="actions" />
34
+ </div>
25
35
  </nav>
26
36
  </template>
27
37
 
@@ -29,6 +39,15 @@
29
39
  defineProps<{
30
40
  items: { label: string; href?: string }[];
31
41
  }>();
42
+
43
+ defineSlots<{
44
+ link?: (scope: {
45
+ item: { label: string; href?: string };
46
+ index: number;
47
+ linkClass: string;
48
+ }) => unknown;
49
+ actions?: () => unknown;
50
+ }>();
32
51
  </script>
33
52
 
34
53
  <style>
@@ -1,5 +1,12 @@
1
1
  @layer components {
2
2
  .pui-breadcrumb {
3
+ display: flex;
4
+ align-items: center;
5
+ justify-content: space-between;
6
+ gap: var(--space-sm);
7
+ }
8
+
9
+ .pui-breadcrumb__trail {
3
10
  display: flex;
4
11
  align-items: center;
5
12
  gap: var(--space-2xs);
@@ -9,6 +16,14 @@
9
16
  padding: 0;
10
17
  margin: 0;
11
18
  flex-wrap: wrap;
19
+ min-inline-size: 0;
20
+ }
21
+
22
+ .pui-breadcrumb__actions {
23
+ display: inline-flex;
24
+ align-items: center;
25
+ gap: var(--space-2xs);
26
+ flex: none;
12
27
  }
13
28
 
14
29
  .pui-breadcrumb__item {
@@ -323,6 +323,17 @@ function onEntryKeydown(event: KeyboardEvent): void {
323
323
  const item = event.currentTarget;
324
324
  if (!(item instanceof HTMLElement)) return;
325
325
 
326
+ if (event.key === ' ' || event.key === 'Spacebar') {
327
+ // `<a>` doesn't activate on Space natively.
328
+ if (item.matches('[data-sidebar-link]')) {
329
+ event.preventDefault();
330
+ item.click();
331
+ }
332
+ return;
333
+ }
334
+
335
+ if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
336
+
326
337
  const entries = focusableEntries();
327
338
  const idx = entries.indexOf(item);
328
339
 
@@ -343,14 +354,6 @@ function onEntryKeydown(event: KeyboardEvent): void {
343
354
  event.preventDefault();
344
355
  entries[entries.length - 1]?.focus();
345
356
  return;
346
- case ' ':
347
- case 'Spacebar':
348
- // `<a>` doesn't activate on Space natively.
349
- if (item.matches('[data-sidebar-link]')) {
350
- event.preventDefault();
351
- item.click();
352
- }
353
- return;
354
357
  default:
355
358
  return;
356
359
  }
@@ -2,10 +2,12 @@
2
2
  <div class="pui-tabs" role="tablist">
3
3
  <button
4
4
  v-for="tab in tabs"
5
+ :id="tab.id"
5
6
  :key="tab.key"
6
7
  type="button"
7
8
  class="pui-tabs__item"
8
9
  role="tab"
10
+ :aria-controls="tab.controls"
9
11
  :aria-selected="modelValue === tab.key ? 'true' : 'false'"
10
12
  :tabindex="modelValue === tab.key ? 0 : -1"
11
13
  :disabled="tab.disabled || undefined"
@@ -22,6 +24,8 @@ interface TabItem {
22
24
  key: string;
23
25
  label: string;
24
26
  disabled?: boolean;
27
+ id?: string;
28
+ controls?: string;
25
29
  }
26
30
 
27
31
  const props = withDefaults(
@@ -39,6 +43,8 @@ const emit = defineEmits<{
39
43
  }>();
40
44
 
41
45
  function handleKeydown(event: KeyboardEvent): void {
46
+ if (!['ArrowRight', 'ArrowLeft', 'Home', 'End'].includes(event.key)) return;
47
+
42
48
  const target = event.currentTarget as HTMLElement;
43
49
  const items = Array.from(
44
50
  target.parentElement!.querySelectorAll<HTMLElement>(