@commmerce/ui 1.0.0-rc.4 → 1.0.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/LICENSE.md +152 -17
- package/README.md +2 -3
- package/THIRD_PARTY_NOTICES.txt +175 -0
- package/dist/base.d.ts +5 -0
- package/dist/base.js +10 -0
- package/dist/calendar.d.ts +95 -0
- package/dist/calendar.js +280 -0
- package/dist/cdn.js +721 -477
- package/dist/components/accordion.js +2 -2
- package/dist/components/action-bar.d.ts +12 -2
- package/dist/components/action-bar.js +10 -5
- package/dist/components/action-menu.d.ts +17 -4
- package/dist/components/action-menu.js +8 -5
- package/dist/components/app-card.d.ts +24 -2
- package/dist/components/app-card.js +25 -10
- package/dist/components/banner.d.ts +2 -2
- package/dist/components/banner.js +4 -3
- package/dist/components/bottom-navigation.d.ts +16 -4
- package/dist/components/bottom-navigation.js +10 -7
- package/dist/components/bottom-sheet.d.ts +4 -1
- package/dist/components/bottom-sheet.js +2 -2
- package/dist/components/checkbox.d.ts +12 -2
- package/dist/components/checkbox.js +7 -2
- package/dist/components/combobox.d.ts +186 -0
- package/dist/components/combobox.js +521 -0
- package/dist/components/comment.d.ts +27 -9
- package/dist/components/comment.js +14 -13
- package/dist/components/data-table.d.ts +14 -2
- package/dist/components/data-table.js +31 -10
- package/dist/components/date-picker.d.ts +179 -0
- package/dist/components/date-picker.js +336 -0
- package/dist/components/date-range-picker.d.ts +251 -0
- package/dist/components/date-range-picker.js +484 -0
- package/dist/components/file-upload.d.ts +30 -4
- package/dist/components/file-upload.js +37 -17
- package/dist/components/filter-panel.d.ts +29 -6
- package/dist/components/filter-panel.js +16 -14
- package/dist/components/flash-offer-bar.d.ts +18 -4
- package/dist/components/flash-offer-bar.js +13 -8
- package/dist/components/header-bar.d.ts +20 -4
- package/dist/components/header-bar.js +20 -13
- package/dist/components/icon.d.ts +7 -0
- package/dist/components/icon.js +9 -1
- package/dist/components/link-tabs.d.ts +16 -4
- package/dist/components/link-tabs.js +10 -7
- package/dist/components/mobile-menu.d.ts +19 -4
- package/dist/components/mobile-menu.js +16 -10
- package/dist/components/modal.d.ts +4 -1
- package/dist/components/nested-list.d.ts +22 -3
- package/dist/components/nested-list.js +25 -12
- package/dist/components/notification-panel.d.ts +26 -5
- package/dist/components/notification-panel.js +17 -11
- package/dist/components/option-group.d.ts +12 -2
- package/dist/components/option-group.js +13 -7
- package/dist/components/page-header.d.ts +18 -2
- package/dist/components/page-header.js +23 -10
- package/dist/components/pagination.d.ts +27 -4
- package/dist/components/pagination.js +28 -16
- package/dist/components/progress-bar.d.ts +12 -2
- package/dist/components/progress-bar.js +9 -4
- package/dist/components/radio.d.ts +12 -1
- package/dist/components/radio.js +12 -6
- package/dist/components/report-tile.d.ts +12 -1
- package/dist/components/report-tile.js +9 -4
- package/dist/components/resource-list.d.ts +36 -5
- package/dist/components/resource-list.js +50 -23
- package/dist/components/save-bar.d.ts +23 -6
- package/dist/components/save-bar.js +14 -12
- package/dist/components/search.d.ts +28 -7
- package/dist/components/search.js +18 -13
- package/dist/components/select.d.ts +12 -2
- package/dist/components/select.js +9 -4
- package/dist/components/side-navigation.d.ts +16 -4
- package/dist/components/side-navigation.js +16 -12
- package/dist/components/side-panel.d.ts +5 -2
- package/dist/components/side-panel.js +5 -3
- package/dist/components/skeleton-display.d.ts +6 -4
- package/dist/components/skeleton-display.js +4 -5
- package/dist/components/smart-tip.d.ts +25 -6
- package/dist/components/smart-tip.js +18 -12
- package/dist/components/spinner.d.ts +4 -3
- package/dist/components/spinner.js +5 -6
- package/dist/components/status-tabs.d.ts +12 -2
- package/dist/components/status-tabs.js +14 -8
- package/dist/components/table-header.d.ts +15 -2
- package/dist/components/table-header.js +13 -5
- package/dist/components/tag.d.ts +12 -2
- package/dist/components/tag.js +9 -4
- package/dist/components/text-field.d.ts +12 -2
- package/dist/components/text-field.js +9 -4
- package/dist/components/thumbnail.d.ts +12 -2
- package/dist/components/thumbnail.js +8 -3
- package/dist/components/toast.d.ts +14 -3
- package/dist/components/toast.js +20 -9
- package/dist/components/toggle.js +2 -0
- package/dist/custom-elements.json +16477 -6434
- package/dist/index.d.ts +7 -1
- package/dist/index.js +5 -1
- package/dist/localize.d.ts +130 -0
- package/dist/localize.js +232 -0
- package/dist/overlay.d.ts +8 -4
- package/dist/overlay.js +9 -12
- package/package.json +5 -4
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ export { CmToggle } from './components/toggle.js';
|
|
|
6
6
|
export { CmSelect } from './components/select.js';
|
|
7
7
|
export type { SelectOption } from './components/select.js';
|
|
8
8
|
export { CmSearch } from './components/search.js';
|
|
9
|
+
export { CmCombobox } from './components/combobox.js';
|
|
10
|
+
export { CmDatePicker } from './components/date-picker.js';
|
|
11
|
+
export { CmDateRangePicker, resolvePreset } from './components/date-range-picker.js';
|
|
12
|
+
export type { DateRangePreset, DateRangePresetKey } from './components/date-range-picker.js';
|
|
9
13
|
export { CmFileUpload } from './components/file-upload.js';
|
|
10
14
|
export { CmOptionGroup } from './components/option-group.js';
|
|
11
15
|
export { CmTag } from './components/tag.js';
|
|
@@ -62,9 +66,11 @@ export { CmLayout, CmLayoutSection } from './components/layout.js';
|
|
|
62
66
|
export { CmPage } from './components/page.js';
|
|
63
67
|
export type { SpaceStep } from './spacing.js';
|
|
64
68
|
export { CmBanner } from './components/banner.js';
|
|
65
|
-
export { CmIcon } from './components/icon.js';
|
|
69
|
+
export { CmIcon, directionalIcons } from './components/icon.js';
|
|
66
70
|
export { CmTooltip } from './components/tooltip.js';
|
|
67
71
|
export { CmAvatar, initials } from './components/avatar.js';
|
|
68
72
|
export { CmThumbnail } from './components/thumbnail.js';
|
|
69
73
|
export type { IconName } from '@commmerce/icons';
|
|
70
74
|
export type { Tone } from './base.js';
|
|
75
|
+
export { configure, setStrings, resetStrings, getDefaultStrings, defineStrings, translate, formatMessage, formatNumber, formatDate, pluralCategory, localeFor, CmLocalizedElement, } from './localize.js';
|
|
76
|
+
export type { CmStrings, CmStringsInput, Message, MessageVars, PluralMessage, StringTable, LocalizedPropertyDeclarations } from './localize.js';
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,9 @@ export { CmRadio, CmRadioGroup } from './components/radio.js';
|
|
|
5
5
|
export { CmToggle } from './components/toggle.js';
|
|
6
6
|
export { CmSelect } from './components/select.js';
|
|
7
7
|
export { CmSearch } from './components/search.js';
|
|
8
|
+
export { CmCombobox } from './components/combobox.js';
|
|
9
|
+
export { CmDatePicker } from './components/date-picker.js';
|
|
10
|
+
export { CmDateRangePicker, resolvePreset } from './components/date-range-picker.js';
|
|
8
11
|
export { CmFileUpload } from './components/file-upload.js';
|
|
9
12
|
export { CmOptionGroup } from './components/option-group.js';
|
|
10
13
|
export { CmTag } from './components/tag.js';
|
|
@@ -50,7 +53,8 @@ export { CmGrid } from './components/grid.js';
|
|
|
50
53
|
export { CmLayout, CmLayoutSection } from './components/layout.js';
|
|
51
54
|
export { CmPage } from './components/page.js';
|
|
52
55
|
export { CmBanner } from './components/banner.js';
|
|
53
|
-
export { CmIcon } from './components/icon.js';
|
|
56
|
+
export { CmIcon, directionalIcons } from './components/icon.js';
|
|
54
57
|
export { CmTooltip } from './components/tooltip.js';
|
|
55
58
|
export { CmAvatar, initials } from './components/avatar.js';
|
|
56
59
|
export { CmThumbnail } from './components/thumbnail.js';
|
|
60
|
+
export { configure, setStrings, resetStrings, getDefaultStrings, defineStrings, translate, formatMessage, formatNumber, formatDate, pluralCategory, localeFor, CmLocalizedElement, } from './localize.js';
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { LitElement, type PropertyDeclaration } from 'lit';
|
|
2
|
+
/** Lit property declarations, plus `string: 'key'` for properties whose default is a built-in string. */
|
|
3
|
+
export type LocalizedPropertyDeclarations = Record<string, PropertyDeclaration & {
|
|
4
|
+
string?: string;
|
|
5
|
+
}>;
|
|
6
|
+
/**
|
|
7
|
+
* Built-in text and number formatting for every component.
|
|
8
|
+
*
|
|
9
|
+
* Each component that shows its own text (button labels, accessible names, status messages) reads it from a
|
|
10
|
+
* string table here instead of hard-coding English. Three layers, first match wins:
|
|
11
|
+
* 1. the component's own properties (`close-label`, `save-label`, …) and its `strings` property;
|
|
12
|
+
* 2. strings the app set with `configure()` / `setStrings()` (per language or for every language);
|
|
13
|
+
* 3. the English defaults each component registers with `defineStrings()`.
|
|
14
|
+
*
|
|
15
|
+
* The namespace of a component is its tag name without `cm-`, in camelCase: `cm-resource-list` → `resourceList`.
|
|
16
|
+
* Keys in the `common` namespace (close, dismiss, loading…) apply to every component that uses them, unless the
|
|
17
|
+
* component's own namespace sets the same key.
|
|
18
|
+
*/
|
|
19
|
+
/** Values substituted into a message: `{name}` placeholders. Numbers are formatted for the locale. */
|
|
20
|
+
export type MessageVars = Record<string, string | number>;
|
|
21
|
+
/** Plural forms, picked with `Intl.PluralRules` from the `count` value. `other` is required. */
|
|
22
|
+
export type PluralMessage = Partial<Record<Intl.LDMLPluralRule, string>> & {
|
|
23
|
+
other: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* One translatable string:
|
|
27
|
+
* - text with `{placeholders}`: `'Page {page} of {total}'` (numbers are formatted for the locale);
|
|
28
|
+
* - plural forms, chosen by `count`: `{ one: '{count} review', other: '{count} reviews' }`;
|
|
29
|
+
* - a function that gets the raw values and returns the text: `({ count }) => …`.
|
|
30
|
+
*/
|
|
31
|
+
export type Message = string | PluralMessage | ((vars: MessageVars) => string);
|
|
32
|
+
/** A namespace's strings: key → message. */
|
|
33
|
+
export type StringTable = Record<string, Message>;
|
|
34
|
+
/**
|
|
35
|
+
* Every namespace and its keys. Components add theirs with module augmentation, next to `defineStrings()`:
|
|
36
|
+
* `declare module '../localize.js' { interface CmStrings { pagination: typeof paginationStrings } }`.
|
|
37
|
+
*/
|
|
38
|
+
export interface CmStrings {
|
|
39
|
+
common: typeof commonStrings;
|
|
40
|
+
}
|
|
41
|
+
/** Strings an app passes in: any namespace, any subset of its keys. */
|
|
42
|
+
export type CmStringsInput = {
|
|
43
|
+
[N in keyof CmStrings]?: {
|
|
44
|
+
[K in keyof CmStrings[N]]?: Message;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/** Shared by several components. A component's own namespace can override any of them. */
|
|
48
|
+
declare const commonStrings: {
|
|
49
|
+
/** Close buttons of Modal, Side panel, Filter panel, Bottom sheet and Notification panel. */
|
|
50
|
+
close: string;
|
|
51
|
+
/** Dismiss buttons of Banner and Toast. */
|
|
52
|
+
dismiss: string;
|
|
53
|
+
/** Loading states: Spinner, Skeleton display, Combobox. */
|
|
54
|
+
loading: string;
|
|
55
|
+
/** Sort direction in sort announcements (Data table, Table header). */
|
|
56
|
+
ascending: string;
|
|
57
|
+
descending: string;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Registers a component's English defaults under its namespace. Call it once at module level in the component's
|
|
61
|
+
* file. Returns the table, so `typeof` gives the keys for the `CmStrings` augmentation.
|
|
62
|
+
*/
|
|
63
|
+
export declare function defineStrings<T extends StringTable>(namespace: string, table: T): T;
|
|
64
|
+
/** The English defaults of every loaded component, by namespace. A copy: changing it changes nothing. */
|
|
65
|
+
export declare function getDefaultStrings(): Record<string, Record<string, Message>>;
|
|
66
|
+
/**
|
|
67
|
+
* Replaces built-in text, for every component on the page. Merges with what was set before.
|
|
68
|
+
* With `locale`, the strings apply only to components whose language (see `configure`) is that locale
|
|
69
|
+
* or starts with it: strings for `fr` apply to `fr-CA` too.
|
|
70
|
+
* @example setStrings({ pagination: { previous: 'Page précédente' }, common: { close: 'Fermer' } }, 'fr');
|
|
71
|
+
*/
|
|
72
|
+
export declare function setStrings(strings: CmStringsInput, locale?: string): void;
|
|
73
|
+
/**
|
|
74
|
+
* Sets the language and text for every component at once.
|
|
75
|
+
* - `locale`: a BCP 47 tag (`fr`, `ar-EG`, `hi-IN`) used for numbers, dates and plural rules, and to pick strings
|
|
76
|
+
* set for a language. Without it, each component uses the `lang` attribute of its nearest ancestor (usually
|
|
77
|
+
* `<html lang>`), then the browser's language.
|
|
78
|
+
* - `strings`: the same object as `setStrings()`, for every language.
|
|
79
|
+
* - `translations`: strings per language, `{ fr: {…}, ar: {…} }`, picked by each component's language.
|
|
80
|
+
* @example configure({ locale: 'fr', strings: { saveBar: { save: 'Enregistrer', discard: 'Annuler' } } });
|
|
81
|
+
*/
|
|
82
|
+
export declare function configure(options?: {
|
|
83
|
+
locale?: string;
|
|
84
|
+
strings?: CmStringsInput;
|
|
85
|
+
translations?: Record<string, CmStringsInput>;
|
|
86
|
+
}): void;
|
|
87
|
+
/** Forgets everything set with `configure()` and `setStrings()`: English text, locale from `lang` again. */
|
|
88
|
+
export declare function resetStrings(): void;
|
|
89
|
+
/** The locale a component formats with: `configure({ locale })`, else the nearest `lang` attribute, else undefined (the browser's). */
|
|
90
|
+
export declare function localeFor(el?: Element | null): string | undefined;
|
|
91
|
+
/** Formats a number for a locale (`Intl.NumberFormat`). */
|
|
92
|
+
export declare function formatNumber(n: number, options?: Intl.NumberFormatOptions, locale?: string): string;
|
|
93
|
+
/** Formats a date for a locale (`Intl.DateTimeFormat`). */
|
|
94
|
+
export declare function formatDate(d: Date, options?: Intl.DateTimeFormatOptions, locale?: string): string;
|
|
95
|
+
/** The plural category of a number in a locale: `one`, `few`, `other`… */
|
|
96
|
+
export declare function pluralCategory(n: number, locale?: string): Intl.LDMLPluralRule;
|
|
97
|
+
/** Fills a message with values: picks the plural form, substitutes `{placeholders}`, or calls the function. */
|
|
98
|
+
export declare function formatMessage(message: Message | undefined, vars?: MessageVars, locale?: string): string;
|
|
99
|
+
/** The text of `namespace.key` for an element (its language, overrides), filled with `vars`. */
|
|
100
|
+
export declare function translate(ns: string, key: string, vars?: MessageVars, el?: Element | null, own?: Record<string, Message> | null): string;
|
|
101
|
+
/**
|
|
102
|
+
* Base class of components with built-in text. Gives them the `strings` property, `t()` for their text in the
|
|
103
|
+
* current language, `formatNumber()` and `locale`, and re-renders them when the app changes strings or language.
|
|
104
|
+
*/
|
|
105
|
+
export declare class CmLocalizedElement extends LitElement {
|
|
106
|
+
static properties: LocalizedPropertyDeclarations;
|
|
107
|
+
/**
|
|
108
|
+
* A property declared with `string: 'key'` (e.g. `saveLabel: { attribute: 'save-label', string: 'save' }`) reads
|
|
109
|
+
* as that built-in string until it is set, so `el.saveLabel` is "Save" in English and the translation elsewhere.
|
|
110
|
+
* Setting it overrides the string for this element; setting `undefined` goes back to the built-in one.
|
|
111
|
+
*/
|
|
112
|
+
static finalize(): void;
|
|
113
|
+
/**
|
|
114
|
+
* Replaces this component's built-in text (labels, accessible names, messages), key by key. In HTML pass JSON:
|
|
115
|
+
* `strings='{"close":"Fermer"}'`. Keys and English defaults are listed under "Built-in text" on the component's
|
|
116
|
+
* page; set text for every component at once with `configure()` or `setStrings()` (see Localisation).
|
|
117
|
+
*/
|
|
118
|
+
strings?: Record<string, Message> | null;
|
|
119
|
+
/** The string namespace: the tag name without `cm-`, in camelCase. */
|
|
120
|
+
protected get stringsNamespace(): string;
|
|
121
|
+
/** The component's text for `key`, in its language, with `{placeholders}` filled from `vars`. */
|
|
122
|
+
protected t(key: string, vars?: MessageVars): string;
|
|
123
|
+
/** The locale used for numbers, dates and plurals. */
|
|
124
|
+
protected get locale(): string | undefined;
|
|
125
|
+
/** A number formatted for the component's locale. */
|
|
126
|
+
protected formatNumber(n: number, options?: Intl.NumberFormatOptions): string;
|
|
127
|
+
connectedCallback(): void;
|
|
128
|
+
disconnectedCallback(): void;
|
|
129
|
+
}
|
|
130
|
+
export {};
|
package/dist/localize.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { LitElement } from 'lit';
|
|
2
|
+
/** Shared by several components. A component's own namespace can override any of them. */
|
|
3
|
+
const commonStrings = {
|
|
4
|
+
/** Close buttons of Modal, Side panel, Filter panel, Bottom sheet and Notification panel. */
|
|
5
|
+
close: 'Close',
|
|
6
|
+
/** Dismiss buttons of Banner and Toast. */
|
|
7
|
+
dismiss: 'Dismiss',
|
|
8
|
+
/** Loading states: Spinner, Skeleton display, Combobox. */
|
|
9
|
+
loading: 'Loading',
|
|
10
|
+
/** Sort direction in sort announcements (Data table, Table header). */
|
|
11
|
+
ascending: 'ascending',
|
|
12
|
+
descending: 'descending',
|
|
13
|
+
};
|
|
14
|
+
const defaults = { common: { ...commonStrings } };
|
|
15
|
+
const global = {};
|
|
16
|
+
const byLocale = {};
|
|
17
|
+
let configuredLocale;
|
|
18
|
+
/**
|
|
19
|
+
* Registers a component's English defaults under its namespace. Call it once at module level in the component's
|
|
20
|
+
* file. Returns the table, so `typeof` gives the keys for the `CmStrings` augmentation.
|
|
21
|
+
*/
|
|
22
|
+
export function defineStrings(namespace, table) {
|
|
23
|
+
defaults[namespace] = { ...defaults[namespace], ...table };
|
|
24
|
+
return table;
|
|
25
|
+
}
|
|
26
|
+
/** The English defaults of every loaded component, by namespace. A copy: changing it changes nothing. */
|
|
27
|
+
export function getDefaultStrings() {
|
|
28
|
+
return Object.fromEntries(Object.entries(defaults).map(([k, v]) => [k, { ...v }]));
|
|
29
|
+
}
|
|
30
|
+
function merge(target, input) {
|
|
31
|
+
for (const [ns, table] of Object.entries(input ?? {})) {
|
|
32
|
+
if (table && typeof table === 'object')
|
|
33
|
+
target[ns] = { ...target[ns], ...table };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Replaces built-in text, for every component on the page. Merges with what was set before.
|
|
38
|
+
* With `locale`, the strings apply only to components whose language (see `configure`) is that locale
|
|
39
|
+
* or starts with it: strings for `fr` apply to `fr-CA` too.
|
|
40
|
+
* @example setStrings({ pagination: { previous: 'Page précédente' }, common: { close: 'Fermer' } }, 'fr');
|
|
41
|
+
*/
|
|
42
|
+
export function setStrings(strings, locale) {
|
|
43
|
+
if (locale)
|
|
44
|
+
merge((byLocale[locale.toLowerCase()] ??= {}), strings);
|
|
45
|
+
else
|
|
46
|
+
merge(global, strings);
|
|
47
|
+
refresh();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Sets the language and text for every component at once.
|
|
51
|
+
* - `locale`: a BCP 47 tag (`fr`, `ar-EG`, `hi-IN`) used for numbers, dates and plural rules, and to pick strings
|
|
52
|
+
* set for a language. Without it, each component uses the `lang` attribute of its nearest ancestor (usually
|
|
53
|
+
* `<html lang>`), then the browser's language.
|
|
54
|
+
* - `strings`: the same object as `setStrings()`, for every language.
|
|
55
|
+
* - `translations`: strings per language, `{ fr: {…}, ar: {…} }`, picked by each component's language.
|
|
56
|
+
* @example configure({ locale: 'fr', strings: { saveBar: { save: 'Enregistrer', discard: 'Annuler' } } });
|
|
57
|
+
*/
|
|
58
|
+
export function configure(options = {}) {
|
|
59
|
+
if ('locale' in options)
|
|
60
|
+
configuredLocale = options.locale || undefined;
|
|
61
|
+
if (options.strings)
|
|
62
|
+
merge(global, options.strings);
|
|
63
|
+
for (const [loc, s] of Object.entries(options.translations ?? {}))
|
|
64
|
+
merge((byLocale[loc.toLowerCase()] ??= {}), s);
|
|
65
|
+
refresh();
|
|
66
|
+
}
|
|
67
|
+
/** Forgets everything set with `configure()` and `setStrings()`: English text, locale from `lang` again. */
|
|
68
|
+
export function resetStrings() {
|
|
69
|
+
configuredLocale = undefined;
|
|
70
|
+
for (const k of Object.keys(global))
|
|
71
|
+
delete global[k];
|
|
72
|
+
for (const k of Object.keys(byLocale))
|
|
73
|
+
delete byLocale[k];
|
|
74
|
+
refresh();
|
|
75
|
+
}
|
|
76
|
+
/** The locale a component formats with: `configure({ locale })`, else the nearest `lang` attribute, else undefined (the browser's). */
|
|
77
|
+
export function localeFor(el) {
|
|
78
|
+
if (configuredLocale)
|
|
79
|
+
return configuredLocale;
|
|
80
|
+
let node = el;
|
|
81
|
+
while (node) {
|
|
82
|
+
if (node instanceof Element && node.hasAttribute('lang') && node.getAttribute('lang'))
|
|
83
|
+
return node.getAttribute('lang');
|
|
84
|
+
node = node.parentNode ?? (node instanceof ShadowRoot ? node.host : null);
|
|
85
|
+
}
|
|
86
|
+
if (typeof document !== 'undefined' && document.documentElement.lang)
|
|
87
|
+
return document.documentElement.lang;
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const cache = new Map();
|
|
91
|
+
function cached(kind, locale, opts, make) {
|
|
92
|
+
const key = `${kind}|${locale ?? ''}|${JSON.stringify(opts)}`;
|
|
93
|
+
let f = cache.get(key);
|
|
94
|
+
if (!f) {
|
|
95
|
+
try {
|
|
96
|
+
f = make(locale);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
f = make(undefined);
|
|
100
|
+
} // an invalid lang attribute falls back to the browser's locale
|
|
101
|
+
cache.set(key, f);
|
|
102
|
+
}
|
|
103
|
+
return f;
|
|
104
|
+
}
|
|
105
|
+
/** Formats a number for a locale (`Intl.NumberFormat`). */
|
|
106
|
+
export function formatNumber(n, options = {}, locale) {
|
|
107
|
+
return cached('n', locale, options, (l) => new Intl.NumberFormat(l, options)).format(n);
|
|
108
|
+
}
|
|
109
|
+
/** Formats a date for a locale (`Intl.DateTimeFormat`). */
|
|
110
|
+
export function formatDate(d, options = {}, locale) {
|
|
111
|
+
return cached('d', locale, options, (l) => new Intl.DateTimeFormat(l, options)).format(d);
|
|
112
|
+
}
|
|
113
|
+
/** The plural category of a number in a locale: `one`, `few`, `other`… */
|
|
114
|
+
export function pluralCategory(n, locale) {
|
|
115
|
+
return cached('p', locale, {}, (l) => new Intl.PluralRules(l)).select(n);
|
|
116
|
+
}
|
|
117
|
+
/** Fills a message with values: picks the plural form, substitutes `{placeholders}`, or calls the function. */
|
|
118
|
+
export function formatMessage(message, vars = {}, locale) {
|
|
119
|
+
if (message === undefined || message === null)
|
|
120
|
+
return '';
|
|
121
|
+
if (typeof message === 'function')
|
|
122
|
+
return String(message(vars));
|
|
123
|
+
let text;
|
|
124
|
+
if (typeof message === 'object') {
|
|
125
|
+
const n = Number(vars.count ?? 0);
|
|
126
|
+
text = message[pluralCategory(n, locale)] ?? message.other ?? '';
|
|
127
|
+
}
|
|
128
|
+
else
|
|
129
|
+
text = String(message);
|
|
130
|
+
return text.replace(/\{(\w+)\}/g, (all, name) => {
|
|
131
|
+
const v = vars[name];
|
|
132
|
+
if (v === undefined)
|
|
133
|
+
return all;
|
|
134
|
+
return typeof v === 'number' ? formatNumber(v, {}, locale) : String(v);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function lookup(ns, key, locale, own) {
|
|
138
|
+
if (own && own[key] !== undefined)
|
|
139
|
+
return own[key];
|
|
140
|
+
const tags = [];
|
|
141
|
+
if (locale) {
|
|
142
|
+
const parts = locale.toLowerCase().split('-');
|
|
143
|
+
for (let i = parts.length; i > 0; i--)
|
|
144
|
+
tags.push(parts.slice(0, i).join('-'));
|
|
145
|
+
}
|
|
146
|
+
// The component's namespace first (app strings for its language, app strings for all, its English default),
|
|
147
|
+
// then the same for `common`. A component that words a common key its own way keeps its wording.
|
|
148
|
+
for (const n of ns === 'common' ? ['common'] : [ns, 'common']) {
|
|
149
|
+
for (const t of tags) {
|
|
150
|
+
const v = byLocale[t]?.[n]?.[key];
|
|
151
|
+
if (v !== undefined)
|
|
152
|
+
return v;
|
|
153
|
+
}
|
|
154
|
+
const v = global[n]?.[key] ?? defaults[n]?.[key];
|
|
155
|
+
if (v !== undefined)
|
|
156
|
+
return v;
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
/** The text of `namespace.key` for an element (its language, overrides), filled with `vars`. */
|
|
161
|
+
export function translate(ns, key, vars, el, own) {
|
|
162
|
+
const locale = localeFor(el);
|
|
163
|
+
return formatMessage(lookup(ns, key, locale, own), vars, locale);
|
|
164
|
+
}
|
|
165
|
+
/* ---------- Keeping rendered components up to date ---------- */
|
|
166
|
+
const hosts = new Set();
|
|
167
|
+
let langObserver;
|
|
168
|
+
function refresh() {
|
|
169
|
+
for (const h of hosts)
|
|
170
|
+
h.requestUpdate();
|
|
171
|
+
}
|
|
172
|
+
function watchLang() {
|
|
173
|
+
if (langObserver || typeof MutationObserver === 'undefined' || typeof document === 'undefined')
|
|
174
|
+
return;
|
|
175
|
+
// A `lang` change anywhere (e.g. a language switcher on <html>) re-renders the components.
|
|
176
|
+
langObserver = new MutationObserver(refresh);
|
|
177
|
+
langObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['lang'], subtree: true });
|
|
178
|
+
}
|
|
179
|
+
const camel = (tag) => tag.replace(/^cm-/, '').replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
180
|
+
/**
|
|
181
|
+
* Base class of components with built-in text. Gives them the `strings` property, `t()` for their text in the
|
|
182
|
+
* current language, `formatNumber()` and `locale`, and re-renders them when the app changes strings or language.
|
|
183
|
+
*/
|
|
184
|
+
export class CmLocalizedElement extends LitElement {
|
|
185
|
+
/**
|
|
186
|
+
* A property declared with `string: 'key'` (e.g. `saveLabel: { attribute: 'save-label', string: 'save' }`) reads
|
|
187
|
+
* as that built-in string until it is set, so `el.saveLabel` is "Save" in English and the translation elsewhere.
|
|
188
|
+
* Setting it overrides the string for this element; setting `undefined` goes back to the built-in one.
|
|
189
|
+
*/
|
|
190
|
+
static finalize() {
|
|
191
|
+
// Before Lit creates the properties: give each `string: 'key'` property an accessor that falls back to the
|
|
192
|
+
// built-in string. Lit then wraps it (attributes, requestUpdate) like any accessor written in a class.
|
|
193
|
+
const own = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
194
|
+
if (!own(this, 'finalized') && own(this, 'properties')) {
|
|
195
|
+
for (const [name, options] of Object.entries(this.properties)) {
|
|
196
|
+
const string = options.string;
|
|
197
|
+
if (!string || own(this.prototype, name))
|
|
198
|
+
continue;
|
|
199
|
+
const key = Symbol(name);
|
|
200
|
+
Object.defineProperty(this.prototype, name, {
|
|
201
|
+
get() { return this[key] ?? this.t(string); },
|
|
202
|
+
set(value) { this[key] = value; },
|
|
203
|
+
configurable: true,
|
|
204
|
+
enumerable: true,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
super.finalize();
|
|
209
|
+
}
|
|
210
|
+
/** The string namespace: the tag name without `cm-`, in camelCase. */
|
|
211
|
+
get stringsNamespace() { return camel(this.localName); }
|
|
212
|
+
/** The component's text for `key`, in its language, with `{placeholders}` filled from `vars`. */
|
|
213
|
+
t(key, vars) {
|
|
214
|
+
return translate(this.stringsNamespace, key, vars, this, this.strings);
|
|
215
|
+
}
|
|
216
|
+
/** The locale used for numbers, dates and plurals. */
|
|
217
|
+
get locale() { return localeFor(this); }
|
|
218
|
+
/** A number formatted for the component's locale. */
|
|
219
|
+
formatNumber(n, options) { return formatNumber(n, options, this.locale); }
|
|
220
|
+
connectedCallback() {
|
|
221
|
+
super.connectedCallback();
|
|
222
|
+
hosts.add(this);
|
|
223
|
+
watchLang();
|
|
224
|
+
}
|
|
225
|
+
disconnectedCallback() {
|
|
226
|
+
super.disconnectedCallback();
|
|
227
|
+
hosts.delete(this);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
CmLocalizedElement.properties = {
|
|
231
|
+
strings: { type: Object },
|
|
232
|
+
};
|
package/dist/overlay.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { nothing, type PropertyValues, type TemplateResult } from 'lit';
|
|
2
|
+
import { CmLocalizedElement } from './localize.js';
|
|
2
3
|
import './components/icon.js';
|
|
3
4
|
/** Stops the page behind an open overlay from scrolling. Counted, so stacked overlays unlock once. */
|
|
4
5
|
export declare function lockScroll(): void;
|
|
@@ -13,17 +14,20 @@ export type CloseReason = 'escape' | 'backdrop' | 'close-button' | 'action' | 'm
|
|
|
13
14
|
* the backdrop close it through a cancelable `cm-close`; page scrolling is locked while open; focus goes back to
|
|
14
15
|
* the element that had it when the overlay opened.
|
|
15
16
|
*/
|
|
16
|
-
export declare class CmOverlay extends
|
|
17
|
+
export declare class CmOverlay extends CmLocalizedElement {
|
|
17
18
|
#private;
|
|
18
19
|
static properties: {
|
|
19
20
|
open: {
|
|
20
21
|
type: BooleanConstructor;
|
|
21
22
|
reflect: boolean;
|
|
22
23
|
};
|
|
23
|
-
heading: {
|
|
24
|
+
heading: {
|
|
25
|
+
string: string;
|
|
26
|
+
};
|
|
24
27
|
label: {};
|
|
25
28
|
closeLabel: {
|
|
26
29
|
attribute: string;
|
|
30
|
+
string: string;
|
|
27
31
|
};
|
|
28
32
|
};
|
|
29
33
|
/** Shows the overlay. Set it, or call `show()` / `close()`. */
|
|
@@ -32,7 +36,7 @@ export declare class CmOverlay extends LitElement {
|
|
|
32
36
|
heading: string;
|
|
33
37
|
/** Accessible name when there is no visible heading. */
|
|
34
38
|
label: string;
|
|
35
|
-
/** Accessible name of the close button. */
|
|
39
|
+
/** Accessible name of the close button. Default: the built-in string `close` ("Close"). */
|
|
36
40
|
closeLabel: string;
|
|
37
41
|
protected uid: string;
|
|
38
42
|
static overlayStyles: import("lit").CSSResult[];
|
package/dist/overlay.js
CHANGED
|
@@ -10,8 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _CmOverlay_instances, _CmOverlay_opener, _CmOverlay_locked, _CmOverlay_downOnBackdrop, _CmOverlay_teardown, _CmOverlay_keydown, _CmOverlay_cancel, _CmOverlay_closed, _CmOverlay_down, _CmOverlay_click;
|
|
13
|
-
import {
|
|
13
|
+
import { html, css, nothing } from 'lit';
|
|
14
14
|
import { base, nextId } from './base.js';
|
|
15
|
+
import { CmLocalizedElement } from './localize.js';
|
|
15
16
|
import './components/icon.js';
|
|
16
17
|
let locks = 0;
|
|
17
18
|
let savedOverflow = '';
|
|
@@ -39,18 +40,14 @@ export function deepActiveElement() {
|
|
|
39
40
|
* the backdrop close it through a cancelable `cm-close`; page scrolling is locked while open; focus goes back to
|
|
40
41
|
* the element that had it when the overlay opened.
|
|
41
42
|
*/
|
|
42
|
-
export class CmOverlay extends
|
|
43
|
+
export class CmOverlay extends CmLocalizedElement {
|
|
43
44
|
constructor() {
|
|
44
45
|
super(...arguments);
|
|
45
46
|
_CmOverlay_instances.add(this);
|
|
46
47
|
/** Shows the overlay. Set it, or call `show()` / `close()`. */
|
|
47
48
|
this.open = false;
|
|
48
|
-
/** Title in the header. Also the dialog's accessible name. */
|
|
49
|
-
this.heading = '';
|
|
50
49
|
/** Accessible name when there is no visible heading. */
|
|
51
50
|
this.label = '';
|
|
52
|
-
/** Accessible name of the close button. */
|
|
53
|
-
this.closeLabel = 'Close';
|
|
54
51
|
this.uid = nextId('cm-ovl');
|
|
55
52
|
_CmOverlay_opener.set(this, null);
|
|
56
53
|
_CmOverlay_locked.set(this, false);
|
|
@@ -64,10 +61,10 @@ export class CmOverlay extends LitElement {
|
|
|
64
61
|
});
|
|
65
62
|
_CmOverlay_cancel.set(this, (e) => { e.preventDefault(); this.requestClose('escape'); });
|
|
66
63
|
/** The dialog closed by itself (e.g. the browser forced it): bring `open` in line. */
|
|
67
|
-
_CmOverlay_closed.set(this, () => { if (this.open) {
|
|
64
|
+
_CmOverlay_closed.set(this, () => { if (this.open && !this.dialog?.open) {
|
|
68
65
|
this.open = false;
|
|
69
66
|
__classPrivateFieldGet(this, _CmOverlay_instances, "m", _CmOverlay_teardown).call(this);
|
|
70
|
-
} });
|
|
67
|
+
} }); // a late close event after a quick reopen is ignored
|
|
71
68
|
_CmOverlay_down.set(this, (e) => { __classPrivateFieldSet(this, _CmOverlay_downOnBackdrop, e.target === e.currentTarget, "f"); });
|
|
72
69
|
_CmOverlay_click.set(this, (e) => {
|
|
73
70
|
if (e.target === e.currentTarget && __classPrivateFieldGet(this, _CmOverlay_downOnBackdrop, "f")) {
|
|
@@ -139,7 +136,7 @@ export class CmOverlay extends LitElement {
|
|
|
139
136
|
renderHeader(extra = nothing) {
|
|
140
137
|
return html `<header class="head" part="header">
|
|
141
138
|
<h2 id="${this.uid}-h">${this.heading}</h2>${extra}
|
|
142
|
-
<button type="button" class="x" part="close" aria-label=${this.closeLabel || '
|
|
139
|
+
<button type="button" class="x" part="close" aria-label=${this.closeLabel || this.t('close')} @click=${() => this.requestClose('close-button')}>
|
|
143
140
|
<cm-icon name="close" size="18"></cm-icon></button>
|
|
144
141
|
</header>`;
|
|
145
142
|
}
|
|
@@ -164,9 +161,9 @@ _CmOverlay_opener = new WeakMap(), _CmOverlay_locked = new WeakMap(), _CmOverlay
|
|
|
164
161
|
};
|
|
165
162
|
CmOverlay.properties = {
|
|
166
163
|
open: { type: Boolean, reflect: true },
|
|
167
|
-
heading: {},
|
|
164
|
+
heading: { string: 'heading' },
|
|
168
165
|
label: {},
|
|
169
|
-
closeLabel: { attribute: 'close-label' },
|
|
166
|
+
closeLabel: { attribute: 'close-label', string: 'close' },
|
|
170
167
|
};
|
|
171
168
|
CmOverlay.overlayStyles = [
|
|
172
169
|
base,
|
|
@@ -182,7 +179,7 @@ CmOverlay.overlayStyles = [
|
|
|
182
179
|
overflow: hidden; outline: none;
|
|
183
180
|
}
|
|
184
181
|
.head {
|
|
185
|
-
display: flex; align-items: center; gap: var(--cm-space-10); flex: none; min-height: 40px; padding: 8px
|
|
182
|
+
display: flex; align-items: center; gap: var(--cm-space-10); flex: none; min-height: 40px; padding-block: 8px; padding-inline: 15px 10px;
|
|
186
183
|
background: var(--cm-bg-panel-header); color: var(--cm-text-on-dark);
|
|
187
184
|
}
|
|
188
185
|
.head h2 { flex: 1; min-width: 0; margin: 0; font-size: 14px; line-height: 20px; font-weight: 500; overflow-wrap: anywhere; }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commmerce/ui",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Commmerce UI web components: build apps that feel native to the Commmerce admin. Works in plain HTML, React, Vue and server-rendered pages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web-components",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"files": [
|
|
34
34
|
"dist",
|
|
35
35
|
"LICENSE.md",
|
|
36
|
+
"THIRD_PARTY_NOTICES.txt",
|
|
36
37
|
"README.md"
|
|
37
38
|
],
|
|
38
39
|
"sideEffects": [
|
|
@@ -45,12 +46,12 @@
|
|
|
45
46
|
"access": "public"
|
|
46
47
|
},
|
|
47
48
|
"scripts": {
|
|
48
|
-
"build": "tsc -p tsconfig.json && esbuild src/index.ts --bundle --minify --format=esm --target=es2021 --outfile=dist/cdn.js && cem analyze --litelement --globs \"src/components/*.ts\" --outdir dist",
|
|
49
|
+
"build": "tsc -p tsconfig.json && esbuild src/index.ts --bundle --minify --format=esm --target=es2021 --outfile=dist/cdn.js && cem analyze --litelement --globs \"src/components/*.ts\" \"src/localize.ts\" \"src/overlay.ts\" --outdir dist",
|
|
49
50
|
"test": "vitest run"
|
|
50
51
|
},
|
|
51
52
|
"dependencies": {
|
|
52
|
-
"@commmerce/icons": "1.0.0
|
|
53
|
-
"@commmerce/tokens": "1.0.0
|
|
53
|
+
"@commmerce/icons": "1.0.0",
|
|
54
|
+
"@commmerce/tokens": "1.0.0",
|
|
54
55
|
"lit": "3.2.1"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|