Skip to content

Instantly share code, notes, and snippets.

@chrisregner
Last active August 29, 2018 03:08
Show Gist options
  • Save chrisregner/fb7c84fa8d59d22df9d93811b8736e92 to your computer and use it in GitHub Desktop.
Save chrisregner/fb7c84fa8d59d22df9d93811b8736e92 to your computer and use it in GitHub Desktop.
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;
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