/** * Animation config — centralized defaults and resolution for all animation timings, delays, and eases. * * @description * Single source of truth for animation constants used by useTransition, Lumina.revealInSequence, * elementController, LuminaDeck onLeave, and LuminaElement. Every timing and ease can be overridden via * `options.animation`, `deck.meta.effects`, or `slide.meta.effects`. Precedence: slide overrides deck * over options over ANIMATION_DEFAULTS. * * Legacy keys in meta.effects are mapped: `animationDuration`→durationIn, `animationDurationOut`→durationOut, * `animationStagger`→stagger, `animationEase`→ease, `animationsEnabled`→enabled, `animationType`→type, * `entranceReveal` (unchanged). * * @example * // Resolve config for a slide (e.g. in a custom component or tooling) * import { resolveTransitionConfig, ANIMATION_DEFAULTS } from 'lumina-slides'; * const cfg = resolveTransitionConfig(store, slide); * console.log(cfg.durationIn, cfg.revealDelayMs, cfg.elementDuration); * * @example * // Override via options when creating the engine * new Lumina("#app", { animation: { durationIn: 1.2, revealDelayMs: 500, elementDuration: 0.6 } }); * * @example * // Override via deck or slide meta (in JSON) * { "meta": { "effects": { "animationDuration": 1, "revealDelayMs": 400 } }, "slides": [...] } * * @module animationConfig * @see Lumina * @see LuminaAnimationOptions * @see IMPLEMENTATION.md Animation options section */ import type { LuminaStore } from './store'; import type { BaseSlideData } from './types'; // --------------------------------------------------------------------------- // DEFAULTS — Single source of truth for all animation constants // --------------------------------------------------------------------------- /** * Default values for every animation setting. All keys are guaranteed to exist. * Override via `options.animation`, `deck.meta.effects`, or `slide.meta.effects`; see resolveTransitionConfig. * * @constant * @type {Readonly>} * @see resolveTransitionConfig * @see LuminaAnimationOptions */ export const ANIMATION_DEFAULTS = { // --- Global --- enabled: true, type: 'fade' as const, durationIn: 0.8, durationOut: 0.5, stagger: 0.1, ease: 'power3.out', entranceReveal: false, // --- useTransition: entrance (by element type) --- /** Multiplier for image reveal duration (durationIn * this). */ imageDurationMultiplier: 1.5, imageEase: 'expo.out', /** Stagger between character spans in split text. */ characterStagger: 0.015, characterDurationFactor: 0.8, characterEase: 'back.out(2)', /** Stagger between .reveal-child elements. */ childRevealStagger: 0.05, childRevealDurationFactor: 0.6, childRevealStart: 0.2, childRevealEase: 'power2.out', /** Duration for standard (non-elastic) when durationIn not set. */ durationInStandard: 0.7, /** Duration for elastic-up type. */ durationInElastic: 1.2, easeElastic: 'elastic.out(1, 0.5)', easeSpring: 'back.out(1.5)', // --- useTransition: exit --- easeOut: 'power2.in', exitCharStagger: 0.005, exitStaggerFactor: 0.5, exitImageDurationFactor: 1.2, exitChildDurationFactor: 0.8, // --- Lumina.revealInSequence --- revealDelayMs: 400, revealDuration: 0.45, revealEase: 'power2.out', // --- Default cascade (when no meta.reveal or slide.reveal) --- /** Delay in ms between elements for implicit default cascade. Default 120. */ defaultCascadeDelayMs: 120, /** Per-element duration (s) for implicit default cascade. Default 0.3. */ defaultCascadeDuration: 0.3, // --- elementController.animate --- elementDuration: 0.5, elementEase: 'power2.out', // --- LuminaDeck onLeave (uses durationOut, easeOut) --- // (no extra keys; durationOut, easeOut above) // --- LuminaElement opacity transition (CSS fallback) --- elementOpacityTransition: '0.35s ease-out', } as const; /** * Fully resolved animation config. Every field is defined (no undefined). Produced by resolveTransitionConfig. * Used internally by useTransition, Lumina.revealInSequence, elementController, LuminaDeck onLeave. * * @typedef {Object} ResolvedTransitionConfig * @property {boolean} enabled - Global animations on/off. * @property {string} type - 'fade'|'slide'|'zoom'|'cascade'|'blur'|'elastic-up'|'spring'|etc. * @property {number} durationIn - Entry animation duration (s). Default 0.8. * @property {number} durationOut - Exit animation duration (s). Default 0.5. * @property {number} stagger - Delay between staggered elements (s). Default 0.1. * @property {string} ease - GSAP ease (e.g. 'power3.out'). * @property {boolean} entranceReveal - If true, run GSAP reveal on .reveal-* when entering. * @property {number} imageDurationMultiplier - Image duration = durationIn * this. Default 1.5. * @property {string} imageEase - Image reveal ease. Default 'expo.out'. * @property {number} characterStagger - Stagger between split-text chars (s). Default 0.015. * @property {number} characterDurationFactor - Character duration factor. Default 0.8. * @property {string} characterEase - Character ease. Default 'back.out(2)'. * @property {number} childRevealStagger - Stagger between .reveal-child (s). Default 0.05. * @property {number} childRevealDurationFactor - Child duration factor. Default 0.6. * @property {number} childRevealStart - Timeline start for child reveal. Default 0.2. * @property {string} childRevealEase - Child reveal ease. Default 'power2.out'. * @property {number} durationInStandard - Fallback when type is not elastic. Default 0.7. * @property {number} durationInElastic - Duration for elastic-up. Default 1.2. * @property {string} easeElastic - Ease for elastic-up. Default 'elastic.out(1, 0.5)'. * @property {string} easeSpring - Ease for spring. Default 'back.out(1.5)'. * @property {string} easeOut - Exit/leave ease. Default 'power2.in'. * @property {number} exitCharStagger - Char stagger on exit. Default 0.005. * @property {number} exitStaggerFactor - Exit element stagger = stagger * this. Default 0.5. * @property {number} exitImageDurationFactor - Exit image duration factor. Default 1.2. * @property {number} exitChildDurationFactor - Exit child duration factor. Default 0.8. * @property {number} revealDelayMs - revealInSequence delay between elements (ms). Default 400. * @property {number} revealDuration - revealInSequence per-element duration (s). Default 0.45. * @property {string} revealEase - revealInSequence ease. Default 'power2.out'. * @property {number} defaultCascadeDelayMs - Default cascade delay between elements (ms). Default 120. * @property {number} defaultCascadeDuration - Default cascade per-element duration (s). Default 0.3. * @property {number} elementDuration - element().animate() default duration (s). Default 0.5. * @property {string} elementEase - element().animate() default ease. Default 'power2.out'. * @property {string} elementOpacityTransition - CSS value for LuminaElement opacity. Default '0.35s ease-out'. */ export type ResolvedTransitionConfig = { enabled: boolean; type: string; durationIn: number; durationOut: number; stagger: number; ease: string; entranceReveal: boolean; imageDurationMultiplier: number; imageEase: string; characterStagger: number; characterDurationFactor: number; characterEase: string; childRevealStagger: number; childRevealDurationFactor: number; childRevealStart: number; childRevealEase: string; durationInStandard: number; durationInElastic: number; easeElastic: string; easeSpring: string; easeOut: string; exitCharStagger: number; exitStaggerFactor: number; exitImageDurationFactor: number; exitChildDurationFactor: number; revealDelayMs: number; revealDuration: number; revealEase: string; defaultCascadeDelayMs: number; defaultCascadeDuration: number; elementDuration: number; elementEase: string; elementOpacityTransition: string; }; const LEGACY_MAP: Record = { animationsEnabled: 'enabled', animationType: 'type', animationDuration: 'durationIn', animationDurationOut: 'durationOut', animationStagger: 'stagger', animationEase: 'ease', entranceReveal: 'entranceReveal', }; function mapLegacy(src: Record | null | undefined): Partial { if (!src || typeof src !== 'object') return {}; const out: Partial = {}; for (const [k, v] of Object.entries(src)) { if (v === undefined) continue; const canon = LEGACY_MAP[k] ?? (k as keyof ResolvedTransitionConfig); if (Object.prototype.hasOwnProperty.call(ANIMATION_DEFAULTS, canon)) { (out as Record)[canon] = v; } } return out; } function pickCanonical(src: Record | null | undefined): Partial { if (!src || typeof src !== 'object') return {}; const out: Partial = {}; const keys = Object.keys(ANIMATION_DEFAULTS) as (keyof ResolvedTransitionConfig)[]; for (const k of keys) { if (Object.prototype.hasOwnProperty.call(src, k) && (src as Record)[k] !== undefined) { (out as Record)[k] = (src as Record)[k]; } } return out; } /** * Resolves the full animation config by merging, in order (later overrides earlier): * 1. ANIMATION_DEFAULTS * 2. options.animation (with legacy key mapping) * 3. deck.meta.effects (with legacy key mapping) * 4. slide.meta.effects (with legacy key mapping) * * Use when you need a single config object for transitions, revealInSequence, element().animate(), * or deck leave. When `store` or `slideData` is null/undefined, only the layers you have are merged. * * @param store - Lumina store (state.options, state.deck). Can be null if building config without engine. * @param slideData - Optional slide for slide-level overrides (slide.meta.effects). Omit for deck-level only. * @returns Resolved config with every key defined (no undefined). Safe to pass to GSAP or timing logic. * * @example * const cfg = resolveTransitionConfig(store, currentSlide); * gsap.to(el, { duration: cfg.durationIn, ease: cfg.ease }); * * @example * // Without slide (deck + options only) * const cfg = resolveTransitionConfig(store, undefined); * * @see ANIMATION_DEFAULTS * @see ResolvedTransitionConfig * @see LuminaAnimationOptions */ export function resolveTransitionConfig(store: LuminaStore | null | undefined, slideData?: Readonly | null): ResolvedTransitionConfig { const base = { ...ANIMATION_DEFAULTS } as unknown as Record; const opt = (store?.state?.options?.animation || {}) as Record; const deckEff = (store?.state?.deck?.meta?.effects || {}) as Record; const slideEff = (slideData?.meta?.effects || {}) as Record; const apply = (raw: Record) => { Object.assign(base, mapLegacy(raw), pickCanonical(raw)); }; apply(opt); apply(deckEff); apply(slideEff); return base as unknown as ResolvedTransitionConfig; }