staffa 0.23.1 → 0.23.3

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.
@@ -33,6 +33,9 @@ A.insertGlobalCss({
33
33
  // On a textarea the insets ride the bottom edge, so a growing box keeps its
34
34
  // send button where the caret is rather than floating it mid-paragraph.
35
35
  "&.s-inset-bottom > .s-inset_start, &.s-inset-bottom > .s-inset_end": "top:auto padding-bottom:0.35em",
36
+ // A column rather than a grid, so the textarea flexes within the box as it
37
+ // would directly in the field.
38
+ "&.s-inset-bottom:has(> .s-inset_start), &.s-inset-bottom:has(> .s-inset_end)": "display:flex flex-direction:column",
36
39
  "> .s-inset_start > svg, > .s-inset_end > svg": "width:1.15em height:1.15em",
37
40
  // A number input's spinner wants the very corner a trailing inset is in, and
38
41
  // it is the inset that was asked for. (Firefox's is `appearance`-controlled.)
@@ -1,14 +1,13 @@
1
1
  import A from "aberdeen";
2
2
  import { applyControlAttrs, drawField, drawInsets } from "./field.js";
3
3
  A.insertGlobalCss({
4
- // `flex-basis:100%` with nothing to stop it shrinking means "take the field's
5
- // height, then give back whatever the label and help need" — so a field told
6
- // how tall to be (`attrs: "h:8rem"`) hands that height to the textarea, while a
7
- // field left to size itself (every ordinary one) has no definite height for the
8
- // basis to resolve against and the textarea is content-sized exactly as before.
9
- // The same rule rides the inset box below, which stands in for the control once
10
- // a prefix/suffix gives it one.
11
- "textarea.s-input, .s-inset.s-inset-bottom": "flex: 0 1 100%; min-height:0",
4
+ // Start from the height the textarea asks for (its rows, or its text when
5
+ // growing), then grow or shrink to whatever room the field has left: a field
6
+ // told how tall to be (`attrs: "h:8rem"`) hands its height to the textarea,
7
+ // while a field left to size itself has no room to share out, so the textarea
8
+ // keeps its own height. The same rule rides the inset box below, which stands
9
+ // in for the control once a prefix/suffix gives it one.
10
+ "textarea.s-input, .s-inset.s-inset-bottom": "flex: 1 1 auto; min-height:0",
12
11
  "textarea.s-input": "resize:vertical min-height:3em line-height:1.45",
13
12
  // Sized to its text there is nothing to scroll; `fitToContent` turns scrolling
14
13
  // on once a caller caps the height (`inputAttrs: "max-height:12em"`).
@@ -71,22 +70,41 @@ export function textarea(opts = {}) {
71
70
  A("change=", opts.change);
72
71
  applyControlAttrs(opts, id, isInvalid, opts.bind);
73
72
  });
74
- if (grow)
75
- requestAnimationFrame(() => fitToContent(el));
73
+ if (grow) {
74
+ // Typing aside, refit when the text is set from code (a value loaded
75
+ // after drawing, a box cleared after sending), and when the box is
76
+ // shown or changes width, which rewraps the text.
77
+ A(() => {
78
+ opts.bind?.value;
79
+ requestAnimationFrame(() => fitToContent(el));
80
+ });
81
+ new ResizeObserver(() => fitToContent(el)).observe(el);
82
+ }
76
83
  });
77
84
  });
78
85
  }
79
86
  function fitToContent(el) {
80
- el.style.height = "auto";
81
- // Measure without a scrollbar: one would narrow the text and wrap it early.
82
- el.style.overflowY = "";
87
+ // Measure a copy taken out of the flow: collapsing the textarea itself would
88
+ // scroll its caret into view of the collapsed box and shrink the page under
89
+ // it, so a capped textarea, or the page, would jump on every keystroke.
90
+ const probe = el.cloneNode();
91
+ probe.value = el.value;
92
+ Object.assign(probe.style, {
93
+ position: "absolute",
94
+ visibility: "hidden",
95
+ width: `${el.offsetWidth}px`,
96
+ height: "auto",
97
+ // Measure without a scrollbar: one would narrow the text and wrap it early.
98
+ overflowY: "",
99
+ });
100
+ el.after(probe);
83
101
  // `scrollHeight` measures the padding box, while the height we set sizes the
84
102
  // border box: add the borders back, or the text ends up two pixels too tall
85
103
  // for its box and the textarea scrolls by that much.
86
- const height = el.scrollHeight + el.offsetHeight - el.clientHeight;
104
+ const height = probe.scrollHeight + probe.offsetHeight - probe.clientHeight;
105
+ probe.remove();
87
106
  el.style.height = `${height}px`;
88
107
  // Scroll only once a `max-height` caps the box: `scrollHeight` is rounded, so
89
108
  // an uncapped box can fall a fraction of a pixel short and show a bar.
90
- if (el.offsetHeight < height)
91
- el.style.overflowY = "auto";
109
+ el.style.overflowY = el.offsetHeight < height ? "auto" : "";
92
110
  }
