@digigov/ui 0.26.2 → 0.26.4

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/admin/AutoComplete/index.d.ts +3 -3
  3. package/admin/AutoComplete/index.js +16 -6
  4. package/admin/CopyToClipboard/__stories__/Default.js +3 -3
  5. package/admin/CopyToClipboard/__stories__/MultipleCopies.js +3 -3
  6. package/admin/CopyToClipboard/index.d.ts +3 -2
  7. package/admin/CopyToClipboard/index.js +6 -4
  8. package/app/Footer/Footer.stories.d.ts +1 -0
  9. package/app/Footer/Footer.stories.js +14 -0
  10. package/app/Footer/__stories__/FooterWithLogo.js +1 -1
  11. package/app/Footer/__stories__/FooterWithSecondaryNavigation.js +1 -1
  12. package/app/Footer/__stories__/YearCopyright.d.ts +2 -0
  13. package/app/Footer/__stories__/YearCopyright.js +44 -0
  14. package/app/QrCodeViewer/QRCode.stories.d.ts +8 -0
  15. package/app/QrCodeViewer/QRCode.stories.js +46 -0
  16. package/app/QrCodeViewer/__stories__/Custom.d.ts +2 -0
  17. package/app/QrCodeViewer/__stories__/Custom.js +37 -0
  18. package/app/QrCodeViewer/__stories__/Default.d.ts +2 -0
  19. package/app/QrCodeViewer/__stories__/Default.js +24 -0
  20. package/app/QrCodeViewer/index.d.ts +8 -0
  21. package/app/QrCodeViewer/index.js +248 -0
  22. package/app/QrCodeViewer/index.mdx +21 -0
  23. package/app/QrCodeViewer/qrcodegen.d.ts +100 -0
  24. package/app/QrCodeViewer/qrcodegen.js +1086 -0
  25. package/app/QrCodeViewer/types.d.ts +29 -0
  26. package/app/QrCodeViewer/types.js +5 -0
  27. package/app/QrCodeViewer/utils.d.ts +11 -0
  28. package/app/QrCodeViewer/utils.js +108 -0
  29. package/app/index.d.ts +1 -0
  30. package/app/index.js +13 -0
  31. package/core/FileUpload/index.d.ts +1 -0
  32. package/core/FileUpload/index.js +14 -0
  33. package/es/admin/AutoComplete/index.js +16 -6
  34. package/es/admin/CopyToClipboard/__stories__/Default.js +2 -2
  35. package/es/admin/CopyToClipboard/__stories__/MultipleCopies.js +3 -3
  36. package/es/admin/CopyToClipboard/index.js +6 -4
  37. package/es/app/Footer/Footer.stories.js +2 -1
  38. package/es/app/Footer/__stories__/FooterWithLogo.js +1 -1
  39. package/es/app/Footer/__stories__/FooterWithSecondaryNavigation.js +1 -1
  40. package/es/app/Footer/__stories__/YearCopyright.js +22 -0
  41. package/es/app/QrCodeViewer/QRCode.stories.js +8 -0
  42. package/es/app/QrCodeViewer/__stories__/Custom.js +22 -0
  43. package/es/app/QrCodeViewer/__stories__/Default.js +11 -0
  44. package/es/app/QrCodeViewer/index.js +224 -0
  45. package/es/app/QrCodeViewer/index.mdx +21 -0
  46. package/es/app/QrCodeViewer/qrcodegen.js +1085 -0
  47. package/es/app/QrCodeViewer/types.js +1 -0
  48. package/es/app/QrCodeViewer/utils.js +95 -0
  49. package/es/app/index.js +1 -0
  50. package/es/core/FileUpload/index.js +1 -0
  51. package/es/govgr/Footer/Copyright.js +7 -10
  52. package/es/layouts/Grid/__stories__/Default.js +19 -18
  53. package/es/layouts/Grid/__stories__/Inline.js +19 -18
  54. package/es/registry.js +8 -0
  55. package/esm/admin/AutoComplete/index.js +16 -6
  56. package/esm/admin/CopyToClipboard/__stories__/Default.js +2 -2
  57. package/esm/admin/CopyToClipboard/__stories__/MultipleCopies.js +3 -3
  58. package/esm/admin/CopyToClipboard/index.js +6 -4
  59. package/esm/app/Footer/Footer.stories.js +2 -1
  60. package/esm/app/Footer/__stories__/FooterWithLogo.js +1 -1
  61. package/esm/app/Footer/__stories__/FooterWithSecondaryNavigation.js +1 -1
  62. package/esm/app/Footer/__stories__/YearCopyright.js +22 -0
  63. package/esm/app/QrCodeViewer/QRCode.stories.js +8 -0
  64. package/esm/app/QrCodeViewer/__stories__/Custom.js +22 -0
  65. package/esm/app/QrCodeViewer/__stories__/Default.js +11 -0
  66. package/esm/app/QrCodeViewer/index.js +224 -0
  67. package/esm/app/QrCodeViewer/index.mdx +21 -0
  68. package/esm/app/QrCodeViewer/qrcodegen.js +1085 -0
  69. package/esm/app/QrCodeViewer/types.js +1 -0
  70. package/esm/app/QrCodeViewer/utils.js +95 -0
  71. package/esm/app/index.js +1 -0
  72. package/esm/core/FileUpload/index.js +1 -0
  73. package/esm/govgr/Footer/Copyright.js +7 -10
  74. package/esm/index.js +1 -1
  75. package/esm/layouts/Grid/__stories__/Default.js +19 -18
  76. package/esm/layouts/Grid/__stories__/Inline.js +19 -18
  77. package/esm/registry.js +8 -0
  78. package/govgr/Footer/Copyright.d.ts +1 -0
  79. package/govgr/Footer/Copyright.js +9 -8
  80. package/layouts/Grid/__stories__/Default.js +20 -18
  81. package/layouts/Grid/__stories__/Inline.js +20 -18
  82. package/package.json +3 -3
  83. package/registry.d.ts +4 -0
  84. package/registry.js +12 -0
