@exhaustive-ui/motion 0.0.0-stage → 3.0.0-alpha.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/LICENSE +23 -0
- package/README.md +63 -2
- package/dist/index.d.mts +271 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +52 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 e-xiste
|
|
4
|
+
Copyright (c) 2026 OpenNG (as per the Optimus UI license)
|
|
5
|
+
Copyright (c) 2016-2026 PrimeTek (as per the PrimeNG community version license)
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
|
15
|
+
all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://exhaustive-ui.e-xiste.com/large-icon.svg" alt="ExhaustiveUI" width="140" />
|
|
3
|
+
</div>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
# @exhaustive-ui/motion
|
|
6
|
+
|
|
7
|
+
Motion and animation utilities for [ExhaustiveUI](https://github.com/e-xiste/exhaustive-ui).
|
|
8
|
+
|
|
9
|
+
This package provides a programmatic way to manage CSS transitions and animations within the ExhaustiveUI ecosystem. It offers a robust API for controlling motion lifecycles, handling dynamic dimensions, and ensuring accessibility.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Programmatic Motion**: Create and control motion instances for any DOM element.
|
|
14
|
+
- **Lifecycle Hooks**: Comprehensive hooks for `before`, `start`, `after`, and `cancelled` stages of both enter and leave phases.
|
|
15
|
+
- **Auto-Dimensions**: Built-in support for smoothly animating to/from `auto` height and width.
|
|
16
|
+
- **Accessibility**: Respects user's `prefers-reduced-motion` settings out of the box.
|
|
17
|
+
- **Configurable**: Flexible options for durations, easing types, and custom CSS class names.
|
|
18
|
+
- **Lightweight**: Zero dependencies (other than internal utilities) and optimized for performance.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @exhaustive-ui/motion
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Core Concepts
|
|
27
|
+
|
|
28
|
+
### createMotion
|
|
29
|
+
The primary entry point to create a motion instance for a target element.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { createMotion } from '@exhaustive-ui/motion';
|
|
33
|
+
|
|
34
|
+
const motion = createMotion(element, {
|
|
35
|
+
name: 'my-fade',
|
|
36
|
+
enter: true,
|
|
37
|
+
leave: true
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Trigger phases
|
|
41
|
+
await motion.enter();
|
|
42
|
+
await motion.leave();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Motion Options
|
|
46
|
+
Fine-tune the behavior with detailed configuration:
|
|
47
|
+
- `name`: Base prefix for CSS classes (e.g., `p-` -> `p-enter-active`).
|
|
48
|
+
- `duration`: Control timing for enter and leave phases independently.
|
|
49
|
+
- `autoHeight` / `autoWidth`: Enable automatic dimension calculation for smooth transitions to variable content sizes.
|
|
50
|
+
- `hooks`: Attach logic to specific points in the animation timeline.
|
|
51
|
+
|
|
52
|
+
### Lifecycle Hooks
|
|
53
|
+
Stay in sync with your animations:
|
|
54
|
+
- `onBeforeEnter` / `onBeforeLeave`
|
|
55
|
+
- `onEnter` / `onLeave`
|
|
56
|
+
- `onAfterEnter` / `onAfterLeave`
|
|
57
|
+
|
|
58
|
+
## Usage in ExhaustiveUI
|
|
59
|
+
|
|
60
|
+
This package is a core dependency of `@exhaustive-ui/core`, powering the animations found in overlays, transitions, and interactive components. It can also be used independently to add standard-compliant motion to custom Angular components.
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
//#region types/index.d.ts
|
|
2
|
+
type MotionType = 'transition' | 'animation';
|
|
3
|
+
type MotionPhase = 'enter' | 'leave';
|
|
4
|
+
type MotionStage = 'Before' | 'Start' | 'After' | 'Cancelled';
|
|
5
|
+
/**
|
|
6
|
+
* Defines the duration of motion effects.
|
|
7
|
+
* It can be a single number representing the duration in milliseconds,
|
|
8
|
+
* or an object specifying different durations for 'enter' and 'leave' phases.
|
|
9
|
+
*/
|
|
10
|
+
type MotionDuration = number | { [P in MotionPhase]?: number; } | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Options for specifying class names during different phases of motion.
|
|
13
|
+
* These class names are applied at the start, during, and at the end of the motion.
|
|
14
|
+
*/
|
|
15
|
+
type ClassNameOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* The class name to apply at the start of the motion.
|
|
18
|
+
*/
|
|
19
|
+
from?: string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* The class name to apply while the motion is active.
|
|
22
|
+
*/
|
|
23
|
+
active?: string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The class name to apply at the end of the motion.
|
|
26
|
+
*/
|
|
27
|
+
to?: string | undefined;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Defines class names for both 'enter' and 'leave' motion phases.
|
|
31
|
+
*/
|
|
32
|
+
interface MotionClassNames {
|
|
33
|
+
/**
|
|
34
|
+
* Class names for the 'enter' motion phase.
|
|
35
|
+
* @see ClassNameOptions
|
|
36
|
+
*/
|
|
37
|
+
enterClass?: ClassNameOptions | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Class names for the 'leave' motion phase.
|
|
40
|
+
* @see ClassNameOptions
|
|
41
|
+
*/
|
|
42
|
+
leaveClass?: ClassNameOptions | undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Metadata about the motion effect, including its type, timeout, and count.
|
|
46
|
+
*/
|
|
47
|
+
type MotionMetadata = {
|
|
48
|
+
/**
|
|
49
|
+
* The type of motion effect, either 'transition' or 'animation'.
|
|
50
|
+
* @see MotionType
|
|
51
|
+
*/
|
|
52
|
+
type: MotionType | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* The timeout duration for the motion effect in milliseconds.
|
|
55
|
+
*/
|
|
56
|
+
timeout: number | 0;
|
|
57
|
+
/**
|
|
58
|
+
* The count of transition or animation properties involved in the motion.
|
|
59
|
+
*/
|
|
60
|
+
count: number | 0;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Event object passed to motion hooks, containing the target element.
|
|
64
|
+
*/
|
|
65
|
+
interface MotionEvent {
|
|
66
|
+
/**
|
|
67
|
+
* The target element of the motion event.
|
|
68
|
+
*/
|
|
69
|
+
element: Element;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Hooks for various stages of the motion lifecycle.
|
|
73
|
+
*/
|
|
74
|
+
interface MotionHooks {
|
|
75
|
+
/**
|
|
76
|
+
* Called before the enter motion starts.
|
|
77
|
+
* @param event - The motion event object.
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
onBeforeEnter?: (event: MotionEvent) => void;
|
|
81
|
+
/**
|
|
82
|
+
* Called when the enter motion starts.
|
|
83
|
+
* @param event - The motion event object.
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
onEnter?: (event: MotionEvent) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Called after the enter motion ends.
|
|
89
|
+
* @param event - The motion event object.
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
onAfterEnter?: (event: MotionEvent) => void;
|
|
93
|
+
/**
|
|
94
|
+
* Called if the enter motion is cancelled.
|
|
95
|
+
* @param event - The motion event object.
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
onEnterCancelled?: (event: MotionEvent) => void;
|
|
99
|
+
/**
|
|
100
|
+
* Called before the leave motion starts.
|
|
101
|
+
* @param event - The motion event object.
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
onBeforeLeave?: (event: MotionEvent) => void;
|
|
105
|
+
/**
|
|
106
|
+
* Called when the leave motion starts.
|
|
107
|
+
* @param event - The motion event object.
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
onLeave?: (event: MotionEvent) => void;
|
|
111
|
+
/**
|
|
112
|
+
* Called after the leave motion ends.
|
|
113
|
+
* @param event - The motion event object.
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
116
|
+
onAfterLeave?: (event: MotionEvent) => void;
|
|
117
|
+
/**
|
|
118
|
+
* Called if the leave motion is cancelled.
|
|
119
|
+
* @param event - The motion event object.
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
onLeaveCancelled?: (event: MotionEvent) => void;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Hooks organized by motion phase and stage.
|
|
126
|
+
*/
|
|
127
|
+
type MotionHooksWithPhase = { [P in MotionPhase]?: { [S in MotionStage as `on${S}`]?: (MotionHooks & {
|
|
128
|
+
[key: string]: unknown;
|
|
129
|
+
})[`on${S extends 'Start' | 'Cancelled' ? '' : S}${Capitalize<P>}${S extends 'Cancelled' ? S : ''}`]; }; };
|
|
130
|
+
/**
|
|
131
|
+
* Class names organized by motion phase.
|
|
132
|
+
*/
|
|
133
|
+
type MotionClassNamesWithPhase = { [P in MotionPhase]: Required<ClassNameOptions>; };
|
|
134
|
+
/**
|
|
135
|
+
* Options for configuring motion effects.
|
|
136
|
+
*/
|
|
137
|
+
interface MotionOptions extends MotionClassNames, MotionHooks {
|
|
138
|
+
/**
|
|
139
|
+
* The base name used for generating default class names.
|
|
140
|
+
*/
|
|
141
|
+
name?: string | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* The type of motion effect to use.
|
|
144
|
+
* @see MotionType
|
|
145
|
+
*/
|
|
146
|
+
type?: MotionType | undefined;
|
|
147
|
+
/**
|
|
148
|
+
* Indicates whether to respect the user's reduced motion preference.
|
|
149
|
+
*/
|
|
150
|
+
safe?: boolean | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* Indicates whether motion effects are disabled.
|
|
153
|
+
*/
|
|
154
|
+
disabled?: boolean | undefined;
|
|
155
|
+
/**
|
|
156
|
+
* Indicates whether the motion should run on the initial render (appear phase).
|
|
157
|
+
*/
|
|
158
|
+
appear?: boolean | undefined;
|
|
159
|
+
/**
|
|
160
|
+
* Indicates whether to perform enter motions.
|
|
161
|
+
*/
|
|
162
|
+
enter?: boolean | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* Indicates whether to perform leave motions.
|
|
165
|
+
*/
|
|
166
|
+
leave?: boolean | undefined;
|
|
167
|
+
/**
|
|
168
|
+
* The duration of the motion effect.
|
|
169
|
+
* @see MotionDuration
|
|
170
|
+
*/
|
|
171
|
+
duration?: MotionDuration | undefined;
|
|
172
|
+
/**
|
|
173
|
+
* Indicates whether to automatically adjust height during the motion.
|
|
174
|
+
*/
|
|
175
|
+
autoHeight?: boolean | undefined;
|
|
176
|
+
/**
|
|
177
|
+
* Indicates whether to automatically adjust width during the motion.
|
|
178
|
+
*/
|
|
179
|
+
autoWidth?: boolean | undefined;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Represents an instance of a motion effect applied to an element.
|
|
183
|
+
*/
|
|
184
|
+
type MotionInstance = {
|
|
185
|
+
/**
|
|
186
|
+
* Starts the enter motion.
|
|
187
|
+
* @returns - A promise that resolves to a cancellation function or void.
|
|
188
|
+
*/
|
|
189
|
+
enter: () => Promise<(() => void) | void>;
|
|
190
|
+
/**
|
|
191
|
+
* Starts the leave motion.
|
|
192
|
+
* @returns - A promise that resolves to a cancellation function or void.
|
|
193
|
+
*/
|
|
194
|
+
leave: () => Promise<(() => void) | void>;
|
|
195
|
+
/**
|
|
196
|
+
* Cancels the motion.
|
|
197
|
+
* @returns
|
|
198
|
+
*/
|
|
199
|
+
cancel: () => void;
|
|
200
|
+
/**
|
|
201
|
+
* Updates the motion instance with a new element and options.
|
|
202
|
+
* @param element - The target element.
|
|
203
|
+
* @param options - The motion options.
|
|
204
|
+
* @returns
|
|
205
|
+
*/
|
|
206
|
+
update: (element: Element, options?: MotionOptions) => void;
|
|
207
|
+
};
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/config/index.d.ts
|
|
210
|
+
declare const DEFAULT_MOTION_OPTIONS: MotionOptions;
|
|
211
|
+
/**
|
|
212
|
+
* Creates a MotionInstance for the given element with the specified options.
|
|
213
|
+
* @param element - The target element for motion effects.
|
|
214
|
+
* @param options - Configuration options for the motion instance.
|
|
215
|
+
* @returns A MotionInstance that can be used to control the motion.
|
|
216
|
+
*/
|
|
217
|
+
declare function createMotion(element: Element, options?: MotionOptions): MotionInstance;
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/utils/index.d.ts
|
|
220
|
+
declare const ANIMATION = "animation";
|
|
221
|
+
declare const TRANSITION = "transition";
|
|
222
|
+
/**
|
|
223
|
+
* Determines whether motion effects should be skipped based on the provided options.
|
|
224
|
+
* @param options - The motion options to evaluate.
|
|
225
|
+
* @returns A boolean indicating whether motion should be skipped.
|
|
226
|
+
*/
|
|
227
|
+
declare function shouldSkipMotion(options: MotionOptions | undefined): boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Merges the provided motion options with the default options.
|
|
230
|
+
* @param inOptions - The motion options to merge.
|
|
231
|
+
* @param defaultOptions - The default motion options.
|
|
232
|
+
* @returns The merged motion options.
|
|
233
|
+
*/
|
|
234
|
+
declare function mergeOptions(inOptions: MotionOptions | undefined, defaultOptions: MotionOptions): MotionOptions;
|
|
235
|
+
/**
|
|
236
|
+
* Resolves class names for motion phases based on the provided options.
|
|
237
|
+
* @param options - The motion options containing class names and base name.
|
|
238
|
+
* @returns The resolved class names organized by motion phase.
|
|
239
|
+
*/
|
|
240
|
+
declare function resolveClassNames(options: MotionOptions | undefined): MotionClassNamesWithPhase;
|
|
241
|
+
/**
|
|
242
|
+
* Retrieves the motion hooks organized by phase based on the provided options.
|
|
243
|
+
* @param options - The motion options containing hooks.
|
|
244
|
+
* @returns The motion hooks organized by phase.
|
|
245
|
+
*/
|
|
246
|
+
declare function getMotionHooks(options: MotionOptions | undefined): MotionHooksWithPhase;
|
|
247
|
+
/**
|
|
248
|
+
* Retrieves motion metadata including type, timeout, and count for the given element.
|
|
249
|
+
* @param element - The target element to retrieve motion metadata from.
|
|
250
|
+
* @param expectedType - The expected type of motion ('transition' or 'animation').
|
|
251
|
+
* @returns The motion metadata including type, timeout, and count.
|
|
252
|
+
*/
|
|
253
|
+
declare function getMotionMetadata(element: Element, expectedType?: MotionMetadata['type']): MotionMetadata;
|
|
254
|
+
/**
|
|
255
|
+
* Resolves the duration for a given animation phase.
|
|
256
|
+
* @param duration - The duration can be a number or an object with `enter` and `leave` properties.
|
|
257
|
+
* @param phase - The phase of the transition/animation, either 'enter' or 'leave'.
|
|
258
|
+
* @returns The resolved duration in milliseconds or null if not specified.
|
|
259
|
+
*/
|
|
260
|
+
declare function resolveDuration(duration: MotionOptions['duration'], phase: MotionPhase): number | null;
|
|
261
|
+
/**
|
|
262
|
+
* Sets CSS custom properties for auto height and/or width on the given element.
|
|
263
|
+
* @param element - The target HTML element.
|
|
264
|
+
* @param autoHeight - Whether to set the auto height CSS variable.
|
|
265
|
+
* @param autoWidth - Whether to set the auto width CSS variable.
|
|
266
|
+
* @returns
|
|
267
|
+
*/
|
|
268
|
+
declare function setAutoDimensionVariables(element: HTMLElement, autoHeight?: boolean, autoWidth?: boolean): void;
|
|
269
|
+
//#endregion
|
|
270
|
+
export { ANIMATION, ClassNameOptions, DEFAULT_MOTION_OPTIONS, MotionClassNames, MotionClassNamesWithPhase, MotionDuration, MotionEvent, MotionHooks, MotionHooksWithPhase, MotionInstance, MotionMetadata, MotionOptions, MotionPhase, MotionStage, MotionType, TRANSITION, createMotion, getMotionHooks, getMotionMetadata, mergeOptions, resolveClassNames, resolveDuration, setAutoDimensionVariables, shouldSkipMotion };
|
|
271
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{addClass as e,getHiddenElementDimensions as t,isPrefersReducedMotion as n,removeClass as r,setCSSProperty as i,toMs as a}from"@exhaustive-ui/utils";const o=`animation`,s=`transition`;function c(e){return e?e.disabled||!!(e.safe&&n()):!1}function l(e,t){return e?{...e,...Object.entries(t).reduce((t,[n,r])=>(t[n]=e[n]??r,t),{})}:t}function u(e){let{name:t,enterClass:n,leaveClass:r}=e||{};return{enter:{from:n?.from||`${t}-enter-from`,to:n?.to||`${t}-enter-to`,active:n?.active||`${t}-enter-active`},leave:{from:r?.from||`${t}-leave-from`,to:r?.to||`${t}-leave-to`,active:r?.active||`${t}-leave-active`}}}function d(e){return{enter:{onBefore:e?.onBeforeEnter,onStart:e?.onEnter,onAfter:e?.onAfterEnter,onCancelled:e?.onEnterCancelled},leave:{onBefore:e?.onBeforeLeave,onStart:e?.onLeave,onAfter:e?.onAfterLeave,onCancelled:e?.onLeaveCancelled}}}function f(e,t){let n=window.getComputedStyle(e),r=e=>{let t=n[`${e}Delay`],r=n[`${e}Duration`];return[t.split(`, `).map(a),r.split(`, `).map(a)]},[i,c]=r(s),[l,u]=r(o),d=Math.max(...c.map((e,t)=>e+i[t])),f=Math.max(...u.map((e,t)=>e+l[t])),p,m=0,h=0;return t===`transition`?d>0&&(p=s,m=d,h=c.length):t===`animation`?f>0&&(p=o,m=f,h=u.length):(m=Math.max(d,f),p=m>0?d>f?s:o:void 0,h=p?p===`transition`?c.length:u.length:0),{type:p,timeout:m,count:h}}function p(e,t){return typeof e==`number`?e:typeof e==`object`&&e[t]!=null?e[t]:null}function m(e,n=!0,r=!1){if(!n&&!r)return;let a=t(e);n&&i(e,`--pui-motion-height`,a.height+`px`),r&&i(e,`--pui-motion-width`,a.width+`px`)}const h={name:`p`,safe:!0,disabled:!1,enter:!0,leave:!0,autoHeight:!0,autoWidth:!1};function g(t,n){if(!t)throw Error(`Element is required.`);let i={},a=!1,o={},s=null,f={},g=e=>{if(Object.assign(i,l(e,h)),!i.enter&&!i.leave)throw Error(`Enter or leave must be true.`);f=d(i),a=c(i),o=u(i),s=null},_=async n=>{s?.();let{onBefore:c,onStart:l,onAfter:u,onCancelled:d}=f[n]||{},h={element:t};if(a){c?.(h),l?.(h),u?.(h);return}let{from:g,active:_,to:y}=o[n]||{};return m(t,i.autoHeight,i.autoWidth),c?.(h),e(t,g),e(t,_),t.offsetHeight,r(t,g),e(t,y),l?.(h),new Promise(e=>{let a=p(i.duration,n),o=()=>{r(t,[y,_]),s=null};s=()=>{o(),d?.(h),e()},v(t,i.type,a,()=>{o(),u?.(h),e()})})};g(n);let y={enter:()=>i.enter?_(`enter`):Promise.resolve(),leave:()=>i.leave?_(`leave`):Promise.resolve(),cancel:()=>{s?.(),s=null},update:(e,n)=>{if(!e)throw Error(`Element is required.`);t=e,y.cancel(),g(n)}};return i.appear&&y.enter(),y}let _=0;function v(e,t,n,r){let i=e._motionEndId=++_,a=()=>{i===e._motionEndId&&r()};if(n!=null)return setTimeout(a,n);let{type:o,timeout:s,count:c}=f(e,t);if(!o){r();return}let l=o+`end`,u=0,d=()=>{e.removeEventListener(l,p,!0),a()},p=t=>{t.target===e&&++u>=c&&d()};e.addEventListener(l,p,{capture:!0,once:!0}),setTimeout(()=>{u<c&&d()},s+1)}export{o as ANIMATION,h as DEFAULT_MOTION_OPTIONS,s as TRANSITION,g as createMotion,d as getMotionHooks,f as getMotionMetadata,l as mergeOptions,u as resolveClassNames,p as resolveDuration,m as setAutoDimensionVariables,c as shouldSkipMotion};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/utils/index.ts","../src/config/index.ts"],"sourcesContent":["import { getHiddenElementDimensions, isPrefersReducedMotion, setCSSProperty, toMs } from '@exhaustive-ui/utils';\nimport type { MotionClassNamesWithPhase, MotionHooksWithPhase, MotionMetadata, MotionOptions, MotionPhase, MotionType } from '../../types';\n\nexport const ANIMATION = 'animation';\nexport const TRANSITION = 'transition';\n\n/**\n * Determines whether motion effects should be skipped based on the provided options.\n * @param options - The motion options to evaluate.\n * @returns A boolean indicating whether motion should be skipped.\n */\nexport function shouldSkipMotion(options: MotionOptions | undefined): boolean {\n if (!options) {\n return false;\n }\n\n return options.disabled || !!(options.safe && isPrefersReducedMotion());\n}\n\n/**\n * Merges the provided motion options with the default options.\n * @param inOptions - The motion options to merge.\n * @param defaultOptions - The default motion options.\n * @returns The merged motion options.\n */\nexport function mergeOptions(inOptions: MotionOptions | undefined, defaultOptions: MotionOptions): MotionOptions {\n if (!inOptions) {\n return defaultOptions;\n }\n\n return {\n ...inOptions,\n ...(Object.entries(defaultOptions).reduce((acc: Record<string, unknown>, [key, value]) => {\n acc[key] = (inOptions as Record<string, unknown>)[key] ?? value;\n\n return acc;\n }, {}) as MotionOptions)\n };\n}\n\n/**\n * Resolves class names for motion phases based on the provided options.\n * @param options - The motion options containing class names and base name.\n * @returns The resolved class names organized by motion phase.\n */\nexport function resolveClassNames(options: MotionOptions | undefined): MotionClassNamesWithPhase {\n const { name, enterClass, leaveClass } = options || {};\n\n return {\n enter: {\n from: enterClass?.from || `${name}-enter-from`,\n to: enterClass?.to || `${name}-enter-to`,\n active: enterClass?.active || `${name}-enter-active`\n },\n leave: {\n from: leaveClass?.from || `${name}-leave-from`,\n to: leaveClass?.to || `${name}-leave-to`,\n active: leaveClass?.active || `${name}-leave-active`\n }\n };\n}\n\n/**\n * Retrieves the motion hooks organized by phase based on the provided options.\n * @param options - The motion options containing hooks.\n * @returns The motion hooks organized by phase.\n */\nexport function getMotionHooks(options: MotionOptions | undefined): MotionHooksWithPhase {\n return {\n enter: {\n onBefore: options?.onBeforeEnter,\n onStart: options?.onEnter,\n onAfter: options?.onAfterEnter,\n onCancelled: options?.onEnterCancelled\n },\n leave: {\n onBefore: options?.onBeforeLeave,\n onStart: options?.onLeave,\n onAfter: options?.onAfterLeave,\n onCancelled: options?.onLeaveCancelled\n }\n };\n}\n\n/**\n * Retrieves motion metadata including type, timeout, and count for the given element.\n * @param element - The target element to retrieve motion metadata from.\n * @param expectedType - The expected type of motion ('transition' or 'animation').\n * @returns The motion metadata including type, timeout, and count.\n */\nexport function getMotionMetadata(element: Element, expectedType?: MotionMetadata['type']): MotionMetadata {\n const styles = window.getComputedStyle(element);\n\n const getDelaysAndDurations = (type: MotionType): [number[], number[]] => {\n const delays = styles[`${type}Delay`];\n const durations = styles[`${type}Duration`];\n\n return [delays.split(', ').map(toMs), durations.split(', ').map(toMs)];\n };\n\n const [transitionDelays, transitionDurations] = getDelaysAndDurations(TRANSITION);\n const [animationDelays, animationDurations] = getDelaysAndDurations(ANIMATION);\n\n const transitionTimeout = Math.max(...transitionDurations.map((d, i) => d + transitionDelays[i]));\n const animationTimeout = Math.max(...animationDurations.map((d, i) => d + animationDelays[i]));\n\n let type: MotionMetadata['type'] = undefined;\n let timeout = 0;\n let count = 0;\n\n if (expectedType === TRANSITION) {\n if (transitionTimeout > 0) {\n type = TRANSITION;\n timeout = transitionTimeout;\n count = transitionDurations.length;\n }\n } else if (expectedType === ANIMATION) {\n if (animationTimeout > 0) {\n type = ANIMATION;\n timeout = animationTimeout;\n count = animationDurations.length;\n }\n } else {\n timeout = Math.max(transitionTimeout, animationTimeout);\n type = timeout > 0 ? (transitionTimeout > animationTimeout ? TRANSITION : ANIMATION) : undefined;\n count = type ? (type === TRANSITION ? transitionDurations.length : animationDurations.length) : 0;\n }\n\n return {\n type,\n timeout,\n count\n };\n}\n\n/**\n * Resolves the duration for a given animation phase.\n * @param duration - The duration can be a number or an object with `enter` and `leave` properties.\n * @param phase - The phase of the transition/animation, either 'enter' or 'leave'.\n * @returns The resolved duration in milliseconds or null if not specified.\n */\nexport function resolveDuration(duration: MotionOptions['duration'], phase: MotionPhase): number | null {\n if (typeof duration === 'number') {\n return duration;\n } else if (typeof duration === 'object' && duration[phase] != null) {\n return duration[phase];\n }\n\n return null;\n}\n\n/**\n * Sets CSS custom properties for auto height and/or width on the given element.\n * @param element - The target HTML element.\n * @param autoHeight - Whether to set the auto height CSS variable.\n * @param autoWidth - Whether to set the auto width CSS variable.\n * @returns\n */\nexport function setAutoDimensionVariables(element: HTMLElement, autoHeight: boolean = true, autoWidth: boolean = false): void {\n if (!autoHeight && !autoWidth) return;\n\n const dimensions = getHiddenElementDimensions(element);\n\n if (autoHeight) {\n setCSSProperty(element, '--pui-motion-height', dimensions.height + 'px');\n }\n\n if (autoWidth) {\n setCSSProperty(element, '--pui-motion-width', dimensions.width + 'px');\n }\n}\n","import { addClass, removeClass } from '@exhaustive-ui/utils';\nimport type { MotionClassNamesWithPhase, MotionHooksWithPhase, MotionInstance, MotionOptions, MotionPhase, MotionType } from '../../types';\nimport { getMotionHooks, getMotionMetadata, mergeOptions, resolveClassNames, resolveDuration, setAutoDimensionVariables, shouldSkipMotion } from '../utils';\n\nexport const DEFAULT_MOTION_OPTIONS: MotionOptions = {\n name: 'p',\n safe: true,\n disabled: false,\n enter: true,\n leave: true,\n autoHeight: true,\n autoWidth: false\n};\n\n/**\n * Creates a MotionInstance for the given element with the specified options.\n * @param element - The target element for motion effects.\n * @param options - Configuration options for the motion instance.\n * @returns A MotionInstance that can be used to control the motion.\n */\nexport function createMotion(element: Element, options?: MotionOptions): MotionInstance {\n if (!element) throw new Error('Element is required.');\n\n const opts: MotionOptions = {};\n let skipMotion = false;\n let classNames: MotionClassNamesWithPhase = {} as MotionClassNamesWithPhase;\n let cancelCurrent: (() => void) | null = null;\n let hooks: MotionHooksWithPhase = {};\n\n const init = (newOpts?: MotionOptions) => {\n Object.assign(opts, mergeOptions(newOpts, DEFAULT_MOTION_OPTIONS));\n if (!opts.enter && !opts.leave) throw new Error('Enter or leave must be true.');\n\n hooks = getMotionHooks(opts);\n skipMotion = shouldSkipMotion(opts);\n classNames = resolveClassNames(opts);\n cancelCurrent = null;\n };\n\n const run = async (phase: MotionPhase): Promise<void> => {\n cancelCurrent?.();\n\n const { onBefore, onStart, onAfter, onCancelled } = hooks[phase] || {};\n const event = { element };\n\n if (skipMotion) {\n onBefore?.(event);\n onStart?.(event);\n onAfter?.(event);\n\n return;\n }\n\n const { from: fromClass, active: activeClass, to: toClass } = classNames[phase] || {};\n\n setAutoDimensionVariables(element as HTMLElement, opts.autoHeight, opts.autoWidth);\n\n onBefore?.(event);\n addClass(element, fromClass);\n addClass(element, activeClass);\n\n //await nextFrame();\n void (element as HTMLElement).offsetHeight; // force reflow\n\n removeClass(element, fromClass);\n addClass(element, toClass);\n onStart?.(event);\n\n return new Promise((resolve) => {\n const duration = resolveDuration(opts.duration, phase);\n\n const cleanup = () => {\n removeClass(element, [toClass, activeClass]);\n cancelCurrent = null;\n };\n\n const onDone = () => {\n cleanup();\n onAfter?.(event);\n resolve();\n };\n\n cancelCurrent = () => {\n cleanup();\n onCancelled?.(event);\n resolve();\n };\n\n whenEnd(element, opts.type, duration, onDone);\n });\n };\n\n init(options);\n\n const instance: MotionInstance = {\n enter: () => {\n if (!opts.enter) return Promise.resolve();\n\n return run('enter');\n },\n leave: () => {\n if (!opts.leave) return Promise.resolve();\n\n return run('leave');\n },\n cancel: () => {\n cancelCurrent?.();\n cancelCurrent = null;\n },\n update: (newElement?: Element, newOptions?: MotionOptions) => {\n if (!newElement) throw new Error('Element is required.');\n\n element = newElement as HTMLElement;\n instance.cancel();\n init(newOptions);\n }\n };\n\n if (opts.appear) instance.enter();\n\n return instance;\n}\n\nlet endId = 0;\n\n/**\n * Ported from Vue.js Transition Component;\n * @see https://github.com/vuejs/core/blob/main/packages/runtime-dom/src/components/Transition.ts#L348\n *\n * When the transition is triggered, it waits for the end of the motion (transition or animation)\n * @param element - The element to wait for the motion end.\n * @param expectedType - The expected type of motion (transition or animation).\n * @param explicitTimeout - An optional explicit timeout in milliseconds.\n * @param resolve - A function to call when the motion ends.\n * @returns A timeout ID if an explicit timeout is provided, otherwise undefined.\n */\nfunction whenEnd(element: Element & { _motionEndId?: number }, expectedType: MotionType | undefined, explicitTimeout: number | null, resolve: () => void) {\n const id = (element._motionEndId = ++endId);\n\n const resolveIfNotStale = () => {\n if (id === element._motionEndId) {\n resolve();\n }\n };\n\n if (explicitTimeout != null) {\n return setTimeout(resolveIfNotStale, explicitTimeout);\n }\n\n const { type, timeout, count } = getMotionMetadata(element, expectedType);\n\n if (!type) {\n resolve();\n\n return;\n }\n\n const endEvent = type + 'end';\n let ended = 0;\n\n const end = () => {\n element.removeEventListener(endEvent, onEnd, true);\n resolveIfNotStale();\n };\n\n const onEnd = (event: Event) => {\n if (event.target === element && ++ended >= count) {\n end();\n }\n };\n\n element.addEventListener(endEvent, onEnd, { capture: true, once: true });\n setTimeout(() => {\n if (ended < count) {\n end();\n }\n }, timeout + 1);\n}\n"],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pB,EACA,QAAS,EAAsB,IAA+B,CAC1D,GAAI,CAAC,EAAY,MAAU,MAAM,sBAAsB,EAEvD,EAAU,EACV,EAAS,OAAO,EAChB,EAAK,CAAU,CACnB,CACJ,EAIA,OAFI,EAAK,QAAQ,EAAS,MAAM,EAEzB,CACX,CAEA,IAAI,EAAQ,EAaZ,SAAS,EAAQ,EAA8C,EAAsC,EAAgC,EAAqB,CACtJ,IAAM,EAAM,EAAQ,aAAe,EAAE,EAE/B,MAA0B,CACxB,IAAO,EAAQ,cACf,EAAQ,CAEhB,EAEA,GAAI,GAAmB,KACnB,OAAO,WAAW,EAAmB,CAAe,EAGxD,GAAM,CAAE,OAAM,UAAS,SAAU,EAAkB,EAAS,CAAY,EAExE,GAAI,CAAC,EAAM,CACP,EAAQ,EAER,MACJ,CAEA,IAAM,EAAW,EAAO,MACpB,EAAQ,EAEN,MAAY,CACd,EAAQ,oBAAoB,EAAU,EAAO,EAAI,EACjD,EAAkB,CACtB,EAEM,EAAS,GAAiB,CACxB,EAAM,SAAW,GAAW,EAAE,GAAS,GACvC,EAAI,CAEZ,EAEA,EAAQ,iBAAiB,EAAU,EAAO,CAAE,QAAS,GAAM,KAAM,EAAK,CAAC,EACvE,eAAiB,CACT,EAAQ,GACR,EAAI,CAEZ,EAAG,EAAU,CAAC,CAClB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exhaustive-ui/motion",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
|
+
"author": "e-xiste",
|
|
5
|
+
"description": "Motion utilities for ExhaustiveUI Libraries",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"motion",
|
|
8
|
+
"animation",
|
|
9
|
+
"transition",
|
|
10
|
+
"composition utilities",
|
|
11
|
+
"exhaustive-ui",
|
|
12
|
+
"angular"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/e-xiste/exhaustive-ui.git",
|
|
18
|
+
"directory": "packages/motion"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/e-xiste/exhaustive-ui/issues"
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.mjs",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"import": "./dist/index.mjs",
|
|
30
|
+
"default": "./dist/index.mjs"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@exhaustive-ui/utils": "3.0.0-alpha.1"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=12.11.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "cross-env NODE_ENV=production INPUT_DIR=src/ OUTPUT_DIR=dist/ pnpm run build:package",
|
|
50
|
+
"build:dev": "cross-env NODE_ENV=development INPUT_DIR=src/ OUTPUT_DIR=dist/ pnpm run build:dev:package",
|
|
51
|
+
"build:package": "tsdown",
|
|
52
|
+
"build:dev:package": "tsdown --watch",
|
|
53
|
+
"dev:link": "pnpm link --global && npm link"
|
|
54
|
+
}
|
|
6
55
|
}
|