@exhaustive-ui/motion 22.0.0-rc.1 → 22.0.0-rc.3
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.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,2 +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`]
|
|
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
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +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.addE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AAK,MAEH,EAAI,OAAO,EAFM,QAAQ,QAAQ,EAI5C,WAAc,CACV,IAAgB,EAChB,EAAgB,IACpB,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"}
|
|
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 // jsdom does not implement transition-* / animation-* computed properties\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"],"mappings":"2JAGA,MAAa,EAAY,YACZ,EAAa,aAO1B,SAAgB,EAAiB,EAA6C,CAK1E,OAJK,EAIE,EAAQ,UAAY,CAAC,EAAE,EAAQ,MAAQ,EAAuB,GAH1D,EAIf,CAQA,SAAgB,EAAa,EAAsC,EAA8C,CAK7G,OAJK,EAIE,CACH,GAAG,EACH,GAAI,OAAO,QAAQ,CAAc,CAAC,CAAC,QAAQ,EAA8B,CAAC,EAAK,MAC3E,EAAI,GAAQ,EAAsC,IAAQ,EAEnD,GACR,CAAC,CAAC,CACT,EAVW,CAWf,CAOA,SAAgB,EAAkB,EAA+D,CAC7F,GAAM,CAAE,OAAM,aAAY,cAAe,GAAW,CAAC,EAErD,MAAO,CACH,MAAO,CACH,KAAM,GAAY,MAAQ,GAAG,EAAK,aAClC,GAAI,GAAY,IAAM,GAAG,EAAK,WAC9B,OAAQ,GAAY,QAAU,GAAG,EAAK,cAC1C,EACA,MAAO,CACH,KAAM,GAAY,MAAQ,GAAG,EAAK,aAClC,GAAI,GAAY,IAAM,GAAG,EAAK,WAC9B,OAAQ,GAAY,QAAU,GAAG,EAAK,cAC1C,CACJ,CACJ,CAOA,SAAgB,EAAe,EAA0D,CACrF,MAAO,CACH,MAAO,CACH,SAAU,GAAS,cACnB,QAAS,GAAS,QAClB,QAAS,GAAS,aAClB,YAAa,GAAS,gBAC1B,EACA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GED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exhaustive-ui/motion",
|
|
3
|
-
"version": "22.0.0-rc.
|
|
3
|
+
"version": "22.0.0-rc.3",
|
|
4
4
|
"author": "e-xiste",
|
|
5
5
|
"description": "Motion utilities for ExhaustiveUI Libraries",
|
|
6
6
|
"keywords": [
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"LICENSE"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@exhaustive-ui/utils": "22.0.0-rc.
|
|
43
|
+
"@exhaustive-ui/utils": "22.0.0-rc.3"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">=12.11.0"
|