svelte-ant-design 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.
- package/README.md +1 -1
- package/dist/Select/Select.svelte +25 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/internal/popup/Popup.svelte +56 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ Ant Design.
|
|
|
18
18
|
|
|
19
19
|
## Status
|
|
20
20
|
|
|
21
|
-
Version `0.5.
|
|
21
|
+
Version `0.5.1`. 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
|
|
|
@@ -266,6 +266,26 @@
|
|
|
266
266
|
scrollToActive();
|
|
267
267
|
});
|
|
268
268
|
|
|
269
|
+
// The list keeps the wheel to itself (`overscroll-behavior: contain`) only while it scrolls. A list that does not
|
|
270
|
+
// scroll would otherwise hold the wheel for nothing, and the page would not move under the cursor. Measured after
|
|
271
|
+
// each change of what the list shows, and once more in the next frame, when the layout has settled. A list that is
|
|
272
|
+
// wrongly taken for one that does not scroll lets the wheel through at its end, which does no harm.
|
|
273
|
+
let listScrolls = $state(false);
|
|
274
|
+
$effect(() => {
|
|
275
|
+
void visibleNodes;
|
|
276
|
+
void popupOpen;
|
|
277
|
+
void listHeight;
|
|
278
|
+
const list = listElement;
|
|
279
|
+
if (!list) {
|
|
280
|
+
listScrolls = false;
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const measure = () => (listScrolls = list.scrollHeight > list.clientHeight);
|
|
284
|
+
measure();
|
|
285
|
+
const frame = requestAnimationFrame(measure);
|
|
286
|
+
return () => cancelAnimationFrame(frame);
|
|
287
|
+
});
|
|
288
|
+
|
|
269
289
|
// ---- Opening and closing ---------------------------------------------------------------------------------
|
|
270
290
|
|
|
271
291
|
/** Changes the open state on behalf of the user. The parent may refuse (a function binding). */
|
|
@@ -814,6 +834,7 @@
|
|
|
814
834
|
aria-labelledby={listLabelledBy}
|
|
815
835
|
class={classList('ads-select-list', classNames?.list)}
|
|
816
836
|
style={styleText(`max-height: ${listHeight}px`, styles?.list)}
|
|
837
|
+
data-scrollable={listScrolls ? '' : undefined}
|
|
817
838
|
bind:this={listElement}
|
|
818
839
|
onscroll={onpopupscroll}
|
|
819
840
|
>
|
|
@@ -1410,6 +1431,10 @@
|
|
|
1410
1431
|
.ads-select-list {
|
|
1411
1432
|
position: relative;
|
|
1412
1433
|
overflow: hidden auto;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
/* Only a list that scrolls keeps the wheel from the page (see `listScrolls`). */
|
|
1437
|
+
.ads-select-list[data-scrollable] {
|
|
1413
1438
|
overscroll-behavior: contain;
|
|
1414
1439
|
}
|
|
1415
1440
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const VERSION = "0.5.
|
|
1
|
+
export declare const VERSION = "0.5.1";
|
|
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.
|
|
2
|
+
export const VERSION = '0.5.1';
|
|
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';
|
|
@@ -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)
|
|
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
|
-
|
|
253
|
-
|
|
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
|
|
272
|
-
frameElement
|
|
273
|
-
frameElement
|
|
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:
|
|
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;
|