(
componentName,
styleNames = [],
themeStyle = {},
parentStyle = {},
elementStyle = {},
)
| 86 | * @returns {{componentStyle, childrenStyle}} The resolved component and children styles. |
| 87 | */ |
| 88 | export default function resolveComponentStyle( |
| 89 | componentName, |
| 90 | styleNames = [], |
| 91 | themeStyle = {}, |
| 92 | parentStyle = {}, |
| 93 | elementStyle = {}, |
| 94 | ) { |
| 95 | // Phase 1: merge the styles in the correct order to resolve the variant styles, |
| 96 | // the component style will be merged as well in this step, but the component |
| 97 | // style merge results are ignored after this step. We need to perform this |
| 98 | // step separately because the style variants may be overridden by any style, so |
| 99 | // the purpose of this phase is to determine the final state of the variant styles. |
| 100 | const mergedStyle = _.merge( |
| 101 | {}, |
| 102 | themeStyle, |
| 103 | parentStyle['*'], |
| 104 | parentStyle[componentName], |
| 105 | ..._.map(styleNames, sn => themeStyle[`.${sn}`]), |
| 106 | ..._.map(styleNames, sn => parentStyle[`*.${sn}`]), |
| 107 | ..._.map(styleNames, sn => parentStyle[`${componentName}.${sn}`]), |
| 108 | elementStyle, |
| 109 | ); |
| 110 | |
| 111 | // Phase 2: merge the component styles, this step is performed by using the |
| 112 | // style from phase 1, so that we are sure that the final style variants are |
| 113 | // applied to component style. |
| 114 | const resolvedStyle = _.merge( |
| 115 | {}, |
| 116 | mergedStyle, |
| 117 | parentStyle['*'], |
| 118 | parentStyle[componentName], |
| 119 | ..._.map(styleNames, sn => mergedStyle[`.${sn}`]), |
| 120 | ..._.map(styleNames, sn => parentStyle[`*.${sn}`]), |
| 121 | ..._.map(styleNames, sn => parentStyle[`${componentName}.${sn}`]), |
| 122 | elementStyle, |
| 123 | ); |
| 124 | |
| 125 | const { componentStyle, childrenStyle } = splitStyle(resolvedStyle); |
| 126 | |
| 127 | return { |
| 128 | componentStyle, |
| 129 | childrenStyle, |
| 130 | }; |
| 131 | } |
no test coverage detected