@syware/jlib 0.16.1 → 0.17.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.
@@ -32,5 +32,9 @@ export declare function placeFloating(anchor: Element, floating: HTMLElement, pl
32
32
  * one in a shadow root (an app shell's main area) never leaves it: the window does not hear it, its element does.
33
33
  */
34
34
  export declare function whenAnchorMoves(anchor: Element, update: () => void, signal: AbortSignal): void;
35
- /** Keeps a floating element by its anchor while the page scrolls or resizes, until the signal aborts. */
35
+ /**
36
+ * Keeps a floating element by its anchor while the page scrolls or resizes, until the signal aborts. The element's
37
+ * own size is watched too: a panel whose content grows (a form that opens a second group of fields, a list that
38
+ * filters) is placed again with its new size, so it takes the side with room and stays inside the window.
39
+ */
36
40
  export declare function followAnchor(anchor: Element, floating: HTMLElement, placement: Placement, signal: AbortSignal, gap?: number): void;
@@ -1,3 +1,4 @@
1
+ import { watchSizes } from "./resize.js";
1
2
  // src/lib/floating.ts
2
3
  var PLACEMENTS = [
3
4
  "top",
@@ -209,12 +210,19 @@ function whenAnchorMoves(anchor, update, signal) {
209
210
  window.addEventListener("resize", update, options);
210
211
  for (const ancestor of flatAncestors(anchor)) ancestor.addEventListener("scroll", update, options);
211
212
  }
212
- /** Keeps a floating element by its anchor while the page scrolls or resizes, until the signal aborts. */
213
+ /**
214
+ * Keeps a floating element by its anchor while the page scrolls or resizes, until the signal aborts. The element's
215
+ * own size is watched too: a panel whose content grows (a form that opens a second group of fields, a list that
216
+ * filters) is placed again with its new size, so it takes the side with room and stays inside the window.
217
+ */
213
218
  function followAnchor(anchor, floating, placement, signal, gap) {
214
219
  const update = () => {
215
220
  placeFloating(anchor, floating, placement, gap);
216
221
  };
217
222
  whenAnchorMoves(anchor, update, signal);
223
+ const sizes = watchSizes(update);
224
+ signal.addEventListener("abort", () => sizes.disconnect(), { once: true });
225
+ sizes.observe(floating);
218
226
  forgetRoom(floating);
219
227
  update();
220
228
  }
@@ -1,11 +1,27 @@
1
- /** The rows shown: the values, or one empty row to type the first one in. */
2
- export declare const rowsOf: (values: readonly string[]) => string[];
3
- /** The rows with the one at `index` changed. */
4
- export declare const withRowAt: (rows: readonly string[], index: number, value: string) => string[];
5
- /** The rows without the one at `index`; one empty row stays when it was the last. */
6
- export declare const withoutRowAt: (rows: readonly string[], index: number) => string[];
1
+ /**
2
+ * A row: its text, and the index it had in the `values` last set from outside — its origin. A row keeps its origin
3
+ * while it is typed in, emptied and filled again; a row added since has none.
4
+ */
5
+ export type InputListRow = {
6
+ value: string;
7
+ origin: number | undefined;
8
+ };
9
+ /** An empty row with no origin, for a list that holds nothing. */
10
+ export declare const emptyRow: () => InputListRow;
11
+ /** The rows shown: the values, each with its index, or one empty row to type the first one in. */
12
+ export declare const rowsOf: (values: readonly string[]) => InputListRow[];
13
+ /** The rows with the one at `index` changed; it keeps its origin. */
14
+ export declare const withRowAt: (rows: readonly InputListRow[], index: number, value: string) => InputListRow[];
15
+ /** The rows without the one at `index`, the others keeping their origin; one empty row stays when it was the last. */
16
+ export declare const withoutRowAt: (rows: readonly InputListRow[], index: number) => InputListRow[];
17
+ /** The rows people filled in. */
18
+ export declare const filledRows: (rows: readonly InputListRow[]) => InputListRow[];
19
+ /** The values kept from a list given from outside: the blank ones left out, as the rows left empty are. */
20
+ export declare const filledValuesOf: (values: readonly string[]) => string[];
7
21
  /** The values kept: the rows people filled in. */
8
- export declare const filledValues: (rows: readonly string[]) => string[];
22
+ export declare const filledValues: (rows: readonly InputListRow[]) => string[];
23
+ /** The origin of each value kept, in the same order: what tells a value from the one it started as. */
24
+ export declare const filledOrigins: (rows: readonly InputListRow[]) => (number | undefined)[];
9
25
  /** Whether another row fits under `max`; none or zero means no limit. */
10
26
  export declare const canAddRow: (count: number, max: number) => boolean;
11
27
  /** Whether two lists hold the same values in the same order. */
@@ -1,14 +1,32 @@
1
1
  // src/lib/input-list.ts
2
- /** The rows shown: the values, or one empty row to type the first one in. */
3
- var rowsOf = (values) => values.length === 0 ? [""] : [...values];
4
- /** The rows with the one at `index` changed. */
5
- var withRowAt = (rows, index, value) => rows.map((row, at) => at === index ? value : row);
6
- /** The rows without the one at `index`; one empty row stays when it was the last. */
7
- var withoutRowAt = (rows, index) => rowsOf(rows.filter((_, at) => at !== index));
2
+ /** An empty row with no origin, for a list that holds nothing. */
3
+ var emptyRow = () => ({
4
+ origin: void 0,
5
+ value: ""
6
+ });
7
+ /** The rows shown: the values, each with its index, or one empty row to type the first one in. */
8
+ var rowsOf = (values) => values.length === 0 ? [emptyRow()] : values.map((value, origin) => ({
9
+ origin,
10
+ value
11
+ }));
12
+ /** The rows with the one at `index` changed; it keeps its origin. */
13
+ var withRowAt = (rows, index, value) => rows.map((row, at) => at === index ? {
14
+ ...row,
15
+ value
16
+ } : row);
17
+ /** The rows without the one at `index`, the others keeping their origin; one empty row stays when it was the last. */
18
+ var withoutRowAt = (rows, index) => {
19
+ const kept = rows.filter((_, at) => at !== index);
20
+ return kept.length === 0 ? [emptyRow()] : kept;
21
+ };
22
+ /** The rows people filled in. */
23
+ var filledRows = (rows) => rows.filter((row) => row.value.trim() !== "");
24
+ /** The values kept from a list given from outside: the blank ones left out, as the rows left empty are. */
25
+ var filledValuesOf = (values) => values.filter((value) => value.trim() !== "");
8
26
  /** The values kept: the rows people filled in. */
9
- var filledValues = (rows) => rows.filter((row) => row.trim() !== "");
27
+ var filledValues = (rows) => filledRows(rows).map((row) => row.value);
28
+ /** The origin of each value kept, in the same order: what tells a value from the one it started as. */
29
+ var filledOrigins = (rows) => filledRows(rows).map((row) => row.origin);
10
30
  /** Whether another row fits under `max`; none or zero means no limit. */
11
31
  var canAddRow = (count, max) => max <= 0 || count < max;
12
- /** Whether two lists hold the same values in the same order. */
13
- var sameValues = (left, right) => left.length === right.length && left.every((value, index) => value === right[index]);
14
- export { canAddRow, filledValues, rowsOf, sameValues, withRowAt, withoutRowAt };
32
+ export { canAddRow, emptyRow, filledOrigins, filledRows, filledValues, filledValuesOf, rowsOf, withRowAt, withoutRowAt };
@@ -2,6 +2,7 @@
2
2
  /** What a form control asks of its internals that jsdom's lack: with them, a page's unit tests can create it. */
3
3
  var FORM_METHODS = {
4
4
  checkValidity: () => true,
5
+ form: null,
5
6
  reportValidity: () => true,
6
7
  setFormValue: () => void 0,
7
8
  setValidity: () => void 0
@@ -61,11 +61,22 @@ export declare class JlInputList extends JlElement {
61
61
  }>>;
62
62
  private readonly _state;
63
63
  private readonly _drawn;
64
+ private readonly _publishing;
64
65
  private readonly _refs;
65
66
  templateHtml(): Node;
66
67
  templateCss(): string;
68
+ /**
69
+ * The origin of each value of `values`: the index it had in the values last set from outside, and undefined for a
70
+ * row added since. A row keeps it while it is typed in, a removed row takes it away, so a corrected value is not
71
+ * taken for a removal beside an addition.
72
+ */
73
+ origins(): (number | undefined)[];
67
74
  onConnect(): void;
68
- /** Values set from outside start the rows again; those the rows themselves gave change nothing. */
75
+ /**
76
+ * Values set from outside start the rows again, the blank ones left out of `values`; those the rows themselves
77
+ * gave change nothing. A list given the values its rows already holds takes their origins, so a page that saves
78
+ * and shows the values again gets origins that count in what it saved.
79
+ */
69
80
  private follow;
70
81
  protected drawTexts(): void;
71
82
  /**
@@ -73,7 +84,7 @@ export declare class JlInputList extends JlElement {
73
84
  * runs as the list appears: a build in each made about ten per list.
74
85
  */
75
86
  private drawRows;
76
- /** Builds every row again, from the values in `_state`. */
87
+ /** Builds every row again, from the rows in `_state`. */
77
88
  private renderRows;
78
89
  /** The add button: its text, and hidden at `max`; the CSS hides it in a disabled list. */
79
90
  private renderAdd;
@@ -88,4 +99,6 @@ export declare class JlInputList extends JlElement {
88
99
  private addRow;
89
100
  private removeRow;
90
101
  private publish;
102
+ /** Keeps `values` without drawing the rows again: the list's own, and the filled ones of those set from outside. */
103
+ private setValues;
91
104
  }
@@ -7,7 +7,7 @@ import { JlButton } from "../atoms/jl-button.js";
7
7
  import { INPUT_TYPES, JlInput } from "../atoms/jl-input.js";
8
8
  import { JlSelect } from "../atoms/jl-select.js";
9
9
  import { JlTextarea } from "../atoms/jl-textarea.js";
10
- import { canAddRow, filledValues, rowsOf, sameValues, withRowAt, withoutRowAt } from "../lib/input-list.js";
10
+ import { canAddRow, emptyRow, filledOrigins, filledValues, filledValuesOf, rowsOf, withRowAt, withoutRowAt } from "../lib/input-list.js";
11
11
  import jl_input_list_default from "./jl-input-list.css.js";
12
12
  import { css } from "@jadis/core";
13
13
  import { Plus, X } from "lucide";
@@ -64,11 +64,12 @@ var JlInputList = class extends JlElement {
64
64
  addLabel = this.useChange("", () => this.renderAdd(), { attribute: "add-label" });
65
65
  /** The text a screen reader says for each row's remove button; jlib's own when empty. */
66
66
  removeLabel = this.useChange("", () => this.drawRows(), { attribute: "remove-label" });
67
- _state = { rows: [""] };
67
+ _state = { rows: rowsOf([]) };
68
68
  _drawn = {
69
69
  kind: void 0,
70
70
  rows: []
71
71
  };
72
+ _publishing = { on: false };
72
73
  _refs = this.useRefs((ref) => ({
73
74
  add: ref(".add"),
74
75
  rows: ref(".rows")
@@ -88,13 +89,26 @@ var JlInputList = class extends JlElement {
88
89
  templateCss() {
89
90
  return css`${BASE_STYLES}${jl_input_list_default}`;
90
91
  }
92
+ /**
93
+ * The origin of each value of `values`: the index it had in the values last set from outside, and undefined for a
94
+ * row added since. A row keeps it while it is typed in, a removed row takes it away, so a corrected value is not
95
+ * taken for a removal beside an addition.
96
+ */
97
+ origins() {
98
+ return filledOrigins(this._state.rows);
99
+ }
91
100
  onConnect() {
92
101
  this._refs.add.events.register("press", () => this.addRow());
93
102
  }
94
- /** Values set from outside start the rows again; those the rows themselves gave change nothing. */
103
+ /**
104
+ * Values set from outside start the rows again, the blank ones left out of `values`; those the rows themselves
105
+ * gave change nothing. A list given the values its rows already holds takes their origins, so a page that saves
106
+ * and shows the values again gets origins that count in what it saved.
107
+ */
95
108
  follow(values) {
96
- if (sameValues(values, filledValues(this._state.rows))) return;
109
+ if (this._publishing.on) return;
97
110
  this._state.rows = rowsOf(values);
111
+ this.setValues(filledValuesOf(values));
98
112
  this.renderRows();
99
113
  }
100
114
  drawTexts() {
@@ -112,10 +126,10 @@ var JlInputList = class extends JlElement {
112
126
  for (const row of this._drawn.rows) this.drawRow(row);
113
127
  this.renderAdd();
114
128
  }
115
- /** Builds every row again, from the values in `_state`. */
129
+ /** Builds every row again, from the rows in `_state`. */
116
130
  renderRows() {
117
131
  this._drawn.kind = this.rowKind();
118
- this._drawn.rows = this._state.rows.map((value, index) => this.row(value, index));
132
+ this._drawn.rows = this._state.rows.map((row, index) => this.row(row.value, index));
119
133
  this._refs.rows.replaceChildren(...this._drawn.rows.map(({ control, remove }) => /* @__PURE__ */ jsxs("div", {
120
134
  class: "row",
121
135
  children: [control, remove]
@@ -197,7 +211,7 @@ var JlInputList = class extends JlElement {
197
211
  }
198
212
  addRow() {
199
213
  if (!canAddRow(this._state.rows.length, this.max.get())) return;
200
- this._state.rows = [...this._state.rows, ""];
214
+ this._state.rows = [...this._state.rows, emptyRow()];
201
215
  this.renderRows();
202
216
  const added = this._refs.rows.lastElementChild?.firstElementChild;
203
217
  if (added instanceof HTMLElement) added.focus();
@@ -208,9 +222,14 @@ var JlInputList = class extends JlElement {
208
222
  this.publish();
209
223
  }
210
224
  publish() {
211
- const values = filledValues(this._state.rows);
225
+ this.setValues(filledValues(this._state.rows));
226
+ this.events.emit("change", this.values.get());
227
+ }
228
+ /** Keeps `values` without drawing the rows again: the list's own, and the filled ones of those set from outside. */
229
+ setValues(values) {
230
+ this._publishing.on = true;
212
231
  this.values.set(values);
213
- this.events.emit("change", values);
232
+ this._publishing.on = false;
214
233
  }
215
234
  };
216
235
  JlInputList.register();
@@ -30,6 +30,8 @@ export declare class JlPopover extends JlElement {
30
30
  show(): void;
31
31
  hide(): void;
32
32
  toggle(): void;
33
+ /** Places the panel again by its trigger: for content whose size changes, the panel follows it as it opens. */
34
+ place(): void;
33
35
  templateHtml(): Node;
34
36
  templateCss(): string;
35
37
  onConnect(): void;
@@ -44,6 +44,10 @@ var JlPopover = class extends JlElement {
44
44
  toggle() {
45
45
  this.open.set((open) => !open);
46
46
  }
47
+ /** Places the panel again by its trigger: for content whose size changes, the panel follows it as it opens. */
48
+ place() {
49
+ this.follow();
50
+ }
47
51
  templateHtml() {
48
52
  return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("slot", { name: "trigger" }), /* @__PURE__ */ jsxs("div", {
49
53
  class: "floating panel",
@@ -2,12 +2,12 @@ import { jlText } from "../lib/texts.js";
2
2
  import { BASE_STYLES } from "../styles/shared.js";
3
3
  import { JlElement } from "../base/jl-element.js";
4
4
  import { jsonOf, oneOf, textOf } from "../lib/attributes.js";
5
+ import { watchSizes } from "../lib/resize.js";
5
6
  import { registerIcons } from "../lib/icons.js";
6
7
  import { JlIcon } from "../atoms/jl-icon.js";
7
8
  import { setOrRemove, uniqueId } from "../lib/dom.js";
8
9
  import { JlButton } from "../atoms/jl-button.js";
9
10
  import { hotkeyLabels, isApplePlatform, matchesHotkey, parseHotkey } from "../lib/hotkey.js";
10
- import { watchSizes } from "../lib/resize.js";
11
11
  import { findItem, firstLeaf, pathInSections } from "../lib/nav-tree.js";
12
12
  import jl_side_nav_default from "./jl-side-nav.css.js";
13
13
  import { css } from "@jadis/core";
@@ -1,9 +1,9 @@
1
1
  import { jlText } from "../lib/texts.js";
2
2
  import { BASE_STYLES } from "../styles/shared.js";
3
3
  import { JlElement } from "../base/jl-element.js";
4
+ import { watchSizes } from "../lib/resize.js";
4
5
  import { registerIcons } from "../lib/icons.js";
5
6
  import { JlIcon } from "../atoms/jl-icon.js";
6
- import { watchSizes } from "../lib/resize.js";
7
7
  import jl_app_shell_default from "./jl-app-shell.css.js";
8
8
  import { css } from "@jadis/core";
9
9
  import { Menu } from "lucide";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syware/jlib",
3
- "version": "0.16.1",
3
+ "version": "0.17.1",
4
4
  "description": "A tree-shakeable component library on Jadis and native Web Components, organised in atomic design",
5
5
  "keywords": [
6
6
  "web-components",