cf-style-container
Cloudflare Style Container
Set of high order components and other helpers for fela based applications.
Installation
$ npm install cf-style-container
Aliased functions from fela and react-fela
We proxy/alias some useful functions from fela without changing their behaviour. See the original documentation for more details. We wrap all Fela APIs so we can eventually switch Fela to a different CSS in JS lib if ever needed.
createComponent(rule, [type])
Very similar to createComponent from react-fela. However, it automatically adds PropTypes from [type]
in case that it is a React Component.
Adds a displayName to the created component, which is FelaComponent(${getReactDisplayName(type)})
. If type
is a primitive such as 'div', then this string is used, otherwise the displayName of the wrapped React Component is used.
The returned Component also has the function setDisplayName
which can be called to set the display name of the HOC again outside of the call to createComponent
. If the type being passed into createComponent is a string, such as 'div', and you wish to set the displayName of the HOC, then you should call setDisplayName
after the call to createComponent
. this will ensure that the name of the HOC will have 'FelaComponent' wrapped around the name of the inner component for consistency.
You should use this HOC every time when you want to use Fela in your component. This is a primary way how to style React components.
import React from 'react';import PropTypes from 'prop-types';import createComponent from 'cf-style-container'; const styles = theme size fontWeight: theme`fontWeight` fontSize: theme`fontSize` lineHeight: theme`lineHeight` marginTop: theme`marginTop`; const Heading = size className children const tagName = 'h' + size; return React;; HeadingpropTypes = size: PropTypesisRequired className: PropTypesstringisRequired children: PropTypesnode; styles Heading;
createStyledComponent(rules, [type])
Creates a new component that implements the component styling API and maps style rules to design system primitives.
Has the same API around displayName
as createComponent
.
Style rules can be defined in a similar fashion to createComponent
during initialisation, but can also be overridden on an instance-by-instance basis using props on the component itself.
const Box = ;Box;<Box ="marine" = => What a lovely blue box!</Box>
createComponentStyles(rules, Component)
Useful when you need multiple classNames (and rules) in one component.
Adds a displayName to the created component, which is ConnectedFelaComponent(${WrappedComponent.displayName || WrappedComponent.Name})
.
The returned Component also has the function setDisplayName
which can be called to set the display name of the HOC again outside of the call to createStyledComponent
. If you wish to set the displayName of the HOC, then you should call setDisplayName
after the call to createStyledComponent
. this will ensure that the name of the HOC will have 'ConnectedFelaComponent' wrapped around the name of the inner component for consistency.
import React from 'react';import PropTypes from 'prop-types';import createComponentStyles from 'cf-style-container'; const mainStyles = theme margin: thememainmargin padding: thememainpadding; const legendStyles = theme padding: themelegendpadding marginBottom: themelegendmarginBottom borderBottom: themelegendborderBottom; const FormFieldset = legend styles <fieldset => <legend => legend </legend> </fieldset>; FormFieldsetpropTypes = styles: PropTypesobjectisRequired legend: PropTypesstringisRequired; mainStyles legendStyles FormFieldset;
Notice that rules are now an object. The names you chose will be used for classNames
accessible as styles.mainStyles
and styles.legendStyles
in this case.
applyTheme(Component, ...themes)
A HOC that ties a Fela component with the theme (adds the theme to its context). The themes can be functions that takes a baseTheme and returns a new theme, or just an object.
Adds a displayName to the created component, which is Themed(${WrappedComponent.displayName || WrappedComponent.Name})
.
The returned Component also has the function setDisplayName
which can be called to set the display name of the HOC again outside of the call to applyTheme
. If you wish to set the displayName of the HOC, then you should call setDisplayName
after the call to applyTheme
. this will ensure that the name of the HOC will have 'Themed' wrapped around the name of the inner component for consistency.
import HeadingUnstyled from './Heading';import HeadingTheme from './HeadingTheme'; import applyTheme from 'cf-style-container'; // overrides HeadingTheme fontWeight1const CustomTheme = fontWeight1: 600 ; const Heading = ; // themed component<Heading />
withTheme(Component)
A HOC that passes the current theme from context into the prop theme
. This is useful
when you need to access the theme without using createComponent
. In other words,
you can't create a new styled component with it.
import withTheme from 'cf-style-container'; const MyComponent = theme <div>Color: themecolorshail</div> MyComponent;
withRenderer(Component)
A HOC that passes the renderer from context into the prop renderer
. This is useful
for third party integration when you need to generate a class name and you can't create
a new styled component with it.
Adds a displayName to the created component, which is withRenderer(${WrappedComponent.displayName || WrappedComponent.Name})
.
The returned Component also has the function setDisplayName
which can be called to set the display name of the HOC again outside of the call to withRenderer
. If you wish to set the displayName of the HOC, then you should call setDisplayName
after the call to withRenderer
. this will ensure that the name of the HOC will have 'withRenderer' wrapped around the name of the inner component for consistency.
import withRenderer from 'cf-style-container'; const MyComponent = theme const styles = fontSize: propsfontSize color: 'red' ; const className = renderer; return <div>Class name: className</div>; MyComponent;
applyStaticStyles(staticStyles, Component)
A HOC that applies a string of static styles to a component using fela's renderStatic. Useful for integration with older libraries that require side loading of a static CSS block.
Accepts a function or a string. If a function is provided, the baseTheme will be provided to the function.
Adds a displayName to the created component, which is WithStaticStyles(${WrappedComponent.displayName || WrappedComponent.Name})
.
The returned Component also has the function setDisplayName
which can be called to set the display name of the HOC again outside of the call to applyStaticStyles
. If you wish to set the displayName of the HOC, then you should call setDisplayName
after the call to applyStaticStyles
. this will ensure that the name of the HOC will have 'WithStaticStyles' wrapped around the name of the inner component for consistency.
import applyStaticStyles from 'cf-style-container'; const staticStyles = '.purple-component { background-color: purple }';// OR// const staticStyles = baseTheme => `.purple-component{background-color: ${baseTheme.purple} }` const MyComponent = <div ='purple-component' />; staticStyles MyComponent;