@easygov/web-components 0.5.0 → 0.7.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 ADDED
@@ -0,0 +1,251 @@
1
+ # @easygov/web-components
2
+
3
+ Framework-agnostic custom elements for EasyGov 2.0, wrapping
4
+ [`@easygov/react-components`](../react-components) as self-contained browser bundles via
5
+ [`@r2wc/react-to-web-component`](https://github.com/bitovi/react-to-web-component). Use this
6
+ package instead of `@easygov/react-components` when your app isn't React — Angular, Vue, plain
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.
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ npm install @easygov/web-components
17
+ ```
18
+
19
+ There are no runtime peer dependencies to install — React and ReactDOM are bundled in. (The
20
+ `package.json` lists `react`/`react-dom` as peer dependencies purely so npm doesn't warn a
21
+ React host app about a duplicate copy; you never need to satisfy them yourself.)
22
+
23
+ ## Usage
24
+
25
+ Each custom element is a side-effecting subpath import — importing it registers the element:
26
+
27
+ ```ts
28
+ import "@easygov/web-components/header"; // registers <eg-easygov-header>
29
+ import "@easygov/web-components/footer"; // registers <eg-easygov-footer>
30
+ ```
31
+
32
+ ```html
33
+ <eg-easygov-header></eg-easygov-header>
34
+
35
+ <main>...</main>
36
+
37
+ <eg-easygov-footer></eg-easygov-footer>
38
+ ```
39
+
40
+ Import only the subpath(s) you need — an app using just the footer never pulls in the header's
41
+ code. Both share one common chunk (React, the design tokens' CSS, `@easygov/react-components`
42
+ itself), so it's fetched once regardless of how many elements you import.
43
+
44
+ Each element renders into an open shadow root with the design system's styles attached
45
+ automatically — no separate stylesheet import required, unlike `@easygov/react-components`.
46
+ The font is the exception, see [Font](#font).
47
+
48
+ ### Font
49
+
50
+ The elements use the font family `"Clash Grotesk Variable"` (Clash Grotesk, Indian Type
51
+ Foundry). Its licence (ITF FFL v2.0) does not allow redistributing the font file through a
52
+ public package, so **this package ships no font files and no `@font-face`**. Without the font
53
+ the elements fall back to the system font.
54
+
55
+ Download the font yourself from [Fontshare](https://www.fontshare.com/fonts/clash-grotesk),
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:
59
+
60
+ ```css
61
+ @font-face {
62
+ font-family: "Clash Grotesk Variable";
63
+ src: url("/fonts/ClashGrotesk-Variable.woff2") format("woff2");
64
+ font-weight: 200 700;
65
+ font-display: swap;
66
+ }
67
+ ```
68
+
69
+ Optionally preload it to avoid a late font swap:
70
+
71
+ ```html
72
+ <link rel="preload" href="/fonts/ClashGrotesk-Variable.woff2" as="font" type="font/woff2" crossorigin />
73
+ ```
74
+
75
+ ### Color contexts
76
+
77
+ The design system has three color contexts, `.eg-context-bright`, `.eg-context-subtle` and
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.
84
+
85
+ ### `<eg-easygov-header>`
86
+
87
+ No attributes. Auth, the display language, and the environment/version badges are all handled
88
+ internally the same way they are in `@easygov/react-components` — see that package's README for
89
+ the underlying `NEXT_PUBLIC_*` env var configuration, since it applies here too (see
90
+ [Build-time configuration](#build-time-configuration) below for the web-components-specific
91
+ wrinkle).
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:
96
+
97
+ ```js
98
+ const header = document.querySelector("eg-easygov-header");
99
+
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
+ header.addEventListener("contextchange", (e) => {
107
+ const { contextJwt, claims } = e.detail; // decoded, NOT signature-verified — see below
108
+ });
109
+ header.addEventListener("sessionchange", (e) => {
110
+ console.log(e.detail.user);
111
+ });
112
+ ```
113
+
114
+ **`contextJwt`/`claims` are decoded but not verified.** Use them to prefill forms or drive
115
+ rendering; a backend that receives `contextJwt` must verify it against core-backend's published
116
+ JWKS before trusting any claim.
117
+
118
+ ### `<eg-easygov-footer>`
119
+
120
+ No attributes, no props, no events — fully static content.
121
+
122
+ ### DisplayLanguage API
123
+
124
+ The header owns the current display language (resolved from the `eg-display-language` cookie
125
+ shared across every EasyGov subdomain, then the browser language, then `"de"`). Read or change it
126
+ from the package root:
127
+
128
+ ```ts
129
+ import {
130
+ getDisplayLanguage,
131
+ setDisplayLanguage,
132
+ subscribeDisplayLanguage,
133
+ } from "@easygov/web-components";
134
+
135
+ getDisplayLanguage(); // 'de' | 'en' | 'fr' | 'it' | ...
136
+ setDisplayLanguage("fr"); // writes the shared cookie; every subscribed header re-renders
137
+ const unsubscribe = subscribeDisplayLanguage(() => {
138
+ console.log("display language changed:", getDisplayLanguage());
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");
157
+ ```
158
+
159
+ Call it once at startup (or whenever your app's version is known); calling it again later simply
160
+ updates the tooltip. Same "don't mix stores" caveat as above applies if the same page also uses
161
+ `@easygov/react-components` directly.
162
+
163
+ ## Framework examples
164
+
165
+ ### Angular
166
+
167
+ `apps/angular-demo` in this repo is a full working example — install `@easygov/web-components`
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:
170
+
171
+ ```ts
172
+ // main.ts
173
+ import "@easygov/web-components/header";
174
+ import "@easygov/web-components/footer";
175
+ ```
176
+
177
+ ```ts
178
+ // components/easygov-header/easygov-header.component.ts
179
+ import { AfterViewInit, Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, EventEmitter, OnDestroy, Output, ViewChild } from "@angular/core";
180
+
181
+ const BRIDGED_EVENTS = ["languagechange", "navigate", "contextchange", "sessionchange"] as const;
182
+
183
+ @Component({
184
+ selector: "app-easygov-header",
185
+ schemas: [CUSTOM_ELEMENTS_SCHEMA],
186
+ template: `<eg-easygov-header #host></eg-easygov-header>`,
187
+ })
188
+ export class EasygovHeaderComponent implements AfterViewInit, OnDestroy {
189
+ @ViewChild("host", { static: true }) private host!: ElementRef<HTMLElement>;
190
+ @Output() languagechange = new EventEmitter<CustomEvent>();
191
+ @Output() navigate = new EventEmitter<CustomEvent>();
192
+ @Output() contextchange = new EventEmitter<CustomEvent>();
193
+ @Output() sessionchange = new EventEmitter<CustomEvent>();
194
+
195
+ private readonly listeners = new Map<string, (event: Event) => void>();
196
+
197
+ ngAfterViewInit(): void {
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);
202
+ }
203
+ }
204
+
205
+ ngOnDestroy(): void {
206
+ for (const [eventName, listener] of this.listeners) {
207
+ this.host.nativeElement.removeEventListener(eventName, listener);
208
+ }
209
+ }
210
+ }
211
+ ```
212
+
213
+ `CUSTOM_ELEMENTS_SCHEMA` is required on any Angular component template that renders a custom
214
+ element directly, or the Angular compiler rejects the unknown tag.
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.
240
+
241
+ ## Testing
242
+
243
+ Components rendered by this package carry the same `data-slot` attributes as
244
+ `@easygov/react-components` (see that package), but they live inside each element's shadow root —
245
+ an external Playwright suite must pierce the shadow boundary (e.g.
246
+ Playwright's `page.locator('eg-easygov-header').locator('...')`, which crosses open shadow roots
247
+ automatically) rather than querying `document` directly.
248
+
249
+ ## License
250
+
251
+ MIT