@nanahoshi/mona-ui 0.28.0 → 0.30.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/docs/api/i18n.md +13 -0
- package/docs/api/locales.md +21 -13
- package/docs/api/qr-code.md +287 -0
- package/docs/qr-code.md +377 -0
- package/fesm2022/nanahoshi-mona-ui-calendar.mjs +9 -8
- package/fesm2022/nanahoshi-mona-ui-i18n-icu.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ar-sa.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-de-de.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-es-es.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-fr-fr.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-id-id.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-it-it.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ja-jp.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ko-kr.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pl-pl.mjs +600 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pt-br.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ru-ru.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-tr-tr.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-cn.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-tw.mjs +1 -0
- package/fesm2022/nanahoshi-mona-ui-locales.mjs +3 -1
- package/fesm2022/nanahoshi-mona-ui-qr-code.mjs +1671 -0
- package/i18n/catalogs/en-US.messages.json +3 -0
- package/llms.txt +4 -2
- package/locales/ar-sa/ar-sa.messages.json +3 -0
- package/locales/de-de/de-de.messages.json +3 -0
- package/locales/es-es/es-es.messages.json +3 -0
- package/locales/fr-fr/fr-fr.messages.json +3 -0
- package/locales/id-id/id-id.messages.json +3 -0
- package/locales/it-it/it-it.messages.json +3 -0
- package/locales/ja-jp/ja-jp.messages.json +3 -0
- package/locales/ko-kr/ko-kr.messages.json +3 -0
- package/locales/pl-pl/package.json +4 -0
- package/locales/pl-pl/pl-pl.messages.json +539 -0
- package/locales/pt-br/pt-br.messages.json +3 -0
- package/locales/ru-ru/ru-ru.messages.json +3 -0
- package/locales/tr-tr/tr-tr.messages.json +3 -0
- package/locales/zh-cn/zh-cn.messages.json +3 -0
- package/locales/zh-tw/zh-tw.messages.json +3 -0
- package/package.json +9 -1
- package/qr-code/package.json +4 -0
- package/types/nanahoshi-mona-ui-calendar.d.ts +2 -2
- package/types/nanahoshi-mona-ui-i18n.d.ts +4 -0
- package/types/nanahoshi-mona-ui-locales-ar-sa.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-de-de.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-es-es.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-fr-fr.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-id-id.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-it-it.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-ja-jp.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-ko-kr.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-pl-pl.d.ts +543 -0
- package/types/nanahoshi-mona-ui-locales-pt-br.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-ru-ru.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-tr-tr.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-zh-cn.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales-zh-tw.d.ts +3 -0
- package/types/nanahoshi-mona-ui-locales.d.ts +2 -1
- package/types/nanahoshi-mona-ui-qr-code.d.ts +322 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# `@nanahoshi/mona-ui/qr-code`
|
|
2
|
+
|
|
3
|
+
Everything `@nanahoshi/mona-ui/qr-code` exports.
|
|
4
|
+
|
|
5
|
+
**Guides:** [QR Code](../qr-code.md)
|
|
6
|
+
|
|
7
|
+
## `QrCodeComponent`
|
|
8
|
+
|
|
9
|
+
Component.
|
|
10
|
+
|
|
11
|
+
Draws a value as a QR code in SVG, and exports it as a PNG, SVG or PDF file.
|
|
12
|
+
|
|
13
|
+
**Selector:** `mona-qr-code`
|
|
14
|
+
**Exported as:** `monaQrCode`
|
|
15
|
+
|
|
16
|
+
### Inputs
|
|
17
|
+
|
|
18
|
+
| Name | Type | Default | Description |
|
|
19
|
+
| --- | --- | --- | --- |
|
|
20
|
+
| `aria-label` | `string` | `""` | Accessible name of the code. Say what scanning it does, such as "QR code for the event page". When empty, falls back to the localized "QR code". The host has `role="img"` while there's a code to show. |
|
|
21
|
+
| `background` | `string` | `"#ffffff"` | Color of the light modules and the quiet zone, as any CSS color. `"transparent"` lets the page show through, which only scans on a light page. |
|
|
22
|
+
| `class` | `string` | `""` | Additional CSS classes merged onto the host element via `tailwind-merge`. |
|
|
23
|
+
| `color` | `string` | `"#000000"` | Color of the dark modules, as any CSS color. Readers need it much darker than `background`. |
|
|
24
|
+
| `encoding` | `QrCodeEncoding` | `"utf-8"` | Character set the text is stored in. `"utf-8"` stores any text; `"iso-8859-1"` stores Latin-1 only, without the ECI header some older readers don't support. |
|
|
25
|
+
| `errorCorrection` | `QrCodeErrorCorrection` | `"M"` | How much of the code can be damaged or hidden and still read. Use `"H"` with an image overlay. A higher level needs more modules for the same value. |
|
|
26
|
+
| `overlay` | `QrCodeOverlay \| null` | `null` | An image or the Swiss cross to draw over the center. The modules beneath it are cleared, so raise `errorCorrection` to keep the code readable; a warning is logged in development when it hides too much. With an overlay, the code is version 2 at least, since version 1 has no room for one. |
|
|
27
|
+
| `quietZone` | `number` | `4` | Width of the light border around the code, in modules. Readers expect 4; a smaller one only scans when the surroundings are light. |
|
|
28
|
+
| `size` | `number \| string` | `200` | Width and height, quiet zone included. A number is pixels; a string can be any CSS length, such as `"100%"`. The code never grows wider than its container. |
|
|
29
|
+
| `value` | `string \| number` | `""` | Text to encode, such as a URL. A number is encoded as its digits. An empty value draws nothing. |
|
|
30
|
+
|
|
31
|
+
### Properties
|
|
32
|
+
|
|
33
|
+
| Name | Type | Description |
|
|
34
|
+
| --- | --- | --- |
|
|
35
|
+
| `error` (read-only) | `Signal<QrCodeValueError \| null>` | Why the value can't be encoded, or `null` when it can or is empty. While it's set, nothing is drawn. Use `validateQrCodeValue` to check a value before it reaches the component, such as in a form. |
|
|
36
|
+
|
|
37
|
+
### Methods
|
|
38
|
+
|
|
39
|
+
| Signature | Description |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| `downloadQrCode(options: QrCodeDownloadOptions): Promise<QrCodeExportResult>` | Exports the code and saves it as a file. PDF export needs the `jspdf` package, which isn't installed with Mona UI. |
|
|
42
|
+
| `exportQrCode(options: QrCodeExportOptions): Promise<QrCodeExportResult>` | Exports the code as a PNG image, an SVG document or a PDF page, drawn the same way it's shown, with the colors resolved to what they paint. Rejects with a `QrCodeExportError`. PDF export needs the `jspdf` package, which isn't installed with Mona UI. |
|
|
43
|
+
|
|
44
|
+
## `QrCodeExportError`
|
|
45
|
+
|
|
46
|
+
Class.
|
|
47
|
+
|
|
48
|
+
The error a QR code export rejects with.
|
|
49
|
+
|
|
50
|
+
### Properties
|
|
51
|
+
|
|
52
|
+
| Name | Type |
|
|
53
|
+
| --- | --- |
|
|
54
|
+
| `code` (read-only) | `QrCodeExportErrorCode` |
|
|
55
|
+
|
|
56
|
+
## `validateQrCodeValue`
|
|
57
|
+
|
|
58
|
+
Function.
|
|
59
|
+
|
|
60
|
+
Checks whether a QR code can hold a value with the given settings, without drawing it: every character has to fit the
|
|
61
|
+
encoding, and the whole value the largest code. An empty value is valid, since the component draws nothing for it.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
function validateQrCodeValue(value: string | number, options?: QrCodeValidationOptions): QrCodeValueError | null;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## `QR_CODE_DEFAULT_MESSAGES`
|
|
68
|
+
|
|
69
|
+
Constant.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const QR_CODE_DEFAULT_MESSAGES: MonaQrCodeMessages;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## `QrCodeDownloadOptions`
|
|
76
|
+
|
|
77
|
+
Interface.
|
|
78
|
+
|
|
79
|
+
What to export a QR code as, and the name of the file to save.
|
|
80
|
+
|
|
81
|
+
**Extends:** `QrCodeExportOptions`
|
|
82
|
+
|
|
83
|
+
### Properties
|
|
84
|
+
|
|
85
|
+
| Name | Type | Description |
|
|
86
|
+
| --- | --- | --- |
|
|
87
|
+
| `fileName?` (read-only) | `string` | The file's name. The format's extension is added when it's missing. Defaults to `qr-code`. |
|
|
88
|
+
|
|
89
|
+
### Inherited from `QrCodeExportOptions`
|
|
90
|
+
|
|
91
|
+
| Name | Type | Description |
|
|
92
|
+
| --- | --- | --- |
|
|
93
|
+
| `format` (read-only) | `QrCodeExportFormat` | |
|
|
94
|
+
| `size?` (read-only) | `number` | The width and height, quiet zone included, in CSS pixels: the image's pixels for PNG, its `width` and `height` for SVG, and the page for PDF, at 96 pixels to the inch. Defaults to the rendered size. A PNG has at least one pixel per module, and its modules are whole pixels, with what's left over added to the quiet zone. |
|
|
95
|
+
|
|
96
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent)
|
|
97
|
+
|
|
98
|
+
## `QrCodeExportOptions`
|
|
99
|
+
|
|
100
|
+
Interface.
|
|
101
|
+
|
|
102
|
+
What to export a QR code as.
|
|
103
|
+
|
|
104
|
+
### Properties
|
|
105
|
+
|
|
106
|
+
| Name | Type | Description |
|
|
107
|
+
| --- | --- | --- |
|
|
108
|
+
| `format` (read-only) | `QrCodeExportFormat` | |
|
|
109
|
+
| `size?` (read-only) | `number` | The width and height, quiet zone included, in CSS pixels: the image's pixels for PNG, its `width` and `height` for SVG, and the page for PDF, at 96 pixels to the inch. Defaults to the rendered size. A PNG has at least one pixel per module, and its modules are whole pixels, with what's left over added to the quiet zone. |
|
|
110
|
+
|
|
111
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent), [`QrCodeDownloadOptions`](qr-code.md#qrcodedownloadoptions)
|
|
112
|
+
|
|
113
|
+
## `QrCodeExportResult`
|
|
114
|
+
|
|
115
|
+
Interface.
|
|
116
|
+
|
|
117
|
+
An exported QR code.
|
|
118
|
+
|
|
119
|
+
### Properties
|
|
120
|
+
|
|
121
|
+
| Name | Type | Description |
|
|
122
|
+
| --- | --- | --- |
|
|
123
|
+
| `blob` (read-only) | `Blob` | |
|
|
124
|
+
| `format` (read-only) | `QrCodeExportFormat` | |
|
|
125
|
+
| `mimeType` (read-only) | `string` | The blob's type: `image/png`, `image/svg+xml` or `application/pdf`. |
|
|
126
|
+
| `size` (read-only) | `number` | The width and height in CSS pixels. |
|
|
127
|
+
|
|
128
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent)
|
|
129
|
+
|
|
130
|
+
## `QrCodeImageOverlay`
|
|
131
|
+
|
|
132
|
+
Interface.
|
|
133
|
+
|
|
134
|
+
An image, such as a logo, drawn over the center of the code.
|
|
135
|
+
|
|
136
|
+
### Properties
|
|
137
|
+
|
|
138
|
+
| Name | Type | Description |
|
|
139
|
+
| --- | --- | --- |
|
|
140
|
+
| `size?` (read-only) | `number` | The image's width and height as a share of the code's, without the quiet zone. The image keeps its aspect ratio inside that square. Defaults to `0.2`. |
|
|
141
|
+
| `src` (read-only) | `string` | The image's URL. Exporting reads it into the file, so an image from another origin has to allow it with CORS. |
|
|
142
|
+
| `type` (read-only) | `"image"` | |
|
|
143
|
+
|
|
144
|
+
**Used by:** [`QrCodeOverlay`](qr-code.md#qrcodeoverlay)
|
|
145
|
+
|
|
146
|
+
## `QrCodeSwissOverlay`
|
|
147
|
+
|
|
148
|
+
Interface.
|
|
149
|
+
|
|
150
|
+
The Swiss cross of a Swiss QR code, the code on a Swiss QR-bill, drawn over the center at the size the standard sets.
|
|
151
|
+
|
|
152
|
+
### Properties
|
|
153
|
+
|
|
154
|
+
| Name | Type |
|
|
155
|
+
| --- | --- |
|
|
156
|
+
| `type` (read-only) | `"swiss"` |
|
|
157
|
+
|
|
158
|
+
**Used by:** [`QrCodeOverlay`](qr-code.md#qrcodeoverlay)
|
|
159
|
+
|
|
160
|
+
## `QrCodeTooLongError`
|
|
161
|
+
|
|
162
|
+
Interface.
|
|
163
|
+
|
|
164
|
+
A value too long for the largest QR code (version 40) at the chosen error correction level.
|
|
165
|
+
|
|
166
|
+
### Properties
|
|
167
|
+
|
|
168
|
+
| Name | Type | Description |
|
|
169
|
+
| --- | --- | --- |
|
|
170
|
+
| `availableBits` (read-only) | `number` | The most bits a version 40 code holds at the chosen error correction level. |
|
|
171
|
+
| `code` (read-only) | `"too-long"` | |
|
|
172
|
+
| `requiredBits` (read-only) | `number` | The bits the value needs. |
|
|
173
|
+
|
|
174
|
+
**Used by:** [`QrCodeValueError`](qr-code.md#qrcodevalueerror)
|
|
175
|
+
|
|
176
|
+
## `QrCodeUnsupportedCharacterError`
|
|
177
|
+
|
|
178
|
+
Interface.
|
|
179
|
+
|
|
180
|
+
A character the chosen encoding can't store, such as `€` in `"iso-8859-1"`.
|
|
181
|
+
|
|
182
|
+
### Properties
|
|
183
|
+
|
|
184
|
+
| Name | Type | Description |
|
|
185
|
+
| --- | --- | --- |
|
|
186
|
+
| `character` (read-only) | `string` | The first character that can't be stored. |
|
|
187
|
+
| `code` (read-only) | `"unsupported-character"` | |
|
|
188
|
+
| `index` (read-only) | `number` | Where that character starts in the value, in UTF-16 code units. |
|
|
189
|
+
|
|
190
|
+
**Used by:** [`QrCodeValueError`](qr-code.md#qrcodevalueerror)
|
|
191
|
+
|
|
192
|
+
## `QrCodeValidationOptions`
|
|
193
|
+
|
|
194
|
+
Interface.
|
|
195
|
+
|
|
196
|
+
The settings a value is checked against, matching the component's inputs of the same names.
|
|
197
|
+
|
|
198
|
+
### Properties
|
|
199
|
+
|
|
200
|
+
| Name | Type | Description |
|
|
201
|
+
| --- | --- | --- |
|
|
202
|
+
| `encoding?` (read-only) | `QrCodeEncoding` | Defaults to `"utf-8"`, as on the component. |
|
|
203
|
+
| `errorCorrection?` (read-only) | `QrCodeErrorCorrection` | Defaults to `"M"`, as on the component. |
|
|
204
|
+
|
|
205
|
+
**Used by:** [`validateQrCodeValue`](qr-code.md#validateqrcodevalue)
|
|
206
|
+
|
|
207
|
+
## `QrCodeEncoding`
|
|
208
|
+
|
|
209
|
+
Type alias.
|
|
210
|
+
|
|
211
|
+
The character set a QR code stores text in.
|
|
212
|
+
- `"utf-8"`: any text. A value with characters outside ASCII starts with an ECI header that tells readers it's UTF-8.
|
|
213
|
+
- `"iso-8859-1"`: Latin-1 only, which every reader assumes when there's no header. Other characters are an error.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
type QrCodeEncoding = "utf-8" | "iso-8859-1";
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent), [`QrCodeValidationOptions`](qr-code.md#qrcodevalidationoptions)
|
|
220
|
+
|
|
221
|
+
## `QrCodeErrorCorrection`
|
|
222
|
+
|
|
223
|
+
Type alias.
|
|
224
|
+
|
|
225
|
+
How much of the code can be damaged or hidden and still read: about 7% at `"L"`, 15% at `"M"`, 25% at `"Q"` and 30%
|
|
226
|
+
at `"H"`. A higher level holds less data at the same size.
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
type QrCodeErrorCorrection = "L" | "M" | "Q" | "H";
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent), [`QrCodeValidationOptions`](qr-code.md#qrcodevalidationoptions)
|
|
233
|
+
|
|
234
|
+
## `QrCodeExportErrorCode`
|
|
235
|
+
|
|
236
|
+
Type alias.
|
|
237
|
+
|
|
238
|
+
Why a QR code export failed.
|
|
239
|
+
- `"not-ready"`: there's no code to export, because the value is empty or can't be encoded.
|
|
240
|
+
- `"unsupported-environment"`: the export ran outside a browser, such as during server-side rendering.
|
|
241
|
+
- `"invalid-size"`: the size isn't a positive number, or a PNG of that size can't give each module a pixel.
|
|
242
|
+
- `"resource-load-failed"`: the overlay image didn't load, or another origin didn't allow reading it.
|
|
243
|
+
- `"png-rasterization-failed"`: the browser couldn't draw the PNG.
|
|
244
|
+
- `"pdf-generation-failed"`: `jspdf` isn't installed, or failed to write the page.
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
type QrCodeExportErrorCode = | "not-ready" | "unsupported-environment" | "invalid-size" | "resource-load-failed" | "png-rasterization-failed" | "pdf-generation-failed";
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
**Used by:** [`QrCodeExportError`](qr-code.md#qrcodeexporterror)
|
|
251
|
+
|
|
252
|
+
## `QrCodeExportFormat`
|
|
253
|
+
|
|
254
|
+
Type alias.
|
|
255
|
+
|
|
256
|
+
The file a QR code exports to: a `"png"` image, an `"svg"` document or a `"pdf"` page.
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
type QrCodeExportFormat = "png" | "svg" | "pdf";
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Used by:** [`QrCodeExportOptions`](qr-code.md#qrcodeexportoptions), [`QrCodeExportResult`](qr-code.md#qrcodeexportresult)
|
|
263
|
+
|
|
264
|
+
## `QrCodeOverlay`
|
|
265
|
+
|
|
266
|
+
Type alias.
|
|
267
|
+
|
|
268
|
+
What to draw over the center of the code. The modules beneath it are cleared, so the overlay relies on error
|
|
269
|
+
correction to read.
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
type QrCodeOverlay = QrCodeImageOverlay | QrCodeSwissOverlay;
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent)
|
|
276
|
+
|
|
277
|
+
## `QrCodeValueError`
|
|
278
|
+
|
|
279
|
+
Type alias.
|
|
280
|
+
|
|
281
|
+
Why a value can't be stored in a QR code.
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
type QrCodeValueError = QrCodeUnsupportedCharacterError | QrCodeTooLongError;
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
**Used by:** [`QrCodeComponent`](qr-code.md#qrcodecomponent), [`validateQrCodeValue`](qr-code.md#validateqrcodevalue)
|
package/docs/qr-code.md
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
# QR Code
|
|
2
|
+
|
|
3
|
+
> Encodes text or a URL as a scannable QR code, with logo overlays and PNG, SVG and PDF export.
|
|
4
|
+
|
|
5
|
+
Entry point: `@nanahoshi/mona-ui/qr-code`
|
|
6
|
+
|
|
7
|
+
**Selector:** `mona-qr-code`
|
|
8
|
+
|
|
9
|
+
`QrCodeComponent` encodes text, such as a URL, a Wi-Fi login or payment data, as a QR code drawn in SVG. It picks the smallest code and the densest modes for the value, can draw a logo or the Swiss cross over the center, and exports the code as a PNG, SVG or PDF file.
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
A QR code lets someone carry a value from a screen or a printout to their phone by pointing the camera at it. Use it for what's tedious to type: a link, a Wi-Fi login, a ticket or a payment.
|
|
14
|
+
|
|
15
|
+
**Use `QrCodeComponent` when you need to:**
|
|
16
|
+
|
|
17
|
+
- Hand a link or a short text to a phone, on screen or on paper
|
|
18
|
+
- Print codes in labels, tickets or invoices, from an exported PNG, SVG or PDF
|
|
19
|
+
- Brand a code with a logo, or draw the Swiss cross of a Swiss QR-bill
|
|
20
|
+
|
|
21
|
+
**Show the value as text as well when:**
|
|
22
|
+
|
|
23
|
+
- Someone may not be able to scan, such as a screen reader user or someone on the device the code opens on. A link next to the code serves both.
|
|
24
|
+
|
|
25
|
+
## Import & Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { QrCodeComponent } from "@nanahoshi/mona-ui/qr-code";
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Add `QrCodeComponent` to your standalone component's `imports` array, and give the code a `value` and a name that says what it's for:
|
|
32
|
+
|
|
33
|
+
```html
|
|
34
|
+
<mona-qr-code value="https://nanahoshi.dev/mona-ui" aria-label="QR code for the Mona UI docs" />
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Examples
|
|
38
|
+
|
|
39
|
+
### Logo overlay
|
|
40
|
+
|
|
41
|
+
`overlay` draws an image over the center, sized by `size` as a share of the code's width, 0.2 by default. The image keeps its aspect ratio. The modules beneath it are cleared rather than covered, so error correction has to make up for them: use `errorCorrection="H"`, which recovers the most. With an overlay, the code is at least version 2, since version 1 has no room for one.
|
|
42
|
+
|
|
43
|
+
`logo-example.component.ts`
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { Component } from "@angular/core";
|
|
47
|
+
import { QrCodeComponent, type QrCodeOverlay } from "@nanahoshi/mona-ui/qr-code";
|
|
48
|
+
|
|
49
|
+
const LOGO_SVG =
|
|
50
|
+
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" rx="14" fill="#4338ca"/>` +
|
|
51
|
+
`<path d="M32 10 37.6 26.4 54 32 37.6 37.6 32 54 26.4 37.6 10 32 26.4 26.4Z" fill="#fff"/></svg>`;
|
|
52
|
+
|
|
53
|
+
@Component({
|
|
54
|
+
selector: "app-qr-code-logo-example",
|
|
55
|
+
imports: [QrCodeComponent],
|
|
56
|
+
templateUrl: "./logo-example.component.html"
|
|
57
|
+
})
|
|
58
|
+
export class QrCodeLogoExampleComponent {
|
|
59
|
+
protected readonly logo: QrCodeOverlay = {
|
|
60
|
+
size: 0.22,
|
|
61
|
+
src: `data:image/svg+xml,${encodeURIComponent(LOGO_SVG)}`,
|
|
62
|
+
type: "image"
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`logo-example.component.html`
|
|
68
|
+
|
|
69
|
+
```html
|
|
70
|
+
<mona-qr-code
|
|
71
|
+
aria-label="QR code for the Mona UI docs"
|
|
72
|
+
errorCorrection="H"
|
|
73
|
+
value="https://nanahoshi.dev/mona-ui"
|
|
74
|
+
[overlay]="logo" />
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
In development, the component warns when the overlay hides more of some error correction block than it can recover. The check counts every codeword under the cleared square as lost, so a code it warns about may still scan, but one it accepts has the correction to spare. Test the codes you print with the readers your users have.
|
|
78
|
+
|
|
79
|
+
### Swiss QR code
|
|
80
|
+
|
|
81
|
+
`{ type: "swiss" }` draws the Swiss cross of a Swiss QR-bill, at 7/46 of the code as the standard sets. The payment data is the `value`, one field a line, as the Swiss Implementation Guidelines lay it out. The component draws the code and the cross; it doesn't check the fields. The standard asks for error correction `"M"`, the default.
|
|
82
|
+
|
|
83
|
+
`swiss-example.component.ts`
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { Component } from "@angular/core";
|
|
87
|
+
import { QrCodeComponent, type QrCodeOverlay } from "@nanahoshi/mona-ui/qr-code";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A Swiss QR-bill's payment data, one field a line, as the Swiss Implementation Guidelines lay it out. The creditor,
|
|
91
|
+
* account and invoice are made up.
|
|
92
|
+
*/
|
|
93
|
+
const PAYMENT = [
|
|
94
|
+
"SPC",
|
|
95
|
+
"0200",
|
|
96
|
+
"1",
|
|
97
|
+
"CH5604835012345678009",
|
|
98
|
+
"S",
|
|
99
|
+
"Example Ltd",
|
|
100
|
+
"Bundesplatz",
|
|
101
|
+
"3",
|
|
102
|
+
"3003",
|
|
103
|
+
"Bern",
|
|
104
|
+
"CH",
|
|
105
|
+
...Array<string>(7).fill(""),
|
|
106
|
+
"120.00",
|
|
107
|
+
"CHF",
|
|
108
|
+
...Array<string>(7).fill(""),
|
|
109
|
+
"NON",
|
|
110
|
+
"",
|
|
111
|
+
"Invoice 2026-114",
|
|
112
|
+
"EPD"
|
|
113
|
+
].join("\n");
|
|
114
|
+
|
|
115
|
+
@Component({
|
|
116
|
+
selector: "app-qr-code-swiss-example",
|
|
117
|
+
imports: [QrCodeComponent],
|
|
118
|
+
templateUrl: "./swiss-example.component.html"
|
|
119
|
+
})
|
|
120
|
+
export class QrCodeSwissExampleComponent {
|
|
121
|
+
protected readonly payment = PAYMENT;
|
|
122
|
+
protected readonly swissCross: QrCodeOverlay = { type: "swiss" };
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`swiss-example.component.html`
|
|
127
|
+
|
|
128
|
+
```html
|
|
129
|
+
<mona-qr-code
|
|
130
|
+
aria-label="Swiss QR code for invoice 2026-114"
|
|
131
|
+
errorCorrection="M"
|
|
132
|
+
[overlay]="swissCross"
|
|
133
|
+
[value]="payment" />
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Export
|
|
137
|
+
|
|
138
|
+
`exportQrCode()` resolves to a `Blob` of a PNG image, an SVG document or a PDF page, and `downloadQrCode()` saves one as a file. Get the component through a template reference with `#qrCode="monaQrCode"`, or with `viewChild(QrCodeComponent)`.
|
|
139
|
+
|
|
140
|
+
`export-example.component.ts`
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { Component, signal } from "@angular/core";
|
|
144
|
+
import { ButtonDirective } from "@nanahoshi/mona-ui/button";
|
|
145
|
+
import { QrCodeComponent, QrCodeExportError, type QrCodeExportFormat } from "@nanahoshi/mona-ui/qr-code";
|
|
146
|
+
|
|
147
|
+
@Component({
|
|
148
|
+
selector: "app-qr-code-export-example",
|
|
149
|
+
imports: [ButtonDirective, QrCodeComponent],
|
|
150
|
+
templateUrl: "./export-example.component.html"
|
|
151
|
+
})
|
|
152
|
+
export class QrCodeExportExampleComponent {
|
|
153
|
+
protected readonly formats: readonly QrCodeExportFormat[] = ["png", "svg", "pdf"];
|
|
154
|
+
protected readonly status = signal("");
|
|
155
|
+
|
|
156
|
+
protected async download(qrCode: QrCodeComponent, format: QrCodeExportFormat): Promise<void> {
|
|
157
|
+
try {
|
|
158
|
+
const { blob } = await qrCode.downloadQrCode({ fileName: "guest-wifi", format, size: 512 });
|
|
159
|
+
this.status.set(`Saved guest-wifi.${format}, ${Math.ceil(blob.size / 1024)} KB.`);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
this.status.set(error instanceof QrCodeExportError ? error.message : "The export failed.");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
`export-example.component.html`
|
|
168
|
+
|
|
169
|
+
```html
|
|
170
|
+
<div class="flex flex-col items-start gap-3">
|
|
171
|
+
<mona-qr-code
|
|
172
|
+
#qrCode="monaQrCode"
|
|
173
|
+
aria-label="QR code that joins the guest Wi-Fi"
|
|
174
|
+
value="WIFI:T:WPA;S:Guest;P:welcome-2026;;" />
|
|
175
|
+
<div class="flex gap-2">
|
|
176
|
+
@for (format of formats; track format) {
|
|
177
|
+
<button monaButton type="button" (click)="download(qrCode, format)">
|
|
178
|
+
Download {{ format.toUpperCase() }}
|
|
179
|
+
</button>
|
|
180
|
+
}
|
|
181
|
+
</div>
|
|
182
|
+
<p aria-live="polite" class="text-sm text-muted-foreground">{{ status() }}</p>
|
|
183
|
+
</div>
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
| Format | What it is | Notes |
|
|
187
|
+
|--------|------------|-------|
|
|
188
|
+
| `"svg"` | A standalone vector document | Sharp at any size, for print and design tools. The overlay image is embedded as a data URL, and the accessible name becomes its `<title>`. |
|
|
189
|
+
| `"png"` | A raster image `size` pixels wide | Every module is a whole number of pixels, and what's left over widens the quiet zone, so the edges stay sharp. It needs at least one pixel per module. |
|
|
190
|
+
| `"pdf"` | One page, `size` CSS pixels wide | Needs the `jspdf` package, which isn't installed with Mona UI. The modules are vector shapes; an overlay image is embedded as a PNG. |
|
|
191
|
+
|
|
192
|
+
`size` defaults to the code's rendered width. Colors are resolved to what they paint, so `var()` and `currentColor` export as the color you see. An overlay image from another origin has to allow reading with CORS, or the export rejects. Every failure is a `QrCodeExportError` whose `code` says why, such as `"not-ready"` when there's no code to export or `"invalid-size"` for a PNG too small for its modules.
|
|
193
|
+
|
|
194
|
+
## Appearance & Styling
|
|
195
|
+
|
|
196
|
+
`appearance-example.component.ts`
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import { Component } from "@angular/core";
|
|
200
|
+
import { QrCodeComponent } from "@nanahoshi/mona-ui/qr-code";
|
|
201
|
+
|
|
202
|
+
@Component({
|
|
203
|
+
selector: "app-qr-code-appearance-example",
|
|
204
|
+
imports: [QrCodeComponent],
|
|
205
|
+
templateUrl: "./appearance-example.component.html"
|
|
206
|
+
})
|
|
207
|
+
export class QrCodeAppearanceExampleComponent {
|
|
208
|
+
protected readonly link = "https://nanahoshi.dev/mona-ui";
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`appearance-example.component.html`
|
|
213
|
+
|
|
214
|
+
```html
|
|
215
|
+
<div class="flex flex-wrap items-end gap-6">
|
|
216
|
+
<mona-qr-code aria-label="QR code for the Mona UI docs" [size]="96" [value]="link" />
|
|
217
|
+
<mona-qr-code aria-label="QR code for the Mona UI docs" color="#1e3a8a" background="#eff6ff" [value]="link" />
|
|
218
|
+
<div class="w-40 rounded-lg bg-white p-3 shadow-sm">
|
|
219
|
+
<mona-qr-code
|
|
220
|
+
aria-label="QR code for the Mona UI docs"
|
|
221
|
+
background="transparent"
|
|
222
|
+
size="100%"
|
|
223
|
+
[quietZone]="0"
|
|
224
|
+
[value]="link" />
|
|
225
|
+
</div>
|
|
226
|
+
</div>
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Size
|
|
230
|
+
|
|
231
|
+
`size` sets the width and height, quiet zone included. A number is pixels, and a string is any CSS length, such as `"100%"` to fill a container. The host stays square, and it never grows wider than its container, so a fixed size shrinks on a narrow screen instead of overflowing.
|
|
232
|
+
|
|
233
|
+
### Colors
|
|
234
|
+
|
|
235
|
+
`color` paints the dark modules and `background` the light ones and the quiet zone. Both take any CSS color, including theme tokens such as `var(--color-foreground)`.
|
|
236
|
+
|
|
237
|
+
The defaults are black on white in every theme, because readers need dark modules on a light background with strong contrast between them. A light-on-dark code only reads on some readers. `background="transparent"` lets the page show through, which suits a light card but not a dark one.
|
|
238
|
+
|
|
239
|
+
### Quiet zone
|
|
240
|
+
|
|
241
|
+
`quietZone` is the light border around the code, in modules. Readers look for 4, the default. Reduce it only where the surroundings are light anyway, such as on a white card with its own padding.
|
|
242
|
+
|
|
243
|
+
### Custom class
|
|
244
|
+
|
|
245
|
+
Merge Tailwind or custom classes onto the host with `class`, such as `class="rounded-md shadow-sm"`.
|
|
246
|
+
|
|
247
|
+
## Behavior
|
|
248
|
+
|
|
249
|
+
### Encoding and capacity
|
|
250
|
+
|
|
251
|
+
The component chooses the smallest of the 40 QR code versions that holds the value, from 21 modules a side to 177. It splits the value into numeric, alphanumeric and byte segments where switching modes saves space, so a long run of digits, like an order number, takes less room than the same count of letters.
|
|
252
|
+
|
|
253
|
+
| `encoding` | Stores | Notes |
|
|
254
|
+
|------------|--------|-------|
|
|
255
|
+
| `"utf-8"` | Any text | The default. Text with characters outside ASCII starts with an ECI header that tells readers it's UTF-8. ASCII needs no header. |
|
|
256
|
+
| `"iso-8859-1"` | Latin-1 only | What readers assume when there's no header. Some older readers don't support ECI; others read every value as UTF-8, so non-ASCII Latin-1 text can come out garbled there. |
|
|
257
|
+
|
|
258
|
+
A version 40 code holds up to 2,953 bytes, 4,296 alphanumeric characters or 7,089 digits at error correction `"L"`, and less at higher levels. A value it can't hold, or a character the encoding can't store, draws nothing. `error` then says why, and a warning is logged in development.
|
|
259
|
+
|
|
260
|
+
### Error correction
|
|
261
|
+
|
|
262
|
+
| `errorCorrection` | Recovers about | Use it for |
|
|
263
|
+
|-------------------|----------------|------------|
|
|
264
|
+
| `"L"` | 7% | The most data in the smallest code, shown clean on a screen |
|
|
265
|
+
| `"M"` | 15% | Most codes. The default. |
|
|
266
|
+
| `"Q"` | 25% | Codes that get worn or dirty, such as on packaging |
|
|
267
|
+
| `"H"` | 30% | Codes with a logo overlay |
|
|
268
|
+
|
|
269
|
+
A higher level holds less, so the same value needs a larger code.
|
|
270
|
+
|
|
271
|
+
### Rendering
|
|
272
|
+
|
|
273
|
+
The code is SVG: a rectangle for the background and one path for every dark module, drawn with `crispEdges` so modules meet without hairlines. It stays sharp at any zoom and in print, costs a handful of elements whatever the code's size, and scales with its box without being redrawn. There's no canvas mode; the PNG export draws to a canvas when you need a raster image.
|
|
274
|
+
|
|
275
|
+
### Forced colors
|
|
276
|
+
|
|
277
|
+
The component opts out of forced colors with `forced-color-adjust: none`, because a code the system recolors may not scan. It keeps the colors you set, which is why the default background is white rather than transparent.
|
|
278
|
+
|
|
279
|
+
## Forms Integration
|
|
280
|
+
|
|
281
|
+
The component displays a value rather than editing one, so it isn't a form control. To check a field before its value reaches the code, use `validateQrCodeValue()`, with the same `encoding` and `errorCorrection` as the component. It returns why the value can't be encoded, or `null`, without drawing anything, so it can run on every keystroke in a signal form's `validate()`.
|
|
282
|
+
|
|
283
|
+
`validation-example.component.ts`
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import { Component, signal } from "@angular/core";
|
|
287
|
+
import { form, FormField, validate } from "@angular/forms/signals";
|
|
288
|
+
import { LabelComponent } from "@nanahoshi/mona-ui/label";
|
|
289
|
+
import { QrCodeComponent, validateQrCodeValue } from "@nanahoshi/mona-ui/qr-code";
|
|
290
|
+
import { TextBoxComponent } from "@nanahoshi/mona-ui/text-box";
|
|
291
|
+
|
|
292
|
+
@Component({
|
|
293
|
+
selector: "app-qr-code-validation-example",
|
|
294
|
+
imports: [FormField, LabelComponent, QrCodeComponent, TextBoxComponent],
|
|
295
|
+
templateUrl: "./validation-example.component.html"
|
|
296
|
+
})
|
|
297
|
+
export class QrCodeValidationExampleComponent {
|
|
298
|
+
readonly #model = signal({ label: "Café Zürich, table 12" });
|
|
299
|
+
// The label printer's scanner reads Latin-1 only, so the codes are stored without a UTF-8 header.
|
|
300
|
+
protected readonly shelf = form(this.#model, path => {
|
|
301
|
+
validate(path.label, ({ value }) => {
|
|
302
|
+
const error = validateQrCodeValue(value(), { encoding: "iso-8859-1" });
|
|
303
|
+
switch (error?.code) {
|
|
304
|
+
case "unsupported-character":
|
|
305
|
+
return { kind: "qrCode", message: `The scanner can't read "${error.character}". Remove it.` };
|
|
306
|
+
case "too-long":
|
|
307
|
+
return { kind: "qrCode", message: "This is too long for a QR code. Shorten it." };
|
|
308
|
+
default:
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
`validation-example.component.html`
|
|
317
|
+
|
|
318
|
+
```html
|
|
319
|
+
<div class="flex max-w-sm flex-col items-start gap-3">
|
|
320
|
+
<mona-label text="Label text">
|
|
321
|
+
<mona-text-box aria-describedby="qr-code-label-error" [formField]="shelf.label" />
|
|
322
|
+
</mona-label>
|
|
323
|
+
<p aria-live="polite" class="text-sm text-error" id="qr-code-label-error">
|
|
324
|
+
@for (error of shelf.label().errors(); track error.kind) {
|
|
325
|
+
{{ error.message }}
|
|
326
|
+
}
|
|
327
|
+
</p>
|
|
328
|
+
@if (shelf.label().valid()) {
|
|
329
|
+
<mona-qr-code
|
|
330
|
+
encoding="iso-8859-1"
|
|
331
|
+
[aria-label]="'QR code for ' + shelf.label().value()"
|
|
332
|
+
[value]="shelf.label().value()" />
|
|
333
|
+
}
|
|
334
|
+
</div>
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Accessibility
|
|
338
|
+
|
|
339
|
+
The host has `role="img"` while there's a code to show, named by `aria-label`, or by the localized "QR code" when it's empty. The SVG inside is hidden from assistive technology. With nothing to show, the host has neither, so an empty box isn't announced as an image.
|
|
340
|
+
|
|
341
|
+
Consumer responsibilities:
|
|
342
|
+
|
|
343
|
+
- Set `aria-label` to say what scanning the code does, such as "QR code for the event page". "QR code" alone doesn't say where it leads.
|
|
344
|
+
- Show what the code holds as text or a link nearby, for those who can't scan it.
|
|
345
|
+
- Keep strong contrast between `color` and `background`; readers need it more than people do.
|
|
346
|
+
|
|
347
|
+
## API
|
|
348
|
+
|
|
349
|
+
Everything below is imported from `@nanahoshi/mona-ui/qr-code`. Each name links to its reference page, with every input, output, property and method.
|
|
350
|
+
|
|
351
|
+
| Name | Kind | Description |
|
|
352
|
+
| --- | --- | --- |
|
|
353
|
+
| [`QrCodeComponent`](api/qr-code.md#qrcodecomponent) | Component | Draws a value as a QR code in SVG, and exports it as a PNG, SVG or PDF file. |
|
|
354
|
+
| [`QrCodeExportError`](api/qr-code.md#qrcodeexporterror) | Class | The error a QR code export rejects with. |
|
|
355
|
+
| [`validateQrCodeValue`](api/qr-code.md#validateqrcodevalue) | Function | Checks whether a QR code can hold a value with the given settings, without drawing it: every character has to fit the encoding, and the whole value the largest code. |
|
|
356
|
+
|
|
357
|
+
### Types
|
|
358
|
+
|
|
359
|
+
| Type | Kind | Description |
|
|
360
|
+
| --- | --- | --- |
|
|
361
|
+
| [`QR_CODE_DEFAULT_MESSAGES`](api/qr-code.md#qr-code-default-messages) | Constant | |
|
|
362
|
+
| [`QrCodeDownloadOptions`](api/qr-code.md#qrcodedownloadoptions) | Interface | What to export a QR code as, and the name of the file to save. |
|
|
363
|
+
| [`QrCodeExportOptions`](api/qr-code.md#qrcodeexportoptions) | Interface | What to export a QR code as. |
|
|
364
|
+
| [`QrCodeExportResult`](api/qr-code.md#qrcodeexportresult) | Interface | An exported QR code. |
|
|
365
|
+
| [`QrCodeImageOverlay`](api/qr-code.md#qrcodeimageoverlay) | Interface | An image, such as a logo, drawn over the center of the code. |
|
|
366
|
+
| [`QrCodeSwissOverlay`](api/qr-code.md#qrcodeswissoverlay) | Interface | The Swiss cross of a Swiss QR code, the code on a Swiss QR-bill, drawn over the center at the size the standard sets. |
|
|
367
|
+
| [`QrCodeTooLongError`](api/qr-code.md#qrcodetoolongerror) | Interface | A value too long for the largest QR code (version 40) at the chosen error correction level. |
|
|
368
|
+
| [`QrCodeUnsupportedCharacterError`](api/qr-code.md#qrcodeunsupportedcharactererror) | Interface | A character the chosen encoding can't store, such as `€` in `"iso-8859-1"`. |
|
|
369
|
+
| [`QrCodeValidationOptions`](api/qr-code.md#qrcodevalidationoptions) | Interface | The settings a value is checked against, matching the component's inputs of the same names. |
|
|
370
|
+
| [`QrCodeEncoding`](api/qr-code.md#qrcodeencoding) | Type alias | The character set a QR code stores text in. - `"utf-8"`: any text. |
|
|
371
|
+
| [`QrCodeErrorCorrection`](api/qr-code.md#qrcodeerrorcorrection) | Type alias | How much of the code can be damaged or hidden and still read: about 7% at `"L"`, 15% at `"M"`, 25% at `"Q"` and 30% at `"H"`. |
|
|
372
|
+
| [`QrCodeExportErrorCode`](api/qr-code.md#qrcodeexporterrorcode) | Type alias | Why a QR code export failed. - `"not-ready"`: there's no code to export, because the value is empty or can't be encoded. - `"unsupported-environment"`: the export ran outside a browser, such as during server-side rendering. - `"invalid-size"`: the size isn't a positive number, or a PNG of that size can't give each module a pixel. - `"resource-load-failed"`: the overlay image didn't load, or another origin didn't allow reading it. - `"png-rasterization-failed"`: the browser couldn't draw the PNG. - `"pdf-generation-failed"`: `jspdf` isn't installed, or failed to write the page. |
|
|
373
|
+
| [`QrCodeExportFormat`](api/qr-code.md#qrcodeexportformat) | Type alias | The file a QR code exports to: a `"png"` image, an `"svg"` document or a `"pdf"` page. |
|
|
374
|
+
| [`QrCodeOverlay`](api/qr-code.md#qrcodeoverlay) | Type alias | What to draw over the center of the code. |
|
|
375
|
+
| [`QrCodeValueError`](api/qr-code.md#qrcodevalueerror) | Type alias | Why a value can't be stored in a QR code. |
|
|
376
|
+
|
|
377
|
+
**Related types:** [`MonaQrCodeMessages`](api/i18n.md#monaqrcodemessages)
|