|
| 1 | +import { createElement, type ComponentType, type ExoticComponent } from "react"; |
| 2 | + |
| 3 | +import { StyleRegistry } from "../specs/StyleRegistry"; |
| 4 | +import { ContainerContext, VariableContext } from "./contexts"; |
| 5 | +import type { useStyledProps } from "./useStyled"; |
| 6 | + |
| 7 | +export function useElement( |
| 8 | + component: ComponentType | ExoticComponent<any>, |
| 9 | + componentId: string, |
| 10 | + next: ReturnType<typeof useStyledProps>, |
| 11 | + p: Record<string, any>, |
| 12 | +) { |
| 13 | + if (next.declarations.active) { |
| 14 | + p.onPress = |
| 15 | + p.onPress ?? |
| 16 | + (() => { |
| 17 | + return; |
| 18 | + }); |
| 19 | + p.onPressIn = onPressIn(componentId, p); |
| 20 | + p.onPressOut = onPressOut(componentId, p); |
| 21 | + } |
| 22 | + |
| 23 | + if (next.declarations.hover) { |
| 24 | + p.onHoverIn = onHoverIn(componentId, p); |
| 25 | + p.onHoverOut = onHoverOut(componentId, p); |
| 26 | + } |
| 27 | + |
| 28 | + if (next.declarations.focus) { |
| 29 | + p.onFocus = onFocus(componentId, p); |
| 30 | + p.onBlur = onBlur(componentId, p); |
| 31 | + } |
| 32 | + |
| 33 | + if (next.declarations.animated) { |
| 34 | + component = getAnimatedComponent(component); |
| 35 | + } |
| 36 | + |
| 37 | + if (next.containerScope === componentId) { |
| 38 | + p = { |
| 39 | + value: next.containerScope, |
| 40 | + children: createElement(component, p), |
| 41 | + }; |
| 42 | + component = ContainerContext; |
| 43 | + } |
| 44 | + |
| 45 | + if (next.variableScope === componentId) { |
| 46 | + p = { |
| 47 | + value: next.variableScope, |
| 48 | + children: createElement(component, p), |
| 49 | + }; |
| 50 | + component = VariableContext; |
| 51 | + } |
| 52 | + |
| 53 | + return createElement(component, p); |
| 54 | +} |
| 55 | + |
| 56 | +const onPressIn = (id: string, props: Record<string, any>) => () => { |
| 57 | + props.onPressIn?.(); |
| 58 | + StyleRegistry.updateComponentState(id, "active", true); |
| 59 | +}; |
| 60 | + |
| 61 | +const onPressOut = (id: string, props: Record<string, any>) => () => { |
| 62 | + props.onPressIn?.(); |
| 63 | + StyleRegistry.updateComponentState(id, "active", false); |
| 64 | +}; |
| 65 | + |
| 66 | +const onHoverIn = (id: string, props: Record<string, any>) => () => { |
| 67 | + props.onHoverIn?.(); |
| 68 | + StyleRegistry.updateComponentState(id, "hover", true); |
| 69 | +}; |
| 70 | + |
| 71 | +const onHoverOut = (id: string, props: Record<string, any>) => () => { |
| 72 | + props.onHoverOut?.(); |
| 73 | + StyleRegistry.updateComponentState(id, "hover", false); |
| 74 | +}; |
| 75 | + |
| 76 | +const onFocus = (id: string, props: Record<string, any>) => () => { |
| 77 | + props.onFocus?.(); |
| 78 | + StyleRegistry.updateComponentState(id, "focus", true); |
| 79 | +}; |
| 80 | + |
| 81 | +const onBlur = (id: string, props: Record<string, any>) => () => { |
| 82 | + props.onBlur?.(); |
| 83 | + StyleRegistry.updateComponentState(id, "focus", false); |
| 84 | +}; |
| 85 | + |
| 86 | +const animatedCache = new WeakMap<ComponentType, ComponentType>(); |
| 87 | +function getAnimatedComponent(component: ComponentType) { |
| 88 | + if ( |
| 89 | + "displayName" in component && |
| 90 | + component.displayName?.startsWith("Animated.") |
| 91 | + ) { |
| 92 | + return component; |
| 93 | + } |
| 94 | + |
| 95 | + const cached = animatedCache.get(component); |
| 96 | + if (cached) { |
| 97 | + return cached; |
| 98 | + } |
| 99 | + |
| 100 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 101 | + const createAnimatedComponent = require("react-native-reanimated") |
| 102 | + .createAnimatedComponent as (component: ComponentType) => ComponentType; |
| 103 | + |
| 104 | + const animatedComponent = createAnimatedComponent(component); |
| 105 | + animatedCache.set(component, animatedComponent); |
| 106 | + return animatedComponent; |
| 107 | +} |
0 commit comments