import React, { useEffect, useMemo } from 'react'; import type { // @ts-ignore IAnimationDriverPlugin, IStyledPlugin, } from '@gluestack-style/react'; import { useStyled } from '@gluestack-style/react'; import { deepMerge, setObjectKeyValue, resolvedTokenization } from './utils'; import { propertyTokenMap } from './propertyTokenMap'; function tokenizeAnimationPropsFromConfig( props: any = {}, config: any, animationAliases: any, path: any = [], tokenizedAnimatedProps: any = {} ) { for (const prop in props) { if (Array.isArray(props[prop])) { path.push(prop); setObjectKeyValue(tokenizedAnimatedProps, path, props[prop]); path.pop(); } else if (animationAliases[prop]) { path.push(prop); const tokenizedValue = resolvedTokenization(props[prop], config); setObjectKeyValue(tokenizedAnimatedProps, path, tokenizedValue); path.pop(); } else if (typeof props[prop] === 'object') { path.push(prop); const tokenizedValue = resolvedTokenization(props[prop], config); setObjectKeyValue(tokenizedAnimatedProps, path, tokenizedValue); // path.pop(); tokenizeAnimationPropsFromConfig( props[prop], config, animationAliases, path, tokenizedAnimatedProps ); path.pop(); } else { path.push(prop); setObjectKeyValue(tokenizedAnimatedProps, path, props[prop]); path.pop(); } } return tokenizedAnimatedProps; } function getVariantProps(props: any, theme: any) { const variantTypes = theme?.variants ? Object.keys(theme.variants) : []; const restProps = { ...props }; const variantProps: any = {}; variantTypes?.forEach((variant) => { if (props[variant]) { variantProps[variant] = props[variant]; // delete restProps[variant]; } }); return { variantProps, restProps, }; } function resolveVariantAnimationProps(variantProps: any, styledObject: any) { let resolvedVariant = {}; Object.keys(variantProps).forEach((variant) => { const variantValue = variantProps[variant]; const variantObject = styledObject?.variants?.[variant]?.[variantValue]; resolvedVariant = deepMerge(resolvedVariant, variantObject); }); return resolvedVariant; } export class AnimationResolver implements IStyledPlugin { name: 'AnimationResolver'; componentDriver: IAnimationDriverPlugin; config = { aliases: { ':animate': 'animate', ':initial': 'initial', ':exit': 'exit', ':initialProps': 'initialProps', ':animateProps': 'animateProps', ':transition': 'transition', ':transformOrigin': 'transformOrigin', ':whileTap': 'whileTap', ':whileHover': 'whileHover', ':onAnimationComplete': 'onAnimationComplete', } as const, tokens: {} as const, animatedPropMap: {} as any, }; AnimatePresenceComp = React.Fragment; register(config: any) { if (this.config) { if (config?.aliases) { this.config.aliases = { ...this.config?.aliases, ...config?.aliases, }; } if (config?.tokens) { this.config.tokens = { ...this.config?.tokens, ...config?.tokens, }; } if (config?.animatedPropMap) { this.config.animatedPropMap = { ...this.config?.animatedPropMap, ...config?.animatedPropMap, }; } // @ts-ignore this.config.ref = config?.ref; } } constructor(ComponentDriverClass: any, config: any = {}) { // @ts-ignore const componentDriver = new ComponentDriverClass(config); this.name = 'AnimationResolver'; this.componentDriver = componentDriver; if (componentDriver.engine.AnimatePresence) { this.AnimatePresenceComp = componentDriver.engine.AnimatePresence; } this.#childrenExitPropsMap = {}; this.#extendedConfig = {}; this.register(componentDriver.config); } #childrenExitPropsMap: any; #extendedConfig: any; inputMiddleWare
(
styledObj = {},
shouldUpdateConfig: any = true,
_?: boolean,
Component?: React.ComponentType,
componentStyleConfig?: any
): {
// @ts-ignore
[key in keyof typeof this.config.aliases]: P[(typeof this.config.aliases)[key]];
} {
const ignoreKeys = new Set();
const uniqueComponentId = componentStyleConfig?.uniqueComponentId;
if (
Component &&
(Component.displayName?.startsWith(
'Gluestack-AnimatedResolver-Animated'
) ||
// @ts-ignore
Component.isAnimatedComponent)
) {
const componentDisplayName = Component.displayName;
const AnimatedComponent =
this?.componentDriver?.engine[
// @ts-ignore
componentDisplayName?.replace(
'Gluestack-AnimatedResolver-Animated',
''
)
];
if (AnimatedComponent) {
AnimatedComponent.isAnimatedComponent = true;
}
// if (!AnimatedComponent) {
// AnimatedComponent = Component;
// }
const resolvedAnimatedProps = this.updateStyledObject(
styledObj,
shouldUpdateConfig,
ignoreKeys,
{},
uniqueComponentId ? [uniqueComponentId] : []
);
const resolvedStyledObjectWithAnimatedProps = deepMerge(
styledObj,
resolvedAnimatedProps
);
// if (shouldUpdateConfig) {
// // @ts-ignore
// return [styledObj, shouldUpdateConfig, _, AnimatedComponent];
// }
// @ts-ignore
return [
resolvedStyledObjectWithAnimatedProps,
shouldUpdateConfig,
_,
Component,
ignoreKeys,
];
}
// @ts-ignore
return [styledObj, shouldUpdateConfig, _, Component, ignoreKeys];
}
updateStyledObject(
styledObject: any = {},
shouldUpdateConfig: boolean,
ignoreKeys: Set