@@ -13,7 +13,6 @@ A.insertGlobalCss({
13
13
  // ─── Global portal state ────────────────────────────────────────────────────
14
14
  // At most one tooltip at a time; the anchor is the element whose rect positions it.
15
15
  const $ttActive = A.proxy(undefined);
16
- let hideTimer = null;
17
16
  function computePos(rect, tipW, tipH, placement) {
18
17
  const gap = 7;
19
18
  const vw = window.innerWidth;
@@ -53,14 +52,6 @@ function computePos(rect, tipW, tipH, placement) {
53
52
  y: Math.max(8, Math.min(y, vh - tipH - 8)),
54
53
  };
55
54
  }
56
- function scheduleHide() {
57
- if (hideTimer)
58
- clearTimeout(hideTimer);
59
- hideTimer = setTimeout(() => {
60
- $ttActive.value = undefined;
61
- hideTimer = null;
62
- }, 100);
63
- }
64
55
  // ─── Portal ──────────────────────────────────────────────────────────────────
65
56
  mountPortal(() => {
66
57
  const active = $ttActive.value;
@@ -68,16 +59,7 @@ mountPortal(() => {
68
59
  return;
69
60
  const { opts, anchor } = active;
70
61
  const placement = opts.placement ?? "top";
71
- const tipEl = A("div.s-tt-tip.s-s.neutral.shadow role=tooltip", opts.attrs, () => {
72
- A("mouseenter=", () => {
73
- if (hideTimer) {
74
- clearTimeout(hideTimer);
75
- hideTimer = null;
76
- }
77
- });
78
- A("mouseleave=", scheduleHide);
79
- drawSlot(opts.tip);
80
- });
62
+ const tipEl = A("div.s-tt-tip.s-s.neutral.shadow role=tooltip", opts.attrs, () => drawSlot(opts.tip));
81
63
  followAnchor(anchor, (rect) => {
82
64
  // Out of the viewport, or on a panel that slid off and went inert: nothing
83
65
  // left to explain, and no `mouseleave` is coming to say so.
@@ -113,22 +95,34 @@ mountPortal(() => {
113
95
  * ```
114
96
  */
115
97
  export function addTooltip(opts) {
98
+ // Pressing the anchor dismisses its tip until the pointer leaves, as a native
99
+ // `title` does: whatever the press opens shouldn't sit under the tip.
100
+ let pressed = false;
116
101
  const show = (e) => {
117
- if (hideTimer) {
118
- clearTimeout(hideTimer);
119
- hideTimer = null;
120
- }
121
- $ttActive.value = { opts, anchor: e.currentTarget };
102
+ if (!pressed)
103
+ $ttActive.value = { opts, anchor: e.currentTarget };
104
+ };
105
+ // Only this anchor's own tip: another one may have taken over since.
106
+ const hide = (e) => {
107
+ if (A.unproxy($ttActive).value?.anchor === e.currentTarget)
108
+ $ttActive.value = undefined;
122
109
  };
123
110
  A("mouseenter=", show);
124
- A("mouseleave=", scheduleHide);
111
+ A("mouseleave=", (e) => {
112
+ pressed = false;
113
+ hide(e);
114
+ });
115
+ A("pointerdown=", (e) => {
116
+ pressed = true;
117
+ hide(e);
118
+ });
125
119
  // Keyboard focus only: programmatic focus (a nav opening on its current row,
126
120
  // say) would pop a tip over the very thing it describes.
127
121
  A("focusin=", (e) => {
128
122
  if (e.target.matches?.(":focus-visible"))
129
123
  show(e);
130
124
  });
131
- A("focusout=", scheduleHide);
125
+ A("focusout=", hide);
132
126
  A.clean(() => {
133
127
  // Compare raw: the proxy's getter wraps stored objects, so `$ttActive.value.opts`
134
128
  // would be a Proxy that never equals our raw `opts`.