MCPcopy
hub / github.com/Shopify/restyle / composeRestyleFunctions

Function composeRestyleFunctions

src/composeRestyleFunctions.ts:12–72  ·  view source on GitHub ↗
(
  restyleFunctions: (
    | RestyleFunctionContainer<TProps, Theme>
    | RestyleFunctionContainer<TProps, Theme>[]
  )[],
)

Source from the content-addressed store, hash-verified

10import {AllProps} from './restyleFunctions';
11
12const composeRestyleFunctions = <
13 Theme extends BaseTheme,
14 TProps extends AllProps<Theme>,
15>(
16 restyleFunctions: (
17 | RestyleFunctionContainer<TProps, Theme>
18 | RestyleFunctionContainer<TProps, Theme>[]
19 )[],
20) => {
21 const flattenedRestyleFunctions = restyleFunctions.reduce(
22 (acc: RestyleFunctionContainer<TProps, Theme>[], item) => {
23 return acc.concat(item);
24 },
25 [],
26 );
27
28 const properties = flattenedRestyleFunctions.map(styleFunc => {
29 return styleFunc.property;
30 });
31 const propertiesMap = properties.reduce(
32 (acc, prop) => ({...acc, [prop]: true}),
33 {} as {[key in keyof TProps]: true},
34 );
35
36 const funcsMap = flattenedRestyleFunctions.reduce(
37 (acc, each) => ({[each.property]: each.func, ...acc}),
38 {} as {[key in keyof TProps]: RestyleFunction<TProps, Theme, string>},
39 );
40
41 // TInputProps is a superset of TProps since TProps are only the Restyle Props
42 const buildStyle = (
43 props: TProps,
44 {
45 theme,
46 dimensions,
47 }: {
48 theme: Theme;
49 dimensions: Dimensions | null;
50 },
51 ): RNStyle => {
52 const styles: ViewStyle = {};
53 const options = {theme, dimensions};
54 // We make the assumption that the props object won't have extra prototype keys.
55 // eslint-disable-next-line guard-for-in
56 for (const key in props) {
57 const mappedProps = funcsMap[key](props, options);
58 // eslint-disable-next-line guard-for-in
59 for (const mappedKey in mappedProps) {
60 styles[mappedKey as keyof ViewStyle] = mappedProps[mappedKey];
61 }
62 }
63
64 const {stylesheet} = StyleSheet.create({stylesheet: styles});
65 return stylesheet;
66 };
67 return {
68 buildStyle,
69 properties,

Callers 5

createVariant.tsFile · 0.85
createRestyleComponentFunction · 0.85
TestButton.tsxFile · 0.85
TestContainer.tsxFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…