qrcodesolid 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +42 -0
- package/README.md +495 -2
- package/dist/color-parse-empty.d.ts +6 -0
- package/dist/color-parse-empty.js +7 -0
- package/dist/color-parse.d.ts +8 -0
- package/dist/color-parse.js +49 -0
- package/dist/index.d.ts +372 -0
- package/dist/index.js +2640 -0
- package/dist/paint-ChCPh-rm.d.ts +29 -0
- package/dist/payload.d.ts +53 -0
- package/dist/payload.js +93 -0
- package/dist/server.d.ts +399 -0
- package/dist/server.js +2833 -0
- package/dist/source/components/GradientDef.jsx +19 -0
- package/dist/source/components/QRCode.jsx +10 -0
- package/dist/source/components/QRCodeCanvas.jsx +72 -0
- package/dist/source/components/QRCodeSVG.jsx +57 -0
- package/dist/source/core/caption.js +77 -0
- package/dist/source/core/color-names.js +34 -0
- package/dist/source/core/color-parse-empty.js +5 -0
- package/dist/source/core/color-parse.js +41 -0
- package/dist/source/core/encode.js +120 -0
- package/dist/source/core/logo.js +90 -0
- package/dist/source/core/options.js +56 -0
- package/dist/source/core/output.js +23 -0
- package/dist/source/core/paint.js +30 -0
- package/dist/source/core/path.js +46 -0
- package/dist/source/core/qrcodegen.js +875 -0
- package/dist/source/core/scene.js +141 -0
- package/dist/source/core/stages.js +24 -0
- package/dist/source/core/warnings.js +38 -0
- package/dist/source/export/index.js +105 -0
- package/dist/source/index.js +16 -0
- package/dist/source/payload/contact.js +42 -0
- package/dist/source/payload/escape.js +3 -0
- package/dist/source/payload/index.js +3 -0
- package/dist/source/payload/message.js +19 -0
- package/dist/source/payload/place.js +40 -0
- package/dist/source/primitives/createColorScheme.js +18 -0
- package/dist/source/primitives/createPixelRatio.js +25 -0
- package/dist/source/primitives/createQRCode.js +47 -0
- package/dist/source/render/canvas.js +46 -0
- package/dist/source/render/color.js +63 -0
- package/dist/source/render/image.js +123 -0
- package/dist/source/render/svg-string.js +55 -0
- package/dist/source/shapes/bars.js +50 -0
- package/dist/source/shapes/finders.js +30 -0
- package/dist/source/shapes/frames.js +62 -0
- package/dist/source/shapes/modules.js +39 -0
- package/dist/source/shapes/types.js +8 -0
- package/llms.txt +117 -0
- package/package.json +97 -4
- package/skills/qrcodesolid/SKILL.md +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rizal Rinaldi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
This package includes a modified copy of the QR Code generator library (TypeScript) in
|
|
26
|
+
`src/core/qrcodegen.ts`.
|
|
27
|
+
|
|
28
|
+
Copyright (c) Project Nayuki. (MIT License)
|
|
29
|
+
https://www.nayuki.io/page/qr-code-generator-library
|
|
30
|
+
|
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
32
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
33
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
34
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
35
|
+
furnished to do so, subject to the following conditions: the above copyright notice and this
|
|
36
|
+
permission notice shall be included in all copies or substantial portions of the Software.
|
|
37
|
+
|
|
38
|
+
The Software is provided "as is", without warranty of any kind, express or implied, including but
|
|
39
|
+
not limited to the warranties of merchantability, fitness for a particular purpose and
|
|
40
|
+
noninfringement. In no event shall the authors or copyright holders be liable for any claim,
|
|
41
|
+
damages or other liability, whether in an action of contract, tort or otherwise, arising from,
|
|
42
|
+
out of or in connection with the Software or the use or other dealings in the Software.
|
package/README.md
CHANGED
|
@@ -1,3 +1,496 @@
|
|
|
1
|
-
#
|
|
1
|
+
# qrcodesolid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/qrcodesolid)
|
|
4
|
+
[](https://bundlephobia.com/package/qrcodesolid)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
[Live playground](https://rizaleow.github.io/qrcodesolid/) · [llms.txt](https://rizaleow.github.io/qrcodesolid/llms.txt)
|
|
8
|
+
|
|
9
|
+
QR code components for [Solid 2](https://v2.solidjs.com). SVG and canvas renderers with the same
|
|
10
|
+
output, custom module and finder shapes, gradients, a logo with a scan guard, captions, frames,
|
|
11
|
+
headless export functions and payload helpers.
|
|
12
|
+
|
|
13
|
+
- Zero runtime dependencies. Peers: `solid-js` and `@solidjs/web` 2.
|
|
14
|
+
- ESM only, `sideEffects: false`, SSR-safe.
|
|
15
|
+
- About 10 KB gzip for `QRCodeSVG`, 11 KB for both renderers, 1.4 KB for `qrcodesolid/payload`
|
|
16
|
+
(minified, peers excluded).
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
bun add qrcodesolid
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick start
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { QRCode } from 'qrcodesolid';
|
|
28
|
+
|
|
29
|
+
function Share() {
|
|
30
|
+
return <QRCode value="https://solidjs.com" size={192} title="Solid website" />;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`QRCode` renders SVG by default. Set `renderAs="canvas"` for a canvas, or import the renderer you
|
|
35
|
+
need: `QRCodeSVG` or `QRCodeCanvas`. Importing one renderer keeps the other out of your bundle.
|
|
36
|
+
|
|
37
|
+
Pass values, not accessors, as props. Props stay reactive:
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { createSignal } from 'solid-js';
|
|
41
|
+
import { QRCodeSVG } from 'qrcodesolid';
|
|
42
|
+
|
|
43
|
+
function Live() {
|
|
44
|
+
const [url, setUrl] = createSignal('https://solidjs.com');
|
|
45
|
+
return (
|
|
46
|
+
<>
|
|
47
|
+
<input value={url()} onInput={(e) => setUrl(e.currentTarget.value)} />
|
|
48
|
+
<QRCodeSVG value={url()} />
|
|
49
|
+
</>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A colour change repaints only. A shape change rebuilds paths only. Only `value`, `level`,
|
|
55
|
+
`encoding` or adding or removing `image` re-encodes.
|
|
56
|
+
|
|
57
|
+
## Props
|
|
58
|
+
|
|
59
|
+
`QRCode`, `QRCodeSVG`, `QRCodeCanvas`, `createQRCode` and the export functions share one options
|
|
60
|
+
type, `QRCodeOptions`.
|
|
61
|
+
|
|
62
|
+
| Prop | Type | Default | Notes |
|
|
63
|
+
| -------------- | -------------------------------------------------- | ------------------------------ | --------------------------------------------------------------------- |
|
|
64
|
+
| `value` | `string \| string[]` | required | Array items encode in order, each in its own mode. |
|
|
65
|
+
| `size` | `number` | `128` | CSS pixels. |
|
|
66
|
+
| `level` | `'L' \| 'M' \| 'Q' \| 'H'` | `'M'`, `'Q'` with an `image` | Error correction. |
|
|
67
|
+
| `margin` | `number` | `4` | Quiet zone in modules. The QR spec asks for 4. |
|
|
68
|
+
| `fg` | `Paint` | `'var(--qr-fg, #000)'` | A colour or a `Gradient`. |
|
|
69
|
+
| `bg` | `Paint` | `'var(--qr-bg, #fff)'` | `'transparent'` draws no background. |
|
|
70
|
+
| `modules` | `ShapeInput` | `square` | See [Shapes](#shapes). |
|
|
71
|
+
| `finder` | `{ outer?, inner?, color?, innerColor? }` | square parts, `fg` colour | `innerColor` defaults to `color`. |
|
|
72
|
+
| `image` | `ImageOptions` | none | See [Logo](#logo-and-scan-guard). |
|
|
73
|
+
| `caption` | `CaptionOptions` | none | See [Caption and frames](#caption-and-frames). |
|
|
74
|
+
| `frame` | `FramePlugin \| FramePlugin[]` | none | `border()`, `card()`, `banner()`, your own, or a stack of them. |
|
|
75
|
+
| `title` | `string` | none | Accessible name. Without it, `role="img"` has no name. |
|
|
76
|
+
| `id` | `string` | none | Sets the element id and the gradient and clip id prefix. |
|
|
77
|
+
| `encoding` | `{ minVersion?, maxVersion?, mask?, boostLevel? }` | `minVersion` 2 with an `image` | Passed to the encoder. `mask` -1 picks the best mask, 0-7 forces one. |
|
|
78
|
+
| `renderAs` | `'svg' \| 'canvas'` | `'svg'` | `QRCode` only. |
|
|
79
|
+
| `onReady` | `(canvas: HTMLCanvasElement) => void` | none | Canvas only. Runs after a draw, once the image has loaded or failed. |
|
|
80
|
+
| `repaint` | any value | none | Canvas only. A new value redraws the canvas. See [Theming](#theming). |
|
|
81
|
+
| `imageTimeout` | `number` | `10000` | Canvas only. Milliseconds before the logo load gives up. |
|
|
82
|
+
|
|
83
|
+
Other attributes, such as `class`, `style` and event handlers, pass through to the `<svg>` or
|
|
84
|
+
`<canvas>`.
|
|
85
|
+
|
|
86
|
+
Data that does not fit throws a `QRCapacityError` (with `version`, `level` and `dataLength`).
|
|
87
|
+
Catch it with `<Errored>`.
|
|
88
|
+
|
|
89
|
+
### Theming
|
|
90
|
+
|
|
91
|
+
The default colours read the CSS custom properties `--qr-fg` and `--qr-bg`, so a theme can set
|
|
92
|
+
them without props. `currentColor` also works.
|
|
93
|
+
|
|
94
|
+
```css
|
|
95
|
+
.dark {
|
|
96
|
+
--qr-fg: #e5e7eb;
|
|
97
|
+
--qr-bg: #111827;
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`QRCodeSVG` keeps `var()` in its markup, so a CSS-only theme change applies at once. The canvas
|
|
102
|
+
resolves colours when it draws. It redraws by itself when `prefers-color-scheme` changes. For a
|
|
103
|
+
theme that a class or attribute switches, pass the theme as `repaint`: a new value redraws it.
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import { QRCodeCanvas } from 'qrcodesolid';
|
|
107
|
+
import { createSignal } from 'solid-js';
|
|
108
|
+
|
|
109
|
+
function ThemedCanvas() {
|
|
110
|
+
const [theme, setTheme] = createSignal('light');
|
|
111
|
+
return (
|
|
112
|
+
<div data-theme={theme()}>
|
|
113
|
+
<button onClick={() => setTheme((t) => (t === 'light' ? 'dark' : 'light'))}>Theme</button>
|
|
114
|
+
<QRCodeCanvas value="https://solidjs.com" repaint={theme()} />
|
|
115
|
+
</div>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`createColorScheme()` returns the `prefers-color-scheme` accessor the canvas uses: `'light'` or
|
|
121
|
+
`'dark'`, and `'light'` on the server.
|
|
122
|
+
|
|
123
|
+
## Shapes
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import { QRCodeSVG, dots } from 'qrcodesolid';
|
|
127
|
+
|
|
128
|
+
function Styled() {
|
|
129
|
+
return (
|
|
130
|
+
<QRCodeSVG
|
|
131
|
+
value="https://solidjs.com"
|
|
132
|
+
modules={dots}
|
|
133
|
+
finder={{ outer: 'rounded', inner: 'dot', color: '#6d28d9' }}
|
|
134
|
+
/>
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
| Kind | Presets |
|
|
140
|
+
| ------------ | ------------------------------------------------------------------------ |
|
|
141
|
+
| Modules | `square`, `dots`, `rounded`, `diamond`, `verticalBars`, `horizontalBars` |
|
|
142
|
+
| Finder outer | `'square'`, `'rounded'`, `'circle'` |
|
|
143
|
+
| Finder inner | `'square'`, `'rounded'`, `'dot'`, `'diamond'` |
|
|
144
|
+
|
|
145
|
+
Our tests decode every module preset with every finder pair at every level with ZXing. The
|
|
146
|
+
weaker jsQR decoder is tested on every module preset with the default finder only. Test your own
|
|
147
|
+
design with the scanners your users have before you print it.
|
|
148
|
+
|
|
149
|
+
`dots`, `diamond` and the bar shapes draw the alignment pattern solid, so scanners can find it.
|
|
150
|
+
|
|
151
|
+
### Custom shapes
|
|
152
|
+
|
|
153
|
+
A module shape draws one dark cell. It gets the cell's top-left corner in scene units (1 unit = 1
|
|
154
|
+
module), its four neighbours, and an `alignment` flag. It returns SVG path data.
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
import { QRCodeSVG, defineModuleShape } from 'qrcodesolid';
|
|
158
|
+
|
|
159
|
+
const plus = defineModuleShape(({ x, y, alignment }) =>
|
|
160
|
+
alignment
|
|
161
|
+
? `M${x} ${y}h1v1h-1z`
|
|
162
|
+
: `M${x + 0.3} ${y}h0.4v0.3h0.3v0.4h-0.3v0.3h-0.4v-0.3h-0.3v-0.4h0.3z`,
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
function Plus() {
|
|
166
|
+
return <QRCodeSVG value="plus" modules={plus} />;
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
A plain function also works: `modules={({ x, y }) => ...}`.
|
|
171
|
+
|
|
172
|
+
A layer shape draws the whole module layer at once, for example to merge runs of cells. It gets
|
|
173
|
+
one `LayerContext` object: the `matrix`, an `include(x, y)` test (false for finders and the logo
|
|
174
|
+
area) and the `offset` of matrix cell (0, 0) in scene units.
|
|
175
|
+
|
|
176
|
+
```tsx
|
|
177
|
+
import { QRCodeSVG, defineLayerShape } from 'qrcodesolid';
|
|
178
|
+
|
|
179
|
+
const blocks = defineLayerShape(({ matrix, include, offset }) => {
|
|
180
|
+
let d = '';
|
|
181
|
+
for (let y = 0; y < matrix.size; y++)
|
|
182
|
+
for (let x = 0; x < matrix.size; x++)
|
|
183
|
+
if (include(x, y) && matrix.dark[y * matrix.size + x])
|
|
184
|
+
d += `M${x + offset.x} ${y + offset.y}h1v1h-1z`;
|
|
185
|
+
return d;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
function Blocks() {
|
|
189
|
+
return <QRCodeSVG value="blocks" modules={blocks} />;
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
A finder part is a function `(x, y) => pathData`, where `(x, y)` is the top-left corner of the 7×7
|
|
194
|
+
pattern. The outer part fills with `evenodd`, so draw the ring as an outline plus a hole.
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
import { QRCodeSVG } from 'qrcodesolid';
|
|
198
|
+
|
|
199
|
+
function Finder() {
|
|
200
|
+
return (
|
|
201
|
+
<QRCodeSVG
|
|
202
|
+
value="finder"
|
|
203
|
+
finder={{ outer: (x, y) => `M${x} ${y}h7v7h-7z M${x + 1} ${y + 1}h5v5h-5z`, inner: 'dot' }}
|
|
204
|
+
/>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
`getFinder(style)` returns the `FinderParts` (`{ outer, inner }`) for a `FinderStyle`. A `Layer.fill` is a
|
|
210
|
+
`PlacedPaint`: a colour or a gradient with absolute coordinates.
|
|
211
|
+
|
|
212
|
+
Keep shapes inside their cell and keep enough contrast. Decode a custom shape before you ship it.
|
|
213
|
+
|
|
214
|
+
## Gradients
|
|
215
|
+
|
|
216
|
+
A `Paint` is a CSS colour or a `Gradient`: `{ type: 'linear', rotate?, stops }` or
|
|
217
|
+
`{ type: 'radial', stops }`. Stops (`GradientStop`) are colours, spread evenly, or
|
|
218
|
+
`[offset, colour]` pairs; a gradient needs at least two stops, checked at runtime. `rotate` is in degrees; 0 runs left to
|
|
219
|
+
right.
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
import { QRCodeSVG } from 'qrcodesolid';
|
|
223
|
+
|
|
224
|
+
function Gradients() {
|
|
225
|
+
return (
|
|
226
|
+
<QRCodeSVG
|
|
227
|
+
value="https://solidjs.com"
|
|
228
|
+
fg={{ type: 'linear', rotate: 45, stops: ['#6d28d9', '#db2777'] }}
|
|
229
|
+
finder={{
|
|
230
|
+
color: {
|
|
231
|
+
type: 'radial',
|
|
232
|
+
stops: [
|
|
233
|
+
[0, '#db2777'],
|
|
234
|
+
[1, '#6d28d9'],
|
|
235
|
+
],
|
|
236
|
+
},
|
|
237
|
+
}}
|
|
238
|
+
/>
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Every gradient spans the QR box (the code plus its margin), in SVG and on canvas alike. This holds
|
|
244
|
+
for frame and caption paints too: they do not get their own box.
|
|
245
|
+
|
|
246
|
+
## Logo and scan guard
|
|
247
|
+
|
|
248
|
+
```tsx
|
|
249
|
+
import { QRCodeSVG } from 'qrcodesolid';
|
|
250
|
+
|
|
251
|
+
function Logo() {
|
|
252
|
+
return (
|
|
253
|
+
<QRCodeSVG
|
|
254
|
+
value="https://solidjs.com/docs/getting-started"
|
|
255
|
+
image={{ src: '/logo.svg', size: 0.22, padding: 1, radius: 0.25 }}
|
|
256
|
+
/>
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
| `image` field | Default | Notes |
|
|
262
|
+
| ------------- | -------- | ------------------------------------------------------------ |
|
|
263
|
+
| `src` | required | |
|
|
264
|
+
| `size` | `0.2` | Width as a fraction of the code width, margin excluded. |
|
|
265
|
+
| `aspect` | `1` | Width over height. |
|
|
266
|
+
| `padding` | `1` | Clear modules around the image. |
|
|
267
|
+
| `radius` | `0` | Corner radius, 0 to 0.5 of the shorter side. |
|
|
268
|
+
| `excavate` | `true` | Removes the modules under the image. |
|
|
269
|
+
| `opacity` | `1` | |
|
|
270
|
+
| `crossOrigin` | none | A `CrossOrigin`: `''`, `'anonymous'` or `'use-credentials'`. |
|
|
271
|
+
|
|
272
|
+
The image keeps its aspect ratio inside its box. A logo hides modules, so the library guards the
|
|
273
|
+
scan:
|
|
274
|
+
|
|
275
|
+
- With an image and no `level`, the level is `Q`.
|
|
276
|
+
- With an image and no `encoding.minVersion`, the version is at least 2 (capped by `maxVersion`).
|
|
277
|
+
Version 1 has no room for a logo.
|
|
278
|
+
- The logo box (image plus padding) may cover only a set share of the modules for its level, and
|
|
279
|
+
only data cells (and the centre alignment pattern from version 7). A short value gives a small code, so its logo shrinks, or is dropped if it would
|
|
280
|
+
be under 2 modules wide. Raise `encoding.minVersion` to keep a larger logo.
|
|
281
|
+
- Level `L` is not recommended with a logo: its budget fits a tiny logo at best.
|
|
282
|
+
|
|
283
|
+
In development builds the library warns in the console when it reduces or drops a logo, when
|
|
284
|
+
solid `fg` and `bg` have a contrast under 3:1, when `fg` is lighter than `bg`, when `margin` is
|
|
285
|
+
under 2, or when the caption is still wider than the code at the minimum font size. The colour
|
|
286
|
+
checks read opaque `#hex`, `rgb()`, `hsl()` and CSS named colours, and skip `var()` and gradients.
|
|
287
|
+
The caption width is an estimate. Production builds skip these checks. The colour parser and its
|
|
288
|
+
named-colour table load through the `#color-parse` import, which resolves to an empty stand-in
|
|
289
|
+
unless your bundler sets the `development` condition, so they stay out of production bundles.
|
|
290
|
+
|
|
291
|
+
## Caption and frames
|
|
292
|
+
|
|
293
|
+
```tsx
|
|
294
|
+
import { QRCodeSVG, banner, border, card } from 'qrcodesolid';
|
|
295
|
+
|
|
296
|
+
function Framed() {
|
|
297
|
+
return (
|
|
298
|
+
<>
|
|
299
|
+
<QRCodeSVG value="caption" caption={{ text: 'solidjs.com', position: 'bottom' }} />
|
|
300
|
+
<QRCodeSVG value="border" frame={border({ width: 1, radius: 2 })} />
|
|
301
|
+
<QRCodeSVG value="card" frame={card({ padding: 2, color: '#f3f4f6' })} />
|
|
302
|
+
<QRCodeSVG value="banner" frame={banner({ text: 'Scan me', color: '#6d28d9' })} />
|
|
303
|
+
<QRCodeSVG value="stack" frame={[border(), banner({ text: 'Scan me' })]} />
|
|
304
|
+
</>
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
- `caption`: `{ text, position?: 'top' | 'bottom', fontSize?: 2, fontFamily?, fontWeight?, color? }`.
|
|
310
|
+
`fontSize` is in modules. `color` defaults to `fg`. The caption sits outside the quiet zone.
|
|
311
|
+
Text that looks wider than the code with its margin shrinks to fit, down to 1 module, and its
|
|
312
|
+
band shrinks with it. `banner` text does the same. The width is an estimate.
|
|
313
|
+
- `border({ width?: 1, radius?: 0, color?: fg })`, `card({ padding?: 2, radius?: 2, color?: '#fff' })`,
|
|
314
|
+
`banner({ text, position?: 'bottom', color?: '#000', textColor?: '#fff', fontSize?: 2 })`.
|
|
315
|
+
- The frame wraps the caption. `size` sets the width; a caption or banner makes the code taller.
|
|
316
|
+
- `frame` also takes an array, applied from the inside out. `[border(), banner({ text: 'Scan me' })]`
|
|
317
|
+
draws the border first, and the banner wraps it. Inner frames move with the code.
|
|
318
|
+
|
|
319
|
+
A frame is a `FramePlugin`: a function from the inner layout to a new layout plus paths and texts.
|
|
320
|
+
A path's `paint` is a `Paint` or a `PaintKey` (`'fg'`, `'bg'`, `'finder'`, `'finderInner'`,
|
|
321
|
+
`'caption'`), which follows the matching prop.
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
import { QRCodeSVG, type FramePlugin, type PaintKey } from 'qrcodesolid';
|
|
325
|
+
|
|
326
|
+
const outline =
|
|
327
|
+
(paint: PaintKey = 'fg'): FramePlugin =>
|
|
328
|
+
(inner) => {
|
|
329
|
+
const layout = {
|
|
330
|
+
width: inner.width + 2,
|
|
331
|
+
height: inner.height + 2,
|
|
332
|
+
qr: { ...inner.qr, x: inner.qr.x + 1, y: inner.qr.y + 1 },
|
|
333
|
+
};
|
|
334
|
+
const [w, h] = [layout.width, layout.height];
|
|
335
|
+
const d = `M0 0h${w}v${h}h-${w}z M1 1h${w - 2}v${h - 2}h-${w - 2}z`;
|
|
336
|
+
return { layout, paths: [{ id: 'frame-outline', d, fillRule: 'evenodd', paint }], texts: [] };
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
function Outlined() {
|
|
340
|
+
return <QRCodeSVG value="outline" frame={outline()} />;
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
A frame may move `qr` and grow the layout. It must not resize `qr`.
|
|
345
|
+
|
|
346
|
+
## Accessibility
|
|
347
|
+
|
|
348
|
+
Both renderers set `role="img"` and name the code the same way. Give every code a `title` that
|
|
349
|
+
says where it leads: the SVG gets a `<title>` with `aria-labelledby`, and the canvas gets
|
|
350
|
+
`aria-label`. A `title` takes precedence over your own `aria-label`. Your own `aria-labelledby`
|
|
351
|
+
outranks both, and with it neither renderer sets a default label. An empty `title` counts as no
|
|
352
|
+
title. With none of these, the accessible name is `QR code`.
|
|
353
|
+
|
|
354
|
+
## Export
|
|
355
|
+
|
|
356
|
+
Headless functions take the same options and need no mounted component.
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
import { copyToClipboard, download, toDataURL, toPNGBlob, toSVGString } from 'qrcodesolid';
|
|
360
|
+
|
|
361
|
+
const options = { value: 'https://solidjs.com', size: 512 };
|
|
362
|
+
|
|
363
|
+
const svg = toSVGString(options); // keeps var() colours
|
|
364
|
+
const file = toSVGString({ ...options, resolveColors: true }); // standalone colours
|
|
365
|
+
const png = await toPNGBlob({ ...options, scale: 2 });
|
|
366
|
+
const url = await toDataURL(options, 'image/svg+xml');
|
|
367
|
+
await download(options, 'qrcode.svg');
|
|
368
|
+
await copyToClipboard(options);
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
| Function | Returns | Runs in |
|
|
372
|
+
| ------------------------------ | -------------------------------- | -------------------------- |
|
|
373
|
+
| `toSVGString(options)` | SVG document string | anywhere, Node included |
|
|
374
|
+
| `toScene(options)` | the `Scene` the renderers draw | anywhere |
|
|
375
|
+
| `toCanvas(options, canvas?)` | a canvas (OffscreenCanvas first) | browser or worker |
|
|
376
|
+
| `toPNGBlob(options)` | PNG `Blob` | browser or worker |
|
|
377
|
+
| `toDataURL(options, type?)` | PNG or SVG data URL | browser or worker |
|
|
378
|
+
| `download(options, filename?)` | saves a file; `.svg` saves SVG | browser |
|
|
379
|
+
| `copyToClipboard(options)` | copies a PNG | browser, in a user gesture |
|
|
380
|
+
|
|
381
|
+
`toCanvas` and `toPNGBlob` also take `scale` (pixels per CSS pixel, default 1).
|
|
382
|
+
|
|
383
|
+
Colours:
|
|
384
|
+
|
|
385
|
+
- `toSVGString` keeps `var()` and `currentColor`, for inline SVG that follows page CSS. Pass
|
|
386
|
+
`resolveColors: true` to replace them.
|
|
387
|
+
- PNG output, data URLs and downloaded SVG files have no page CSS, so they resolve colours to the
|
|
388
|
+
`var()` fallback. `currentColor` becomes `#000`. Pass `element` to resolve them against that
|
|
389
|
+
element instead, as `QRCodeCanvas` does.
|
|
390
|
+
|
|
391
|
+
Logos:
|
|
392
|
+
|
|
393
|
+
- A downloaded or data-URL SVG fetches the logo and embeds it as a data URL, so the file stands
|
|
394
|
+
alone. The fetch uses CORS and follows `crossOrigin`. If it fails, the SVG keeps the logo URL
|
|
395
|
+
and the console shows a warning. `toSVGString` stays synchronous and keeps the URL.
|
|
396
|
+
- In the DOM, a cross-origin logo without CORS taints the canvas. Then `toPNGBlob` rejects with a
|
|
397
|
+
`SecurityError`, and so do the PNG paths of `toDataURL`, `download` and `copyToClipboard`. Serve
|
|
398
|
+
the logo with CORS and set `crossOrigin: 'anonymous'`, or use a same-origin URL.
|
|
399
|
+
- In a worker, the logo loads with `fetch` in CORS mode. If that fails, the code draws without the
|
|
400
|
+
logo.
|
|
401
|
+
- A logo load gives up after `imageTimeout` milliseconds (default 10000), in the DOM and in a
|
|
402
|
+
fetch. `QRCodeCanvas` takes `imageTimeout` too. A logo fetch is rejected when it is larger than `imageMaxBytes` (default 2 MB, checked by
|
|
403
|
+
`Content-Length` and while the body streams). `imageMaxBytes` does not apply to the DOM raster
|
|
404
|
+
path, because the browser loads the image itself. Every export function takes both options.
|
|
405
|
+
- The export functions warn about a failed logo in production builds too, since a missing logo is
|
|
406
|
+
otherwise silent. `QRCodeCanvas` and `QRCodeSVG` warn in dev builds only. Each warning gives the
|
|
407
|
+
reason, such as the timeout or the size limit.
|
|
408
|
+
|
|
409
|
+
## Payloads
|
|
410
|
+
|
|
411
|
+
`qrcodesolid/payload` builds the strings that phone scanners act on, with correct escaping.
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
import { event, geo, mailto, mecard, sms, tel, vcard, wifi } from 'qrcodesolid/payload';
|
|
415
|
+
|
|
416
|
+
wifi({ ssid: 'Home', password: 'p;ss', auth: 'WPA' }); // WIFI:T:WPA;S:Home;P:p\;ss;;
|
|
417
|
+
vcard({ name: { first: 'Ada', last: 'Lovelace' }, email: 'ada@example.com' });
|
|
418
|
+
mecard({ name: 'Ada Lovelace', phone: '+15551234567' });
|
|
419
|
+
mailto({ to: 'hi@example.com', subject: 'Hello' });
|
|
420
|
+
sms({ to: '+15551234567', body: 'Hi' });
|
|
421
|
+
tel('+1 (555) 123-4567'); // tel:+15551234567
|
|
422
|
+
geo({ lat: 51.5, lng: -0.12 });
|
|
423
|
+
event({ title: 'Launch', start: new Date('2027-01-01T10:00:00Z') });
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
## Headless rendering
|
|
427
|
+
|
|
428
|
+
`createQRCode(options)` returns accessors for the `matrix`, `geometry` and `scene`, for your own
|
|
429
|
+
renderer. Pass it the props object, not a snapshot, so it stays reactive.
|
|
430
|
+
|
|
431
|
+
```tsx
|
|
432
|
+
import { createQRCode, type QRCodeOptions } from 'qrcodesolid';
|
|
433
|
+
|
|
434
|
+
function ModuleCount(props: QRCodeOptions) {
|
|
435
|
+
const { matrix } = createQRCode(props);
|
|
436
|
+
return (
|
|
437
|
+
<p>
|
|
438
|
+
Version {matrix().version}, {matrix().size}×{matrix().size} modules
|
|
439
|
+
</p>
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
`encode(value, options)` returns the raw `QRMatrix` without Solid. Its `level` defaults to `'L'`, not the
|
|
445
|
+
components' `'M'`. `drawScene(ctx, scene, options)`
|
|
446
|
+
draws a scene on any 2D context.
|
|
447
|
+
`sceneToSVGString(scene, { size, idPrefix, title? })` writes a scene as an SVG document, and
|
|
448
|
+
`resolveSceneColors(scene, element?)` replaces its `var()` and `currentColor` paints with plain
|
|
449
|
+
colours.
|
|
450
|
+
|
|
451
|
+
## SSR
|
|
452
|
+
|
|
453
|
+
- `QRCodeSVG` renders on the server. Its ids come from `createUniqueId`, so they match on hydration.
|
|
454
|
+
- `QRCodeCanvas` renders an empty `<canvas>` on the server and draws in the browser.
|
|
455
|
+
- The `solid` export condition ships JSX source, so your Solid compiler builds it for DOM or SSR.
|
|
456
|
+
Without that condition, Node resolves an SSR build and browsers a DOM build.
|
|
457
|
+
- `toSVGString` runs in Node, for example to write a static SVG file.
|
|
458
|
+
|
|
459
|
+
## Migrating
|
|
460
|
+
|
|
461
|
+
From qrcode.react and qrcode.vue:
|
|
462
|
+
|
|
463
|
+
| qrcode.react | qrcode.vue | qrcodesolid |
|
|
464
|
+
| ----------------------------- | -------------------------------------------------------------------- | -------------------------------------------------- |
|
|
465
|
+
| `fgColor` | `foreground` | `fg` |
|
|
466
|
+
| `bgColor` | `background` | `bg` |
|
|
467
|
+
| `marginSize`, `includeMargin` | `margin` | `margin` (in modules) |
|
|
468
|
+
| `imageSettings` | `imageSettings` | `image`; see the logo notes below |
|
|
469
|
+
| none | `gradient`, `gradientType`, `gradientStartColor`, `gradientEndColor` | `fg={{ type, rotate, stops }}` |
|
|
470
|
+
| none | `radius` | `modules={rounded}` |
|
|
471
|
+
| `QRCodeSVG`, `QRCodeCanvas` | `renderAs` | unchanged: `renderAs`, `QRCodeSVG`, `QRCodeCanvas` |
|
|
472
|
+
| `minVersion`, `boostLevel` | none | `encoding={{ minVersion, boostLevel }}` |
|
|
473
|
+
|
|
474
|
+
Watch the defaults:
|
|
475
|
+
|
|
476
|
+
- `level` is `'L'` in qrcode.react and qrcode.vue, `'M'` here, and `'Q'` here with a logo.
|
|
477
|
+
- qrcode.vue's `renderAs` defaults to `'canvas'`. Here `renderAs` defaults to `'svg'`.
|
|
478
|
+
- `margin` is 4 here. qrcode.react and qrcode.vue default to 0, which drops the quiet zone the spec
|
|
479
|
+
asks for. Pass `margin={0}` to match them, if the page gives the code its own white space.
|
|
480
|
+
- `image.size` is a fraction of the code width without margin. To convert pixel `width` on a
|
|
481
|
+
`size`-pixel code with margin `m` and `n` modules: `width / size * (n + 2m) / n`. Roughly
|
|
482
|
+
`width / size` with no margin.
|
|
483
|
+
- With an image, the level defaults to `Q` and the logo may shrink to keep the code scannable.
|
|
484
|
+
- The logo is always centred: `imageSettings.x` and `y` have no equivalent. Pixel `width` and
|
|
485
|
+
`height` map to `image.size` (a fraction of the width) plus `image.aspect` (width / height).
|
|
486
|
+
|
|
487
|
+
## Credits
|
|
488
|
+
|
|
489
|
+
- The encoder is [Project Nayuki's QR Code generator](https://www.nayuki.io/page/qr-code-generator-library),
|
|
490
|
+
MIT, vendored with its licence header.
|
|
491
|
+
- The API and render approach draw on [qrcode.react](https://github.com/zpao/qrcode.react) and
|
|
492
|
+
[qrcode.vue](https://github.com/scopewu/qrcode.vue).
|
|
493
|
+
|
|
494
|
+
## License
|
|
495
|
+
|
|
496
|
+
MIT © Rizal Rinaldi
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { r as Paint } from "./paint-ChCPh-rm.js";
|
|
2
|
+
/**
|
|
3
|
+
* The production stand-in for `parseColor`. Only the dev warnings read colours, so
|
|
4
|
+
* production bundles skip the parser and its named-colour table.
|
|
5
|
+
*/
|
|
6
|
+
export declare const parseColor: (_paint: Paint) => number[] | null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { r as Paint } from "./paint-ChCPh-rm.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses #rgb, #rrggbb, rgb(), hsl() and CSS named colours to 0-255 channels.
|
|
4
|
+
* Returns null for anything else, and for a colour that is not opaque.
|
|
5
|
+
* `warnings.ts` imports it as `#color-parse`: the `development` condition resolves here, and any
|
|
6
|
+
* other build gets `color-parse-empty.ts`, so production bundles skip the named-colour table.
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseColor(paint: Paint): number[] | null;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/core/color-names.ts
|
|
2
|
+
const words = "aliceblue f0f8ff antiquewhite faebd7 aqua 00ffff aquamarine 7fffd4 azure f0ffff beige f5f5dc bisque ffe4c4 black 000000 blanchedalmond ffebcd blue 0000ff blueviolet 8a2be2 brown a52a2a burlywood deb887 cadetblue 5f9ea0 chartreuse 7fff00 chocolate d2691e coral ff7f50 cornflowerblue 6495ed cornsilk fff8dc crimson dc143c cyan 00ffff darkblue 00008b darkcyan 008b8b darkgoldenrod b8860b darkgray a9a9a9 darkgreen 006400 darkgrey a9a9a9 darkkhaki bdb76b darkmagenta 8b008b darkolivegreen 556b2f darkorange ff8c00 darkorchid 9932cc darkred 8b0000 darksalmon e9967a darkseagreen 8fbc8f darkslateblue 483d8b darkslategray 2f4f4f darkslategrey 2f4f4f darkturquoise 00ced1 darkviolet 9400d3 deeppink ff1493 deepskyblue 00bfff dimgray 696969 dimgrey 696969 dodgerblue 1e90ff firebrick b22222 floralwhite fffaf0 forestgreen 228b22 fuchsia ff00ff gainsboro dcdcdc ghostwhite f8f8ff gold ffd700 goldenrod daa520 gray 808080 green 008000 greenyellow adff2f grey 808080 honeydew f0fff0 hotpink ff69b4 indianred cd5c5c indigo 4b0082 ivory fffff0 khaki f0e68c lavender e6e6fa lavenderblush fff0f5 lawngreen 7cfc00 lemonchiffon fffacd lightblue add8e6 lightcoral f08080 lightcyan e0ffff lightgoldenrodyellow fafad2 lightgray d3d3d3 lightgreen 90ee90 lightgrey d3d3d3 lightpink ffb6c1 lightsalmon ffa07a lightseagreen 20b2aa lightskyblue 87cefa lightslategray 778899 lightslategrey 778899 lightsteelblue b0c4de lightyellow ffffe0 lime 00ff00 limegreen 32cd32 linen faf0e6 magenta ff00ff maroon 800000 mediumaquamarine 66cdaa mediumblue 0000cd mediumorchid ba55d3 mediumpurple 9370db mediumseagreen 3cb371 mediumslateblue 7b68ee mediumspringgreen 00fa9a mediumturquoise 48d1cc mediumvioletred c71585 midnightblue 191970 mintcream f5fffa mistyrose ffe4e1 moccasin ffe4b5 navajowhite ffdead navy 000080 oldlace fdf5e6 olive 808000 olivedrab 6b8e23 orange ffa500 orangered ff4500 orchid da70d6 palegoldenrod eee8aa palegreen 98fb98 paleturquoise afeeee palevioletred db7093 papayawhip ffefd5 peachpuff ffdab9 peru cd853f pink ffc0cb plum dda0dd powderblue b0e0e6 purple 800080 rebeccapurple 663399 red ff0000 rosybrown bc8f8f royalblue 4169e1 saddlebrown 8b4513 salmon fa8072 sandybrown f4a460 seagreen 2e8b57 seashell fff5ee sienna a0522d silver c0c0c0 skyblue 87ceeb slateblue 6a5acd slategray 708090 slategrey 708090 snow fffafa springgreen 00ff7f steelblue 4682b4 tan d2b48c teal 008080 thistle d8bfd8 tomato ff6347 turquoise 40e0d0 violet ee82ee wheat f5deb3 white ffffff whitesmoke f5f5f5 yellow ffff00 yellowgreen 9acd32".split(" ");
|
|
3
|
+
const colorNames = new Map(words.flatMap((word, i) => i % 2 === 0 ? [[word, `#${words[i + 1]}`]] : []));
|
|
4
|
+
// src/core/color-parse.ts
|
|
5
|
+
const alphaPart = String.raw`(?:\s*[,/]\s*([\d.]+)(%)?)?\s*\)$`;
|
|
6
|
+
const rgbPattern = new RegExp(String.raw`^rgba?\(\s*(\d+)[\s,]+(\d+)[\s,]+(\d+)` + alphaPart);
|
|
7
|
+
const hslPattern = new RegExp(String.raw`^hsla?\(\s*(-?[\d.]+)(?:deg)?[\s,]+([\d.]+)%[\s,]+([\d.]+)%` + alphaPart);
|
|
8
|
+
const isOpaque = (alpha, percent) => alpha === void 0 || Number(alpha) >= (percent ? 100 : 1);
|
|
9
|
+
/** HSL with hue in degrees and saturation and lightness in 0-1, to 0-255 channels. */
|
|
10
|
+
function hslToRgb(hue, saturation, lightness) {
|
|
11
|
+
const a = saturation * Math.min(lightness, 1 - lightness);
|
|
12
|
+
return [
|
|
13
|
+
0,
|
|
14
|
+
8,
|
|
15
|
+
4
|
|
16
|
+
].map((n) => {
|
|
17
|
+
const k = (n + hue / 30) % 12;
|
|
18
|
+
return Math.round(255 * (lightness - a * Math.max(-1, Math.min(k - 3, 9 - k, 1))));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parses #rgb, #rrggbb, rgb(), hsl() and CSS named colours to 0-255 channels.
|
|
23
|
+
* Returns null for anything else, and for a colour that is not opaque.
|
|
24
|
+
* `warnings.ts` imports it as `#color-parse`: the `development` condition resolves here, and any
|
|
25
|
+
* other build gets `color-parse-empty.ts`, so production bundles skip the named-colour table.
|
|
26
|
+
*/
|
|
27
|
+
function parseColor(paint) {
|
|
28
|
+
if (typeof paint !== "string") return null;
|
|
29
|
+
const s = paint.trim().toLowerCase();
|
|
30
|
+
const named = colorNames.get(s);
|
|
31
|
+
if (named) return parseColor(named);
|
|
32
|
+
const hex = /^#([\da-f]{3}|[\da-f]{6})$/.exec(s)?.[1];
|
|
33
|
+
if (hex) {
|
|
34
|
+
const full = hex.length === 3 ? [...hex].map((c) => c + c).join("") : hex;
|
|
35
|
+
return [
|
|
36
|
+
0,
|
|
37
|
+
2,
|
|
38
|
+
4
|
|
39
|
+
].map((i) => parseInt(full.slice(i, i + 2), 16));
|
|
40
|
+
}
|
|
41
|
+
const rgb = rgbPattern.exec(s);
|
|
42
|
+
if (rgb) return isOpaque(rgb[4], rgb[5]) ? rgb.slice(1, 4).map(Number) : null;
|
|
43
|
+
const hsl = hslPattern.exec(s);
|
|
44
|
+
if (!hsl || !isOpaque(hsl[4], hsl[5])) return null;
|
|
45
|
+
const hue = (Number(hsl[1]) % 360 + 360) % 360;
|
|
46
|
+
const clamp = (v) => Math.min(1, Math.max(0, Number(v) / 100));
|
|
47
|
+
return hslToRgb(hue, clamp(hsl[2]), clamp(hsl[3]));
|
|
48
|
+
}
|
|
49
|
+
export { parseColor };
|