@commmerce/ui 1.3.0 → 1.5.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.
Files changed (106) hide show
  1. package/THIRD_PARTY_NOTICES.txt +101 -0
  2. package/dist/calendar.js +15 -19
  3. package/dist/cdn.js +1669 -1193
  4. package/dist/components/accordion.js +9 -8
  5. package/dist/components/action-bar.js +15 -15
  6. package/dist/components/action-menu.js +10 -9
  7. package/dist/components/app-card.js +15 -14
  8. package/dist/components/app-frame.d.ts +15 -0
  9. package/dist/components/app-frame.js +22 -2
  10. package/dist/components/badge.d.ts +2 -1
  11. package/dist/components/badge.js +4 -3
  12. package/dist/components/banner.js +6 -5
  13. package/dist/components/board.js +18 -18
  14. package/dist/components/bottom-navigation.d.ts +1 -1
  15. package/dist/components/bottom-navigation.js +9 -7
  16. package/dist/components/bottom-sheet.d.ts +1 -1
  17. package/dist/components/bottom-sheet.js +9 -8
  18. package/dist/components/button.d.ts +3 -3
  19. package/dist/components/button.js +22 -19
  20. package/dist/components/calendar.d.ts +380 -0
  21. package/dist/components/calendar.js +1480 -0
  22. package/dist/components/card.d.ts +1 -1
  23. package/dist/components/card.js +18 -12
  24. package/dist/components/checkbox.js +8 -7
  25. package/dist/components/color-field.js +9 -13
  26. package/dist/components/combobox.d.ts +8 -0
  27. package/dist/components/combobox.js +18 -18
  28. package/dist/components/comment.d.ts +1 -1
  29. package/dist/components/comment.js +12 -13
  30. package/dist/components/data-table.js +12 -11
  31. package/dist/components/date-picker.js +1 -1
  32. package/dist/components/date-range-picker.js +7 -7
  33. package/dist/components/empty-state.d.ts +1 -1
  34. package/dist/components/empty-state.js +16 -16
  35. package/dist/components/file-upload.js +18 -16
  36. package/dist/components/filter-panel.js +1 -1
  37. package/dist/components/flash-offer-bar.js +8 -7
  38. package/dist/components/global-search.d.ts +1 -1
  39. package/dist/components/global-search.js +12 -9
  40. package/dist/components/grid.d.ts +1 -1
  41. package/dist/components/grid.js +1 -1
  42. package/dist/components/header-bar.js +38 -31
  43. package/dist/components/info-panel.d.ts +1 -1
  44. package/dist/components/info-panel.js +11 -10
  45. package/dist/components/layout.d.ts +1 -1
  46. package/dist/components/layout.js +1 -1
  47. package/dist/components/link-tabs.js +8 -7
  48. package/dist/components/metric.js +4 -4
  49. package/dist/components/mobile-menu.js +16 -15
  50. package/dist/components/modal.js +2 -1
  51. package/dist/components/nested-list.d.ts +2 -2
  52. package/dist/components/nested-list.js +21 -22
  53. package/dist/components/notification-panel.js +12 -11
  54. package/dist/components/option-group.js +10 -11
  55. package/dist/components/page-header.js +16 -15
  56. package/dist/components/page.d.ts +18 -4
  57. package/dist/components/page.js +22 -7
  58. package/dist/components/pagination.d.ts +1 -1
  59. package/dist/components/pagination.js +16 -9
  60. package/dist/components/progress-bar.js +4 -3
  61. package/dist/components/radio.js +12 -12
  62. package/dist/components/report-tile.d.ts +2 -2
  63. package/dist/components/report-tile.js +18 -18
  64. package/dist/components/resource-list.d.ts +22 -6
  65. package/dist/components/resource-list.js +92 -52
  66. package/dist/components/save-bar.js +8 -8
  67. package/dist/components/search.d.ts +5 -2
  68. package/dist/components/search.js +20 -14
  69. package/dist/components/segmented-control.d.ts +67 -0
  70. package/dist/components/segmented-control.js +76 -0
  71. package/dist/components/select.d.ts +8 -0
  72. package/dist/components/select.js +18 -18
  73. package/dist/components/side-navigation.js +20 -17
  74. package/dist/components/side-panel.js +4 -3
  75. package/dist/components/signature-field.d.ts +153 -0
  76. package/dist/components/signature-field.js +728 -0
  77. package/dist/components/skeleton-display.js +4 -4
  78. package/dist/components/skeleton.d.ts +1 -1
  79. package/dist/components/skeleton.js +2 -2
  80. package/dist/components/smart-tip.d.ts +1 -1
  81. package/dist/components/smart-tip.js +16 -16
  82. package/dist/components/spinner.js +1 -1
  83. package/dist/components/stack.d.ts +1 -1
  84. package/dist/components/stack.js +1 -1
  85. package/dist/components/status-tabs.d.ts +4 -3
  86. package/dist/components/status-tabs.js +26 -12
  87. package/dist/components/table-header.d.ts +4 -3
  88. package/dist/components/table-header.js +22 -15
  89. package/dist/components/tag.js +3 -4
  90. package/dist/components/text-field.d.ts +1 -1
  91. package/dist/components/text-field.js +8 -15
  92. package/dist/components/thumbnail.js +1 -1
  93. package/dist/components/timeline.js +12 -11
  94. package/dist/components/toast.js +6 -6
  95. package/dist/components/toggle.js +12 -11
  96. package/dist/components/tooltip.js +4 -3
  97. package/dist/custom-elements.json +13655 -9770
  98. package/dist/field.d.ts +15 -0
  99. package/dist/field.js +47 -0
  100. package/dist/index.d.ts +4 -0
  101. package/dist/index.js +3 -0
  102. package/dist/overlay.js +5 -4
  103. package/dist/popover.js +8 -8
  104. package/dist/spacing.d.ts +14 -4
  105. package/dist/spacing.js +13 -4
  106. package/package.json +3 -3
