react-native-unistyles
Version:
Level up your React Native StyleSheet
71 lines (68 loc) • 2.43 kB
JavaScript
;
import React, { forwardRef, useLayoutEffect, useRef } from 'react';
import { Pressable as NativePressableReactNative } from 'react-native';
import { passForwardedRef } from '../../core';
import { UnistylesShadowRegistry } from '../../specs';
import { jsx as _jsx } from "react/jsx-runtime";
const getStyles = (styleProps = {}) => {
const unistyleKey = Object.keys(styleProps).find(key => key.startsWith('unistyles_'));
if (!unistyleKey) {
return styleProps;
}
return {
// styles without C++ state
...styleProps[unistyleKey].uni__getStyles(),
[unistyleKey]: styleProps[unistyleKey]
};
};
export const Pressable = /*#__PURE__*/forwardRef(({
variants,
style,
...props
}, forwardedRef) => {
const storedRef = useRef(null);
const scopedTheme = UnistylesShadowRegistry.getScopedTheme();
useLayoutEffect(() => {
return () => {
if (storedRef.current) {
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.remove(storedRef.current);
}
};
}, []);
return /*#__PURE__*/_jsx(NativePressableReactNative, {
...props,
ref: ref => {
const isPropStyleAFunction = typeof style === 'function';
const unistyles = isPropStyleAFunction ? style.call(style, {
pressed: false
}) : getStyles(style);
if (ref) {
storedRef.current = ref;
}
return passForwardedRef(ref, forwardedRef, () => {
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.add(ref, unistyles);
}, () => {
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.remove(ref);
});
},
style: state => {
const isPropStyleAFunction = typeof style === 'function';
const previousScopedTheme = UnistylesShadowRegistry.getScopedTheme();
UnistylesShadowRegistry.setScopedTheme(scopedTheme);
const unistyles = isPropStyleAFunction ? style.call(style, state) : getStyles(style);
if (!storedRef.current) {
return unistyles;
}
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.remove(storedRef.current);
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.add(storedRef.current, unistyles);
UnistylesShadowRegistry.setScopedTheme(previousScopedTheme);
return unistyles;
}
});
});
//# sourceMappingURL=Pressable.native.js.map