@mszczygiel-projects/ui-core-foundations 0.0.0-stage → 1.0.0-alpha.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/README.md +14 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +10 -0
- package/dist/config.d.ts +200 -0
- package/dist/config.js +255 -0
- package/dist/generated/tokens.d.ts +2331 -0
- package/dist/generated/tokens.js +2328 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +1 -0
- package/dist/pipeline/build.d.ts +32 -0
- package/dist/pipeline/build.js +104 -0
- package/dist/pipeline/emit-css.d.ts +55 -0
- package/dist/pipeline/emit-css.js +473 -0
- package/dist/pipeline/emit-manifest.d.ts +75 -0
- package/dist/pipeline/emit-manifest.js +279 -0
- package/dist/pipeline/emit-tailwind.d.ts +11 -0
- package/dist/pipeline/emit-tailwind.js +121 -0
- package/dist/pipeline/emit-ts.d.ts +13 -0
- package/dist/pipeline/emit-ts.js +73 -0
- package/dist/pipeline/emit-typography.d.ts +40 -0
- package/dist/pipeline/emit-typography.js +115 -0
- package/dist/pipeline/figma.d.ts +53 -0
- package/dist/pipeline/figma.js +231 -0
- package/dist/pipeline/model.d.ts +42 -0
- package/dist/pipeline/model.js +42 -0
- package/dist/pipeline/modes.d.ts +27 -0
- package/dist/pipeline/modes.js +26 -0
- package/dist/pipeline/names.d.ts +36 -0
- package/dist/pipeline/names.js +87 -0
- package/dist/pipeline/parse.d.ts +29 -0
- package/dist/pipeline/parse.js +214 -0
- package/dist/pipeline/resolve.d.ts +44 -0
- package/dist/pipeline/resolve.js +178 -0
- package/dist/pipeline/snapshot.d.ts +15 -0
- package/dist/pipeline/snapshot.js +38 -0
- package/dist/pipeline/validate.d.ts +80 -0
- package/dist/pipeline/validate.js +317 -0
- package/dist/pipeline/values.d.ts +13 -0
- package/dist/pipeline/values.js +81 -0
- package/dist/run-cli.d.ts +8 -0
- package/dist/run-cli.js +93 -0
- package/dist/tailwind.css +868 -0
- package/dist/tokens.css +4211 -0
- package/dist/tokens.json +49474 -0
- package/dist/tokens.json.d.ts +6 -0
- package/dist/typography.css +155 -0
- package/package.json +122 -3
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mszczygiel-projects/ui-core-foundations
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The design tokens of UI Core Library, built from the variables of its Figma Foundations file:
|
|
4
|
+
|
|
5
|
+
| Export | What it is |
|
|
6
|
+
| ---------------- | ------------------------- |
|
|
7
|
+
| `tokens.css` | CSS custom properties |
|
|
8
|
+
| `tailwind.css` | a Tailwind 4 theme |
|
|
9
|
+
| `typography.css` | text style classes |
|
|
10
|
+
| `tokens.json` | a manifest of every token |
|
|
11
|
+
| `.` | typed references |
|
|
12
|
+
|
|
13
|
+
The package also ships the `ui-core-foundations` CLI, which builds the same files from your own Figma export.
|
|
14
|
+
|
|
15
|
+
The documentation site shows how to use it: [Getting started](https://mszczygiel-projects.github.io/ui-core-library/?path=/docs/guides-getting-started--docs), [Configuration](https://mszczygiel-projects.github.io/ui-core-library/?path=/docs/guides-configuration--docs), [Custom tokens and icons](https://mszczygiel-projects.github.io/ui-core-library/?path=/docs/guides-custom-tokens-and-icons--docs) and [Foundations](https://mszczygiel-projects.github.io/ui-core-library/?path=/docs/foundations-overview--docs).
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The configuration that every component on a page reads: the texts the components render on
|
|
3
|
+
* their own and the look of their loaders. The web component and React packages, and two copies
|
|
4
|
+
* of one package, share it, so its state lives under a global symbol. That state holds only what
|
|
5
|
+
* the application set; each copy merges it over its own defaults, so a newer copy still finds
|
|
6
|
+
* every label its components need.
|
|
7
|
+
*/
|
|
8
|
+
/** The look of every loader: the Loader component and the loading state of other components. */
|
|
9
|
+
export type LoaderVariant = 'spinner';
|
|
10
|
+
/** The looks of loaders that this version of the library draws. */
|
|
11
|
+
export declare const loaderVariants: readonly LoaderVariant[];
|
|
12
|
+
/** Data for a whole validation sentence; each field specifies its own reason vocabulary. */
|
|
13
|
+
export interface FieldValidationContext {
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
readonly locale: string;
|
|
16
|
+
readonly min?: number | string;
|
|
17
|
+
readonly max?: number | string;
|
|
18
|
+
readonly step?: number;
|
|
19
|
+
readonly precision?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Every text that a component renders on its own, grouped by component. A label that holds a
|
|
23
|
+
* value is a function of that value, so that a whole sentence is translated at once.
|
|
24
|
+
*/
|
|
25
|
+
export interface UiCoreLabels {
|
|
26
|
+
readonly dateTimeField: {
|
|
27
|
+
/** The name of the button and dialog of the combined picker. */
|
|
28
|
+
readonly openPicker: string;
|
|
29
|
+
/** The visible name of the time control. */
|
|
30
|
+
readonly timeLabel: string;
|
|
31
|
+
/** The name of the button that closes the date-time picker. */
|
|
32
|
+
readonly done: string;
|
|
33
|
+
/** A complete sentence describing the failed constraint. */
|
|
34
|
+
readonly validationMessage: (context: FieldValidationContext) => string;
|
|
35
|
+
};
|
|
36
|
+
readonly timeField: {
|
|
37
|
+
/** A complete sentence for inconsistent time limits. */
|
|
38
|
+
readonly inconsistentLimits: string;
|
|
39
|
+
};
|
|
40
|
+
readonly numberField: {
|
|
41
|
+
/** The name of the decrement button. */
|
|
42
|
+
readonly decrement: string;
|
|
43
|
+
/** The name of the increment button. */
|
|
44
|
+
readonly increment: string;
|
|
45
|
+
/** A complete sentence describing the failed constraint. */
|
|
46
|
+
readonly validationMessage: (context: FieldValidationContext) => string;
|
|
47
|
+
};
|
|
48
|
+
readonly dateField: {
|
|
49
|
+
/** The name of the button that opens the calendar of a date field. */
|
|
50
|
+
readonly openCalendar: string;
|
|
51
|
+
/** A complete sentence describing the expected display format. */
|
|
52
|
+
readonly formatHint: (example: string) => string;
|
|
53
|
+
/** A complete sentence describing the failed constraint. */
|
|
54
|
+
readonly validationMessage: (context: FieldValidationContext) => string;
|
|
55
|
+
};
|
|
56
|
+
readonly breadcrumbs: {
|
|
57
|
+
/** The accessible name of the navigation of a trail. */
|
|
58
|
+
readonly label: string;
|
|
59
|
+
};
|
|
60
|
+
readonly button: {
|
|
61
|
+
/** The description of a button while its action is in progress. */
|
|
62
|
+
readonly loading: string;
|
|
63
|
+
};
|
|
64
|
+
readonly calendar: {
|
|
65
|
+
/** The name of the button that shows the previous month. */
|
|
66
|
+
readonly previousMonth: string;
|
|
67
|
+
/** The name of the button that shows the next month. */
|
|
68
|
+
readonly nextMonth: string;
|
|
69
|
+
/** The name of the button that shows the previous year in the month grid. */
|
|
70
|
+
readonly previousYear: string;
|
|
71
|
+
/** The name of the button that shows the next year in the month grid. */
|
|
72
|
+
readonly nextYear: string;
|
|
73
|
+
/** The name of the button that shows the previous page of years. */
|
|
74
|
+
readonly previousYears: string;
|
|
75
|
+
/** The name of the button that shows the next page of years. */
|
|
76
|
+
readonly nextYears: string;
|
|
77
|
+
/** The name of the heading button that opens the month grid, with the month it shows. */
|
|
78
|
+
readonly chooseMonth: (monthAndYear: string) => string;
|
|
79
|
+
/** The name of the heading button that opens the year grid, with the year it shows. */
|
|
80
|
+
readonly chooseYear: (year: string) => string;
|
|
81
|
+
};
|
|
82
|
+
readonly chip: {
|
|
83
|
+
/** The word that names the dismiss button of a chip, read before the text of the chip. */
|
|
84
|
+
readonly dismiss: string;
|
|
85
|
+
};
|
|
86
|
+
readonly combobox: {
|
|
87
|
+
/** The name of the button that clears the chosen options and the query. */
|
|
88
|
+
readonly clear: string;
|
|
89
|
+
/** The text of the option that creates one from the query, as a whole phrase. */
|
|
90
|
+
readonly create: (query: string) => string;
|
|
91
|
+
};
|
|
92
|
+
readonly dialog: {
|
|
93
|
+
/** The name of the button that closes a dialog. */
|
|
94
|
+
readonly close: string;
|
|
95
|
+
};
|
|
96
|
+
readonly drawer: {
|
|
97
|
+
/** The name of the button that closes a drawer. */
|
|
98
|
+
readonly close: string;
|
|
99
|
+
};
|
|
100
|
+
readonly fileInputField: {
|
|
101
|
+
/** The text of the zone that takes files from a dialog or a drop. */
|
|
102
|
+
readonly prompt: string;
|
|
103
|
+
/** The text of the button that replaces a previewed image. */
|
|
104
|
+
readonly replace: string;
|
|
105
|
+
/** The name of the button that removes a file, as a whole phrase. */
|
|
106
|
+
readonly remove: (fileName: string) => string;
|
|
107
|
+
};
|
|
108
|
+
readonly iconButton: {
|
|
109
|
+
/** The description of an icon button while its action is in progress. */
|
|
110
|
+
readonly loading: string;
|
|
111
|
+
};
|
|
112
|
+
readonly listbox: {
|
|
113
|
+
/** The message of a list without options. */
|
|
114
|
+
readonly empty: string;
|
|
115
|
+
/** The message of a list while its options load. */
|
|
116
|
+
readonly loading: string;
|
|
117
|
+
};
|
|
118
|
+
readonly loader: {
|
|
119
|
+
/** The accessible name of a loader that has no `label` of its own. */
|
|
120
|
+
readonly label: string;
|
|
121
|
+
};
|
|
122
|
+
readonly notification: {
|
|
123
|
+
/** The word that names the dismiss button of a notification, read before its heading. */
|
|
124
|
+
readonly dismiss: string;
|
|
125
|
+
};
|
|
126
|
+
readonly pagination: {
|
|
127
|
+
/** The accessible name of the navigation between pages. */
|
|
128
|
+
readonly label: string;
|
|
129
|
+
/** The name of the button that moves to the previous page. */
|
|
130
|
+
readonly previous: string;
|
|
131
|
+
/** The name of the button that moves to the next page. */
|
|
132
|
+
readonly next: string;
|
|
133
|
+
/** The name of the button of a page, as a whole phrase. */
|
|
134
|
+
readonly page: (page: number) => string;
|
|
135
|
+
/** The status text, as a whole sentence. */
|
|
136
|
+
readonly status: (page: number, pageCount: number) => string;
|
|
137
|
+
/** The label of the field that moves to a typed page. */
|
|
138
|
+
readonly jump: string;
|
|
139
|
+
/** The text after the jump field in a narrow container. */
|
|
140
|
+
readonly total: (pageCount: number) => string;
|
|
141
|
+
};
|
|
142
|
+
readonly passwordField: {
|
|
143
|
+
/** The name of the button that shows the password as plain text, whatever its state. */
|
|
144
|
+
readonly reveal: string;
|
|
145
|
+
};
|
|
146
|
+
readonly searchField: {
|
|
147
|
+
/** The name of the button that clears the query. */
|
|
148
|
+
readonly clear: string;
|
|
149
|
+
};
|
|
150
|
+
readonly selectField: {
|
|
151
|
+
/** The name of the button that clears the chosen option. */
|
|
152
|
+
readonly clear: string;
|
|
153
|
+
};
|
|
154
|
+
readonly splitButton: {
|
|
155
|
+
/** The name of the button that opens the menu of a split button, and of the menu. */
|
|
156
|
+
readonly menu: string;
|
|
157
|
+
/** The description of a split button while its main action is in progress. */
|
|
158
|
+
readonly loading: string;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** The configuration as components read it: always complete. */
|
|
162
|
+
export interface UiCoreConfig {
|
|
163
|
+
/** The preferred locale; a field's locale prop takes precedence. */
|
|
164
|
+
readonly locale?: string;
|
|
165
|
+
readonly labels: UiCoreLabels;
|
|
166
|
+
readonly loaderVariant: LoaderVariant;
|
|
167
|
+
}
|
|
168
|
+
/** What {@link configureUiCore} takes: any group of labels and any label in it, alone. */
|
|
169
|
+
export interface UiCoreConfigInput {
|
|
170
|
+
/** BCP 47 locale used before the browser language. Reset clears it. */
|
|
171
|
+
readonly locale?: string;
|
|
172
|
+
readonly labels?: {
|
|
173
|
+
readonly [Group in keyof UiCoreLabels]?: Partial<UiCoreLabels[Group]>;
|
|
174
|
+
};
|
|
175
|
+
readonly loaderVariant?: LoaderVariant;
|
|
176
|
+
}
|
|
177
|
+
/** The English labels that the packages ship. */
|
|
178
|
+
export declare const defaultLabels: UiCoreLabels;
|
|
179
|
+
/**
|
|
180
|
+
* Returns the configuration. The same object comes back until the configuration changes, so it
|
|
181
|
+
* can serve as the snapshot of `useSyncExternalStore`.
|
|
182
|
+
*/
|
|
183
|
+
export declare function getUiCoreConfig(): UiCoreConfig;
|
|
184
|
+
/**
|
|
185
|
+
* Sets labels, locale and the look of loaders for every component on the page, those already rendered
|
|
186
|
+
* included. Labels merge one by one over the earlier configuration, so setting one label keeps
|
|
187
|
+
* the others.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* configureUiCore({ labels: { loader: { label: 'Ładowanie' } } });
|
|
191
|
+
*/
|
|
192
|
+
export declare function configureUiCore(input: UiCoreConfigInput): void;
|
|
193
|
+
/** Restores English labels, the default loader and automatic locale selection. */
|
|
194
|
+
export declare function resetUiCoreConfig(): void;
|
|
195
|
+
/**
|
|
196
|
+
* Calls `listener` after every change of the configuration, and returns the function that stops
|
|
197
|
+
* it. Components subscribe through it, the web components through their state and React with
|
|
198
|
+
* `useSyncExternalStore`.
|
|
199
|
+
*/
|
|
200
|
+
export declare function subscribeUiCoreConfig(listener: () => void): () => void;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The configuration that every component on a page reads: the texts the components render on
|
|
3
|
+
* their own and the look of their loaders. The web component and React packages, and two copies
|
|
4
|
+
* of one package, share it, so its state lives under a global symbol. That state holds only what
|
|
5
|
+
* the application set; each copy merges it over its own defaults, so a newer copy still finds
|
|
6
|
+
* every label its components need.
|
|
7
|
+
*/
|
|
8
|
+
/** The looks of loaders that this version of the library draws. */
|
|
9
|
+
export const loaderVariants = ['spinner'];
|
|
10
|
+
/** The English labels that the packages ship. */
|
|
11
|
+
export const defaultLabels = freezeLabels({
|
|
12
|
+
dateTimeField: {
|
|
13
|
+
openPicker: 'Choose date and time',
|
|
14
|
+
timeLabel: 'Time',
|
|
15
|
+
done: 'Done',
|
|
16
|
+
validationMessage: ({ reason, min, max }) => {
|
|
17
|
+
switch (reason) {
|
|
18
|
+
case 'incomplete':
|
|
19
|
+
return 'Complete the date and time.';
|
|
20
|
+
case 'min':
|
|
21
|
+
return `Enter a date and time on or after ${String(min)}.`;
|
|
22
|
+
case 'max':
|
|
23
|
+
return `Enter a date and time on or before ${String(max)}.`;
|
|
24
|
+
case 'disabled':
|
|
25
|
+
return 'This date is unavailable.';
|
|
26
|
+
case 'constraints':
|
|
27
|
+
return 'The date and time limits are inconsistent.';
|
|
28
|
+
default:
|
|
29
|
+
return 'Enter a valid date and time.';
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
timeField: { inconsistentLimits: 'The time limits are inconsistent.' },
|
|
34
|
+
numberField: {
|
|
35
|
+
decrement: 'Decrease value',
|
|
36
|
+
increment: 'Increase value',
|
|
37
|
+
validationMessage: ({ reason, min, max, step, precision }) => {
|
|
38
|
+
switch (reason) {
|
|
39
|
+
case 'min':
|
|
40
|
+
return `Enter a number greater than or equal to ${String(min)}.`;
|
|
41
|
+
case 'max':
|
|
42
|
+
return `Enter a number less than or equal to ${String(max)}.`;
|
|
43
|
+
case 'step':
|
|
44
|
+
return `Enter a number in steps of ${String(step ?? 1)} from ${String(min ?? 0)}.`;
|
|
45
|
+
case 'precision':
|
|
46
|
+
return `Enter a number with at most ${String(precision)} decimal places.`;
|
|
47
|
+
case 'incomplete':
|
|
48
|
+
return 'Complete the number.';
|
|
49
|
+
case 'constraints':
|
|
50
|
+
return 'The number limits are inconsistent.';
|
|
51
|
+
default:
|
|
52
|
+
return 'Enter a valid number.';
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
dateField: {
|
|
57
|
+
openCalendar: 'Choose date',
|
|
58
|
+
formatHint: (example) => `Use the format ${example}.`,
|
|
59
|
+
validationMessage: ({ reason, min, max }) => {
|
|
60
|
+
switch (reason) {
|
|
61
|
+
case 'min':
|
|
62
|
+
return `Enter a date on or after ${String(min)}.`;
|
|
63
|
+
case 'max':
|
|
64
|
+
return `Enter a date on or before ${String(max)}.`;
|
|
65
|
+
case 'disabled':
|
|
66
|
+
return 'This date is unavailable.';
|
|
67
|
+
case 'incomplete':
|
|
68
|
+
return 'Complete the date.';
|
|
69
|
+
case 'constraints':
|
|
70
|
+
return 'The date limits are inconsistent.';
|
|
71
|
+
default:
|
|
72
|
+
return 'Enter a valid date.';
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
breadcrumbs: { label: 'Breadcrumb' },
|
|
77
|
+
button: { loading: 'Loading' },
|
|
78
|
+
calendar: {
|
|
79
|
+
previousMonth: 'Previous month',
|
|
80
|
+
nextMonth: 'Next month',
|
|
81
|
+
previousYear: 'Previous year',
|
|
82
|
+
nextYear: 'Next year',
|
|
83
|
+
previousYears: 'Previous years',
|
|
84
|
+
nextYears: 'Next years',
|
|
85
|
+
chooseMonth: (monthAndYear) => `${monthAndYear}, choose a month`,
|
|
86
|
+
chooseYear: (year) => `${year}, choose a year`,
|
|
87
|
+
},
|
|
88
|
+
chip: { dismiss: 'Remove' },
|
|
89
|
+
combobox: { clear: 'Clear', create: (query) => `Create “${query}”` },
|
|
90
|
+
dialog: { close: 'Close' },
|
|
91
|
+
drawer: { close: 'Close' },
|
|
92
|
+
fileInputField: {
|
|
93
|
+
prompt: 'Drag files here or browse',
|
|
94
|
+
replace: 'Replace',
|
|
95
|
+
remove: (fileName) => `Remove ${fileName}`,
|
|
96
|
+
},
|
|
97
|
+
iconButton: { loading: 'Loading' },
|
|
98
|
+
listbox: { empty: 'No options', loading: 'Loading options' },
|
|
99
|
+
loader: { label: 'Loading' },
|
|
100
|
+
notification: { dismiss: 'Dismiss' },
|
|
101
|
+
pagination: {
|
|
102
|
+
label: 'Pagination',
|
|
103
|
+
previous: 'Previous page',
|
|
104
|
+
next: 'Next page',
|
|
105
|
+
page: (page) => `Page ${String(page)}`,
|
|
106
|
+
status: (page, pageCount) => `Page ${String(page)} of ${String(pageCount)}`,
|
|
107
|
+
jump: 'Go to page',
|
|
108
|
+
total: (pageCount) => `of ${String(pageCount)}`,
|
|
109
|
+
},
|
|
110
|
+
passwordField: { reveal: 'Show password' },
|
|
111
|
+
searchField: { clear: 'Clear search' },
|
|
112
|
+
selectField: { clear: 'Clear selection' },
|
|
113
|
+
splitButton: { menu: 'More actions', loading: 'Loading' },
|
|
114
|
+
});
|
|
115
|
+
const DEFAULT_CONFIG = Object.freeze({
|
|
116
|
+
labels: defaultLabels,
|
|
117
|
+
loaderVariant: 'spinner',
|
|
118
|
+
});
|
|
119
|
+
const STATE_KEY = Symbol.for('ui-core.config');
|
|
120
|
+
/** Created on first use, so that loading the module changes nothing. */
|
|
121
|
+
function sharedState() {
|
|
122
|
+
const holder = globalThis;
|
|
123
|
+
holder[STATE_KEY] ??= {
|
|
124
|
+
version: 0,
|
|
125
|
+
labels: {},
|
|
126
|
+
loaderVariant: undefined,
|
|
127
|
+
listeners: new Set(),
|
|
128
|
+
};
|
|
129
|
+
return holder[STATE_KEY];
|
|
130
|
+
}
|
|
131
|
+
let cached;
|
|
132
|
+
/**
|
|
133
|
+
* Returns the configuration. The same object comes back until the configuration changes, so it
|
|
134
|
+
* can serve as the snapshot of `useSyncExternalStore`.
|
|
135
|
+
*/
|
|
136
|
+
export function getUiCoreConfig() {
|
|
137
|
+
const state = sharedState();
|
|
138
|
+
if (cached?.version !== state.version) {
|
|
139
|
+
cached = {
|
|
140
|
+
version: state.version,
|
|
141
|
+
config: Object.freeze({
|
|
142
|
+
locale: state.locale,
|
|
143
|
+
labels: mergeLabels(defaultLabels, state.labels),
|
|
144
|
+
loaderVariant: isLoaderVariant(state.loaderVariant)
|
|
145
|
+
? state.loaderVariant
|
|
146
|
+
: DEFAULT_CONFIG.loaderVariant,
|
|
147
|
+
}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return cached.config;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Sets labels, locale and the look of loaders for every component on the page, those already rendered
|
|
154
|
+
* included. Labels merge one by one over the earlier configuration, so setting one label keeps
|
|
155
|
+
* the others.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* configureUiCore({ labels: { loader: { label: 'Ładowanie' } } });
|
|
159
|
+
*/
|
|
160
|
+
export function configureUiCore(input) {
|
|
161
|
+
const state = sharedState();
|
|
162
|
+
const labels = mergeOverrides(state.labels, input.labels ?? {});
|
|
163
|
+
const loaderVariant = input.loaderVariant ?? state.loaderVariant;
|
|
164
|
+
const locale = input.locale ?? state.locale;
|
|
165
|
+
if (labels === state.labels && loaderVariant === state.loaderVariant && locale === state.locale) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
state.labels = labels;
|
|
169
|
+
state.loaderVariant = loaderVariant;
|
|
170
|
+
state.locale = locale;
|
|
171
|
+
changed(state);
|
|
172
|
+
}
|
|
173
|
+
/** Restores English labels, the default loader and automatic locale selection. */
|
|
174
|
+
export function resetUiCoreConfig() {
|
|
175
|
+
const state = sharedState();
|
|
176
|
+
if (Object.keys(state.labels).length === 0 &&
|
|
177
|
+
state.loaderVariant === undefined &&
|
|
178
|
+
state.locale === undefined) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
state.labels = {};
|
|
182
|
+
state.loaderVariant = undefined;
|
|
183
|
+
state.locale = undefined;
|
|
184
|
+
changed(state);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Calls `listener` after every change of the configuration, and returns the function that stops
|
|
188
|
+
* it. Components subscribe through it, the web components through their state and React with
|
|
189
|
+
* `useSyncExternalStore`.
|
|
190
|
+
*/
|
|
191
|
+
export function subscribeUiCoreConfig(listener) {
|
|
192
|
+
const { listeners } = sharedState();
|
|
193
|
+
listeners.add(listener);
|
|
194
|
+
return () => {
|
|
195
|
+
listeners.delete(listener);
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function changed(state) {
|
|
199
|
+
state.version += 1;
|
|
200
|
+
for (const listener of [...state.listeners]) {
|
|
201
|
+
listener();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function isLoaderVariant(value) {
|
|
205
|
+
return loaderVariants.some((variant) => variant === value);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Merges new overrides into the earlier ones, leaf by leaf. It returns the earlier object when
|
|
209
|
+
* nothing changes, so that setting the same label twice notifies nobody.
|
|
210
|
+
*/
|
|
211
|
+
function mergeOverrides(earlier, input) {
|
|
212
|
+
let result = earlier;
|
|
213
|
+
for (const [group, leaves] of entriesOf(input)) {
|
|
214
|
+
if (typeof leaves !== 'object' || leaves === null) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
for (const [key, value] of entriesOf(leaves)) {
|
|
218
|
+
if (value === undefined || result[group]?.[key] === value) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
result = { ...result, [group]: { ...result[group], [key]: value } };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Takes the defaults and replaces the leaves the application set. A group or a label that this
|
|
228
|
+
* copy does not know, such as one from a newer copy, is left out.
|
|
229
|
+
*/
|
|
230
|
+
function mergeLabels(defaults, overrides) {
|
|
231
|
+
const groups = toGroups(defaults);
|
|
232
|
+
const merged = {};
|
|
233
|
+
for (const [group, leaves] of Object.entries(groups)) {
|
|
234
|
+
const own = overrides[group] ?? {};
|
|
235
|
+
const entries = Object.entries(leaves).map(([key, value]) => {
|
|
236
|
+
const override = own[key];
|
|
237
|
+
return [key, typeof override === typeof value ? override : value];
|
|
238
|
+
});
|
|
239
|
+
merged[group] = Object.freeze(Object.fromEntries(entries));
|
|
240
|
+
}
|
|
241
|
+
return Object.freeze(merged);
|
|
242
|
+
}
|
|
243
|
+
/** The entries of an object whose values are not known, such as the input of a JavaScript caller. */
|
|
244
|
+
function entriesOf(value) {
|
|
245
|
+
return Object.entries(value);
|
|
246
|
+
}
|
|
247
|
+
function toGroups(labels) {
|
|
248
|
+
return labels;
|
|
249
|
+
}
|
|
250
|
+
function freezeLabels(labels) {
|
|
251
|
+
for (const leaves of Object.values(toGroups(labels))) {
|
|
252
|
+
Object.freeze(leaves);
|
|
253
|
+
}
|
|
254
|
+
return Object.freeze(labels);
|
|
255
|
+
}
|