@@ -0,0 +1,728 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _CmSignatureField_instances, _CmSignatureField_internals, _CmSignatureField_id, _CmSignatureField_defaultValue, _CmSignatureField_defaultTyped, _CmSignatureField_strokes, _CmSignatureField_active, _CmSignatureField_image, _CmSignatureField_own, _CmSignatureField_frame, _CmSignatureField_typeTimer, _CmSignatureField_typeFrom, _CmSignatureField_resize, _CmSignatureField_width, _CmSignatureField_observe, _CmSignatureField_canvas_get, _CmSignatureField_fit, _CmSignatureField_draw, _CmSignatureField_paint, _CmSignatureField_render, _CmSignatureField_point, _CmSignatureField_onDown, _CmSignatureField_onMove, _CmSignatureField_onUp, _CmSignatureField_fontFamily_get, _CmSignatureField_loadFont, _CmSignatureField_onType, _CmSignatureField_typed, _CmSignatureField_onTypeChange, _CmSignatureField_sync, _CmSignatureField_emit, _CmSignatureField_export, _CmSignatureField_select, _CmSignatureField_onTabKey, _CmSignatureField_say, _CmSignatureField_announce, _CmSignatureField_keepFocus, _CmSignatureField_onUndo, _CmSignatureField_onClear;
13
+ import { LitElement, html, css, nothing } from 'lit';
14
+ import { live } from 'lit/directives/live.js';
15
+ import { base, define, nextId, logicalKey } from '../base.js';
16
+ import { CmLocalizedElement, defineStrings } from '../localize.js';
17
+ const strings = defineStrings('signatureField', {
18
+ /** Name of the Draw / Type tab list when the field has no label. */
19
+ modes: 'How to sign',
20
+ /** The two tabs. */
21
+ draw: 'Draw',
22
+ type: 'Type',
23
+ /** Accessible name of the drawing area, and the same once something is drawn. */
24
+ pad: 'Signature drawing area',
25
+ padSigned: 'Signature drawing area, signed',
26
+ /** Read with the drawing area: how to draw, and the keyboard alternative. */
27
+ instructions: 'Draw your signature with a mouse, finger or pen. To sign with the keyboard, choose Type.',
28
+ /** Hint above the line while nothing is drawn. */
29
+ signHere: 'Sign here',
30
+ /** Accessible name and placeholder of the box in Type mode. */
31
+ typeLabel: 'Type your full name',
32
+ typePlaceholder: 'Type your full name',
33
+ /** Undo and Clear buttons: visible text and accessible names. */
34
+ undo: 'Undo',
35
+ undoLabel: 'Undo last stroke',
36
+ clear: 'Clear',
37
+ clearLabel: 'Clear signature',
38
+ /** Announced after Clear and Undo. */
39
+ cleared: 'Signature cleared',
40
+ undone: 'Last stroke removed',
41
+ /** Validation message when `required` and nothing is signed. */
42
+ required: 'Please sign here.',
43
+ });
44
+ /** Width of the pad over its height. */
45
+ const RATIO = 3;
46
+ /** Where the signing line sits, as a fraction of the pad's height from the top. */
47
+ const BASELINE = 0.7;
48
+ /** Stroke width at pressure 0.5, as a fraction of the pad's width (3.3px on a 600px pad). */
49
+ const LINE = 0.0055;
50
+ /**
51
+ * Ink of the exported image. Always the light theme's `text-default` (black) on transparent, whatever the theme on
52
+ * screen: a signature goes into documents and PDFs, which are light. On screen the ink is `text-default` of the
53
+ * current theme, so it stays visible in dark.
54
+ */
55
+ const EXPORT_INK = '#000000';
56
+ /** Space around the ink in the exported image, in CSS pixels (doubled with the 2x scale). */
57
+ const PADDING = 8;
58
+ /** Resolution of the exported image. */
59
+ const SCALE = 2;
60
+ /** Typing waits this long before `change` / `cm-change`. */
61
+ const TYPE_DELAY = 400;
62
+ const mid = (a, b) => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, p: (a.p + b.p) / 2 });
63
+ /**
64
+ * Signature field: sign by drawing with a mouse, finger or pen, or by typing a name that is shown in a handwriting
65
+ * font. Gives a PNG of the signature, cropped to the ink, as a data URL (`value`, `toDataURL()`) or a Blob
66
+ * (`toBlob()`). Participates in native forms: submits the data URL under `name`; `required` blocks submission
67
+ * until something is signed. For e-sign screens ("Sign now") and signing pages.
68
+ *
69
+ * Load `@commmerce/tokens/fonts.css` (the hosted build's fonts.css) for the typed signature's font, Dancing Script;
70
+ * without it, typed signatures use the system's cursive font.
71
+ * @element cm-signature-field
72
+ * @fires cm-change - After a stroke is finished, Undo, Clear, a switch between Draw and Type, or typing stops for 400ms. `detail`: `{ value, empty, mode }`. A plain `change` event (no detail) follows it, for code that treats every form field alike.
73
+ * @csspart label - The label.
74
+ * @csspart tabs - The Draw / Type tab list.
75
+ * @csspart pad - The signing area (both modes).
76
+ * @csspart canvas - The drawing canvas.
77
+ * @csspart input - The text box in Type mode.
78
+ * @csspart undo - The Undo button (Draw mode).
79
+ * @csspart clear - The Clear button.
80
+ * @cssprop --cm-signature-font - Font of typed signatures. Default: Dancing Script, then cursive system fonts.
81
+ */
82
+ export class CmSignatureField extends CmLocalizedElement {
83
+ constructor() {
84
+ super(...arguments);
85
+ _CmSignatureField_instances.add(this);
86
+ /** Visible label above the field, e.g. "Your signature". Without one, give the element an `aria-label`. */
87
+ this.label = '';
88
+ /** Lowercase bracketed note after the label. */
89
+ this.hint = '';
90
+ /** One sentence under the field in text-tertiary, e.g. what the signer agrees to. */
91
+ this.helpText = '';
92
+ /** Error message from your own validation; turns the pad's border critical and is announced. */
93
+ this.error = '';
94
+ /** Form field name. */
95
+ this.name = '';
96
+ /**
97
+ * The signature as a PNG data URL (`data:image/png;base64,…`), cropped to the ink, or empty when nothing is
98
+ * signed. Updated after every stroke and keystroke. Set it to a PNG or SVG data URL (or any image URL the page
99
+ * may read) to show a saved signature in Draw mode; set it to `''` to clear both modes.
100
+ */
101
+ this.value = '';
102
+ /** The selected tab: `draw` (pointer, touch, pen) or `type` (keyboard). Each keeps its own signature. */
103
+ this.mode = 'draw';
104
+ /** The name typed in Type mode. Set it to prefill the box with the signer's name. */
105
+ this.typedName = '';
106
+ /** Shows a red asterisk and blocks form submission until something is signed. */
107
+ this.required = false;
108
+ /** Blocks interaction. Also set by a disabled `<fieldset>`. */
109
+ this.disabled = false;
110
+ /** @internal Text in the polite status region. */
111
+ this._announce = '';
112
+ _CmSignatureField_internals.set(this, this.attachInternals());
113
+ _CmSignatureField_id.set(this, nextId('cm-sig'));
114
+ _CmSignatureField_defaultValue.set(this, '');
115
+ _CmSignatureField_defaultTyped.set(this, '');
116
+ _CmSignatureField_strokes.set(this, []);
117
+ /** The stroke being drawn and its pointer. */
118
+ _CmSignatureField_active.set(this, void 0);
119
+ /** A saved signature shown under the strokes (from `value`). */
120
+ _CmSignatureField_image.set(this, void 0);
121
+ /** The value this element produced itself; any other value set from outside is loaded as an image. */
122
+ _CmSignatureField_own.set(this, '');
123
+ _CmSignatureField_frame.set(this, 0);
124
+ _CmSignatureField_typeTimer.set(this, void 0);
125
+ /** The value when the current burst of typing started. */
126
+ _CmSignatureField_typeFrom.set(this, '');
127
+ _CmSignatureField_resize.set(this, void 0);
128
+ /** Last laid-out width of the pad, so exports from a hidden pad keep their size. */
129
+ _CmSignatureField_width.set(this, 0);
130
+ }
131
+ connectedCallback() {
132
+ super.connectedCallback();
133
+ if (!this.hasUpdated) {
134
+ __classPrivateFieldSet(this, _CmSignatureField_defaultValue, this.value, "f");
135
+ __classPrivateFieldSet(this, _CmSignatureField_defaultTyped, this.typedName, "f");
136
+ }
137
+ else
138
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_observe).call(this); // moved or re-added: watch the pad's size again
139
+ }
140
+ disconnectedCallback() {
141
+ super.disconnectedCallback();
142
+ __classPrivateFieldGet(this, _CmSignatureField_resize, "f")?.disconnect();
143
+ __classPrivateFieldSet(this, _CmSignatureField_resize, undefined, "f");
144
+ cancelAnimationFrame(__classPrivateFieldGet(this, _CmSignatureField_frame, "f"));
145
+ __classPrivateFieldSet(this, _CmSignatureField_frame, 0, "f");
146
+ if (__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f")) {
147
+ clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
148
+ __classPrivateFieldSet(this, _CmSignatureField_typeTimer, undefined, "f");
149
+ }
150
+ }
151
+ get form() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").form; }
152
+ get validity() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").validity; }
153
+ get validationMessage() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").validationMessage; }
154
+ checkValidity() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").checkValidity(); }
155
+ reportValidity() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").reportValidity(); }
156
+ formResetCallback() {
157
+ __classPrivateFieldSet(this, _CmSignatureField_strokes, [], "f");
158
+ __classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
159
+ __classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
160
+ if (__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f")) {
161
+ clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
162
+ __classPrivateFieldSet(this, _CmSignatureField_typeTimer, undefined, "f");
163
+ }
164
+ this.typedName = __classPrivateFieldGet(this, _CmSignatureField_defaultTyped, "f");
165
+ if (__classPrivateFieldGet(this, _CmSignatureField_defaultValue, "f")) {
166
+ __classPrivateFieldSet(this, _CmSignatureField_own, '', "f");
167
+ this.value = __classPrivateFieldGet(this, _CmSignatureField_defaultValue, "f");
168
+ }
169
+ else
170
+ this.updateComplete.then(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, false));
171
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
172
+ }
173
+ formDisabledCallback(d) { this.disabled = d; }
174
+ formStateRestoreCallback(state) { this.value = state; }
175
+ /** True when the current mode has nothing signed: no strokes (or saved image) in Draw, no text in Type. */
176
+ get empty() {
177
+ return this.mode === 'type' ? !this.typedName.trim() : !__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").length && !__classPrivateFieldGet(this, _CmSignatureField_image, "f");
178
+ }
179
+ /**
180
+ * The signature as a PNG data URL: black ink on transparent at 2x the pad's size, cropped to the ink with 8px
181
+ * (16 image pixels) around it. Empty string when nothing is signed.
182
+ */
183
+ toDataURL() {
184
+ return __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_export).call(this)?.toDataURL('image/png') ?? '';
185
+ }
186
+ /** The same PNG as `toDataURL()` as a Blob, for uploading. `null` when nothing is signed. */
187
+ toBlob() {
188
+ const c = __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_export).call(this);
189
+ return c ? new Promise((resolve) => c.toBlob(resolve, 'image/png')) : Promise.resolve(null);
190
+ }
191
+ /** Removes the current mode's signature (Draw: every stroke and any saved image; Type: the text). */
192
+ clear() {
193
+ if (this.mode === 'type')
194
+ this.typedName = '';
195
+ else {
196
+ __classPrivateFieldSet(this, _CmSignatureField_strokes, [], "f");
197
+ __classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
198
+ __classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
199
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
200
+ }
201
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, true);
202
+ }
203
+ /** Removes the last stroke in Draw mode (then the saved image, if one was loaded). */
204
+ undo() {
205
+ if (this.mode !== 'draw')
206
+ return;
207
+ if (__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").length)
208
+ __classPrivateFieldGet(this, _CmSignatureField_strokes, "f").pop();
209
+ else
210
+ __classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
211
+ __classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
212
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
213
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, true);
214
+ }
215
+ willUpdate(changed) {
216
+ if (changed.has('value') && (this.value ?? '') !== __classPrivateFieldGet(this, _CmSignatureField_own, "f")) {
217
+ // Set from outside: '' clears both modes; anything else is a saved signature, shown in Draw.
218
+ const v = this.value ?? '';
219
+ __classPrivateFieldSet(this, _CmSignatureField_own, v, "f");
220
+ __classPrivateFieldSet(this, _CmSignatureField_strokes, [], "f");
221
+ __classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
222
+ if (!v) {
223
+ __classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
224
+ if (this.hasUpdated)
225
+ this.typedName = '';
226
+ }
227
+ else {
228
+ const img = new Image();
229
+ img.onload = () => { if (__classPrivateFieldGet(this, _CmSignatureField_image, "f") === img)
230
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this); };
231
+ img.src = v;
232
+ __classPrivateFieldSet(this, _CmSignatureField_image, img, "f");
233
+ this.mode = 'draw';
234
+ }
235
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
236
+ }
237
+ else if (this.hasUpdated && (changed.has('mode') || (changed.has('typedName') && this.mode === 'type'))) {
238
+ // Another tab, or a name set in code: the value follows (events are fired by the user actions only).
239
+ const next = this.empty ? '' : this.toDataURL();
240
+ __classPrivateFieldSet(this, _CmSignatureField_own, next, "f");
241
+ this.value = next;
242
+ }
243
+ }
244
+ firstUpdated() {
245
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_observe).call(this);
246
+ // A typed signature exported before the font arrives would use the fallback font: load it now.
247
+ if (this.typedName && !this.value)
248
+ this.updateComplete.then(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, false));
249
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_loadFont).call(this);
250
+ }
251
+ updated(changed) {
252
+ if (changed.has('mode') && this.mode === 'type')
253
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_loadFont).call(this);
254
+ __classPrivateFieldGet(this, _CmSignatureField_internals, "f").setFormValue(this.value || null, this.value);
255
+ const anchor = (this.mode === 'type'
256
+ ? this.renderRoot.querySelector('.typed')
257
+ : this.renderRoot.querySelector('.tab[aria-selected="true"]')) ?? undefined;
258
+ if (this.error)
259
+ __classPrivateFieldGet(this, _CmSignatureField_internals, "f").setValidity({ customError: true }, this.error, anchor);
260
+ else if (this.required && this.empty)
261
+ __classPrivateFieldGet(this, _CmSignatureField_internals, "f").setValidity({ valueMissing: true }, this.t('required'), anchor);
262
+ else
263
+ __classPrivateFieldGet(this, _CmSignatureField_internals, "f").setValidity({});
264
+ }
265
+ render() {
266
+ const id = __classPrivateFieldGet(this, _CmSignatureField_id, "f");
267
+ const hostLabel = this.getAttribute('aria-label');
268
+ const message = this.error;
269
+ const describedBy = [message && `${id}-err`, this.helpText && `${id}-help`].filter(Boolean).join(' ');
270
+ const empty = this.empty;
271
+ const drawEmpty = !__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").length && !__classPrivateFieldGet(this, _CmSignatureField_image, "f");
272
+ const tab = (mode) => html `<button type="button" role="tab" class="tab" id="${id}-tab-${mode}"
273
+ aria-selected=${this.mode === mode ? 'true' : 'false'} aria-controls="${id}-panel-${mode}"
274
+ tabindex=${this.mode === mode ? '0' : '-1'} ?disabled=${this.disabled}
275
+ @click=${() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_select).call(this, mode, true)} @keydown=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onTabKey)}>${this.t(mode)}</button>`;
276
+ return html `<div class="field">
277
+ ${this.label
278
+ ? html `<span class="label" id="${id}-l" part="label">${this.label}${this.required ? html `<span class="req" aria-hidden="true">*</span>` : nothing}${this.hint ? html `<span class="hint">[${this.hint}]</span>` : nothing}</span>`
279
+ : nothing}
280
+ <div class="bar">
281
+ <div class="tabs" role="tablist" part="tabs"
282
+ aria-labelledby=${this.label ? `${id}-l` : nothing}
283
+ aria-label=${this.label ? nothing : hostLabel || this.t('modes')}>${tab('draw')}${tab('type')}</div>
284
+ <div class="actions">
285
+ ${this.mode === 'draw'
286
+ ? html `<button type="button" class="act" part="undo" aria-label=${this.t('undoLabel')} ?disabled=${this.disabled || drawEmpty} @click=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUndo)}>${this.t('undo')}</button>`
287
+ : nothing}
288
+ <button type="button" class="act" part="clear" aria-label=${this.t('clearLabel')} ?disabled=${this.disabled || empty} @click=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onClear)}>${this.t('clear')}</button>
289
+ </div>
290
+ </div>
291
+ <div class="panel panel-draw" role="tabpanel" id="${id}-panel-draw" aria-labelledby="${id}-tab-draw"
292
+ tabindex=${this.disabled ? '-1' : '0'} ?hidden=${this.mode !== 'draw'}>
293
+ <div class="pad" part="pad">
294
+ <span class="line" aria-hidden="true"></span>
295
+ ${drawEmpty ? html `<span class="here" aria-hidden="true">${this.t('signHere')}</span>` : nothing}
296
+ <canvas part="canvas" role="img" aria-label=${drawEmpty ? this.t('pad') : this.t('padSigned')}
297
+ aria-describedby=${[`${id}-instr`, describedBy].filter(Boolean).join(' ')}
298
+ @pointerdown=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onDown)} @pointermove=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onMove)} @pointerup=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUp)} @pointercancel=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUp)}
299
+ @lostpointercapture=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUp)} @transitionend=${() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_paint).call(this)}></canvas>
300
+ </div>
301
+ <span class="vh" id="${id}-instr">${this.t('instructions')}</span>
302
+ </div>
303
+ <div class="panel panel-type" role="tabpanel" id="${id}-panel-type" aria-labelledby="${id}-tab-type" ?hidden=${this.mode !== 'type'}>
304
+ <div class="pad" part="pad">
305
+ <span class="line" aria-hidden="true"></span>
306
+ <input class="typed" part="input" type="text" autocomplete="name" spellcheck="false" maxlength="80"
307
+ .value=${live(this.typedName ?? '')} placeholder=${this.t('typePlaceholder')}
308
+ aria-label=${this.label ? `${this.label}: ${this.t('typeLabel')}` : this.t('typeLabel')}
309
+ aria-required=${this.required ? 'true' : nothing} aria-invalid=${message ? 'true' : nothing}
310
+ aria-describedby=${describedBy || nothing} ?disabled=${this.disabled}
311
+ @input=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onType)} @change=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onTypeChange)} />
312
+ </div>
313
+ </div>
314
+ ${message ? html `<p class="error" id="${id}-err" role="alert">${message}</p>` : nothing}
315
+ ${this.helpText ? html `<p class="help" id="${id}-help">${this.helpText}</p>` : nothing}
316
+ <div class="vh" role="status" aria-live="polite">${this._announce}</div>
317
+ </div>`;
318
+ }
319
+ }
320
+ _CmSignatureField_internals = new WeakMap(), _CmSignatureField_id = new WeakMap(), _CmSignatureField_defaultValue = new WeakMap(), _CmSignatureField_defaultTyped = new WeakMap(), _CmSignatureField_strokes = new WeakMap(), _CmSignatureField_active = new WeakMap(), _CmSignatureField_image = new WeakMap(), _CmSignatureField_own = new WeakMap(), _CmSignatureField_frame = new WeakMap(), _CmSignatureField_typeTimer = new WeakMap(), _CmSignatureField_typeFrom = new WeakMap(), _CmSignatureField_resize = new WeakMap(), _CmSignatureField_width = new WeakMap(), _CmSignatureField_instances = new WeakSet(), _CmSignatureField_observe = function _CmSignatureField_observe() {
321
+ const pad = this.renderRoot.querySelector('.panel-draw .pad');
322
+ if (!pad || __classPrivateFieldGet(this, _CmSignatureField_resize, "f"))
323
+ return;
324
+ __classPrivateFieldSet(this, _CmSignatureField_resize, new ResizeObserver(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_fit).call(this)), "f");
325
+ __classPrivateFieldGet(this, _CmSignatureField_resize, "f").observe(pad);
326
+ }, _CmSignatureField_canvas_get = function _CmSignatureField_canvas_get() { return this.renderRoot.querySelector('canvas'); }, _CmSignatureField_fit = function _CmSignatureField_fit() {
327
+ const canvas = __classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_canvas_get);
328
+ if (!canvas)
329
+ return;
330
+ const w = canvas.clientWidth, h = canvas.clientHeight;
331
+ if (!w || !h)
332
+ return;
333
+ __classPrivateFieldSet(this, _CmSignatureField_width, w, "f");
334
+ const dpr = window.devicePixelRatio || 1;
335
+ const pw = Math.round(w * dpr), ph = Math.round(h * dpr);
336
+ if (canvas.width !== pw || canvas.height !== ph) {
337
+ canvas.width = pw;
338
+ canvas.height = ph;
339
+ }
340
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_paint).call(this);
341
+ }, _CmSignatureField_draw = function _CmSignatureField_draw() {
342
+ if (__classPrivateFieldGet(this, _CmSignatureField_frame, "f"))
343
+ return;
344
+ __classPrivateFieldSet(this, _CmSignatureField_frame, requestAnimationFrame(() => { __classPrivateFieldSet(this, _CmSignatureField_frame, 0, "f"); __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_paint).call(this); }), "f");
345
+ }, _CmSignatureField_paint = function _CmSignatureField_paint() {
346
+ const canvas = __classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_canvas_get);
347
+ if (!canvas || !canvas.width)
348
+ return;
349
+ const ctx = canvas.getContext('2d');
350
+ const w = canvas.clientWidth || __classPrivateFieldGet(this, _CmSignatureField_width, "f");
351
+ if (!w)
352
+ return;
353
+ const scale = canvas.width / w;
354
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
355
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
356
+ ctx.setTransform(scale, 0, 0, scale, 0, 0);
357
+ const ink = getComputedStyle(canvas).color || EXPORT_INK;
358
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_render).call(this, ctx, w, ink, true);
359
+ }, _CmSignatureField_render = function _CmSignatureField_render(ctx, w, ink, screen) {
360
+ const h = w / RATIO;
361
+ const img = __classPrivateFieldGet(this, _CmSignatureField_image, "f");
362
+ if (img?.complete && img.naturalWidth) {
363
+ const k = Math.min((w * 0.9) / img.naturalWidth, (h * 0.9) / img.naturalHeight);
364
+ const iw = img.naturalWidth * k, ih = img.naturalHeight * k;
365
+ const x = (w - iw) / 2, y = (h - ih) / 2;
366
+ if (screen && !sameColor(ink, EXPORT_INK)) {
367
+ // Dark theme: show the saved (dark) ink in the screen's ink colour, so it stays visible.
368
+ const tmp = document.createElement('canvas');
369
+ tmp.width = Math.max(1, Math.round(iw * 2));
370
+ tmp.height = Math.max(1, Math.round(ih * 2));
371
+ const t = tmp.getContext('2d');
372
+ t.drawImage(img, 0, 0, tmp.width, tmp.height);
373
+ t.globalCompositeOperation = 'source-in';
374
+ t.fillStyle = ink;
375
+ t.fillRect(0, 0, tmp.width, tmp.height);
376
+ ctx.drawImage(tmp, x, y, iw, ih);
377
+ }
378
+ else
379
+ ctx.drawImage(img, x, y, iw, ih);
380
+ }
381
+ ctx.strokeStyle = ink;
382
+ ctx.fillStyle = ink;
383
+ ctx.lineCap = 'round';
384
+ ctx.lineJoin = 'round';
385
+ for (const s of __classPrivateFieldGet(this, _CmSignatureField_strokes, "f"))
386
+ drawStroke(ctx, s, w);
387
+ }, _CmSignatureField_point = function _CmSignatureField_point(e, rect) {
388
+ // Pressure only from pens: mice report a fixed 0.5 and many touch screens 0 or 1.
389
+ const p = e.pointerType === 'pen' && e.pressure > 0 ? e.pressure : 0.5;
390
+ return { x: (e.clientX - rect.left) / rect.width, y: (e.clientY - rect.top) / rect.width, p };
391
+ }, _CmSignatureField_onDown = function _CmSignatureField_onDown(e) {
392
+ if (this.disabled || __classPrivateFieldGet(this, _CmSignatureField_active, "f") || !e.isPrimary || (e.pointerType === 'mouse' && e.button !== 0))
393
+ return;
394
+ const canvas = e.currentTarget;
395
+ e.preventDefault();
396
+ try {
397
+ canvas.setPointerCapture(e.pointerId);
398
+ }
399
+ catch { /* synthetic events have no active pointer */ }
400
+ const stroke = [__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_point).call(this, e, canvas.getBoundingClientRect())];
401
+ __classPrivateFieldSet(this, _CmSignatureField_active, { id: e.pointerId, stroke }, "f");
402
+ __classPrivateFieldGet(this, _CmSignatureField_strokes, "f").push(stroke);
403
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
404
+ }, _CmSignatureField_onMove = function _CmSignatureField_onMove(e) {
405
+ const a = __classPrivateFieldGet(this, _CmSignatureField_active, "f");
406
+ if (!a || e.pointerId !== a.id)
407
+ return;
408
+ e.preventDefault();
409
+ const rect = e.currentTarget.getBoundingClientRect();
410
+ const events = typeof e.getCoalescedEvents === 'function' ? e.getCoalescedEvents() : [];
411
+ for (const ev of events.length ? events : [e]) {
412
+ const pt = __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_point).call(this, ev, rect);
413
+ const last = a.stroke[a.stroke.length - 1];
414
+ // Skip points under 1.5 CSS px apart: they add noise, not shape.
415
+ if (Math.hypot(pt.x - last.x, pt.y - last.y) * rect.width < 1.5)
416
+ continue;
417
+ a.stroke.push(pt);
418
+ }
419
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
420
+ }, _CmSignatureField_onUp = function _CmSignatureField_onUp(e) {
421
+ const a = __classPrivateFieldGet(this, _CmSignatureField_active, "f");
422
+ if (!a || e.pointerId !== a.id)
423
+ return;
424
+ if (e.type === 'pointerup')
425
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onMove).call(this, e);
426
+ __classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
427
+ try {
428
+ e.currentTarget.releasePointerCapture(e.pointerId);
429
+ }
430
+ catch { /* not captured */ }
431
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
432
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, true);
433
+ }, _CmSignatureField_fontFamily_get = function _CmSignatureField_fontFamily_get() {
434
+ const input = this.renderRoot.querySelector('.typed');
435
+ return (input && getComputedStyle(input).fontFamily) || 'cursive';
436
+ }, _CmSignatureField_loadFont = function _CmSignatureField_loadFont() {
437
+ const fonts = document.fonts;
438
+ if (!fonts || !this.isConnected)
439
+ return;
440
+ const font = `400 40px ${__classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_fontFamily_get)}`;
441
+ let ready = true;
442
+ try {
443
+ ready = fonts.check(font, this.typedName || 'Signature');
444
+ }
445
+ catch { /* invalid font string: use what there is */ }
446
+ if (ready)
447
+ return;
448
+ fonts.load(font, this.typedName || 'Signature').then(() => {
449
+ // Re-export once the font is in, so the PNG matches what the signer sees.
450
+ if (this.mode === 'type' && this.typedName.trim())
451
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, false);
452
+ }, () => { });
453
+ }, _CmSignatureField_onType = function _CmSignatureField_onType(e) {
454
+ e.stopPropagation();
455
+ // The value follows in willUpdate, so the form value never lags; the events wait until typing stops.
456
+ if (!__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"))
457
+ __classPrivateFieldSet(this, _CmSignatureField_typeFrom, this.value, "f");
458
+ else
459
+ clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
460
+ this.typedName = e.target.value;
461
+ __classPrivateFieldSet(this, _CmSignatureField_typeTimer, setTimeout(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_typed).call(this), TYPE_DELAY), "f");
462
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_loadFont).call(this);
463
+ }, _CmSignatureField_typed = function _CmSignatureField_typed() {
464
+ if (!__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"))
465
+ return;
466
+ clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
467
+ __classPrivateFieldSet(this, _CmSignatureField_typeTimer, undefined, "f");
468
+ if (this.value !== __classPrivateFieldGet(this, _CmSignatureField_typeFrom, "f"))
469
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_emit).call(this);
470
+ }, _CmSignatureField_onTypeChange = function _CmSignatureField_onTypeChange(e) {
471
+ // The native change of the inner box doesn't cross the shadow boundary on its own; ours is fired by #emit.
472
+ e.stopPropagation();
473
+ this.updateComplete.then(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_typed).call(this));
474
+ }, _CmSignatureField_sync = function _CmSignatureField_sync(emit) {
475
+ const next = this.empty ? '' : this.toDataURL();
476
+ const changed = next !== this.value;
477
+ if (changed) {
478
+ __classPrivateFieldSet(this, _CmSignatureField_own, next, "f");
479
+ this.value = next;
480
+ }
481
+ this.requestUpdate();
482
+ if (emit && changed)
483
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_emit).call(this);
484
+ }, _CmSignatureField_emit = function _CmSignatureField_emit() {
485
+ const detail = { value: this.value, empty: this.empty, mode: this.mode };
486
+ this.dispatchEvent(new CustomEvent('cm-change', { detail, bubbles: true, composed: true }));
487
+ this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
488
+ }, _CmSignatureField_export = function _CmSignatureField_export() {
489
+ if (this.empty)
490
+ return undefined;
491
+ const w = Math.max(__classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_canvas_get)?.clientWidth || __classPrivateFieldGet(this, _CmSignatureField_width, "f") || 600, 300);
492
+ const h = w / RATIO;
493
+ const full = document.createElement('canvas');
494
+ full.width = Math.round(w * SCALE);
495
+ full.height = Math.round(h * SCALE);
496
+ const ctx = full.getContext('2d', { willReadFrequently: true });
497
+ ctx.scale(SCALE, SCALE);
498
+ if (this.mode === 'type') {
499
+ let size = Math.min(h * 0.45, 64);
500
+ const family = __classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_fontFamily_get);
501
+ ctx.font = `400 ${size}px ${family}`;
502
+ const text = this.typedName.trim();
503
+ const max = w - 2 * PADDING;
504
+ const measured = ctx.measureText(text).width;
505
+ if (measured > max) {
506
+ size *= max / measured;
507
+ ctx.font = `400 ${size}px ${family}`;
508
+ }
509
+ ctx.direction = getComputedStyle(this).direction === 'rtl' ? 'rtl' : 'ltr';
510
+ ctx.textAlign = 'center';
511
+ ctx.textBaseline = 'alphabetic';
512
+ ctx.fillStyle = EXPORT_INK;
513
+ ctx.fillText(text, w / 2, h * BASELINE);
514
+ }
515
+ else
516
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_render).call(this, ctx, w, EXPORT_INK, false);
517
+ return crop(full, PADDING * SCALE);
518
+ }, _CmSignatureField_select = function _CmSignatureField_select(mode, focus) {
519
+ if (this.disabled)
520
+ return;
521
+ if (focus)
522
+ this.updateComplete.then(() => this.renderRoot.querySelector(`#${__classPrivateFieldGet(this, _CmSignatureField_id, "f")}-tab-${mode}`)?.focus());
523
+ if (mode === this.mode)
524
+ return;
525
+ const before = this.value;
526
+ this.mode = mode;
527
+ this.updateComplete.then(() => {
528
+ if (mode === 'draw')
529
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_fit).call(this);
530
+ if (this.value !== before)
531
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_emit).call(this);
532
+ });
533
+ }, _CmSignatureField_onTabKey = function _CmSignatureField_onTabKey(e) {
534
+ const key = logicalKey(e, this);
535
+ let next;
536
+ if (key === 'ArrowRight' || key === 'ArrowLeft')
537
+ next = this.mode === 'draw' ? 'type' : 'draw';
538
+ else if (e.key === 'Home')
539
+ next = 'draw';
540
+ else if (e.key === 'End')
541
+ next = 'type';
542
+ else
543
+ return;
544
+ e.preventDefault();
545
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_select).call(this, next, true);
546
+ }, _CmSignatureField_say = function _CmSignatureField_say(key) {
547
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_announce).call(this, this.t(key));
548
+ }, _CmSignatureField_announce = function _CmSignatureField_announce(text) {
549
+ // Empty first, so the same message twice in a row is read again.
550
+ this._announce = '';
551
+ this.updateComplete.then(() => { this._announce = text; });
552
+ }, _CmSignatureField_keepFocus = function _CmSignatureField_keepFocus(hadFocus) {
553
+ this.updateComplete.then(() => {
554
+ const active = this.shadowRoot?.activeElement;
555
+ if (!hadFocus || (active && !active.disabled))
556
+ return;
557
+ const target = this.mode === 'type'
558
+ ? this.renderRoot.querySelector('.typed')
559
+ : this.renderRoot.querySelector('.tab[aria-selected="true"]');
560
+ target?.focus();
561
+ });
562
+ }, _CmSignatureField_onUndo = function _CmSignatureField_onUndo() {
563
+ if (this.disabled || this.mode !== 'draw' || this.empty)
564
+ return;
565
+ const hadFocus = this.matches(':focus-within');
566
+ this.undo();
567
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_say).call(this, 'undone');
568
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_keepFocus).call(this, hadFocus);
569
+ }, _CmSignatureField_onClear = function _CmSignatureField_onClear() {
570
+ if (this.disabled || this.empty)
571
+ return;
572
+ const hadFocus = this.matches(':focus-within');
573
+ this.clear();
574
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_say).call(this, 'cleared');
575
+ __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_keepFocus).call(this, hadFocus);
576
+ };
577
+ CmSignatureField.formAssociated = true;
578
+ CmSignatureField.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
579
+ CmSignatureField.properties = {
580
+ label: {},
581
+ hint: {},
582
+ helpText: { attribute: 'help-text' },
583
+ error: { reflect: true },
584
+ name: { reflect: true },
585
+ value: {},
586
+ mode: { reflect: true },
587
+ typedName: { attribute: 'typed-name' },
588
+ required: { type: Boolean, reflect: true },
589
+ disabled: { type: Boolean, reflect: true },
590
+ _announce: { state: true },
591
+ };
592
+ CmSignatureField.styles = [
593
+ base,
594
+ css `
595
+ :host { display: block; }
596
+ .field { display: flex; flex-direction: column; gap: var(--cm-space-8); }
597
+ .label { font-size: var(--cm-type-label-size); color: var(--cm-text-default); }
598
+ .req { color: var(--cm-text-critical); margin-inline-start: 2px; }
599
+ .hint { margin-inline-start: var(--cm-space-8); font-size: 12px; color: var(--cm-text-tertiary); text-transform: lowercase; }
600
+ .bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--cm-space-8); }
601
+ .tabs { display: flex; gap: var(--cm-space-20); }
602
+ .tab {
603
+ position: relative; min-height: var(--cm-control-height-sm); padding: 0 2px; font: inherit; font-size: var(--cm-font-size-14); font-weight: 500;
604
+ color: var(--cm-text-tertiary); background: none; border: 0; cursor: pointer;
605
+ }
606
+ .tab::after { content: ''; position: absolute; inset-inline: 0; bottom: 2px; height: 2px; border-radius: var(--cm-radius-full); background: transparent; }
607
+ .tab:hover:not(:disabled), .tab[aria-selected='true'] { color: var(--cm-text-default); }
608
+ .tab[aria-selected='true']::after { background: var(--cm-border-strong); }
609
+ .tab:focus-visible { outline: 2px solid var(--cm-border-strong); outline-offset: 2px; border-radius: var(--cm-radius-xs); }
610
+ .tab:disabled { color: var(--cm-text-secondary); cursor: not-allowed; }
611
+ .actions { display: flex; gap: var(--cm-space-8); margin-inline-start: auto; }
612
+ /* Secondary slim Button (32px): the pad's own small actions. */
613
+ .act {
614
+ display: inline-flex; align-items: center; height: var(--cm-control-height-sm); padding: 0 var(--cm-space-12);
615
+ font: inherit; font-size: var(--cm-font-size-13); font-weight: 500; line-height: 1; white-space: nowrap; cursor: pointer;
616
+ background: var(--cm-bg-hover); color: var(--cm-text-default);
617
+ border: var(--cm-border-1) solid var(--cm-border-strong); border-radius: var(--cm-radius-sm);
618
+ }
619
+ .act:hover:not(:disabled) { background: var(--cm-bg-surface); }
620
+ .act:focus-visible { outline: 2px solid var(--cm-border-strong); outline-offset: 2px; }
621
+ .act:disabled { background: var(--cm-action-disabled); border-color: var(--cm-action-disabled); color: var(--cm-action-disabled-text); cursor: not-allowed; }
622
+ .panel:focus { outline: none; }
623
+ .pad {
624
+ position: relative; width: 100%; aspect-ratio: ${RATIO} / 1; overflow: hidden; container-type: inline-size;
625
+ background: var(--cm-bg-surface); border: var(--cm-border-1) solid var(--cm-border-input); border-radius: var(--cm-radius-sm);
626
+ }
627
+ .panel:focus-visible .pad, .pad:focus-within { border-color: var(--cm-border-strong); box-shadow: 0 0 0 1px var(--cm-border-strong); }
628
+ :host([error]:not([error=''])) .pad { border-color: var(--cm-border-critical); }
629
+ canvas {
630
+ position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair;
631
+ /* The ink: read in script. The transition makes a theme change fire transitionend, which repaints the ink. */
632
+ color: var(--cm-text-default); transition-property: color; transition-duration: 1ms; /* scale-exempt: not motion; a 1ms colour transition makes a theme change fire transitionend so the ink repaints */
633
+ }
634
+ .line {
635
+ position: absolute; inset-inline: var(--cm-space-12); top: ${BASELINE * 100}%; height: 0;
636
+ border-top: var(--cm-border-1) dashed var(--cm-border-input); pointer-events: none;
637
+ }
638
+ .here {
639
+ position: absolute; inset-inline-start: var(--cm-space-12); bottom: calc(${(1 - BASELINE) * 100}% + 4px);
640
+ font-size: 12px; color: var(--cm-text-tertiary); pointer-events: none; user-select: none;
641
+ }
642
+ .typed {
643
+ position: absolute; inset-inline: var(--cm-space-12); bottom: calc(${(1 - BASELINE) * 100}% - 0.18em);
644
+ width: calc(100% - 2 * var(--cm-space-12)); padding: 0; margin: 0;
645
+ font-family: var(--cm-signature-font, 'Dancing Script', 'Segoe Script', 'Bradley Hand', 'Snell Roundhand', 'Brush Script MT', cursive);
646
+ font-size: clamp(24px, 9cqi, 48px); font-weight: 400; line-height: 1.25; text-align: center; /* scale-exempt: a typed signature scales with the pad, like handwriting */
647
+ color: var(--cm-text-default); background: transparent; border: 0; outline: none;
648
+ }
649
+ .typed::placeholder {
650
+ font-family: var(--cm-font-sans, 'Poppins', system-ui, sans-serif); font-size: 14px; color: var(--cm-text-tertiary);
651
+ }
652
+ :host([disabled]) .pad { background: var(--cm-bg-input-filled); border-color: var(--cm-border-subtle); }
653
+ :host([disabled]) canvas, :host([disabled]) .typed { cursor: not-allowed; }
654
+ :host([disabled]) .typed { color: var(--cm-text-secondary); }
655
+ .help { margin: 0; font-size: var(--cm-type-body-sm-size); line-height: var(--cm-type-body-sm-line-height); color: var(--cm-text-tertiary); }
656
+ .error { margin: 0; font-size: var(--cm-type-body-sm-size); line-height: var(--cm-type-body-sm-line-height); color: var(--cm-text-critical); }
657
+ @media (pointer: coarse) { .act, .tab { min-height: var(--cm-control-height-touch); } }
658
+
659
+ .vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
660
+ `,
661
+ ];
662
+ /** Draws one stroke through the midpoints of its points (quadratic smoothing), each piece as wide as its pressure. */
663
+ function drawStroke(ctx, s, w) {
664
+ const width = (p) => w * LINE * (0.35 + p * 1.3);
665
+ if (s.length === 1) {
666
+ ctx.beginPath();
667
+ ctx.arc(s[0].x * w, s[0].y * w, width(s[0].p) / 2, 0, Math.PI * 2);
668
+ ctx.fill();
669
+ return;
670
+ }
671
+ const P = (q) => [q.x * w, q.y * w];
672
+ let start = s[0];
673
+ for (let i = 1; i < s.length - 1; i++) {
674
+ const end = mid(s[i], s[i + 1]);
675
+ ctx.beginPath();
676
+ ctx.moveTo(...P(start));
677
+ ctx.quadraticCurveTo(...P(s[i]), ...P(end));
678
+ ctx.lineWidth = width(s[i].p);
679
+ ctx.stroke();
680
+ start = end;
681
+ }
682
+ const last = s[s.length - 1];
683
+ ctx.beginPath();
684
+ ctx.moveTo(...P(start));
685
+ ctx.lineTo(...P(last));
686
+ ctx.lineWidth = width(last.p);
687
+ ctx.stroke();
688
+ }
689
+ /** A copy of the canvas cut to its non-transparent pixels plus `pad` pixels each side; undefined if it is blank. */
690
+ function crop(c, pad) {
691
+ const { width, height } = c;
692
+ const data = c.getContext('2d', { willReadFrequently: true }).getImageData(0, 0, width, height).data;
693
+ let top = height, left = width, right = -1, bottom = -1;
694
+ for (let y = 0; y < height; y++) {
695
+ const row = y * width * 4;
696
+ for (let x = 0; x < width; x++) {
697
+ if (data[row + x * 4 + 3] > 8) {
698
+ if (x < left)
699
+ left = x;
700
+ if (x > right)
701
+ right = x;
702
+ if (y < top)
703
+ top = y;
704
+ bottom = y;
705
+ }
706
+ }
707
+ }
708
+ if (right < 0)
709
+ return undefined;
710
+ const out = document.createElement('canvas');
711
+ out.width = right - left + 1 + 2 * pad;
712
+ out.height = bottom - top + 1 + 2 * pad;
713
+ out.getContext('2d').drawImage(c, left, top, right - left + 1, bottom - top + 1, pad, pad, right - left + 1, bottom - top + 1);
714
+ return out;
715
+ }
716
+ /** Whether two CSS colours (as computed: `rgb(…)`, or hex) are the same. */
717
+ function sameColor(a, b) {
718
+ const norm = (c) => {
719
+ const m = /^#([0-9a-f]{6})$/i.exec(c.trim());
720
+ if (m) {
721
+ const n = parseInt(m[1], 16);
722
+ return `${(n >> 16) & 255},${(n >> 8) & 255},${n & 255}`;
723
+ }
724
+ return (/\d+\s*,\s*\d+\s*,\s*\d+/.exec(c)?.[0] ?? c).replace(/\s/g, '');
725
+ };
726
+ return norm(a) === norm(b);
727
+ }
728
+ define('cm-signature-field', CmSignatureField);