@easygov/web-components 0.10.0 → 0.12.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 +160 -161
- package/dist/chunk-ARCC34L3.js +27 -0
- package/dist/chunk-ARCC34L3.js.map +1 -0
- package/dist/{chunk-JFHEJ4IX.js → chunk-STIOWK2O.js} +2 -2
- package/dist/{chunk-JFHEJ4IX.js.map → chunk-STIOWK2O.js.map} +1 -1
- package/dist/footer.js +1 -1
- package/dist/header.js +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
- package/dist/chunk-KQKWV6UI.js +0 -27
- package/dist/chunk-KQKWV6UI.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
# @easygov/web-components
|
|
2
2
|
|
|
3
|
-
Framework-agnostic custom elements for EasyGov
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
HTML/JS, etc.
|
|
8
|
-
|
|
9
|
-
Each bundle inlines React, ReactDOM, and its own copy of `@easygov/react-components`, so there is
|
|
10
|
-
nothing else to install and no risk of a version clash with an app that happens to use React for
|
|
11
|
-
something else.
|
|
3
|
+
Framework-agnostic custom elements for EasyGov: the EasyGov header and footer for apps that are
|
|
4
|
+
not written in React (Angular, Vue, plain HTML/JS, …). Each bundle contains React and the
|
|
5
|
+
components, so there is nothing else to install and no conflict with a React version your app
|
|
6
|
+
may use.
|
|
12
7
|
|
|
13
8
|
## Install
|
|
14
9
|
|
|
@@ -16,46 +11,38 @@ something else.
|
|
|
16
11
|
npm install @easygov/web-components
|
|
17
12
|
```
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
React host app about a duplicate copy; you never need to satisfy them yourself.)
|
|
14
|
+
The `react`/`react-dom` peer dependencies only exist so npm doesn't warn React host apps; you
|
|
15
|
+
don't need to install them.
|
|
22
16
|
|
|
23
17
|
## Usage
|
|
24
18
|
|
|
25
|
-
|
|
19
|
+
Importing a subpath registers its element:
|
|
26
20
|
|
|
27
21
|
```ts
|
|
28
|
-
import "@easygov/web-components/header"; //
|
|
29
|
-
import "@easygov/web-components/footer"; //
|
|
22
|
+
import "@easygov/web-components/header"; // <eg-easygov-header>
|
|
23
|
+
import "@easygov/web-components/footer"; // <eg-easygov-footer>
|
|
30
24
|
```
|
|
31
25
|
|
|
32
26
|
```html
|
|
33
27
|
<eg-easygov-header></eg-easygov-header>
|
|
34
|
-
|
|
35
28
|
<main>...</main>
|
|
36
|
-
|
|
37
29
|
<eg-easygov-footer></eg-easygov-footer>
|
|
38
30
|
```
|
|
39
31
|
|
|
40
|
-
Import only
|
|
41
|
-
|
|
42
|
-
itself), so it's fetched once regardless of how many elements you import.
|
|
32
|
+
Import only what you need; both share one common chunk that is loaded once. Neither element has
|
|
33
|
+
attributes or properties.
|
|
43
34
|
|
|
44
|
-
|
|
45
|
-
automatically — no separate stylesheet import required, unlike `@easygov/react-components`.
|
|
46
|
-
The font is the exception, see [Font](#font).
|
|
35
|
+
### Styling
|
|
47
36
|
|
|
48
|
-
|
|
37
|
+
Each element renders into an open shadow root with its styles included. Host page CSS does not
|
|
38
|
+
reach inside, and that includes the color context classes: the header is Bright, the footer Dark.
|
|
49
39
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
host the unmodified `ClashGrotesk-Variable.woff2` in your app, and declare it in the **host
|
|
57
|
-
page's** CSS (font faces declared in the page are available inside the shadow roots) with
|
|
58
|
-
exactly this family name:
|
|
40
|
+
**Font:** the elements use `"Clash Grotesk Variable"` (Clash Grotesk, Indian Type Foundry). Its
|
|
41
|
+
licence (ITF FFL v2.0) forbids redistributing it, so **this package ships no font**; without it the
|
|
42
|
+
elements fall back to the system font. Download it from
|
|
43
|
+
[Fontshare](https://www.fontshare.com/fonts/clash-grotesk), host the unmodified
|
|
44
|
+
`ClashGrotesk-Variable.woff2` and declare it in the **host page's** CSS (page font faces apply
|
|
45
|
+
inside shadow roots):
|
|
59
46
|
|
|
60
47
|
```css
|
|
61
48
|
@font-face {
|
|
@@ -66,185 +53,197 @@ exactly this family name:
|
|
|
66
53
|
}
|
|
67
54
|
```
|
|
68
55
|
|
|
69
|
-
|
|
56
|
+
### Configuration
|
|
70
57
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
58
|
+
The header derives the Core, Info and Keycloak URLs from the page's hostname by replacing its
|
|
59
|
+
first label: on `fach-xyz.in.easygov.swiss` it uses `core.in.easygov.swiss`,
|
|
60
|
+
`info.in.easygov.swiss` and `auth.in.easygov.swiss/auth/realms/easygov`. There is nothing to
|
|
61
|
+
configure: the published bundle contains no URLs. On a host without that structure (e.g.
|
|
62
|
+
`localhost`) the header runs without login and makes no API calls. If you need other URLs, use
|
|
63
|
+
`@easygov/react-components`, where they are set at your app's build time.
|
|
76
64
|
|
|
77
|
-
|
|
78
|
-
`.eg-context-dark`: content inside a container with one of these classes recolors to that context,
|
|
79
|
-
the innermost class wins when they nest, and Bright is the default. The elements set their
|
|
80
|
-
contexts themselves inside their shadow roots (the header is Bright, the footer Dark with a Bright
|
|
81
|
-
partner band); a context class in the host page does not reach into a shadow root and has no
|
|
82
|
-
effect on them. Popups (such as the header's language and navigation menus) render in a portal and
|
|
83
|
-
always use the default (bright) context.
|
|
65
|
+
## `<eg-easygov-header>`
|
|
84
66
|
|
|
85
|
-
|
|
67
|
+
Login, customer account, display language and environment badge are handled internally. The
|
|
68
|
+
header reports to your app through DOM events. All of them bubble and are `composed`, so you can
|
|
69
|
+
listen on the element or on an ancestor.
|
|
86
70
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
No props are mapped, and none of the header's callback props (`onLanguageChange`, `onNavigate`,
|
|
94
|
-
`onContextChange`, `onLogout`) exist here either — function props can't cross the custom-element
|
|
95
|
-
boundary. Listen for the corresponding `CustomEvent`s instead:
|
|
71
|
+
| Event | `detail` | Fired when |
|
|
72
|
+
| ---------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
73
|
+
| `contextchange` | `{ contextJwt, claims, contentChanged }` | A new context token arrives: after sign-in, after every URL path change and on every renewal (about every 4 minutes). |
|
|
74
|
+
| `sessionchange` | `{ user: { sub, name } \| null }` | The signed-in user changes: once after load if a session exists, on login, and on logout (`user: null`). |
|
|
75
|
+
| `languagechange` | `{ locale }` (`'de' \| 'en' \| 'fr' \| 'it'`) | Once after mount and whenever the display language changes. The header also sets `<html lang>`. |
|
|
76
|
+
| `navigate` | `{ path }` | The user clicks a header link (`path` is a Core path such as `/dashboard`). Informational: the browser follows the link anyway. |
|
|
96
77
|
|
|
97
78
|
```js
|
|
98
79
|
const header = document.querySelector("eg-easygov-header");
|
|
99
80
|
|
|
100
|
-
header.addEventListener("languagechange", (e) => {
|
|
101
|
-
console.log(e.detail.locale); // 'de' | 'en' | 'fr' | 'it'
|
|
102
|
-
});
|
|
103
|
-
header.addEventListener("navigate", (e) => {
|
|
104
|
-
console.log(e.detail.path);
|
|
105
|
-
});
|
|
106
81
|
header.addEventListener("contextchange", (e) => {
|
|
107
|
-
const { contextJwt, claims } = e.detail;
|
|
82
|
+
const { contextJwt, claims, contentChanged } = e.detail;
|
|
83
|
+
latestContextJwt = contextJwt; // always keep the newest token
|
|
84
|
+
if (contentChanged) prefillForm(claims); // only on a real change
|
|
108
85
|
});
|
|
109
86
|
header.addEventListener("sessionchange", (e) => {
|
|
110
|
-
|
|
87
|
+
if (e.detail.user === null) clearUserData();
|
|
111
88
|
});
|
|
89
|
+
header.addEventListener("languagechange", (e) =>
|
|
90
|
+
setAppLanguage(e.detail.locale),
|
|
91
|
+
);
|
|
112
92
|
```
|
|
113
93
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
94
|
+
### `contextchange` in detail
|
|
95
|
+
|
|
96
|
+
On every URL path change (client-side navigation included; query string and fragment don't count)
|
|
97
|
+
the header sends the screen URL to core-backend. Core resolves the signed-in user, the selected
|
|
98
|
+
customer account, the ServiceApp of the screen and the selected process and returns them as a
|
|
99
|
+
signed JWT, `contextJwt`. `claims` is its decoded payload:
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"currentUser": {
|
|
104
|
+
"id": "6b1f3c2e-8a7d-4e0b-9c5a-2f4d8e1a7b90",
|
|
105
|
+
"eiamIdentity": "CH-1234-5678-9012",
|
|
106
|
+
"eiamSSOsessionID": "a1b2c3d4e5",
|
|
107
|
+
"qoA": 30,
|
|
108
|
+
"gtcAccepted": true,
|
|
109
|
+
"registerDate": "2025-03-14T08:30:00Z",
|
|
110
|
+
"idpUsed": "externalIDP\\AgovIDP",
|
|
111
|
+
"emailAddress": "anna.muster@example.ch",
|
|
112
|
+
"firstName": "Anna",
|
|
113
|
+
"lastName": "Muster",
|
|
114
|
+
"roles": []
|
|
115
|
+
},
|
|
116
|
+
"customerAccount": {
|
|
117
|
+
"id": "acc-4711",
|
|
118
|
+
"accountType": "ORGANIZATION",
|
|
119
|
+
"label": "Muster AG",
|
|
120
|
+
"status": "VERIFIED",
|
|
121
|
+
"legalForm": "AG",
|
|
122
|
+
"uid": "CHE-123.456.789",
|
|
123
|
+
"managementRole": {
|
|
124
|
+
"roleType": "AGENT",
|
|
125
|
+
"servicePermission": {
|
|
126
|
+
"serviceAppId": "app-42",
|
|
127
|
+
"permissionLevel": "EDIT"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
"serviceApp": {
|
|
132
|
+
"id": "app-42",
|
|
133
|
+
"businessModuleId": "urn_core_businessmodule_example",
|
|
134
|
+
"individualServicePermissionModel": false
|
|
135
|
+
},
|
|
136
|
+
"process": {
|
|
137
|
+
"id": "proc-0815",
|
|
138
|
+
"status": "REQUEST_IN_PROGRESS",
|
|
139
|
+
"serviceAppId": "app-42"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
117
143
|
|
|
118
|
-
|
|
144
|
+
**Why the event repeats:** `contextJwt` is short-lived (5 minutes by default), so the header fetches
|
|
145
|
+
a new one about 60 seconds before it expires and fires `contextchange` again. `contentChanged` is
|
|
146
|
+
`true` for the first context after sign-in and whenever `claims` differs from the previous event,
|
|
147
|
+
and `false` for a pure renewal. Always store the newest `contextJwt`. Prefill only when
|
|
148
|
+
`contentChanged` is `true`, or every renewal overwrites what the user typed. A failed renewal is
|
|
149
|
+
retried every 15 seconds; the header shows an error dialog only when the token expires or core
|
|
150
|
+
refuses for good. On logout no `contextchange` fires; use `sessionchange`.
|
|
119
151
|
|
|
120
|
-
|
|
152
|
+
**The claims are decoded, not verified.** Use them for prefilling and rendering. A backend
|
|
153
|
+
receiving `contextJwt` must verify it against core-backend's published JWKS.
|
|
121
154
|
|
|
122
|
-
|
|
155
|
+
## `<eg-easygov-footer>`
|
|
123
156
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
157
|
+
Static content: no attributes, no events.
|
|
158
|
+
|
|
159
|
+
## JavaScript API
|
|
160
|
+
|
|
161
|
+
The package root exports:
|
|
127
162
|
|
|
128
163
|
```ts
|
|
129
164
|
import {
|
|
130
165
|
getDisplayLanguage,
|
|
131
166
|
setDisplayLanguage,
|
|
132
167
|
subscribeDisplayLanguage,
|
|
168
|
+
setBusinessModuleVersion,
|
|
169
|
+
type HeaderContextChangeDetail,
|
|
133
170
|
} from "@easygov/web-components";
|
|
134
171
|
|
|
135
|
-
getDisplayLanguage(); // 'de' | 'en' | 'fr' | 'it'
|
|
136
|
-
setDisplayLanguage("fr"); // writes the shared cookie;
|
|
137
|
-
const unsubscribe = subscribeDisplayLanguage(() =>
|
|
138
|
-
console.log(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
**Don't mix this with `@easygov/react-components`'s `useEasygovDisplayLanguage()` on the same
|
|
143
|
-
page.** This bundle inlines its own copy of `@easygov/react-components`, so the two would read
|
|
144
|
-
from different store instances and drift apart the moment a user picks a language. If a page
|
|
145
|
-
already uses `@easygov/react-components` directly, use its hook/functions instead of this
|
|
146
|
-
package's.
|
|
147
|
-
|
|
148
|
-
### Publishing the business module's version
|
|
149
|
-
|
|
150
|
-
A consuming app can publish its own version number so it shows up in the header's environment
|
|
151
|
-
badge tooltip, alongside the library and backend versions:
|
|
152
|
-
|
|
153
|
-
```ts
|
|
154
|
-
import { setBusinessModuleVersion } from "@easygov/web-components";
|
|
155
|
-
|
|
156
|
-
setBusinessModuleVersion("2.4.1");
|
|
172
|
+
getDisplayLanguage(); // 'de' | 'en' | 'fr' | 'it'
|
|
173
|
+
setDisplayLanguage("fr"); // writes the shared language cookie; the header re-renders
|
|
174
|
+
const unsubscribe = subscribeDisplayLanguage(() =>
|
|
175
|
+
console.log(getDisplayLanguage()),
|
|
176
|
+
);
|
|
177
|
+
setBusinessModuleVersion("2.4.1"); // shown in the environment badge's tooltip
|
|
157
178
|
```
|
|
158
179
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
`@easygov/react-components`
|
|
162
|
-
|
|
163
|
-
## Framework examples
|
|
180
|
+
The display language comes from the `eg-display-language` cookie shared across all EasyGov
|
|
181
|
+
subdomains, else the browser language, else `de`. Don't combine this package with the hooks or
|
|
182
|
+
functions of `@easygov/react-components` on the same page: each bundle has its own state.
|
|
164
183
|
|
|
165
|
-
|
|
184
|
+
## Angular
|
|
166
185
|
|
|
167
|
-
|
|
168
|
-
as a real npm dependency (not `workspace:*`) and side-effect-import the subpaths you need, then
|
|
169
|
-
wrap each custom element in a small Angular component so its events become `@Output()`s:
|
|
186
|
+
Register the elements once, in `main.ts`:
|
|
170
187
|
|
|
171
188
|
```ts
|
|
172
|
-
// main.ts
|
|
173
189
|
import "@easygov/web-components/header";
|
|
174
190
|
import "@easygov/web-components/footer";
|
|
175
191
|
```
|
|
176
192
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
193
|
+
Add the `@font-face` from [Styling](#styling) to your global `styles.css`. Then use the elements in
|
|
194
|
+
any component that declares `CUSTOM_ELEMENTS_SCHEMA` (otherwise Angular rejects the unknown
|
|
195
|
+
tags). Angular's event binding works directly on the custom events:
|
|
180
196
|
|
|
181
|
-
|
|
197
|
+
```ts
|
|
198
|
+
import { Component, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
|
|
199
|
+
import type { HeaderContextChangeDetail } from "@easygov/web-components";
|
|
182
200
|
|
|
183
201
|
@Component({
|
|
184
|
-
selector: "app-
|
|
202
|
+
selector: "app-root",
|
|
185
203
|
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
186
|
-
template:
|
|
204
|
+
template: `
|
|
205
|
+
<eg-easygov-header
|
|
206
|
+
(contextchange)="onContextChange($event)"
|
|
207
|
+
(sessionchange)="onSessionChange($event)"
|
|
208
|
+
(languagechange)="onLanguageChange($event)"
|
|
209
|
+
></eg-easygov-header>
|
|
210
|
+
<router-outlet />
|
|
211
|
+
<eg-easygov-footer></eg-easygov-footer>
|
|
212
|
+
`,
|
|
187
213
|
})
|
|
188
|
-
export class
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
for (const eventName of BRIDGED_EVENTS) {
|
|
199
|
-
const listener = (event: Event) => this[eventName].emit(event as CustomEvent);
|
|
200
|
-
this.listeners.set(eventName, listener);
|
|
201
|
-
this.host.nativeElement.addEventListener(eventName, listener);
|
|
214
|
+
export class AppComponent {
|
|
215
|
+
private contextJwt: string | null = null;
|
|
216
|
+
|
|
217
|
+
onContextChange(event: Event): void {
|
|
218
|
+
const { contextJwt, claims, contentChanged } = (
|
|
219
|
+
event as CustomEvent<HeaderContextChangeDetail>
|
|
220
|
+
).detail;
|
|
221
|
+
this.contextJwt = contextJwt;
|
|
222
|
+
if (contentChanged) {
|
|
223
|
+
// prefill from claims
|
|
202
224
|
}
|
|
203
225
|
}
|
|
204
226
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
227
|
+
onSessionChange(event: Event): void {
|
|
228
|
+
const { user } = (
|
|
229
|
+
event as CustomEvent<{ user: { sub: string; name: string } | null }>
|
|
230
|
+
).detail;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
onLanguageChange(event: Event): void {
|
|
234
|
+
const { locale } = (event as CustomEvent<{ locale: string }>).detail;
|
|
209
235
|
}
|
|
210
236
|
}
|
|
211
237
|
```
|
|
212
238
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
### Plain HTML / vanilla JS
|
|
217
|
-
|
|
218
|
-
See `packages/web-components/demo/index.html` in this repo — run
|
|
219
|
-
`pnpm --filter @easygov/web-components demo` after building, then open
|
|
220
|
-
`http://localhost:4173/demo/` for a live local page exercising both elements, their events, and
|
|
221
|
-
the DisplayLanguage API.
|
|
222
|
-
|
|
223
|
-
## Build-time configuration
|
|
224
|
-
|
|
225
|
-
`EasygovHeader` needs `NEXT_PUBLIC_CORE_ORIGIN` / `NEXT_PUBLIC_CORE_API_ORIGIN` /
|
|
226
|
-
`NEXT_PUBLIC_OIDC_AUTHORITY` / `NEXT_PUBLIC_INFO_ORIGIN` on any host outside `*.easygov.swiss`
|
|
227
|
-
(see `@easygov/react-components`'s README for what each does and when it's required).
|
|
228
|
-
|
|
229
|
-
**Unlike `@easygov/react-components`, you cannot set these when *your* app builds** — this
|
|
230
|
-
package ships a pre-built bundle with those values already inlined by tsup at the time
|
|
231
|
-
`@easygov/web-components` itself was built and published (see this repo's
|
|
232
|
-
`packages/web-components/tsup.config.ts` and the `@easygov/web-components#build` task in
|
|
233
|
-
`turbo.json`). If your deployment target needs origins other than what the published package
|
|
234
|
-
was built with, you have two options:
|
|
235
|
-
|
|
236
|
-
- Rely on the automatic `*.easygov.swiss` subdomain derivation (no env vars needed there), or
|
|
237
|
-
- Fork/rebuild this package yourself with the env vars set for your target, or use
|
|
238
|
-
`@easygov/react-components` directly instead, where `NEXT_PUBLIC_*` is inlined at *your*
|
|
239
|
-
app's build time.
|
|
239
|
+
Put the header in the root component so it stays mounted across route changes; it notices
|
|
240
|
+
Angular Router navigations by itself.
|
|
240
241
|
|
|
241
242
|
## Testing
|
|
242
243
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
Playwright's `page.locator('eg-easygov-header').locator('...')`, which crosses open shadow roots
|
|
247
|
-
automatically) rather than querying `document` directly.
|
|
244
|
+
The rendered components carry the same `data-slot` attributes as `@easygov/react-components`,
|
|
245
|
+
inside the element's shadow root. Playwright locators pierce open shadow roots, e.g.
|
|
246
|
+
`page.locator('eg-easygov-header').locator('[data-slot="header-nav-dashboard"]')`.
|
|
248
247
|
|
|
249
248
|
## License
|
|
250
249
|
|