Last active
August 29, 2018 03:08
-
-
Save chrisregner/fb7c84fa8d59d22df9d93811b8736e92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const customisableStyledComponents = (componentFactory, defaultStyledCmpts) => | |
(overrideStyledCmpts = {}) => { | |
const customisedCmpts = Object.entries(overrideStyledCmpts) | |
.map(([componentName, customiseCmptFn]) => | |
[componentName, customiseCmptFn(defaultStyledCmpts[componentName])]) | |
.reduce((acc, [k, v]) => | |
({ ...acc, [k]: v }), {}); | |
const mergedComponents = { ...defaultStyledCmpts, ...customisedCmpts }; | |
return componentFactory(mergedComponents); | |
}; | |
export default injectableStyles; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import customisableStyledComponents from './customisableStyledComponents.js' | |
const defaultStyledComponents = { | |
Wrapper: styled.div`background: red;`, | |
Button: styled.div`background: yellow;`, | |
} | |
const myComponentFactory = ({ Wrapper, Button }) => { | |
const MyComponent = () => ( | |
<Wrapper> | |
<Button /> | |
</Wrapper> | |
) | |
return MyComponent | |
} | |
const CustomisableFactory = customisableStyledComponents(myComponentFactory, defaultStyledComponents) | |
const DefaultMyComponent = CustomisableFactory() | |
const CustomMyComponent = CustomisableFactory({ | |
Wrapper: x => x.extend` | |
background: blue; | |
`, | |
Button: () => AnotherComponentAltogether, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment