@diegopluna/narvi 0.5.0 → 0.5.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.
@@ -66,7 +66,9 @@
66
66
  display: 'flex',
67
67
  flexDirection: 'column',
68
68
  flexShrink: 0,
69
- width: 'var(--narvi-panel-width)',
69
+ // The handle's size while it is dragged, which is not inherited, so the panel restyles
70
+ // alone as it moves; its width once it is let go
71
+ width: 'var(--narvi-size, var(--narvi-panel-width))',
70
72
  minWidth: 0,
71
73
  minHeight: 0,
72
74
  overflow: 'clip',
@@ -201,7 +203,6 @@
201
203
  defaultValue={defaultWidth}
202
204
  edge="start"
203
205
  target={element}
204
- property="--narvi-panel-width"
205
206
  label={edgeLabel ?? `Resize the ${label.toLowerCase()} panel`}
206
207
  title={edgeTitle}
207
208
  sx={styles.handle}
@@ -3,8 +3,13 @@
3
3
 
4
4
  While dragging, the size is written straight onto the target as a custom
5
5
  property, so nothing re-renders on every pointer move; the bound value
6
- changes once, when the drag ends. The keyboard moves it in steps, Home and
7
- End go to the limits, and a double-click puts it back. (Eregion's ResizableEdge.)
6
+ changes once, when the drag ends, and the property is put back as it was.
7
+ Draw the target from both, the property first and the value as its fallback:
8
+ `width: var(--narvi-size, 320px)`. Narvi registers `--narvi-size` as not
9
+ inherited, so a move restyles the target alone, not what is in it.
10
+
11
+ The keyboard moves it in steps, Home and End go to the limits, and a
12
+ double-click puts it back. (Eregion's ResizableEdge.)
8
13
  -->
9
14
  <script lang="ts" module>
10
15
  import * as stylex from '@stylexjs/stylex';
@@ -30,9 +35,23 @@
30
35
  edge?: 'start' | 'end' | undefined;
31
36
  /** `vertical` resizes widths; `horizontal` resizes heights, as for a drawer below. */
32
37
  orientation?: 'vertical' | 'horizontal' | undefined;
33
- /** The element being resized, and the custom property its size is read from. */
38
+ /** The element being resized. */
34
39
  target?: HTMLElement | null | undefined;
40
+ /**
41
+ * The custom property the size is written to on `target` while dragging, and taken off
42
+ * again when the drag ends. `--narvi-size` by default, which Narvi registers as not
43
+ * inherited. Register one of your own the same way
44
+ * (`@property --x { syntax: '*'; inherits: false; }`), or every move restyles all that
45
+ * is in the target.
46
+ */
35
47
  property?: string | undefined;
48
+ /** Called as a drag starts, before the first move. */
49
+ onDragStart?: (() => void) | undefined;
50
+ /**
51
+ * Called as a drag ends, with the size it ended at, once `value` has it. If the handle
52
+ * goes away mid-drag, it is called with the size from before the drag, which is kept.
53
+ */
54
+ onDragEnd?: ((value: number) => void) | undefined;
36
55
  /** Names the handle, for assistive technology: "Resize the panel". */
37
56
  label: string;
38
57
  sx?: Sx | undefined;
@@ -82,6 +101,23 @@
82
101
  '::after': { backgroundColor: colors.accent, transitionDelay: '0ms' }
83
102
  }
84
103
  });
104
+
105
+ const SIZE_PROPERTY = '--narvi-size';
106
+
107
+ let registered = false;
108
+ /**
109
+ * Registers the handle's own property as not inherited and with no initial value, so writing
110
+ * it restyles the target alone, and where it is unset its fallback applies. Once, in a browser.
111
+ */
112
+ function register() {
113
+ if (registered || typeof CSS === 'undefined' || !('registerProperty' in CSS)) return;
114
+ registered = true;
115
+ try {
116
+ CSS.registerProperty({ name: SIZE_PROPERTY, syntax: '*', inherits: false });
117
+ } catch {
118
+ // Registered already, by another copy of Narvi or by the app
119
+ }
120
+ }
85
121
  </script>
86
122
 
87
123
  <script lang="ts">
@@ -94,24 +130,32 @@
94
130
  edge = 'start',
95
131
  orientation = 'vertical',
96
132
  target,
97
- property = '--narvi-size',
133
+ property = SIZE_PROPERTY,
134
+ onDragStart,
135
+ onDragEnd,
98
136
  label,
99
137
  sx,
100
138
  class: className,
101
139
  ...rest
102
140
  }: ResizeHandleProps = $props();
103
141
 
142
+ register();
143
+
104
144
  let dragging = $state(false);
