@zag-js/toast 2.0.0-next.2 → 2.0.0-next.4
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/dist/index.d.mts +10 -17
- package/dist/index.d.ts +10 -17
- package/dist/index.js +18 -45
- package/dist/index.mjs +5 -11
- package/dist/toast-group.connect.d.mts +4 -8
- package/dist/toast-group.connect.d.ts +4 -8
- package/dist/toast-group.connect.js +82 -103
- package/dist/toast-group.connect.mjs +77 -67
- package/dist/toast-group.machine.d.mts +2 -8
- package/dist/toast-group.machine.d.ts +2 -8
- package/dist/toast-group.machine.js +295 -290
- package/dist/toast-group.machine.mjs +290 -254
- package/dist/toast.anatomy.d.mts +2 -6
- package/dist/toast.anatomy.d.ts +2 -6
- package/dist/toast.anatomy.js +8 -40
- package/dist/toast.anatomy.mjs +4 -14
- package/dist/toast.connect.d.mts +4 -8
- package/dist/toast.connect.d.ts +4 -8
- package/dist/toast.connect.js +176 -160
- package/dist/toast.connect.mjs +171 -124
- package/dist/toast.dom.d.mts +21 -14
- package/dist/toast.dom.d.ts +21 -14
- package/dist/toast.dom.js +50 -48
- package/dist/toast.dom.mjs +35 -17
- package/dist/toast.machine.d.mts +2 -8
- package/dist/toast.machine.d.ts +2 -8
- package/dist/toast.machine.js +404 -288
- package/dist/toast.machine.mjs +400 -253
- package/dist/toast.store.d.mts +2 -8
- package/dist/toast.store.d.ts +2 -8
- package/dist/toast.store.js +345 -263
- package/dist/toast.store.mjs +341 -238
- package/dist/toast.types.d.mts +460 -427
- package/dist/toast.types.d.ts +460 -427
- package/dist/toast.types.js +4 -18
- package/dist/toast.types.mjs +1 -0
- package/dist/toast.utils.d.mts +10 -13
- package/dist/toast.utils.d.ts +10 -13
- package/dist/toast.utils.js +185 -198
- package/dist/toast.utils.mjs +175 -168
- package/dist/utils/swipe.d.mts +16 -0
- package/dist/utils/swipe.d.ts +16 -0
- package/dist/utils/swipe.js +100 -0
- package/dist/utils/swipe.mjs +93 -0
- package/package.json +8 -8
package/dist/toast.utils.mjs
CHANGED
|
@@ -1,180 +1,187 @@
|
|
|
1
|
+
import { toPx } from "@zag-js/utils";
|
|
1
2
|
// src/toast.utils.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
DEFAULT: 5e3
|
|
3
|
+
const defaultTimeouts = {
|
|
4
|
+
info: 5e3,
|
|
5
|
+
error: 5e3,
|
|
6
|
+
success: 2e3,
|
|
7
|
+
loading: 1e400,
|
|
8
|
+
warning: 5e3,
|
|
9
|
+
DEFAULT: 5e3
|
|
10
10
|
};
|
|
11
11
|
function getToastDuration(duration, type) {
|
|
12
|
-
|
|
12
|
+
return duration ?? defaultTimeouts[type] ?? defaultTimeouts.DEFAULT;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
const getOffsets = (offsets) => typeof offsets === "string" ? {
|
|
15
|
+
left: offsets,
|
|
16
|
+
right: offsets,
|
|
17
|
+
bottom: offsets,
|
|
18
|
+
top: offsets
|
|
19
|
+
} : offsets;
|
|
15
20
|
function getGroupPlacementStyle(service, placement) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (computedPlacement.includes("bottom")) {
|
|
46
|
-
const offset = computedOffset.bottom;
|
|
47
|
-
styles.bottom = `max(env(safe-area-inset-bottom, 0px), ${offset})`;
|
|
48
|
-
}
|
|
49
|
-
if (!computedPlacement.includes("left")) {
|
|
50
|
-
const offset = computedOffset.right;
|
|
51
|
-
styles.insetInlineEnd = `calc(env(safe-area-inset-right, 0px) + ${offset})`;
|
|
52
|
-
}
|
|
53
|
-
if (!computedPlacement.includes("right")) {
|
|
54
|
-
const offset = computedOffset.left;
|
|
55
|
-
styles.insetInlineStart = `calc(env(safe-area-inset-left, 0px) + ${offset})`;
|
|
56
|
-
}
|
|
57
|
-
return styles;
|
|
21
|
+
const { prop, computed, context } = service;
|
|
22
|
+
const { offsets, gap } = prop("store").attrs;
|
|
23
|
+
const heights = context.get("heights");
|
|
24
|
+
const computedOffset = getOffsets(offsets);
|
|
25
|
+
const rtl = prop("dir") === "rtl";
|
|
26
|
+
const computedPlacement = placement.replace("-start", rtl ? "-right" : "-left").replace("-end", rtl ? "-left" : "-right");
|
|
27
|
+
const isRighty = computedPlacement.includes("right");
|
|
28
|
+
const isLefty = computedPlacement.includes("left");
|
|
29
|
+
const styles = {
|
|
30
|
+
position: "fixed",
|
|
31
|
+
pointerEvents: computed("count") > 0 ? void 0 : "none",
|
|
32
|
+
display: "flex",
|
|
33
|
+
flexDirection: "column",
|
|
34
|
+
"--gap": toPx(gap),
|
|
35
|
+
"--first-height": toPx(heights[0]?.height || 0),
|
|
36
|
+
"--viewport-offset-left": computedOffset.left,
|
|
37
|
+
"--viewport-offset-right": computedOffset.right,
|
|
38
|
+
"--viewport-offset-top": computedOffset.top,
|
|
39
|
+
"--viewport-offset-bottom": computedOffset.bottom
|
|
40
|
+
};
|
|
41
|
+
let alignItems = "center";
|
|
42
|
+
if (isRighty) alignItems = "flex-end";
|
|
43
|
+
if (isLefty) alignItems = "flex-start";
|
|
44
|
+
styles.alignItems = alignItems;
|
|
45
|
+
if (computedPlacement.includes("top")) styles.top = `max(env(safe-area-inset-top, 0px), ${computedOffset.top})`;
|
|
46
|
+
if (computedPlacement.includes("bottom")) styles.bottom = `max(env(safe-area-inset-bottom, 0px), ${computedOffset.bottom})`;
|
|
47
|
+
if (!computedPlacement.includes("left")) styles.insetInlineEnd = `calc(env(safe-area-inset-right, 0px) + ${computedOffset.right})`;
|
|
48
|
+
if (!computedPlacement.includes("right")) styles.insetInlineStart = `calc(env(safe-area-inset-left, 0px) + ${computedOffset.left})`;
|
|
49
|
+
return styles;
|
|
58
50
|
}
|
|
59
51
|
function getPlacementStyle(service, visible) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
"--y": "calc(var(--lift) * var(--offset) + var(--lift) * -100%)"
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
if (frontmost && !visible) {
|
|
142
|
-
assign({
|
|
143
|
-
"--y": "calc(var(--lift) * -100%)"
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
return styles;
|
|
52
|
+
const { prop, context, computed } = service;
|
|
53
|
+
const parent = prop("parent");
|
|
54
|
+
const placement = parent.computed("placement");
|
|
55
|
+
const { gap } = parent.prop("store").attrs;
|
|
56
|
+
const [side] = placement.split("-");
|
|
57
|
+
const mounted = context.get("mounted");
|
|
58
|
+
const height = computed("height");
|
|
59
|
+
const frontmost = computed("frontmost");
|
|
60
|
+
const sibling = !frontmost;
|
|
61
|
+
const stacked = prop("parent").state.matches("stack");
|
|
62
|
+
const overlap = !stacked;
|
|
63
|
+
const type = prop("type");
|
|
64
|
+
const totalDuration = getToastDuration(prop("duration"), type);
|
|
65
|
+
const duration = type === "loading" || !Number.isFinite(totalDuration) ? Number.MAX_SAFE_INTEGER : totalDuration;
|
|
66
|
+
const offset = computed("heightIndex") * gap + computed("heightBefore");
|
|
67
|
+
const swipeOffset = context.get("swipeOffset");
|
|
68
|
+
const swipedOut = !visible ? context.get("swipeDirection") : null;
|
|
69
|
+
const exiting = !visible && !swipedOut;
|
|
70
|
+
const styles = {
|
|
71
|
+
position: "absolute",
|
|
72
|
+
pointerEvents: "auto",
|
|
73
|
+
"--opacity": "0",
|
|
74
|
+
"--remove-delay": `${prop("removeDelay")}ms`,
|
|
75
|
+
"--duration": `${duration}ms`,
|
|
76
|
+
"--initial-height": toPx(height),
|
|
77
|
+
"--offset": toPx(offset),
|
|
78
|
+
"--index": prop("index"),
|
|
79
|
+
"--z-index": computed("zIndex"),
|
|
80
|
+
"--lift-amount": "calc(var(--lift) * var(--gap))",
|
|
81
|
+
"--y": "100%",
|
|
82
|
+
"--x": "0px",
|
|
83
|
+
"--swipe-x": toPx(swipeOffset.x),
|
|
84
|
+
"--swipe-y": toPx(swipeOffset.y)
|
|
85
|
+
};
|
|
86
|
+
const assign = (overrides) => Object.assign(styles, overrides);
|
|
87
|
+
if (side === "top") assign({
|
|
88
|
+
top: "0",
|
|
89
|
+
"--sign": "-1",
|
|
90
|
+
"--y": "-100%",
|
|
91
|
+
"--lift": "1"
|
|
92
|
+
});
|
|
93
|
+
else if (side === "bottom") assign({
|
|
94
|
+
bottom: "0",
|
|
95
|
+
"--sign": "1",
|
|
96
|
+
"--y": "100%",
|
|
97
|
+
"--lift": "-1"
|
|
98
|
+
});
|
|
99
|
+
if (mounted) {
|
|
100
|
+
assign({
|
|
101
|
+
"--y": "0",
|
|
102
|
+
"--opacity": "1"
|
|
103
|
+
});
|
|
104
|
+
if (stacked) assign({
|
|
105
|
+
"--y": "calc(var(--lift) * var(--offset))",
|
|
106
|
+
"--height": "var(--initial-height)"
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (!visible) assign({
|
|
110
|
+
"--opacity": "0",
|
|
111
|
+
pointerEvents: "none"
|
|
112
|
+
});
|
|
113
|
+
if (sibling && overlap) {
|
|
114
|
+
assign({
|
|
115
|
+
"--base-scale": "var(--index) * 0.05 + 1",
|
|
116
|
+
"--y": "calc(var(--lift-amount) * var(--index))",
|
|
117
|
+
"--scale": "calc(-1 * var(--base-scale))",
|
|
118
|
+
"--height": "var(--first-height)"
|
|
119
|
+
});
|
|
120
|
+
if (exiting) assign({ "--y": "calc(var(--sign) * 40%)" });
|
|
121
|
+
}
|
|
122
|
+
if (sibling && stacked && exiting) assign({ "--y": "calc(var(--lift) * var(--offset) + var(--lift) * -100%)" });
|
|
123
|
+
if (frontmost && exiting) assign({ "--y": "calc(var(--lift) * -100%)" });
|
|
124
|
+
const [throwX, throwY] = swipedOut ? swipeThrow[swipedOut] : ["0px", "0px"];
|
|
125
|
+
assign({
|
|
126
|
+
"--x": `calc(${styles["--x"]} + var(--swipe-x) + ${throwX})`,
|
|
127
|
+
"--y": `calc(${styles["--y"]} + var(--swipe-y) + ${throwY})`
|
|
128
|
+
});
|
|
129
|
+
return styles;
|
|
147
130
|
}
|
|
131
|
+
const swipeThrow = {
|
|
132
|
+
up: ["0px", "-100%"],
|
|
133
|
+
down: ["0px", "100%"],
|
|
134
|
+
left: ["-100%", "0px"],
|
|
135
|
+
right: ["100%", "0px"]
|
|
136
|
+
};
|
|
148
137
|
function getGhostBeforeStyle(service, visible) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
height: "calc(var(--initial-height) + 80%)"
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
return styles;
|
|
138
|
+
const { computed } = service;
|
|
139
|
+
const styles = {
|
|
140
|
+
position: "absolute",
|
|
141
|
+
inset: "0",
|
|
142
|
+
scale: "1 2",
|
|
143
|
+
pointerEvents: visible ? "none" : "auto"
|
|
144
|
+
};
|
|
145
|
+
const assign = (overrides) => Object.assign(styles, overrides);
|
|
146
|
+
if (computed("frontmost") && !visible) assign({ height: "calc(var(--initial-height) + 80%)" });
|
|
147
|
+
return styles;
|
|
163
148
|
}
|
|
164
149
|
function getGhostAfterStyle() {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
150
|
+
return {
|
|
151
|
+
position: "absolute",
|
|
152
|
+
left: "0",
|
|
153
|
+
height: "calc(var(--gap) + 2px)",
|
|
154
|
+
bottom: "100%",
|
|
155
|
+
width: "100%"
|
|
156
|
+
};
|
|
172
157
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
158
|
+
function setHeight(parent, item) {
|
|
159
|
+
const { id, height } = item;
|
|
160
|
+
parent.context.set("heights", (prev) => {
|
|
161
|
+
const existing = prev.find((i) => i.id === id);
|
|
162
|
+
if (existing) {
|
|
163
|
+
if (existing.height === height) return prev;
|
|
164
|
+
return prev.map((i) => i.id === id ? {
|
|
165
|
+
...i,
|
|
166
|
+
height
|
|
167
|
+
} : i);
|
|
168
|
+
}
|
|
169
|
+
const toasts = parent.context.get("toasts");
|
|
170
|
+
const rank = (id) => toasts.findIndex((t) => t.id === id);
|
|
171
|
+
const index = rank(id);
|
|
172
|
+
let insertAt = prev.findIndex((i) => {
|
|
173
|
+
const r = rank(i.id);
|
|
174
|
+
return r === -1 || r > index;
|
|
175
|
+
});
|
|
176
|
+
if (insertAt === -1) insertAt = prev.length;
|
|
177
|
+
return [
|
|
178
|
+
...prev.slice(0, insertAt),
|
|
179
|
+
{
|
|
180
|
+
id,
|
|
181
|
+
height
|
|
182
|
+
},
|
|
183
|
+
...prev.slice(insertAt)
|
|
184
|
+
];
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
export { defaultTimeouts, getGhostAfterStyle, getGhostBeforeStyle, getGroupPlacementStyle, getPlacementStyle, getToastDuration, setHeight };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Placement, SwipeDirection } from "../toast.types.mjs";
|
|
2
|
+
import { Point } from "@zag-js/types";
|
|
3
|
+
/** Distance (px) a release must pass, in an allowed direction, to dismiss */
|
|
4
|
+
export declare const SWIPE_THRESHOLD = 40;
|
|
5
|
+
export declare function getDisplacement(direction: SwipeDirection, delta: Point): number;
|
|
6
|
+
/** Swipe toward the nearest edges: the placement's side, plus the aligned edge */
|
|
7
|
+
export declare function getDefaultSwipeDirections(placement: Placement, dir?: "ltr" | "rtl"): SwipeDirection[];
|
|
8
|
+
export interface SwipeMoveResult {
|
|
9
|
+
offset: Point;
|
|
10
|
+
direction: SwipeDirection | null;
|
|
11
|
+
}
|
|
12
|
+
export interface SwipeTracker {
|
|
13
|
+
move: (point: Point) => SwipeMoveResult;
|
|
14
|
+
end: () => SwipeDirection | null;
|
|
15
|
+
}
|
|
16
|
+
export declare function createSwipeTracker(directions: SwipeDirection[], start: Point): SwipeTracker;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Placement, SwipeDirection } from "../toast.types.js";
|
|
2
|
+
import { Point } from "@zag-js/types";
|
|
3
|
+
/** Distance (px) a release must pass, in an allowed direction, to dismiss */
|
|
4
|
+
export declare const SWIPE_THRESHOLD = 40;
|
|
5
|
+
export declare function getDisplacement(direction: SwipeDirection, delta: Point): number;
|
|
6
|
+
/** Swipe toward the nearest edges: the placement's side, plus the aligned edge */
|
|
7
|
+
export declare function getDefaultSwipeDirections(placement: Placement, dir?: "ltr" | "rtl"): SwipeDirection[];
|
|
8
|
+
export interface SwipeMoveResult {
|
|
9
|
+
offset: Point;
|
|
10
|
+
direction: SwipeDirection | null;
|
|
11
|
+
}
|
|
12
|
+
export interface SwipeTracker {
|
|
13
|
+
move: (point: Point) => SwipeMoveResult;
|
|
14
|
+
end: () => SwipeDirection | null;
|
|
15
|
+
}
|
|
16
|
+
export declare function createSwipeTracker(directions: SwipeDirection[], start: Point): SwipeTracker;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
// src/utils/swipe.ts
|
|
6
|
+
/** Distance (px) a release must pass, in an allowed direction, to dismiss */
|
|
7
|
+
const SWIPE_THRESHOLD = 40;
|
|
8
|
+
/** Pulling back this far (px) from the furthest point cancels the dismiss */
|
|
9
|
+
const REVERSE_CANCEL_THRESHOLD = 10;
|
|
10
|
+
/** Exponent that damps movement in directions that can't dismiss */
|
|
11
|
+
const DAMPING_EXPONENT = .5;
|
|
12
|
+
/** Movement (px) before a press counts as a drag */
|
|
13
|
+
const MIN_DRAG_DISTANCE = 1;
|
|
14
|
+
function getDisplacement(direction, delta) {
|
|
15
|
+
switch (direction) {
|
|
16
|
+
case "up": return -delta.y;
|
|
17
|
+
case "down": return delta.y;
|
|
18
|
+
case "left": return -delta.x;
|
|
19
|
+
case "right": return delta.x;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Swipe toward the nearest edges: the placement's side, plus the aligned edge */
|
|
23
|
+
function getDefaultSwipeDirections(placement, dir = "ltr") {
|
|
24
|
+
const [side, align] = placement.split("-");
|
|
25
|
+
const directions = [side === "top" ? "up" : "down"];
|
|
26
|
+
if (align) {
|
|
27
|
+
const towardEnd = align === "end";
|
|
28
|
+
directions.push(towardEnd !== (dir === "rtl") ? "right" : "left");
|
|
29
|
+
}
|
|
30
|
+
return directions;
|
|
31
|
+
}
|
|
32
|
+
const damp = (value) => Math.sign(value) * Math.abs(value) ** DAMPING_EXPONENT;
|
|
33
|
+
function createSwipeTracker(directions, start) {
|
|
34
|
+
const allows = (direction) => directions.includes(direction);
|
|
35
|
+
const horizontal = allows("left") || allows("right");
|
|
36
|
+
const vertical = allows("up") || allows("down");
|
|
37
|
+
const bidirectional = (direction) => direction === "left" || direction === "right" ? allows("left") && allows("right") : allows("up") && allows("down");
|
|
38
|
+
let origin = start;
|
|
39
|
+
let firstMove = true;
|
|
40
|
+
let dragging = false;
|
|
41
|
+
let lockedAxis = null;
|
|
42
|
+
let intended = null;
|
|
43
|
+
let delta = {
|
|
44
|
+
x: 0,
|
|
45
|
+
y: 0
|
|
46
|
+
};
|
|
47
|
+
const furthest = {};
|
|
48
|
+
function getCandidate() {
|
|
49
|
+
if ((lockedAxis ?? (Math.abs(delta.x) >= Math.abs(delta.y) ? "x" : "y")) === "x") return delta.x > 0 ? "right" : "left";
|
|
50
|
+
return delta.y > 0 ? "down" : "up";
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
move(point) {
|
|
54
|
+
if (firstMove) {
|
|
55
|
+
origin = point;
|
|
56
|
+
firstMove = false;
|
|
57
|
+
}
|
|
58
|
+
delta = {
|
|
59
|
+
x: point.x - origin.x,
|
|
60
|
+
y: point.y - origin.y
|
|
61
|
+
};
|
|
62
|
+
if (!dragging && Math.hypot(delta.x, delta.y) >= MIN_DRAG_DISTANCE) {
|
|
63
|
+
dragging = true;
|
|
64
|
+
if (horizontal && vertical) lockedAxis = Math.abs(delta.x) > Math.abs(delta.y) ? "x" : "y";
|
|
65
|
+
}
|
|
66
|
+
if (dragging && !intended) {
|
|
67
|
+
const candidate = getCandidate();
|
|
68
|
+
if (allows(candidate)) intended = candidate;
|
|
69
|
+
}
|
|
70
|
+
for (const direction of directions) furthest[direction] = Math.max(furthest[direction] ?? 0, getDisplacement(direction, delta));
|
|
71
|
+
const x = horizontal && lockedAxis !== "y" ? delta.x : 0;
|
|
72
|
+
const y = vertical && lockedAxis !== "x" ? delta.y : 0;
|
|
73
|
+
const dampX = x > 0 && !allows("right") || x < 0 && !allows("left");
|
|
74
|
+
const dampY = y > 0 && !allows("down") || y < 0 && !allows("up");
|
|
75
|
+
return {
|
|
76
|
+
offset: {
|
|
77
|
+
x: dampX ? damp(x) : x,
|
|
78
|
+
y: dampY ? damp(y) : y
|
|
79
|
+
},
|
|
80
|
+
direction: intended
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
end() {
|
|
84
|
+
if (!dragging) return null;
|
|
85
|
+
for (const direction of directions) {
|
|
86
|
+
if (lockedAxis === "x" && (direction === "up" || direction === "down")) continue;
|
|
87
|
+
if (lockedAxis === "y" && (direction === "left" || direction === "right")) continue;
|
|
88
|
+
const displacement = getDisplacement(direction, delta);
|
|
89
|
+
if (displacement <= 40) continue;
|
|
90
|
+
if ((furthest[direction] ?? 0) - displacement >= REVERSE_CANCEL_THRESHOLD && !bidirectional(direction)) continue;
|
|
91
|
+
return direction;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
exports.SWIPE_THRESHOLD = SWIPE_THRESHOLD;
|
|
98
|
+
exports.createSwipeTracker = createSwipeTracker;
|
|
99
|
+
exports.getDefaultSwipeDirections = getDefaultSwipeDirections;
|
|
100
|
+
exports.getDisplacement = getDisplacement;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// src/utils/swipe.ts
|
|
2
|
+
/** Distance (px) a release must pass, in an allowed direction, to dismiss */
|
|
3
|
+
const SWIPE_THRESHOLD = 40;
|
|
4
|
+
/** Pulling back this far (px) from the furthest point cancels the dismiss */
|
|
5
|
+
const REVERSE_CANCEL_THRESHOLD = 10;
|
|
6
|
+
/** Exponent that damps movement in directions that can't dismiss */
|
|
7
|
+
const DAMPING_EXPONENT = .5;
|
|
8
|
+
/** Movement (px) before a press counts as a drag */
|
|
9
|
+
const MIN_DRAG_DISTANCE = 1;
|
|
10
|
+
function getDisplacement(direction, delta) {
|
|
11
|
+
switch (direction) {
|
|
12
|
+
case "up": return -delta.y;
|
|
13
|
+
case "down": return delta.y;
|
|
14
|
+
case "left": return -delta.x;
|
|
15
|
+
case "right": return delta.x;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Swipe toward the nearest edges: the placement's side, plus the aligned edge */
|
|
19
|
+
function getDefaultSwipeDirections(placement, dir = "ltr") {
|
|
20
|
+
const [side, align] = placement.split("-");
|
|
21
|
+
const directions = [side === "top" ? "up" : "down"];
|
|
22
|
+
if (align) {
|
|
23
|
+
const towardEnd = align === "end";
|
|
24
|
+
directions.push(towardEnd !== (dir === "rtl") ? "right" : "left");
|
|
25
|
+
}
|
|
26
|
+
return directions;
|
|
27
|
+
}
|
|
28
|
+
const damp = (value) => Math.sign(value) * Math.abs(value) ** DAMPING_EXPONENT;
|
|
29
|
+
function createSwipeTracker(directions, start) {
|
|
30
|
+
const allows = (direction) => directions.includes(direction);
|
|
31
|
+
const horizontal = allows("left") || allows("right");
|
|
32
|
+
const vertical = allows("up") || allows("down");
|
|
33
|
+
const bidirectional = (direction) => direction === "left" || direction === "right" ? allows("left") && allows("right") : allows("up") && allows("down");
|
|
34
|
+
let origin = start;
|
|
35
|
+
let firstMove = true;
|
|
36
|
+
let dragging = false;
|
|
37
|
+
let lockedAxis = null;
|
|
38
|
+
let intended = null;
|
|
39
|
+
let delta = {
|
|
40
|
+
x: 0,
|
|
41
|
+
y: 0
|
|
42
|
+
};
|
|
43
|
+
const furthest = {};
|
|
44
|
+
function getCandidate() {
|
|
45
|
+
if ((lockedAxis ?? (Math.abs(delta.x) >= Math.abs(delta.y) ? "x" : "y")) === "x") return delta.x > 0 ? "right" : "left";
|
|
46
|
+
return delta.y > 0 ? "down" : "up";
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
move(point) {
|
|
50
|
+
if (firstMove) {
|
|
51
|
+
origin = point;
|
|
52
|
+
firstMove = false;
|
|
53
|
+
}
|
|
54
|
+
delta = {
|
|
55
|
+
x: point.x - origin.x,
|
|
56
|
+
y: point.y - origin.y
|
|
57
|
+
};
|
|
58
|
+
if (!dragging && Math.hypot(delta.x, delta.y) >= MIN_DRAG_DISTANCE) {
|
|
59
|
+
dragging = true;
|
|
60
|
+
if (horizontal && vertical) lockedAxis = Math.abs(delta.x) > Math.abs(delta.y) ? "x" : "y";
|
|
61
|
+
}
|
|
62
|
+
if (dragging && !intended) {
|
|
63
|
+
const candidate = getCandidate();
|
|
64
|
+
if (allows(candidate)) intended = candidate;
|
|
65
|
+
}
|
|
66
|
+
for (const direction of directions) furthest[direction] = Math.max(furthest[direction] ?? 0, getDisplacement(direction, delta));
|
|
67
|
+
const x = horizontal && lockedAxis !== "y" ? delta.x : 0;
|
|
68
|
+
const y = vertical && lockedAxis !== "x" ? delta.y : 0;
|
|
69
|
+
const dampX = x > 0 && !allows("right") || x < 0 && !allows("left");
|
|
70
|
+
const dampY = y > 0 && !allows("down") || y < 0 && !allows("up");
|
|
71
|
+
return {
|
|
72
|
+
offset: {
|
|
73
|
+
x: dampX ? damp(x) : x,
|
|
74
|
+
y: dampY ? damp(y) : y
|
|
75
|
+
},
|
|
76
|
+
direction: intended
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
end() {
|
|
80
|
+
if (!dragging) return null;
|
|
81
|
+
for (const direction of directions) {
|
|
82
|
+
if (lockedAxis === "x" && (direction === "up" || direction === "down")) continue;
|
|
83
|
+
if (lockedAxis === "y" && (direction === "left" || direction === "right")) continue;
|
|
84
|
+
const displacement = getDisplacement(direction, delta);
|
|
85
|
+
if (displacement <= 40) continue;
|
|
86
|
+
if ((furthest[direction] ?? 0) - displacement >= REVERSE_CANCEL_THRESHOLD && !bidirectional(direction)) continue;
|
|
87
|
+
return direction;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export { SWIPE_THRESHOLD, createSwipeTracker, getDefaultSwipeDirections, getDisplacement };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/toast",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.4",
|
|
4
4
|
"description": "Core logic for the toast widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@zag-js/anatomy": "2.0.0-next.
|
|
33
|
-
"@zag-js/core": "2.0.0-next.
|
|
34
|
-
"@zag-js/dom-query": "2.0.0-next.
|
|
35
|
-
"@zag-js/dismissable": "2.0.0-next.
|
|
36
|
-
"@zag-js/utils": "2.0.0-next.
|
|
37
|
-
"@zag-js/types": "2.0.0-next.
|
|
32
|
+
"@zag-js/anatomy": "2.0.0-next.4",
|
|
33
|
+
"@zag-js/core": "2.0.0-next.4",
|
|
34
|
+
"@zag-js/dom-query": "2.0.0-next.4",
|
|
35
|
+
"@zag-js/dismissable": "2.0.0-next.4",
|
|
36
|
+
"@zag-js/utils": "2.0.0-next.4",
|
|
37
|
+
"@zag-js/types": "2.0.0-next.4"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"clean-package": "2.2.0"
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"./package.json": "./package.json"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
|
-
"build": "
|
|
70
|
+
"build": "tsdown",
|
|
71
71
|
"lint": "oxlint src",
|
|
72
72
|
"typecheck": "tsc --noEmit"
|
|
73
73
|
}
|