MCPcopy Index your code
hub / github.com/Shopify/restyle / createRestyleFunction

Function createRestyleFunction

src/createRestyleFunction.ts:23–133  ·  view source on GitHub ↗
({
  property,
  transform,
  styleProperty,
  themeKey,
}: {
  property: P;
  transform?: StyleTransformFunction<Theme, K, TProps[P]>;
  styleProperty?: S;
  themeKey?: K;
})

Source from the content-addressed store, hash-verified

21const memoizedThemes: WeakMap<BaseTheme, any> = new WeakMap();
22
23const createRestyleFunction = <
24 Theme extends BaseTheme = BaseTheme,
25 TProps extends {[key: string]: any} = {[key: string]: any},
26 P extends keyof TProps = keyof TProps,
27 K extends keyof Theme | undefined = undefined,
28 S extends RNStyleProperty = RNStyleProperty,
29>({
30 property,
31 transform,
32 styleProperty,
33 themeKey,
34}: {
35 property: P;
36 transform?: StyleTransformFunction<Theme, K, TProps[P]>;
37 styleProperty?: S;
38 themeKey?: K;
39}) => {
40 const styleProp = styleProperty || property.toString();
41
42 const func: RestyleFunction<TProps, Theme, S | P> = (
43 props,
44 {theme, dimensions},
45 ) => {
46 if (memoizedThemes.get(theme) == null) {
47 memoizedThemes.set(theme, {});
48 }
49
50 const memoizedMapHashKey = (() => {
51 if (
52 themeKey &&
53 property &&
54 props[property] &&
55 typeof themeKey === 'string' &&
56 typeof property === 'string'
57 ) {
58 /*
59 The following code is required to ensure all variants that have different breakpoint objects are turned into unique strings. By simply retuning String(props[property]), two different variants with breakpoints will return the same string.
60 For example, if we have the following variant:
61 spacingVariant: {
62 defaults: {},
63 noPadding: {
64 phone: 'none',
65 tablet: 'none',
66 },
67 mediumPadding: {
68 phone: 'm',
69 tablet: 'm',
70 }
71 }
72 using String(props[property]) will turn both variants into [object Object], making them equivalent and resulting in separate styles being memoized into the same hash key.
73 By building the propertyValue string ourselves from the breakpoints, we can format the variants to be "phone:nonetablet:none" and "phone:mtablet:m" respectively, making each memoized hash key unique.
74 */
75 let propertyValue = '';
76 if (typeof props[property] === 'object') {
77 for (const [breakpoint, value] of Object.entries(props[property])) {
78 propertyValue += `${breakpoint}:${value}`;
79 }
80 } else {

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…