svelte-ant-design 0.5.0 → 0.5.2

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.
package/README.md CHANGED
@@ -18,7 +18,7 @@ Ant Design.
18
18
 
19
19
  ## Status
20
20
 
21
- Version `0.5.0`. Available components: `Button`, `Checkbox` (with `CheckboxGroup`), `Input` (with `InputTextArea`, `InputPassword`, `InputSearch` and `InputOTP`), `Select` (with `SelectOption` and `SelectOptGroup`), `Switch`. More components are planned.
21
+ Version `0.5.2`. Available components: `Button`, `Checkbox` (with `CheckboxGroup`), `Input` (with `InputTextArea`, `InputPassword`, `InputSearch` and `InputOTP`), `Select` (with `SelectOption` and `SelectOptGroup`), `Switch`. More components are planned.
22
22
 
23
23
  ## Installation
24
24
 
@@ -2,6 +2,7 @@
2
2
  import { onMount, untrack, type Snippet } from 'svelte';
3
3
  import type { HTMLAttributes } from 'svelte/elements';
4
4
  import Popup from '../internal/popup/Popup.svelte';
5
+ import { clickable } from '../internal/clickable.js';
5
6
  import { resolveOption } from '../internal/options.js';
6
7
  import { classList, styleText } from '../internal/semantic.js';
7
8
  import { setSelectContext, type Registration } from './context.js';
@@ -266,6 +267,26 @@
266
267
  scrollToActive();
267
268
  });
268
269
 
270
+ // The list keeps the wheel to itself (`overscroll-behavior: contain`) only while it scrolls. A list that does not
271
+ // scroll would otherwise hold the wheel for nothing, and the page would not move under the cursor. Measured after
272
+ // each change of what the list shows, and once more in the next frame, when the layout has settled. A list that is
273
+ // wrongly taken for one that does not scroll lets the wheel through at its end, which does no harm.
274
+ let listScrolls = $state(false);
275
+ $effect(() => {
276
+ void visibleNodes;
277
+ void popupOpen;
278
+ void listHeight;
279
+ const list = listElement;
280
+ if (!list) {
281
+ listScrolls = false;
282
+ return;
283
+ }
284
+ const measure = () => (listScrolls = list.scrollHeight > list.clientHeight);
285
+ measure();
286
+ const frame = requestAnimationFrame(measure);
287
+ return () => cancelAnimationFrame(frame);
288
+ });
289
+
269
290
  // ---- Opening and closing ---------------------------------------------------------------------------------
270
291
 
271
292
  /** Changes the open state on behalf of the user. The parent may refuse (a function binding). */
@@ -779,6 +800,7 @@
779
800
  classNames?.listItem
780
801
  )}
781
802
  style={styleText(styles?.listItem)}
803
+ use:clickable
782
804
  onclick={() => selectEntry(entry)}
