@zeyos/zx 5.7.0 → 5.7.1

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.
@@ -11,6 +11,11 @@ import { icon } from '../../core/icons.js';
11
11
  * @property {number} [step=1] Increment applied by the buttons and arrow keys.
12
12
  * @property {number} [largeStep=10] Multiple of `step` applied by PageUp/PageDown.
13
13
  * @property {number|null} [precision=null] Decimal places; derived from `step` when null.
14
+ * @property {boolean} [snap=true] Whether a typed or assigned value is moved onto the `step` grid.
15
+ * With `false` the value is kept exactly as entered — `step` is then only the increment of the
16
+ * buttons and arrow keys, and decimals are rounded only to an explicit `precision`. `min` and
17
+ * `max` still apply. Use it for values that are not multiples of anything: an identifier, a
18
+ * seed, a measurement.
14
19
  * @property {boolean} [wrap=false] Whether stepping past a bound wraps to the other one.
15
20
  * @property {string} [placeholder=''] Empty-state text.
16
21
  * @property {string|null} [label=null] Accessible name for the spinbutton.
@@ -48,6 +53,7 @@ export class NumberField extends Component {
48
53
  step: 1,
49
54
  largeStep: 10,
50
55
  precision: null,
56
+ snap: true,
51
57
  wrap: false,
52
58
  placeholder: '',
53
59
  label: null,
@@ -75,9 +81,13 @@ export class NumberField extends Component {
75
81
 
76
82
  const step = normalizeStep(this.options.step);
77
83
  this._step = step;
78
- this._precision = this.options.precision === null
79
- ? decimalsOf(step)
80
- : Math.max(0, Math.trunc(Number(this.options.precision) || 0));
84
+ this._snap = this.options.snap !== false;
85
+ const explicit = this.options.precision !== null && this.options.precision !== undefined;
86
+ this._precision = explicit
87
+ ? Math.max(0, Math.trunc(Number(this.options.precision) || 0))
88
+ : decimalsOf(step);
89
+ // Without snapping, only a precision the application asked for may round a value.
90
+ this._fixedPrecision = this._snap || explicit;
81
91
 
82
92
  const down = this._stepButton('down', 'minus');
83
93
  const up = this._stepButton('up', 'plus');
@@ -86,7 +96,7 @@ export class NumberField extends Component {
86
96
  class: 'zx-number-field__input',
87
97
  type: 'text',
88
98
  role: 'spinbutton',
89
- inputMode: this._precision > 0 ? 'decimal' : 'numeric',
99
+ inputMode: this._precision > 0 || !this._fixedPrecision ? 'decimal' : 'numeric',
90
100
  autocomplete: 'off',
91
101
  spellcheck: false,
92
102
  placeholder: String(this.options.placeholder ?? ''),
@@ -153,7 +163,8 @@ export class NumberField extends Component {
153
163
  }
154
164
 
155
165
  /**
156
- * Sets the value, snapping it to `step` and clamping it into `[min, max]`.
166
+ * Sets the value, clamping it into `[min, max]` and — unless `snap` is off — moving it onto the
167
+ * `step` grid.
157
168
  * @param {number|string|null} next Next value; empty strings and null clear the field.
158
169
  * @param {{silent?: boolean}} [options={}] Set `silent` to suppress `change`.
159
170
  * @returns {this}
@@ -277,6 +288,16 @@ export class NumberField extends Component {
277
288
  _coerce(raw) {
278
289
  const parsed = parseNumber(raw, this.options.group ? this._separators() : null);
279
290
  if (parsed === null) return null;
291
+ if (!this._snap) {
292
+ // Past the digits a double can round to, rounding would only turn a tiny value into zero.
293
+ if (this._decimalsFor(parsed) > ROUNDABLE_DECIMALS) return clampNumber(parsed, this._rangeMin, this._rangeMax);
294
+ return snapNumber(parsed, {
295
+ min: this._rangeMin,
296
+ max: this._rangeMax,
297
+ step: 0,
298
+ precision: this._decimalsFor(parsed)
299
+ });
300
+ }
280
301
  return snapNumber(parsed, {
281
302
  min: this._rangeMin,
282
303
  max: this._rangeMax,
@@ -301,7 +322,8 @@ export class NumberField extends Component {
301
322
  const base = current === null ? (min ?? 0) : current;
302
323
  // An empty field steps to the lower bound first, so the first click lands on a real value.
303
324
  const start = current === null && count > 0 && min !== null ? min - this._step * count : base;
304
- let next = round(start + this._step * count, this._precision);
325
+ const decimals = this._decimalsFor(start);
326
+ let next = decimals > ROUNDABLE_DECIMALS ? start + this._step * count : round(start + this._step * count, decimals);
305
327
 
306
328
  if (this.options.wrap && min !== null && max !== null) {
307
329
  if (next > max) next = min;
@@ -412,17 +434,29 @@ export class NumberField extends Component {
412
434
  */
413
435
  _format(value, editing) {
414
436
  // Ungrouped fields use a plain machine format, which `parseNumber` reads heuristically.
415
- if (!this.options.group) return value.toFixed(this._precision);
437
+ const decimals = this._decimalsFor(value);
438
+ if (!this.options.group) return value.toFixed(decimals);
416
439
  // Grouped fields stay in the locale's notation even while editing — only the thousands
417
440
  // separators drop away. Display and input therefore always use the same decimal character,
418
441
  // so a value the field rendered is always a value it can read back.
419
442
  return new Intl.NumberFormat(formattingLocale(this.options.locale), {
420
- minimumFractionDigits: this._precision,
421
- maximumFractionDigits: this._precision,
443
+ minimumFractionDigits: decimals,
444
+ maximumFractionDigits: decimals,
422
445
  useGrouping: !editing
423
446
  }).format(value);
424
447
  }
425
448
 
449
+ /**
450
+ * Decimal places a value is held and shown at: the field's precision, widened to the value's
451
+ * own decimals when nothing fixed the precision (`snap: false` without `precision`).
452
+ * @param {number} value Value in question.
453
+ * @returns {number}
454
+ */
455
+ _decimalsFor(value) {
456
+ if (this._fixedPrecision) return this._precision;
457
+ return Math.min(MAX_DECIMALS, Math.max(this._precision, decimalsOf(value)));
458
+ }
459
+
426
460
  /**
427
461
  * @param {string} key Message key.
428
462
  * @param {string} fallback Text used when the host has no translation.
@@ -541,13 +575,32 @@ function normalizeStep(step) {
541
575
  return Number.isFinite(value) && value > 0 ? value : 1;
542
576
  }
543
577
 
544
- /** @param {number} step @returns {number} */
545
- function decimalsOf(step) {
546
- const text = String(step);
547
- const exponent = text.match(/e-(\d+)/i);
548
- if (exponent) return Number(exponent[1]);
549
- const fraction = text.split('.')[1];
550
- return fraction ? fraction.length : 0;
578
+ /** Most decimals a value is displayed with (the limit of `toFixed`). */
579
+ const MAX_DECIMALS = 100;
580
+ /** Most decimals `round()` works to; an unsnapped value needing more is left unrounded. */
581
+ const ROUNDABLE_DECIMALS = 15;
582
+
583
+ /**
584
+ * @param {number} value Value to bound.
585
+ * @param {number|null} min Lower bound.
586
+ * @param {number|null} max Upper bound.
587
+ * @returns {number}
588
+ */
589
+ function clampNumber(value, min, max) {
590
+ if (min !== null && value < min) return min;
591
+ if (max !== null && value > max) return max;
592
+ return value;
593
+ }
594
+
595
+ /**
596
+ * Decimal places in a number's shortest exact notation, exponent form included (`2.5e-7` has 8).
597
+ * @param {number} value Number to measure.
598
+ * @returns {number}
599
+ */
600
+ function decimalsOf(value) {
601
+ const [mantissa, exponent = '0'] = String(value).toLowerCase().split('e');
602
+ const fraction = mantissa.split('.')[1]?.length ?? 0;
603
+ return Math.max(0, fraction - Number(exponent));
551
604
  }
552
605
 
553
606
  /**
@@ -48,7 +48,7 @@ const GROW_SIGN = Object.freeze({ start: 1, end: -1, top: 1, bottom: -1 });
48
48
  * @property {Element|string|null} [scope=null] Element whose nearest Zx theme scope owns a floating sheet; defaults to the opener.
49
49
  * @property {(event: CustomEvent<Record<string, never>>) => void} [onopen] Open event listener.
50
50
  * @property {(event: CustomEvent<{result: unknown}>) => void} [onclose] Close event listener.
51
- * @property {(event: CustomEvent<Record<string, never>>) => void} [oncancel] Cancel event listener.
51
+ * @property {(event: CustomEvent<import('../modal/modal.js').ModalCancelDetail>) => void} [oncancel] Cancel event listener.
52
52
  * @property {(event: CustomEvent<{size: number, ratio: number}>) => void} [onresize] Resize listener.
53
53
  * @property {(event: CustomEvent<{docked: boolean, dock: import('../dock/dock.js').Dock|null}>) => void} [ondockchange] Dock-state listener.
54
54
  */
@@ -213,8 +213,10 @@ export class Sheet extends Dialog {
213
213
  return;
214
214
  }
215
215
  if (this.options.closable && next < min / 2) {
216
+ // Restored first, so a prevented dismissal leaves the sheet at the size it had.
216
217
  this._applySize(this.#dragStart);
217
- this.close();
218
+ this.#syncHandleAria();
219
+ this._requestDismiss('drag');
218
220
  return;
219
221
  }
220
222
  const detents = this.#detents(total);
@@ -386,11 +388,16 @@ export class Sheet extends Dialog {
386
388
  ...entry,
387
389
  // An Escape pressed outside the sheet and everything it opened is not addressed to it.
388
390
  onEscape: (event) => {
389
- if (!this.options.closable || !this._overlayContains(/** @type {Node|null} */ (event.target))) return false;
390
- if (!this.emit('cancel').defaultPrevented) this.close();
391
+ if (!this._overlayContains(/** @type {Node|null} */ (event.target))) return false;
392
+ // Refused, but still reported — and the key stays free for whatever is underneath.
393
+ if (!this.options.closable) {
394
+ this.emit('cancel', { reason: 'escape' });
395
+ return false;
396
+ }
397
+ this._requestDismiss('escape');
391
398
  return undefined;
392
399
  },
393
- onOutsidePointer: this.options.lightDismiss ? () => { this.close(); } : undefined,
400
+ onOutsidePointer: this.options.lightDismiss ? () => { this._requestDismiss('outside'); } : undefined,
394
401
  // Hosted inside a modal that has now closed, the sheet would stay open but never be seen.
395
402
  onParentClose: () => {
396
403
  const holder = this.el.parentElement?.closest('dialog');
package/src/core/dom.js CHANGED
@@ -286,11 +286,54 @@ export function snapshotTarget(element) {
286
286
  */
287
287
  export function restoreTarget(element, snapshot) {
288
288
  if (!snapshot) return;
289
- for (const attribute of Array.from(element.attributes)) element.removeAttribute(attribute.name);
290
- for (const [name, value] of snapshot.attributes) element.setAttribute(name, value);
289
+ /*
290
+ * `style` goes back through the CSSOM (see `restoreInlineStyle`), so restoring a target never
291
+ * writes an inline style attribute that a strict Content Security Policy would block. While
292
+ * every captured attribute is still present they are corrected in place, which keeps their
293
+ * order and leaves an untouched `style` byte for byte as it was; only a target that lost one is
294
+ * rebuilt from nothing, in captured order.
295
+ */
296
+ const styled = /** @type {Element & ElementCSSInlineStyle} */ (element);
297
+ const cssom = typeof styled.style?.cssText === 'string';
298
+ const saved = new Map(snapshot.attributes);
299
+ const intact = snapshot.attributes.every(([name]) => element.hasAttribute(name));
300
+ for (const attribute of Array.from(element.attributes)) {
301
+ if (!intact || !saved.has(attribute.name)) {
302
+ if (attribute.name === 'style' && cssom) restoreInlineStyle(styled, null);
303
+ else element.removeAttribute(attribute.name);
304
+ }
305
+ }
306
+ for (const [name, value] of snapshot.attributes) {
307
+ if (name === 'style' && cssom) restoreInlineStyle(styled, value);
308
+ else if (element.getAttribute(name) !== value) element.setAttribute(name, value);
309
+ }
291
310
  element.replaceChildren(...snapshot.children);
292
311
  }
293
312
 
313
+ /**
314
+ * Puts an element's inline style back to a captured attribute value.
315
+ *
316
+ * Written through the CSSOM, never the attribute: under a `style-src` policy without
317
+ * `'unsafe-inline'`, setting the `style` attribute is an inline style the browser blocks and
318
+ * reports, while `style.cssText` is not governed by it. A style that was absent (or empty) is
319
+ * cleared and its attribute removed — after a read, because Chrome reflects CSSOM changes into the
320
+ * attribute lazily, and a removal it has not reflected yet comes back as an empty `style=""`,
321
+ * which the next capture would then try to restore.
322
+ * @param {Element & ElementCSSInlineStyle} element Element whose inline style to restore.
323
+ * @param {string|null} value Attribute value captured earlier, or null when it was absent.
324
+ * @returns {void}
325
+ */
326
+ export function restoreInlineStyle(element, value) {
327
+ // The read is also the flush: it makes the attribute current before anything compares or removes.
328
+ const current = element.getAttribute('style');
329
+ // Untouched styles keep their exact text; `cssText` would re-serialize `color:red` as `color: red;`.
330
+ if (current === value || (!value && current === null)) return;
331
+ element.style.cssText = value ?? '';
332
+ // Read either way: it places a restored attribute now, in order, and flushes before a removal.
333
+ element.getAttribute('style');
334
+ if (!value) element.removeAttribute('style');
335
+ }
336
+
294
337
  /**
295
338
  * Escapes text for safe insertion into an HTML string.
296
339
  * @param {unknown} str Value to escape.
package/src/core/icons.js CHANGED
@@ -181,6 +181,10 @@ export const icons = Object.freeze({
181
181
  error: outline('M2 12a10 10 0 1 0 20 0a10 10 0 1 0-20 0zM12 8V12M12 16H12.01'), // circle-alert
182
182
  success: outline('M2 12a10 10 0 1 0 20 0a10 10 0 1 0-20 0zM16 9l-5.5 5.5L8 12'), // circle-check
183
183
  upload: outline('M12 3v12M17 8l-5-5-5 5M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4'),
184
+ download: outline('M12 15V3M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4M7 10l5 5 5-5'),
185
+ 'box-archive': outline('M3 3h18a1 1 0 0 1 1 1v3a1 1 0 0 1-1 1h-18a1 1 0 0 1-1-1v-3a1 1 0 0 1 1-1zM4 8v11a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8M10 12h4'), // archive
186
+ pencil: outline('M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497zM15 5l4 4'),
187
+ 'pen-to-square': outline('M12 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7M18.375 2.625a1 1 0 0 1 3 3l-9.013 9.014a2 2 0 0 1-.853.505l-2.873.84a.5.5 0 0 1-.62-.62l.84-2.873a2 2 0 0 1 .506-.852z'), // square-pen
184
188
  drag: outline('M8 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0zM8 5a1 1 0 1 0 2 0a1 1 0 1 0-2 0zM8 19a1 1 0 1 0 2 0a1 1 0 1 0-2 0zM14 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0zM14 5a1 1 0 1 0 2 0a1 1 0 1 0-2 0zM14 19a1 1 0 1 0 2 0a1 1 0 1 0-2 0z'), // grip-vertical
185
189
  star: outline('M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z'),
186
190
  heart: outline('M2 9.5a5.5 5.5 0 0 1 9.591-3.676.56.56 0 0 0 .818 0A5.49 5.49 0 0 1 22 9.5c0 2.29-1.5 4-3 5.5l-5.492 5.313a2 2 0 0 1-3 .019L5 15c-1.5-1.5-3-3.2-3-5.5'),
@@ -204,8 +208,14 @@ export const icons = Object.freeze({
204
208
  // </lucide-glyphs>
205
209
  });
206
210
 
207
- /** Legacy gx Factory icon names mapped to their Zx equivalents. */
211
+ /**
212
+ * Other names for bundled glyphs: legacy gx Factory names, and the older Font Awesome names
213
+ * (`archive`, `edit`) that are still the ones most code reaches for. A glyph an application
214
+ * registered under the alias itself takes precedence (see `icon()`).
215
+ */
208
216
  const aliases = Object.freeze({
217
+ archive: 'box-archive',
218
+ edit: 'pen-to-square',
209
219
  settings: 'gear',
210
220
  clear: 'x',
211
221
  checked: 'check',
@@ -352,7 +362,8 @@ export function icon(name, options = {}) {
352
362
  const spec = parseIconSpec(name);
353
363
  if (spec.classes) return faIcon(spec.classes, options);
354
364
 
355
- const resolved = aliases[spec.name] ?? spec.name;
365
+ // An application that registered a glyph under an alias name keeps its own glyph.
366
+ const resolved = Object.hasOwn(extraGlyphs, spec.name) ? spec.name : aliases[spec.name] ?? spec.name;
356
367
  if (spec.provider === 'builtin') return builtinIcon(resolved, options);
357
368
  if (spec.provider === 'fa') {
358
369
  return faIcon(faIconClasses(resolved, {
@@ -1,4 +1,5 @@
1
1
  // @ts-check
2
+ import { restoreInlineStyle } from './dom.js';
2
3
  import { clamp, uid } from './util.js';
3
4
 
4
5
  const PLACEMENTS = new Set([
@@ -112,9 +113,9 @@ export function position(anchor, floating, options = {}) {
112
113
  const rest = anchorNames(anchor).filter((name) => name !== anchorName);
113
114
  if (rest.length) /** @type {HTMLElement} */ (anchor).style.setProperty('anchor-name', rest.join(', '));
114
115
  else /** @type {HTMLElement} */ (anchor).style.removeProperty('anchor-name');
115
- if (anchorStyle === null && anchor.getAttribute('style') === '') anchor.removeAttribute('style');
116
+ if (!anchorStyle && anchor.getAttribute('style') === '') anchor.removeAttribute('style');
116
117
  }
117
- restoreAttribute(floating, 'style', floatingStyle);
118
+ restoreInlineStyle(floating, floatingStyle);
118
119
  restoreAttribute(floating, 'popover', popover);
119
120
  }
120
121
  };