@ongov/ontario-design-system-component-library 4.3.0 → 4.3.1-alpha.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.
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/ontario-accordion_43.cjs.entry.js +404 -223
- package/dist/cjs/ontario-accordion_43.cjs.entry.js.map +1 -1
- package/dist/cjs/ontario-design-system-components.cjs.js +1 -1
- package/dist/collection/components/ontario-card/ontario-card.css +13 -5
- package/dist/collection/components/ontario-card/ontario-card.js +10 -10
- package/dist/collection/components/ontario-card/ontario-card.js.map +1 -1
- package/dist/collection/components/ontario-card/test/ontario-cards.spec.js +1 -1
- package/dist/collection/components/ontario-card/test/ontario-cards.spec.js.map +1 -1
- package/dist/collection/components/ontario-header/ontario-header.js +17 -3
- package/dist/collection/components/ontario-header/ontario-header.js.map +1 -1
- package/dist/collection/components/ontario-header/test/ontario-header.spec.js +3 -3
- package/dist/collection/components/ontario-header/test/ontario-header.spec.js.map +1 -1
- package/dist/collection/components/ontario-language-toggle/ontario-language-toggle.js +193 -47
- package/dist/collection/components/ontario-language-toggle/ontario-language-toggle.js.map +1 -1
- package/dist/collection/components/ontario-language-toggle/test/ontario-language-toggle.spec.js +16 -3
- package/dist/collection/components/ontario-language-toggle/test/ontario-language-toggle.spec.js.map +1 -1
- package/dist/collection/i18n/global.i18n.json +172 -162
- package/dist/collection/utils/common/language-types.js +1 -1
- package/dist/collection/utils/common/language-types.js.map +1 -1
- package/dist/collection/utils/helper/utils-types.js +2 -0
- package/dist/collection/utils/helper/utils-types.js.map +1 -0
- package/dist/collection/utils/helper/utils.js +26 -0
- package/dist/collection/utils/helper/utils.js.map +1 -1
- package/dist/components/global.i18n.js +184 -173
- package/dist/components/global.i18n.js.map +1 -1
- package/dist/components/ontario-card.js +11 -11
- package/dist/components/ontario-card.js.map +1 -1
- package/dist/components/ontario-header.js +11 -3
- package/dist/components/ontario-header.js.map +1 -1
- package/dist/components/ontario-language-toggle2.js +180 -37
- package/dist/components/ontario-language-toggle2.js.map +1 -1
- package/dist/components/utils.js +28 -0
- package/dist/components/utils.js.map +1 -1
- package/dist/esm/loader.js +1 -1
- package/dist/esm/ontario-accordion_43.entry.js +404 -223
- package/dist/esm/ontario-accordion_43.entry.js.map +1 -1
- package/dist/esm/ontario-design-system-components.js +1 -1
- package/dist/ontario-design-system-components/i18n/global.i18n.json +172 -162
- package/dist/ontario-design-system-components/ontario-design-system-components.esm.js +1 -1
- package/dist/ontario-design-system-components/ontario-design-system-components.esm.js.map +1 -1
- package/dist/ontario-design-system-components/{p-cff41424.entry.js → p-abe58ec9.entry.js} +697 -608
- package/dist/ontario-design-system-components/p-abe58ec9.entry.js.map +1 -0
- package/dist/types/components/ontario-header/ontario-header.d.ts +13 -2
- package/dist/types/components/ontario-language-toggle/ontario-language-toggle.d.ts +75 -8
- package/dist/types/components.d.ts +12 -6
- package/dist/types/utils/common/language-types.d.ts +2 -1
- package/dist/types/utils/helper/utils-types.d.ts +2 -0
- package/dist/types/utils/helper/utils.d.ts +21 -0
- package/package.json +3 -3
- package/src/components/ontario-card/ontario-card.scss +13 -6
- package/src/components/ontario-card/ontario-card.tsx +8 -6
- package/src/components/ontario-card/test/ontario-cards.spec.tsx +1 -1
- package/src/components/ontario-header/ontario-header.tsx +13 -4
- package/src/components/ontario-header/test/ontario-header.spec.tsx +3 -3
- package/src/components/ontario-language-toggle/ontario-language-toggle.tsx +192 -45
- package/src/components/ontario-language-toggle/readme.md +10 -10
- package/src/components/ontario-language-toggle/test/__snapshots__/ontario-language-toggle.spec.tsx.snap +1 -1
- package/src/components/ontario-language-toggle/test/ontario-language-toggle.spec.tsx +17 -3
- package/src/components.d.ts +12 -6
- package/src/french.html +37 -0
- package/src/index.html +19 -0
- package/src/translations/global.i18n.json +172 -162
- package/src/utils/common/language-types.ts +2 -1
- package/src/utils/helper/utils-types.ts +2 -0
- package/src/utils/helper/utils.ts +30 -0
- package/www/build/ontario-design-system-components.esm.js +1 -1
- package/www/build/ontario-design-system-components.esm.js.map +1 -1
- package/www/build/{p-cff41424.entry.js → p-abe58ec9.entry.js} +697 -608
- package/www/build/p-abe58ec9.entry.js.map +1 -0
- package/www/build/{p-b61db0f6.js → p-fdc9ab6d.js} +1 -1
- package/www/french.html +37 -0
- package/www/i18n/global.i18n.json +172 -162
- package/www/index.html +21 -2
- package/dist/ontario-design-system-components/p-cff41424.entry.js.map +0 -1
- package/www/build/p-cff41424.entry.js.map +0 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Component, Prop, State, Event, EventEmitter, Watch, h, Fragment } from '@stencil/core';
|
|
2
2
|
|
|
3
|
-
import { Language } from '../../utils/common/language-types';
|
|
4
|
-
import { validateLanguage } from '../../utils/validation/validation-functions';
|
|
3
|
+
import { supportedLanguages, Language } from '../../utils/common/language-types';
|
|
5
4
|
|
|
6
5
|
import { default as translations } from '../../translations/global.i18n.json';
|
|
7
6
|
import { HeaderLanguageToggleEventDetails } from '../../utils/events/common-events.interface';
|
|
7
|
+
import { validateValueAgainstArray } from '../../utils/validation/validation-functions';
|
|
8
|
+
import { ConsoleMessageClass } from '../../utils/console-message/console-message';
|
|
9
|
+
import { printArray, getRootHTMLElement } from '../../utils/helper/utils';
|
|
8
10
|
|
|
9
11
|
@Component({
|
|
10
12
|
tag: 'ontario-language-toggle',
|
|
@@ -12,7 +14,14 @@ import { HeaderLanguageToggleEventDetails } from '../../utils/events/common-even
|
|
|
12
14
|
shadow: true,
|
|
13
15
|
})
|
|
14
16
|
export class OntarioLanguageToggle {
|
|
15
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The language of the component.
|
|
19
|
+
*
|
|
20
|
+
* In most cases, the language toggle should be the source of truth for determining the site language.
|
|
21
|
+
*
|
|
22
|
+
* Only pass a language value here if necessary.
|
|
23
|
+
*/
|
|
24
|
+
@Prop({ mutable: true }) language?: Language;
|
|
16
25
|
|
|
17
26
|
/**
|
|
18
27
|
* The size of the language toggle button.
|
|
@@ -38,89 +47,227 @@ export class OntarioLanguageToggle {
|
|
|
38
47
|
@State() translations: any = translations;
|
|
39
48
|
|
|
40
49
|
/**
|
|
41
|
-
*
|
|
50
|
+
* Internal state used as the source of truth for component language.
|
|
51
|
+
*/
|
|
52
|
+
@State() private languageState: Language;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Internal state used to render the text on the language toggle UI.
|
|
56
|
+
*/
|
|
57
|
+
@State() private oppositeLanguageLabel: { fullWord: string; abbreviation: Language } | undefined = undefined;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Updates the language and languageState props when changes to the language prop are detected.
|
|
42
61
|
*/
|
|
43
|
-
@
|
|
44
|
-
|
|
45
|
-
let lang: string | Language;
|
|
62
|
+
@Watch('language')
|
|
63
|
+
updateLanguage() {
|
|
46
64
|
if (this.language) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
65
|
+
if (!validateValueAgainstArray(this.language, supportedLanguages)) {
|
|
66
|
+
this.showLanguageWarning(this.language);
|
|
67
|
+
this.language = this.translations.siteLanguage.abbreviation.en as Language;
|
|
68
|
+
}
|
|
69
|
+
this.languageState = this.language;
|
|
52
70
|
}
|
|
71
|
+
this.setAppLanguageHandler();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Event that fires during the setAppLanguageHandler() method.
|
|
76
|
+
*
|
|
77
|
+
* The event contains the current language (after language logic has already occurred).
|
|
78
|
+
*/
|
|
79
|
+
@Event() setAppLanguage: EventEmitter<Language>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Event that fires when the language toggle is pressed/clicked.
|
|
83
|
+
*
|
|
84
|
+
* The event contains the oldLanguage along with the newLanguage.
|
|
85
|
+
*/
|
|
86
|
+
@Event() headerLanguageToggled: EventEmitter<HeaderLanguageToggleEventDetails>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* This function sets the languageState (if not already set).
|
|
90
|
+
*
|
|
91
|
+
* It also emits the setAppLanguage() event, updates the component language label, and
|
|
92
|
+
* updates the <html> tag lang attribute with the languageState value.
|
|
93
|
+
*
|
|
94
|
+
* It gets called by the connectedCallback() component lifecycle hook, and by the
|
|
95
|
+
* updateLanguage() method, which is fired on the watch for the language prop.
|
|
96
|
+
*/
|
|
97
|
+
private setAppLanguageHandler() {
|
|
98
|
+
const defaultLang = this.translations.siteLanguage.abbreviation.en;
|
|
99
|
+
const rootLang = getRootHTMLElement()?.lang;
|
|
100
|
+
|
|
101
|
+
// If languageState is not set, set it equal to the following cadence:
|
|
102
|
+
// language prop value, <html> tag lang attribute, or default to "en"
|
|
103
|
+
if (!this.languageState) {
|
|
104
|
+
if (this.language) {
|
|
105
|
+
this.languageState = this.language;
|
|
106
|
+
} else if (rootLang) {
|
|
107
|
+
if (validateValueAgainstArray(rootLang, supportedLanguages)) {
|
|
108
|
+
this.languageState = rootLang as Language;
|
|
109
|
+
} else {
|
|
110
|
+
this.showLanguageWarning(rootLang, 'document');
|
|
111
|
+
this.languageState = defaultLang;
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
this.languageState = defaultLang;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.setAppLanguage.emit(this.languageState);
|
|
53
119
|
|
|
54
|
-
this.
|
|
55
|
-
|
|
120
|
+
this.oppositeLanguageLabel = {
|
|
121
|
+
fullWord: this.getOppositeLanguageFullWord(),
|
|
122
|
+
abbreviation: this.getOppositeLanguageAbbrievation(),
|
|
123
|
+
};
|
|
56
124
|
|
|
57
|
-
this.updateHTMLLang(
|
|
125
|
+
this.updateHTMLLang();
|
|
58
126
|
}
|
|
59
127
|
|
|
60
128
|
/**
|
|
61
129
|
* An event that emits to other components that the language toggle button has been toggled.
|
|
130
|
+
*
|
|
131
|
+
* @param {Language} oldLanguage - The language prior to the language toggle being pressed.
|
|
132
|
+
* @param {globalThis.Event} event - event that triggered the function (e.g. onclick).
|
|
62
133
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
134
|
+
private handleHeaderLanguageToggled(oldLanguage: Language, event?: globalThis.Event) {
|
|
135
|
+
this.languageState =
|
|
136
|
+
oldLanguage === this.translations.siteLanguage.abbreviation.en
|
|
137
|
+
? this.translations.siteLanguage.abbreviation.fr
|
|
138
|
+
: this.translations.siteLanguage.abbreviation.en;
|
|
67
139
|
|
|
68
|
-
this.headerLanguageToggled.emit({ oldLanguage:
|
|
140
|
+
this.headerLanguageToggled.emit({ oldLanguage: oldLanguage, newLanguage: this.languageState });
|
|
69
141
|
|
|
70
|
-
this.updateHTMLLang(
|
|
142
|
+
this.updateHTMLLang();
|
|
71
143
|
|
|
72
144
|
if (this.customLanguageToggle && event) {
|
|
73
145
|
this.customLanguageToggle(event);
|
|
74
146
|
}
|
|
75
147
|
}
|
|
76
148
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
149
|
+
/**
|
|
150
|
+
* Prints a warning message to the console about using an incorrect language for the component.
|
|
151
|
+
*
|
|
152
|
+
* @param {string} lang - The incorrect language that was received.
|
|
153
|
+
* @param {string} type - prop/document | Where the incorrect language is coming from.
|
|
154
|
+
*/
|
|
155
|
+
private showLanguageWarning(lang: string, type: 'prop' | 'document' = 'prop') {
|
|
156
|
+
const propOrDocumentMessage =
|
|
157
|
+
type === 'prop' ? `The language prop value of ${lang} ` : `The HTML document lang attribute value of ${lang} `;
|
|
158
|
+
const message = new ConsoleMessageClass();
|
|
159
|
+
message
|
|
160
|
+
.addDesignSystemTag()
|
|
161
|
+
.addRegularText(propOrDocumentMessage)
|
|
162
|
+
.addRegularText('is not a valid language value for the ')
|
|
163
|
+
.addMonospaceText(' <ontario-language-toggle> ')
|
|
164
|
+
.addRegularText(`component. Valid language values are ${printArray([...supportedLanguages])}. `)
|
|
165
|
+
.addRegularText(`A default language value of ${this.translations.siteLanguage.abbreviation.en} will be applied.`)
|
|
166
|
+
.printMessage();
|
|
81
167
|
}
|
|
82
168
|
|
|
83
|
-
|
|
84
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Returns abbreviated text for the opposite language.
|
|
171
|
+
*
|
|
172
|
+
* @returns {Language}
|
|
173
|
+
*/
|
|
174
|
+
private getOppositeLanguageAbbrievation(): Language {
|
|
175
|
+
return this.languageState === this.translations.siteLanguage.abbreviation.en
|
|
176
|
+
? this.translations.siteLanguage.abbreviation.fr
|
|
177
|
+
: this.translations.siteLanguage.abbreviation.en;
|
|
178
|
+
}
|
|
85
179
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Returns full word text for the opposite language.
|
|
182
|
+
*
|
|
183
|
+
* @returns {string}
|
|
184
|
+
*/
|
|
185
|
+
private getOppositeLanguageFullWord(): string {
|
|
186
|
+
return this.languageState === this.translations.siteLanguage.abbreviation.en
|
|
187
|
+
? this.translations.siteLanguage.fullWord.fr
|
|
188
|
+
: this.translations.siteLanguage.fullWord.en;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/*
|
|
192
|
+
* Updates the <html> lang attribute based on component languageState.
|
|
193
|
+
*/
|
|
194
|
+
private updateHTMLLang = () => {
|
|
195
|
+
const htmlElement = getRootHTMLElement();
|
|
93
196
|
|
|
94
|
-
|
|
197
|
+
if (htmlElement) {
|
|
198
|
+
htmlElement.setAttribute('lang', this.languageState);
|
|
199
|
+
}
|
|
95
200
|
};
|
|
96
201
|
|
|
202
|
+
/**
|
|
203
|
+
* Component life cycle hook.
|
|
204
|
+
*
|
|
205
|
+
* https://stenciljs.com/docs/component-lifecycle#connectedcallback
|
|
206
|
+
*/
|
|
97
207
|
connectedCallback() {
|
|
98
|
-
this.
|
|
208
|
+
this.setAppLanguageHandler();
|
|
99
209
|
}
|
|
100
210
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
211
|
+
/**
|
|
212
|
+
* Component life cycle hook.
|
|
213
|
+
*
|
|
214
|
+
* https://stenciljs.com/docs/component-lifecycle#componentdidload
|
|
215
|
+
*/
|
|
216
|
+
componentDidLoad() {
|
|
217
|
+
/**
|
|
218
|
+
* Creates a MutationObserver (a type of watch) on the <html> tag lang attribute.
|
|
219
|
+
*
|
|
220
|
+
* When changes occur, the oppositeLanguageLabel state variable regenerates.
|
|
221
|
+
*
|
|
222
|
+
* This is to act as a form of callback and create a subtle delay between page content
|
|
223
|
+
* updating and the language toggle label updating.
|
|
224
|
+
*/
|
|
225
|
+
const observer = new MutationObserver((mutations) => {
|
|
226
|
+
mutations.forEach((mutation) => {
|
|
227
|
+
switch (mutation.type) {
|
|
228
|
+
case 'attributes':
|
|
229
|
+
switch (mutation.attributeName) {
|
|
230
|
+
case 'lang':
|
|
231
|
+
this.oppositeLanguageLabel = {
|
|
232
|
+
fullWord: this.getOppositeLanguageFullWord(),
|
|
233
|
+
abbreviation: this.getOppositeLanguageAbbrievation(),
|
|
234
|
+
};
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
});
|
|
104
241
|
|
|
242
|
+
// Only create/trigger the MutationObserver if the <html> element exists.
|
|
243
|
+
if (getRootHTMLElement()) {
|
|
244
|
+
const options = { attributes: true };
|
|
245
|
+
observer.observe(getRootHTMLElement(), options);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
render() {
|
|
105
250
|
return (
|
|
106
251
|
<a
|
|
252
|
+
aria-label={this.translations.languageToggle.ariaLabel[`${this.oppositeLanguageLabel?.abbreviation}`]}
|
|
107
253
|
class={
|
|
108
254
|
this.size === 'default'
|
|
109
255
|
? 'ontario-language-toggler ontario-language-toggler--default'
|
|
110
256
|
: 'ontario-language-toggler ontario-language-toggler--small'
|
|
111
257
|
}
|
|
112
258
|
href={this.url ? this.url : '#'}
|
|
113
|
-
|
|
114
|
-
|
|
259
|
+
hreflang={this.oppositeLanguageLabel?.abbreviation}
|
|
260
|
+
lang={this.oppositeLanguageLabel?.abbreviation}
|
|
261
|
+
onClick={(e) => this.handleHeaderLanguageToggled(this.languageState, e)}
|
|
115
262
|
>
|
|
116
263
|
{this.size === 'small' ? (
|
|
117
|
-
<span>{
|
|
264
|
+
<span>{this.oppositeLanguageLabel?.fullWord}</span>
|
|
118
265
|
) : (
|
|
119
266
|
<Fragment>
|
|
120
|
-
<abbr title={
|
|
121
|
-
{
|
|
267
|
+
<abbr title={this.oppositeLanguageLabel?.fullWord} class="ontario-show-for-small-only">
|
|
268
|
+
{this.oppositeLanguageLabel?.abbreviation.toUpperCase()}
|
|
122
269
|
</abbr>
|
|
123
|
-
<span class="ontario-show-for-medium">{
|
|
270
|
+
<span class="ontario-show-for-medium">{this.oppositeLanguageLabel?.fullWord}</span>
|
|
124
271
|
</Fragment>
|
|
125
272
|
)}
|
|
126
273
|
</a>
|
|
@@ -10,19 +10,19 @@ It is used in the ontario-header component.
|
|
|
10
10
|
|
|
11
11
|
## Properties
|
|
12
12
|
|
|
13
|
-
| Property | Attribute | Description
|
|
14
|
-
| ---------------------- | ---------- |
|
|
15
|
-
| `customLanguageToggle` | -- | A custom function to pass to the language toggle button. This is optional.
|
|
16
|
-
| `language` | `language` |
|
|
17
|
-
| `size` | `size` | The size of the language toggle button. If no prop is passed, it will be set to the `default` size.
|
|
18
|
-
| `url` | `url` | The URL to change to when the language toggle button is clicked. This is optional.
|
|
13
|
+
| Property | Attribute | Description | Type | Default |
|
|
14
|
+
| ---------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------- | ----------- |
|
|
15
|
+
| `customLanguageToggle` | -- | A custom function to pass to the language toggle button. This is optional. | `((event: Event) => void) \| undefined` | `undefined` |
|
|
16
|
+
| `language` | `language` | The language of the component. In most cases, the language toggle should be the source of truth for determining the site language. Only pass a language value here if necessary. | `"en" \| "fr" \| undefined` | `undefined` |
|
|
17
|
+
| `size` | `size` | The size of the language toggle button. If no prop is passed, it will be set to the `default` size. | `"default" \| "small" \| undefined` | `'default'` |
|
|
18
|
+
| `url` | `url` | The URL to change to when the language toggle button is clicked. This is optional. | `string \| undefined` | `undefined` |
|
|
19
19
|
|
|
20
20
|
## Events
|
|
21
21
|
|
|
22
|
-
| Event | Description
|
|
23
|
-
| ----------------------- |
|
|
24
|
-
| `headerLanguageToggled` |
|
|
25
|
-
| `setAppLanguage` |
|
|
22
|
+
| Event | Description | Type |
|
|
23
|
+
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------- |
|
|
24
|
+
| `headerLanguageToggled` | Event that fires when the language toggle is pressed/clicked. The event contains the oldLanguage along with the newLanguage. | `CustomEvent<HeaderLanguageToggleEventDetails>` |
|
|
25
|
+
| `setAppLanguage` | Event that fires during the setAppLanguageHandler() method. The event contains the current language (after language logic has already occurred). | `CustomEvent<"en" \| "fr">` |
|
|
26
26
|
|
|
27
27
|
## Dependencies
|
|
28
28
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
exports[`ontario-language-toggle snapshot should render the expected html 1`] = `
|
|
4
4
|
<ontario-language-toggle>
|
|
5
5
|
<mock:shadow-root>
|
|
6
|
-
<a aria-label="
|
|
6
|
+
<a aria-label="Cliquez pour changer la langue en français" class="ontario-language-toggler ontario-language-toggler--default" href="#" hreflang="fr" lang="fr">
|
|
7
7
|
<abbr class="ontario-show-for-small-only" title="Français">
|
|
8
8
|
FR
|
|
9
9
|
</abbr>
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { newSpecPage } from '@stencil/core/testing';
|
|
2
2
|
import { OntarioLanguageToggle } from '../ontario-language-toggle';
|
|
3
|
+
import { mutationObserverMock } from '../../../utils/tests/mutation-observer.mock';
|
|
4
|
+
|
|
5
|
+
global.MutationObserver = mutationObserverMock;
|
|
3
6
|
|
|
4
7
|
describe('ontario-language-toggle', () => {
|
|
5
8
|
describe('snapshot', () => {
|
|
@@ -22,7 +25,7 @@ describe('ontario-language-toggle', () => {
|
|
|
22
25
|
expect(page.root).toEqualHtml(`
|
|
23
26
|
<ontario-language-toggle>
|
|
24
27
|
<mock:shadow-root>
|
|
25
|
-
<a aria-label="
|
|
28
|
+
<a aria-label="Cliquez pour changer la langue en français" class="ontario-language-toggler ontario-language-toggler--default" href="#" hreflang="fr" lang="fr">
|
|
26
29
|
<abbr class="ontario-show-for-small-only" title="Français">
|
|
27
30
|
FR
|
|
28
31
|
</abbr>
|
|
@@ -36,7 +39,18 @@ describe('ontario-language-toggle', () => {
|
|
|
36
39
|
expect(page.rootInstance.size).toBe('default');
|
|
37
40
|
});
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
/**
|
|
43
|
+
* TODO: Determine if this is testable.
|
|
44
|
+
*
|
|
45
|
+
* Not sure how to properly test against this, seems to be a race condition.
|
|
46
|
+
*
|
|
47
|
+
* Takes a few processes before the html lang attribute is updated.
|
|
48
|
+
*
|
|
49
|
+
* Using setTimeout() leads to a false positive.
|
|
50
|
+
*
|
|
51
|
+
* Using page.waitForChanges() or autoApplyChanges: true seem to have no effect.
|
|
52
|
+
*/
|
|
53
|
+
it.skip('should render a default language of English on the <html> tag if no language prop is passed', async () => {
|
|
40
54
|
const page = await newSpecPage({
|
|
41
55
|
components: [OntarioLanguageToggle],
|
|
42
56
|
html: `<ontario-language-toggle></ontario-language-toggle>`,
|
|
@@ -54,7 +68,7 @@ describe('ontario-language-toggle', () => {
|
|
|
54
68
|
expect(page.root).toEqualHtml(`
|
|
55
69
|
<ontario-language-toggle size="small" url="/en" language="fr">
|
|
56
70
|
<mock:shadow-root>
|
|
57
|
-
<a aria-label="
|
|
71
|
+
<a aria-label="Click to switch the language to English" class="ontario-language-toggler ontario-language-toggler--small" href="/en" hreflang="en" lang="en">
|
|
58
72
|
<span>
|
|
59
73
|
English
|
|
60
74
|
</span>
|
package/src/components.d.ts
CHANGED
|
@@ -1725,7 +1725,10 @@ export namespace Components {
|
|
|
1725
1725
|
* A custom function to pass to the language toggle button. This is optional.
|
|
1726
1726
|
*/
|
|
1727
1727
|
customLanguageToggle?: (event: globalThis.Event) => void;
|
|
1728
|
-
|
|
1728
|
+
/**
|
|
1729
|
+
* The language of the component. In most cases, the language toggle should be the source of truth for determining the site language. Only pass a language value here if necessary.
|
|
1730
|
+
*/
|
|
1731
|
+
language?: Language;
|
|
1729
1732
|
/**
|
|
1730
1733
|
* The size of the language toggle button. If no prop is passed, it will be set to the `default` size.
|
|
1731
1734
|
*/
|
|
@@ -2893,7 +2896,7 @@ declare global {
|
|
|
2893
2896
|
new (): HTMLOntarioInputElement;
|
|
2894
2897
|
};
|
|
2895
2898
|
interface HTMLOntarioLanguageToggleElementEventMap {
|
|
2896
|
-
setAppLanguage:
|
|
2899
|
+
setAppLanguage: Language;
|
|
2897
2900
|
headerLanguageToggled: HeaderLanguageToggleEventDetails;
|
|
2898
2901
|
}
|
|
2899
2902
|
interface HTMLOntarioLanguageToggleElement extends Components.OntarioLanguageToggle, HTMLStencilElement {
|
|
@@ -4923,15 +4926,18 @@ declare namespace LocalJSX {
|
|
|
4923
4926
|
* A custom function to pass to the language toggle button. This is optional.
|
|
4924
4927
|
*/
|
|
4925
4928
|
customLanguageToggle?: (event: globalThis.Event) => void;
|
|
4926
|
-
language?: Language | string;
|
|
4927
4929
|
/**
|
|
4928
|
-
*
|
|
4930
|
+
* The language of the component. In most cases, the language toggle should be the source of truth for determining the site language. Only pass a language value here if necessary.
|
|
4931
|
+
*/
|
|
4932
|
+
language?: Language;
|
|
4933
|
+
/**
|
|
4934
|
+
* Event that fires when the language toggle is pressed/clicked. The event contains the oldLanguage along with the newLanguage.
|
|
4929
4935
|
*/
|
|
4930
4936
|
onHeaderLanguageToggled?: (event: OntarioLanguageToggleCustomEvent<HeaderLanguageToggleEventDetails>) => void;
|
|
4931
4937
|
/**
|
|
4932
|
-
*
|
|
4938
|
+
* Event that fires during the setAppLanguageHandler() method. The event contains the current language (after language logic has already occurred).
|
|
4933
4939
|
*/
|
|
4934
|
-
onSetAppLanguage?: (event: OntarioLanguageToggleCustomEvent<
|
|
4940
|
+
onSetAppLanguage?: (event: OntarioLanguageToggleCustomEvent<Language>) => void;
|
|
4935
4941
|
/**
|
|
4936
4942
|
* The size of the language toggle button. If no prop is passed, it will be set to the `default` size.
|
|
4937
4943
|
*/
|
package/src/french.html
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html dir="ltr" lang="fr">
|
|
3
|
+
<head>
|
|
4
|
+
<link rel="stylesheet" href="/build/ontario-design-system-components.css" />
|
|
5
|
+
<meta charset="utf-8" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" />
|
|
7
|
+
<title>Ontario Design System - component samples</title>
|
|
8
|
+
<script type="module" src="/build/ontario-design-system-components.esm.js"></script>
|
|
9
|
+
<script nomodule src="/build/ontario-design-system-components.js"></script>
|
|
10
|
+
</head>
|
|
11
|
+
|
|
12
|
+
<body>
|
|
13
|
+
<ontario-header
|
|
14
|
+
id="ontarioHeader"
|
|
15
|
+
type="serviceOntario"
|
|
16
|
+
application-header-info='{
|
|
17
|
+
"title": "Application name",
|
|
18
|
+
"href": "/application-homepage",
|
|
19
|
+
"maxSubheaderDesktopLinks": "3",
|
|
20
|
+
"maxSubheaderTabletLinks": "2",
|
|
21
|
+
"maxSubheaderMobileLinks": "1"
|
|
22
|
+
}'
|
|
23
|
+
language-toggle-options='{
|
|
24
|
+
"englishLink": "/index.html",
|
|
25
|
+
"frenchLink" : "/french.html"
|
|
26
|
+
}'
|
|
27
|
+
>
|
|
28
|
+
</ontario-header>
|
|
29
|
+
|
|
30
|
+
<script>
|
|
31
|
+
function activateHeaderType(headerType) {
|
|
32
|
+
const ontarioHeaderElement = document.querySelector('ontario-header');
|
|
33
|
+
ontarioHeaderElement.type = headerType;
|
|
34
|
+
}
|
|
35
|
+
</script>
|
|
36
|
+
</body>
|
|
37
|
+
</html>
|
package/src/index.html
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
<a href="https://designsystem.ontario.ca/">Ontario Design System</a> web components now available!!!
|
|
15
15
|
</ontario-critical-alert>
|
|
16
16
|
<ontario-header
|
|
17
|
+
id="ontarioHeader"
|
|
17
18
|
type="serviceOntario"
|
|
18
19
|
application-header-info='{
|
|
19
20
|
"title": "Application name",
|
|
@@ -22,6 +23,10 @@
|
|
|
22
23
|
"maxSubheaderTabletLinks": "2",
|
|
23
24
|
"maxSubheaderMobileLinks": "1"
|
|
24
25
|
}'
|
|
26
|
+
language-toggle-options='{
|
|
27
|
+
"englishLink": "/",
|
|
28
|
+
"frenchLink" : "/french.html"
|
|
29
|
+
}'
|
|
25
30
|
>
|
|
26
31
|
</ontario-header>
|
|
27
32
|
|
|
@@ -1203,6 +1208,20 @@
|
|
|
1203
1208
|
<ontario-back-to-top />
|
|
1204
1209
|
|
|
1205
1210
|
<script>
|
|
1211
|
+
const changeLanguage = (e) => {
|
|
1212
|
+
e.preventDefault();
|
|
1213
|
+
let header = document.getElementById('ontarioHeader');
|
|
1214
|
+
header.customLanguageToggle = () => {
|
|
1215
|
+
setTimeout(() => {
|
|
1216
|
+
window.location.href = '/french.html';
|
|
1217
|
+
console.log(header);
|
|
1218
|
+
console.log('Lang switch');
|
|
1219
|
+
}, 5000);
|
|
1220
|
+
};
|
|
1221
|
+
};
|
|
1222
|
+
|
|
1223
|
+
window.addEventListener('load', changeLanguage);
|
|
1224
|
+
|
|
1206
1225
|
function testFunction() {
|
|
1207
1226
|
alert('HELLO WORLD!');
|
|
1208
1227
|
}
|