783
805
  onmousemove={() => {
784
806
  if (activeValue === entry.value || isDisabled) return;
@@ -814,6 +836,7 @@
814
836
  aria-labelledby={listLabelledBy}
815
837
  class={classList('ads-select-list', classNames?.list)}
816
838
  style={styleText(`max-height: ${listHeight}px`, styles?.list)}
839
+ data-scrollable={listScrolls ? '' : undefined}
817
840
  bind:this={listElement}
818
841
  onscroll={onpopupscroll}
819
842
  >
@@ -1410,6 +1433,10 @@
1410
1433
  .ads-select-list {
1411
1434
  position: relative;
1412
1435
  overflow: hidden auto;
1436
+ }
1437
+
1438
+ /* Only a list that scrolls keeps the wheel from the page (see `listScrolls`). */
1439
+ .ads-select-list[data-scrollable] {
1413
1440
  overscroll-behavior: contain;
1414
1441
  }
1415
1442
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "0.5.0";
1
+ export declare const VERSION = "0.5.2";
2
2
  export { default as Button } from './Button/Button.svelte';
3
3
  export { default as Switch } from './Switch/Switch.svelte';
4
4
  export { default as Checkbox } from './Checkbox/Checkbox.svelte';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Public API of the library. Components are re-exported from here.
2
- export const VERSION = '0.5.0';
2
+ export const VERSION = '0.5.2';
3
3
  export { default as Button } from './Button/Button.svelte';
4
4
  export { default as Switch } from './Switch/Switch.svelte';
5
5
  export { default as Checkbox } from './Checkbox/Checkbox.svelte';
@@ -0,0 +1 @@
1
+ export declare function clickable(node: HTMLElement): void;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Makes WebKit send the `click` of a tap to an element that is not a link, a button or a field.
3
+ *
4
+ * In WebKit a tap on such an element sends no `click` (and no `mousedown`) unless the element has a click listener
5
+ * of its own, a `tabindex` or an `onclick` attribute. Svelte does not put the listener of `onclick` on the element:
6
+ * it puts one on the document (event delegation), which WebKit does not count, so a `div` with an `onclick` in
7
+ * Svelte cannot be tapped. An empty listener on the element itself is enough; the real handler stays where it is.
8
+ * Use it as `use:clickable` on an element whose `onclick` is the only way to act on it, such as an option of a list.
9
+ */
10
+ const noop = () => { };
11
+ export function clickable(node) {
12
+ node.addEventListener('click', noop);
13
+ }
@@ -106,6 +106,11 @@
106
106
  let leaveToken = 0;
107
107
  let leaveCleanup: (() => void) | null = null;
108
108
  let lastPlacement: PopupPlacement | null = null;
109
+ // The translation of the last placement: exactly as computed, and as written (rounded to a device pixel). The
110
+ // panel's rectangle minus the written one is where its containing block starts.
111
+ let exact: { x: number; y: number } | null = null;
112
+ let appliedX = 0;
113
+ let appliedY = 0;
109
114
 
110
115
  function cancelLeave(): void {
111
116
  leaveToken++;
@@ -117,8 +122,14 @@
117
122
  cancelLeave();
118
123
  shown = false;
119
124
  if (destroyOnHidden) mounted = false;
120
- if (panel) delete panel.dataset.positioned;
125
+ if (panel) {
126
+ delete panel.dataset.positioned;
127
+ panel.style.transform = '';
128
+ }
121
129
  lastPlacement = null;
130
+ exact = null;
131
+ appliedX = 0;
132
+ appliedY = 0;
122
133
  }
123
134
 
124
135
  /** Plays the leave animation and hides the panel when it ends; at once when there is no animation. */
@@ -221,6 +232,21 @@
221
232
  }
222
233
 
223
234
  const round = (value: number) => Math.round(value * 100) / 100;
235
+ /** A transform is not snapped to the pixel grid as a `left` or a `top` is, and text drawn between pixels blurs. */
236
+ const snap = (value: number) => Math.round(value * window.devicePixelRatio) / window.devicePixelRatio;
237
+
238
+ /** Writes a `data-` attribute only when it changes: a write of the same value still costs a style recalculation. */
239
+ function setData(element: HTMLElement, name: string, value: string): void {
240
+ if (element.dataset[name] !== value) element.dataset[name] = value;
241
+ }
242
+
243
+ /** The anchor stays where it is on the screen when the page scrolls: it is a point, or inside a fixed element. */
244
+ function anchoredToViewport(): boolean {
245
+ for (let node: Element | null = anchorElement; node; node = node.parentElement) {
246
+ if (getComputedStyle(node).position === 'fixed') return true;
247
+ }
248
+ return anchorElement === null;
249
+ }
224
250
 
225
251
  /** Measures and places the panel. Runs only while the panel exists and is shown. */
226
252
  function update(): void {
@@ -230,13 +256,18 @@
230
256
  const computed = getComputedStyle(element);
231
257
  const arrowSize = arrowOn ? parseFloat(computed.getPropertyValue('--ads-popup-arrow-size')) || 8 : 0;
232
258
 
259
+ // The panel is in the same space as its anchor: in the page, so that the page moves both in the same frame
260
+ // (decision 13), unless the anchor stays on the screen, and then so does the panel.
261
+ element.toggleAttribute('data-fixed', anchoredToViewport());
233
262
  if (matchWidth === false) element.style.minWidth = '';
234
263
  else element.style.minWidth = `${Math.max(typeof matchWidth === 'number' ? matchWidth : 0, box.width)}px`;
235
- // Measure the natural size: at the origin the panel has the whole viewport to lay out in.
236
- element.style.left = '0px';
237
- element.style.top = '0px';
238
264
  const view = document.documentElement;
265
+ // A panel sits at the corner of its containing block, which may be narrow: it is still as wide as it would be
266
+ // on a fixed panel, that is, up to the viewport.
267
+ element.style.maxWidth = `${view.clientWidth}px`;
239
268
  const rtl = getComputedStyle(anchorElement ?? element).direction === 'rtl';
269
+ // Where the containing block starts, in the viewport: where the panel is, less what was added to put it there.
270
+ const origin = element.getBoundingClientRect();
240
271
 
241
272
  const result = computePlacement({
242
273
  anchor: box,
@@ -249,8 +280,16 @@
249
280
  arrow: arrowOn ? { size: arrowSize, pointAtCenter, edge: 16 } : null
250
281
  });
251
282
 
252
- element.style.left = `${round(result.x)}px`;
253
- element.style.top = `${round(result.y)}px`;
283
+ // Nothing is written when the placement did not change: while the page scrolls the two numbers differ only by
284
+ // the noise of the arithmetic, and a rounding flipping between two pixels would shake the panel.
285
+ const x = result.x - (origin.left - appliedX);
286
+ const y = result.y - (origin.top - appliedY);
287
+ if (exact === null || Math.abs(x - exact.x) > 0.01 || Math.abs(y - exact.y) > 0.01) {
288
+ exact = { x, y };
289
+ appliedX = snap(x);
290
+ appliedY = snap(y);
291
+ element.style.transform = `translate(${appliedX}px, ${appliedY}px)`;
292
+ }
254
293
  if (result.arrow !== null) {
255
294
  // The arrow is positioned against the padding box of the frame. Take the border off, so that it still
256
295
  // points at the right place and sits on the outer edge of a frame that has one.
@@ -268,9 +307,9 @@
268
307
  element.style.setProperty('--ads-popup-arrow-offset', `${round(result.arrow - startBorder)}px`);
269
308
  element.style.setProperty('--ads-popup-arrow-shift', `${facingBorder}px`);
270
309
  }
271
- element.dataset.positioned = '';
272
- frameElement.dataset.placement = result.placement;
273
- frameElement.dataset.side = result.side;
310
+ setData(element, 'positioned', '');
311
+ setData(frameElement, 'placement', result.placement);
312
+ setData(frameElement, 'side', result.side);
274
313
  if (lastPlacement !== result.placement) {
275
314
  lastPlacement = result.placement;
276
315
  untrack(() => onplacement?.(result.placement));
@@ -360,13 +399,20 @@
360
399
  {/if}
361
400
 
362
401
  <style>
402
+ /* In the page, with the anchor: the page scrolls both in the same frame. Moved by `transform`, from the corner. */
363
403
  .ads-popup {
364
- position: fixed;
404
+ position: absolute;
365
405
  top: 0;
366
406
  left: 0;
407
+ width: max-content;
367
408
  box-sizing: border-box;
368
409
  }
369
410
 
411
+ /* An anchor that stays on the screen (a point, or inside a fixed element) gets a panel that stays too. */
412
+ .ads-popup[data-fixed] {
413
+ position: fixed;
414
+ }
415
+
370
416
  /* Hidden until the first placement, so the panel never shows at the origin. */
371
417
  .ads-popup:not([data-positioned]) {
372
418
  visibility: hidden;
@@ -5,6 +5,10 @@
5
5
  * are open: a press outside closes the popups it is outside of, and Escape closes the topmost one only.
6
6
  * A popup opened from inside another popup (a Select inside a Popover) is its child: a press in the child
7
7
  * is inside the parent as well, so the parent stays open while the child is used.
8
+ *
9
+ * A mouse or a pen presses on `pointerdown`. A finger is decided when it comes up, and only if it was a tap:
10
+ * a finger that goes down may be about to scroll the page, and closing at that moment would close the popup
11
+ * under the user's hand (decision 14).
8
12
  */
9
13
  export type DismissReason = 'outsidePress' | 'escape';
10
14
  export interface Layer {
@@ -5,6 +5,10 @@
5
5
  * are open: a press outside closes the popups it is outside of, and Escape closes the topmost one only.
6
6
  * A popup opened from inside another popup (a Select inside a Popover) is its child: a press in the child
7
7
  * is inside the parent as well, so the parent stays open while the child is used.
8
+ *
9
+ * A mouse or a pen presses on `pointerdown`. A finger is decided when it comes up, and only if it was a tap:
10
+ * a finger that goes down may be about to scroll the page, and closing at that moment would close the popup
11
+ * under the user's hand (decision 14).
8
12
  */
9
13
  const stack = [];
10
14
  /** The open popups, the oldest first. The last one is the topmost. */
@@ -32,14 +36,58 @@ export function isInsideLayer(layer, target) {
32
36
  return true;
33
37
  return stack.some((other) => other !== layer && parentOf(other) === layer && isInsideLayer(other, target));
34
38
  }
39
+ /** How far a finger may move and still be a tap, in CSS pixels. */
40
+ const TAP_SLOP = 10;
41
+ let touch = null;
42
+ function outsideLayers(target) {
43
+ return [...stack].reverse().filter((layer) => layer.outsidePress() && !isInsideLayer(layer, target));
44
+ }
45
+ function endTouch() {
46
+ touch = null;
47
+ document.removeEventListener('pointermove', onTouchMove, true);
48
+ document.removeEventListener('pointerup', onTouchUp, true);
49
+ document.removeEventListener('pointercancel', endTouch, true);
50
+ }
51
+ /** A finger that moves is scrolling or dragging, not pressing. */
52
+ function onTouchMove(event) {
53
+ const { id, x, y } = touch;
54
+ if (event.pointerId === id && Math.hypot(event.clientX - x, event.clientY - y) > TAP_SLOP)
55
+ endTouch();
56
+ }
57
+ function onTouchUp(event) {
58
+ const { id, layers } = touch;
59
+ if (event.pointerId !== id)
60
+ return;
61
+ endTouch();
62
+ // A popup may have closed since the finger went down.
63
+ for (const layer of layers)
64
+ if (stack.includes(layer))
65
+ layer.dismiss('outsidePress');
66
+ }
35
67
  function onPointerDown(event) {
36
68
  // A press on the document always has a node as its target.
37
69
  const target = event.target;
38
- // Topmost first. A copy, because a dismissal may unregister a layer.
39
- for (const layer of [...stack].reverse()) {
40
- if (layer.outsidePress() && !isInsideLayer(layer, target))
41
- layer.dismiss('outsidePress');
70
+ if (event.pointerType !== 'touch') {
71
+ // Topmost first. A copy, because a dismissal may unregister a layer.
72
+ for (const layer of [...stack].reverse()) {
73
+ if (layer.outsidePress() && !isInsideLayer(layer, target))
74
+ layer.dismiss('outsidePress');
75
+ }
76
+ return;
42
77
  }
78
+ // A second finger: a pinch, or a scroll with two fingers.
79
+ if (touch) {
80
+ endTouch();
81
+ return;
82
+ }
83
+ const layers = outsideLayers(target);
84
+ if (layers.length === 0)
85
+ return;
86
+ touch = { id: event.pointerId, x: event.clientX, y: event.clientY, layers };
87
+ document.addEventListener('pointermove', onTouchMove, { capture: true, passive: true });
88
+ document.addEventListener('pointerup', onTouchUp, true);
89
+ // The browser takes the touch for scrolling the page: it cancels the pointer.
90
+ document.addEventListener('pointercancel', endTouch, true);
43
91
  }
44
92
  function onKeyDown(event) {
45
93
  if (event.key !== 'Escape' || event.defaultPrevented || event.isComposing)
@@ -56,6 +104,8 @@ function listen() {
56
104
  document.addEventListener('keydown', onKeyDown);
57
105
  }
58
106
  function unlisten() {
107
+ if (touch)
108
+ endTouch();
59
109
  document.removeEventListener('pointerdown', onPointerDown, true);
60
110
  document.removeEventListener('keydown', onKeyDown);
61
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-ant-design",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "An independent UI component library for Svelte 5, inspired by Ant Design.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -59,6 +59,7 @@
59
59
  "test:coverage": "vitest run --coverage",
60
60
  "test:watch": "vitest",
61
61
  "test:e2e": "playwright test",
62
+ "test:floor": "node scripts/floor/run.mjs",
62
63
  "test:docs": "npm run verify -w docs && npm run test:e2e -w docs",
63
64
  "test:mutation": "node scripts/mutation-test.mjs",
64
65
  "lint": "eslint ."