@@ -0,0 +1,224 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
4
+ var _excluded = ["value", "size", "level", "bgColor", "fgColor", "includeMargin", "style", "imageSettings"],
5
+ _excluded2 = ["value", "size", "level", "bgColor", "fgColor", "includeMargin", "imageSettings"],
6
+ _excluded3 = ["renderAs"];
7
+ import React, { useRef, useEffect, useState } from 'react';
8
+ import qrcodegen from '@digigov/ui/app/QrCodeViewer/qrcodegen';
9
+ import { generatePath, excavateModules, getImageSettings, MARGIN_SIZE } from '@digigov/ui/app/QrCodeViewer/utils';
10
+ var DEFAULT_SIZE = 128;
11
+ var DEFAULT_LEVEL = 'L';
12
+ var DEFAULT_BGCOLOR = '#FFFFFF';
13
+ var DEFAULT_FGCOLOR = '#000000';
14
+ var DEFAULT_INCLUDEMARGIN = false;
15
+
16
+ var SUPPORTS_PATH2D = function () {
17
+ try {
18
+ new Path2D().addPath(new Path2D());
19
+ } catch (e) {
20
+ return false;
21
+ }
22
+
23
+ return true;
24
+ }();
25
+
26
+ var ERROR_LEVEL_MAP = {
27
+ L: qrcodegen.QrCode.Ecc.LOW,
28
+ M: qrcodegen.QrCode.Ecc.MEDIUM,
29
+ Q: qrcodegen.QrCode.Ecc.QUARTILE,
30
+ H: qrcodegen.QrCode.Ecc.HIGH
31
+ };
32
+
33
+ function QrCodeViewerCanvas(props) {
34
+ var value = props.value,
35
+ _props$size = props.size,
36
+ size = _props$size === void 0 ? DEFAULT_SIZE : _props$size,
37
+ _props$level = props.level,
38
+ level = _props$level === void 0 ? DEFAULT_LEVEL : _props$level,
39
+ _props$bgColor = props.bgColor,
40
+ bgColor = _props$bgColor === void 0 ? DEFAULT_BGCOLOR : _props$bgColor,
41
+ _props$fgColor = props.fgColor,
42
+ fgColor = _props$fgColor === void 0 ? DEFAULT_FGCOLOR : _props$fgColor,
43
+ _props$includeMargin = props.includeMargin,
44
+ includeMargin = _props$includeMargin === void 0 ? DEFAULT_INCLUDEMARGIN : _props$includeMargin,
45
+ style = props.style,
46
+ imageSettings = props.imageSettings,
47
+ otherProps = _objectWithoutProperties(props, _excluded);
48
+
49
+ var imgSrc = imageSettings === null || imageSettings === void 0 ? void 0 : imageSettings.src;
50
+
51
+ var _canvas = useRef(null);
52
+
53
+ var _image = useRef(null); // We're just using this state to trigger rerenders when images load. We
54
+ // Don't actually read the value anywhere. A smarter use of useEffect would
55
+ // depend on this value.
56
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
57
+
58
+
59
+ var _useState = useState(false),
60
+ _useState2 = _slicedToArray(_useState, 2),
61
+ isImgLoaded = _useState2[0],
62
+ setIsImageLoaded = _useState2[1];
63
+
64
+ useEffect(function () {
65
+ // Always update the canvas. It's cheap enough and we want to be correct
66
+ // with the current state.
67
+ if (_canvas.current !== null) {
68
+ var canvas = _canvas.current;
69
+ var ctx = canvas.getContext('2d');
70
+
71
+ if (!ctx) {
72
+ return;
73
+ }
74
+
75
+ var cells = qrcodegen.QrCode.encodeText(value, ERROR_LEVEL_MAP[level]).getModules();
76
+ var margin = includeMargin ? MARGIN_SIZE : 0;
77
+ var numCells = cells.length + margin * 2;
78
+ var calculatedImageSettings = getImageSettings(cells, size, includeMargin, imageSettings);
79
+ var image = _image.current;
80
+ var haveImageToRender = calculatedImageSettings != null && image !== null && image.complete && image.naturalHeight !== 0 && image.naturalWidth !== 0;
81
+
82
+ if (haveImageToRender && calculatedImageSettings) {
83
+ if (calculatedImageSettings.excavation != null) {
84
+ cells = excavateModules(cells, calculatedImageSettings.excavation);
85
+ }
86
+ } // We're going to scale this so that the number of drawable units
87
+ // matches the number of cells. This avoids rounding issues, but does
88
+ // result in some potentially unwanted single pixel issues between
89
+ // blocks, only in environments that don't support Path2D.
90
+
91
+
92
+ var pixelRatio = window.devicePixelRatio || 1;
93
+ canvas.height = canvas.width = size * pixelRatio;
94
+ var scale = size / numCells * pixelRatio;
95
+ ctx.scale(scale, scale); // Draw solid background, only paint dark modules.
96
+
97
+ ctx.fillStyle = bgColor;
98
+ ctx.fillRect(0, 0, numCells, numCells);
99
+ ctx.fillStyle = fgColor;
100
+
101
+ if (SUPPORTS_PATH2D) {
102
+ // $FlowFixMe: Path2D c'tor doesn't support args yet.
103
+ ctx.fill(new Path2D(generatePath(cells, margin)));
104
+ } else {
105
+ cells.forEach(function (row, rdx) {
106
+ row.forEach(function (cell, cdx) {
107
+ if (cell) {
108
+ ctx.fillRect(cdx + margin, rdx + margin, 1, 1);
109
+ }
110
+ });
111
+ });
112
+ }
113
+
114
+ if (haveImageToRender && calculatedImageSettings) {
115
+ if (image instanceof HTMLImageElement) {
116
+ ctx.drawImage(image, calculatedImageSettings.x + margin, calculatedImageSettings.y + margin, calculatedImageSettings.w, calculatedImageSettings.h);
117
+ }
118
+ }
119
+ }
120
+ }); // Ensure we mark image loaded as false here so we trigger updating the
121
+ // canvas in our other effect.
122
+
123
+ useEffect(function () {
124
+ setIsImageLoaded(false);
125
+ }, [imgSrc, isImgLoaded]);
126
+
127
+ var canvasStyle = _extends({
128
+ height: size,
129
+ width: size
130
+ }, style);
131
+
132
+ var img;
133
+
134
+ if (imgSrc != null) {
135
+ img = /*#__PURE__*/React.createElement("img", {
136
+ src: imgSrc,
137
+ key: imgSrc,
138
+ style: {
139
+ display: 'none'
140
+ },
141
+ onLoad: function onLoad() {
142
+ setIsImageLoaded(true);
143
+ },
144
+ ref: _image
145
+ });
146
+ }
147
+
148
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("canvas", _extends({
149
+ style: canvasStyle,
150
+ height: size,
151
+ width: size,
152
+ ref: _canvas
153
+ }, otherProps)), img);
154
+ }
155
+
156
+ function QrCodeViewerSVG(props) {
157
+ var value = props.value,
158
+ _props$size2 = props.size,
159
+ size = _props$size2 === void 0 ? DEFAULT_SIZE : _props$size2,
160
+ _props$level2 = props.level,
161
+ level = _props$level2 === void 0 ? DEFAULT_LEVEL : _props$level2,
162
+ _props$bgColor2 = props.bgColor,
163
+ bgColor = _props$bgColor2 === void 0 ? DEFAULT_BGCOLOR : _props$bgColor2,
164
+ _props$fgColor2 = props.fgColor,
165
+ fgColor = _props$fgColor2 === void 0 ? DEFAULT_FGCOLOR : _props$fgColor2,
166
+ _props$includeMargin2 = props.includeMargin,
167
+ includeMargin = _props$includeMargin2 === void 0 ? DEFAULT_INCLUDEMARGIN : _props$includeMargin2,
168
+ imageSettings = props.imageSettings,
169
+ otherProps = _objectWithoutProperties(props, _excluded2);
170
+
171
+ var cells = qrcodegen.QrCode.encodeText(value, ERROR_LEVEL_MAP[level]).getModules();
172
+ var margin = includeMargin ? MARGIN_SIZE : 0;
173
+ var numCells = cells.length + margin * 2;
174
+ var calculatedImageSettings = getImageSettings(cells, size, includeMargin, imageSettings);
175
+ var image;
176
+
177
+ if (imageSettings != null && calculatedImageSettings != null) {
178
+ if (calculatedImageSettings.excavation != null) {
179
+ cells = excavateModules(cells, calculatedImageSettings.excavation);
180
+ }
181
+
182
+ image = /*#__PURE__*/React.createElement("image", {
183
+ xlinkHref: imageSettings.src,
184
+ height: calculatedImageSettings.h,
185
+ width: calculatedImageSettings.w,
186
+ x: calculatedImageSettings.x + margin,
187
+ y: calculatedImageSettings.y + margin,
188
+ preserveAspectRatio: "none"
189
+ });
190
+ } // Drawing strategy: instead of a rect per module, we're going to create a
191
+ // single path for the dark modules and layer that on top of a light rect,
192
+ // for a total of 2 DOM nodes. We pay a bit more in string concat but that's
193
+ // way faster than DOM ops.
194
+ // For level 1, 441 nodes -> 2
195
+ // For level 40, 31329 -> 2
196
+
197
+
198
+ var fgPath = generatePath(cells, margin);
199
+ return /*#__PURE__*/React.createElement("svg", _extends({
200
+ height: size,
201
+ width: size,
202
+ viewBox: "0 0 ".concat(numCells, " ").concat(numCells)
203
+ }, otherProps), /*#__PURE__*/React.createElement("path", {
204
+ fill: bgColor,
205
+ d: "M0,0 h".concat(numCells, "v").concat(numCells, "H0z"),
206
+ shapeRendering: "crispEdges"
207
+ }), /*#__PURE__*/React.createElement("path", {
208
+ fill: fgColor,
209
+ d: fgPath,
210
+ shapeRendering: "crispEdges"
211
+ }), image);
212
+ }
213
+
214
+ export var QrCodeViewer = function QrCodeViewer(props) {
215
+ var renderAs = props.renderAs,
216
+ otherProps = _objectWithoutProperties(props, _excluded3);
217
+
218
+ if (renderAs === 'svg') {
219
+ return /*#__PURE__*/React.createElement(QrCodeViewerSVG, otherProps);
220
+ }
221
+
222
+ return /*#__PURE__*/React.createElement(QrCodeViewerCanvas, otherProps);
223
+ };
224
+ export default QrCodeViewer;
@@ -0,0 +1,21 @@
1
+ ---
2
+ id: qr-code
3
+ title: QrCodeViewer
4
+
5
+ ---
6
+
7
+ ```bash
8
+ import QrCodeViewer from '@digigov/ui/app/QrCodeViewer';
9
+ ```
10
+
11
+ ## How to use
12
+
13
+ ### Default QrCodeViewer
14
+
15
+ <Story packageName="@digigov/ui" component="app/QrCodeViewer" story="Default.tsx" />
16
+
17
+ ### Custom QrCodeViewer
18
+
19
+ <Story packageName="@digigov/ui" component="app/QrCodeViewer" story="Custom.tsx" />
20
+
21
+