import { computed, inject, type MaybeRefOrGetter, toValue } from 'vue'; import { StoreKey } from '../core/store'; import type { ElementState } from '../core/types'; /** @internal */ type CRef = import('vue').ComputedRef; /** * Return type of {@link useElementState}. Composable bindings for visibility, style, and class * derived from the engine’s element state (written by engine.element(id).show/hide/opacity/etc.). */ export interface UseElementStateReturn { /** false when engine.element(id).hide() was called; true otherwise. Default true. */ visible: CRef; /** Combined style: opacity, transform, and any style from the store. Apply as :style="state.style". */ style: CRef>; /** Extra CSS classes from the store. Merge with component classes. */ class: CRef; } /** * Composable that subscribes to the engine’s element state for a given id. Use in custom * layouts when you need to apply visibility/style/class yourself instead of using * LuminaElement. Requires Lumina app context (StoreKey). If store is missing, returns * safe defaults (visible true, empty style and class). * * @param id - Element id: string, Ref, or getter. From resolveId, elemId, or explicit id. * @returns {UseElementStateReturn} visible, style, class. Bind :style and :class. When visible is * false, style includes opacity: 0 so the element keeps its layout space (no v-show/display:none). * * @example * const id = computed(() => resolveId(props.data, props.slideIndex, ['tag'])); * const state = useElementState(id); * // In template: ... * * @see LuminaElement * @see resolveId * @see ElementState */ export function useElementState(id: MaybeRefOrGetter): UseElementStateReturn { const store = inject(StoreKey); if (!store) { return { visible: computed(() => true), style: computed(() => ({})), class: computed(() => '') }; } const visible = computed(() => { const resolved = toValue(id); if (!resolved) return true; const s = store.state.elementState[resolved]; return s?.visible !== false; }); const style = computed(() => { const resolved = toValue(id); if (!resolved) return {}; const s = store.state.elementState[resolved] as ElementState | undefined; const out: Record = {}; // When not visible: opacity 0 so the element keeps its layout space (like slides in any app) if (s?.visible === false) { out.opacity = 0; } else if (s?.opacity !== undefined) { out.opacity = s.opacity; } if (s?.transform) out.transform = s.transform; // Only apply safe animation/visual styles from state, not layout-critical properties // This prevents state from breaking component layouts (e.g., display, position, flex properties) if (s?.style && typeof s.style === 'object') { const safeStyleProps = ['opacity', 'transform', 'transition', 'filter', 'backdropFilter', 'willChange', 'pointerEvents', 'zIndex', 'color', 'backgroundColor', 'borderColor']; Object.entries(s.style).forEach(([key, value]) => { // Only copy safe properties that don't affect layout structure if (safeStyleProps.includes(key) || key.startsWith('--')) { out[key] = value; } }); } return out; }); const classVal = computed(() => { const resolved = toValue(id); if (!resolved) return ''; const s = store.state.elementState[resolved]; return (s?.class as string) || ''; }); return { visible, style, class: classVal }; }