145
+ /** Ends the drag under way: keeps the size it reached, or with `false`, the one before it. */
146
+ let stop: ((keep: boolean) => void) | undefined;
105
147
  const limit = () => (typeof max === 'function' ? max() : max);
106
148
  const clamp = (size: number, upper = limit()) =>
107
149
  Math.round(Math.min(Math.max(size, min), Math.max(min, upper)));
108
150
 
151
+ // Gone mid-drag, as when its panel goes narrow: nothing is left on the target
152
+ $effect(() => () => stop?.(false));
153
+
109
154
  function onpointerdown(event: PointerEvent & { currentTarget: HTMLDivElement }) {
110
- if (event.button !== 0) return;
155
+ if (event.button !== 0 || stop) return;
111
156
  event.preventDefault();
112
157
  const handle = event.currentTarget;
113
158
  handle.setPointerCapture(event.pointerId);
114
- dragging = true;
115
159
  const start = orientation === 'vertical' ? event.clientX : event.clientY;
116
160
  const from = value;
117
161
  const upper = limit();
@@ -120,22 +164,44 @@
120
164
  let sign = edge === 'start' ? -1 : 1;
121
165
  if (orientation === 'vertical' && rtl) sign = -sign;
122
166
  let size = from;
167
+ // This drag's target and property, and what the target held there before it
168
+ const element = target;
169
+ const name = property;
170
+ const before = element?.style.getPropertyValue(name) ?? '';
171
+ const priority = element?.style.getPropertyPriority(name) ?? '';
172
+ let written = false;
123
173
 
124
174
  const move = (moving: PointerEvent) => {
125
175
  const now = orientation === 'vertical' ? moving.clientX : moving.clientY;
126
- size = clamp(from + (now - start) * sign, upper);
127
- target?.style.setProperty(property, `${size}px`);
176
+ const next = clamp(from + (now - start) * sign, upper);
177
+ // A move that ends at the same pixel, or past a limit, restyles nothing
178
+ if (written && next === size) return;
179
+ size = next;
180
+ written = true;
181
+ element?.style.setProperty(name, `${size}px`);
128
182
  };
129
- const up = () => {
183
+ const end = () => stop?.(true);
184
+ stop = (keep) => {
185
+ stop = undefined;
130
186
  dragging = false;
131
187
  handle.removeEventListener('pointermove', move);
132
- handle.removeEventListener('pointerup', up);
133
- handle.removeEventListener('pointercancel', up);
134
- value = size;
188
+ handle.removeEventListener('pointerup', end);
189
+ handle.removeEventListener('pointercancel', end);
190
+ handle.removeEventListener('lostpointercapture', end);
191
+ if (keep) value = size;
192
+ // What was there comes back, and the value draws the target from here, before the
193
+ // next frame: Svelte applies it in a microtask
194
+ if (before) element?.style.setProperty(name, before, priority);
195
+ else element?.style.removeProperty(name);
196
+ onDragEnd?.(keep ? size : from);
135
197
  };
136
198
  handle.addEventListener('pointermove', move);
137
- handle.addEventListener('pointerup', up);
138
- handle.addEventListener('pointercancel', up);
199
+ handle.addEventListener('pointerup', end);
200
+ handle.addEventListener('pointercancel', end);
201
+ // The capture is lost without an up when the handle leaves the document, say
202
+ handle.addEventListener('lostpointercapture', end);
203
+ dragging = true;
204
+ onDragStart?.();
139
205
  }
140
206
 
141
207
  function onkeydown(event: KeyboardEvent & { currentTarget: HTMLDivElement }) {
@@ -17,9 +17,23 @@ export type ResizeHandleProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'>
17
17
  edge?: 'start' | 'end' | undefined;
18
18
  /** `vertical` resizes widths; `horizontal` resizes heights, as for a drawer below. */
19
19
  orientation?: 'vertical' | 'horizontal' | undefined;
20
- /** The element being resized, and the custom property its size is read from. */
20
+ /** The element being resized. */
21
21
  target?: HTMLElement | null | undefined;
22
+ /**
23
+ * The custom property the size is written to on `target` while dragging, and taken off
24
+ * again when the drag ends. `--narvi-size` by default, which Narvi registers as not
25
+ * inherited. Register one of your own the same way
26
+ * (`@property --x { syntax: '*'; inherits: false; }`), or every move restyles all that
27
+ * is in the target.
28
+ */
22
29
  property?: string | undefined;
30
+ /** Called as a drag starts, before the first move. */
31
+ onDragStart?: (() => void) | undefined;
32
+ /**
33
+ * Called as a drag ends, with the size it ended at, once `value` has it. If the handle
34
+ * goes away mid-drag, it is called with the size from before the drag, which is kept.
35
+ */
36
+ onDragEnd?: ((value: number) => void) | undefined;
23
37
  /** Names the handle, for assistive technology: "Resize the panel". */
24
38
  label: string;
25
39
  sx?: Sx | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diegopluna/narvi",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "packageManager": "pnpm@12.18.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,7 +12,7 @@ metadata:
12
12
  Grid, and Narvi's conventions for colour, labels and copy.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.0'
15
+ library_version: '0.5.1'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -36,6 +36,7 @@ sources:
36
36
  - src/lib/components/tab-strip/TabStrip.svelte
37
37
  - src/lib/components/split-button/SplitButton.svelte
38
38
  - src/lib/components/app-shell/*.svelte
39
+ - src/lib/components/resize-handle/ResizeHandle.svelte
39
40
  - src/lib/components/composer/ComposerModelPicker.svelte
40
41
  - src/lib/components/questionnaire/Questionnaire.svelte
41
42
  - src/lib/components/approval/Approval.svelte
@@ -154,10 +155,9 @@ Children that render nothing (an `{#if}` that is false) leave no empty body.
154
155
 
155
156
  ### Forms
156
157
 
157
- Wrap a text control (`Input`, `Textarea` or `Select`), a `Checkbox` or a `Switch` in `Field`.
158
- They read Field's context, so it gives them a label, help text (`description`) and an error message (`error`), and
159
- wires up `id`, `aria-describedby` and `aria-invalid`. Don't hand-write a `<label>` or the ARIA
160
- wiring for them.
158
+ Wrap a text control (`Input`, `Textarea` or `Select`), a `Checkbox` or a `Switch` in `Field`. They
159
+ read its context, so it gives them a label, help text (`description`) and an error (`error`), and
160
+ wires up `id`, `aria-describedby` and `aria-invalid`. Don't hand-write their `<label>` or ARIA.
161
161
 
162
162
  ```svelte
163
163
  <Field label="Email" description="We only write when a run needs you." error={emailError}>
@@ -296,6 +296,25 @@ page is laid out once per show or hide. What is under `AppShell.Header` glides o
296
296
  for 200ms; the header, the panel and its edge take their places at once. Don't animate the
297
297
  sidebar's width yourself, and don't put a `translate` of your own on `AppShell.Main`'s children.
298
298
 
299
+ ### Resize handles
300
+
301
+ A `ResizeHandle` of your own (`bind:value`, `min`, `max`, `defaultValue`, `edge`,
302
+ `orientation`, `target`, `property`) writes the size onto `target` as `property` while it is
303
+ dragged, and takes it off again when the drag ends, once `value` has the size. Draw the target
304
+ from both, the property first and the value as its fallback:
305
+
306
+ ```svelte
307
+ <div bind:this={side} style:width="var(--narvi-size, {width}px)">…</div>
308
+ <ResizeHandle bind:value={width} edge="end" target={side} label="Resize the sidebar" />
309
+ ```
310
+
311
+ `--narvi-size`, the default, is registered as not inherited, so a move restyles the target
312
+ alone. A `property` of your own needs the same, or every move restyles all that is in the
313
+ target: `@property --pane-size { syntax: '*'; inherits: false; }`, with no initial value so the
314
+ fallback applies once it's let go. Don't read the property from inside the target; it isn't
315
+ inherited. `onDragStart()` and `onDragEnd(value)` bracket a drag, say to keep an iframe from
316
+ taking the pointer. A handle removed mid-drag ends it with the size from before.
317
+
299
318
  ## AI chat
300
319
 
301
320
  | Component | Use it for | Docs slug |
@@ -12,7 +12,7 @@ metadata:
12
12
  patching the DOM, and decide which links and images text from a model may load.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.0'
15
+ library_version: '0.5.1'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -11,7 +11,7 @@ metadata:
11
11
  4 when the app uses it.
12
12
  type: lifecycle
13
13
  library: '@diegopluna/narvi'
14
- library_version: '0.5.0'
14
+ library_version: '0.5.1'
15
15
  sources:
16
16
  - src/lib/vite.ts
17
17
  - src/lib/base.css
@@ -12,7 +12,7 @@ metadata:
12
12
  hover, motion and forced-colour preferences.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.0'
15
+ library_version: '0.5.1'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -12,7 +12,7 @@ metadata:
12
12
  menus and dialogs, and resolve token colours for drawing code that cannot read CSS variables.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.0'
15
+ library_version: '0.5.1'
16
16
  requires:
17
17
  - setup
18
18
  sources: