React Polymorph
React Polymorph is a UI framework for React, that separates logic, markup and theming of components. It's inspired by react-toolbox (but more flexible), powered by CSS Modules and harmoniously integrates with your webpack workflow, although you can use any other module bundler.
Why?
- Existing React UI frameworks are too hard to customize.
- Overriding css styles is not enough for complex components.
- You need multiple variations of a component with shared logic.
- You need multiple, completely unique themes for your components.
How:
Separate monolithic React components into:
- Component (logic) - Only handle UI logic, do not render markup.
- Skin (markup) - Only render the markup, delegate to component.
- Theme (styling) - Only concerned about styling your skin.
Installation & Usage
React Polymorph can be installed as an npm package:
$ npm install --save react-polymorph
Usage in Webpack Projects
Now you can import and use components like this in your app:
Each component's skin that you apply to your component will receive its styles (css/scss) via a theme.
React-polymorph comes with Simple themes & skins out of the box, but all themes are completely customizable.
Components and Skins
Imagine you need a standard text Input
component for text and a NumericInput
for floating point numbers. The only difference is the logic of the component,
in both cases it is "just" an input field showing some text:
Standard Input
The standard input is as simple as possible and does not have much logic:
Numeric Input
The numeric input however is specialized in guiding the user to
enter correct floating point numbers:
This is a simple example that shows how you can make/use specialized versions
of basic components by composition - a core idea of react-polymorph
!
Textarea
The textarea is as simple as possible and does not have much logic:
Button
The button is as simple as possible and does not have much logic:
Select
The select component is like standard select but with additional logic for adding custom option renderer and opening directions (upward / downward):
Checkbox
The checkbox is as simple as possible and does not have much logic:
Switch
The switch is as simple as possible and does not have much logic. Like checkbox but uses a different skin part:
Toggler
The toggler is as simple as possible and does not have much logic. Like checkbox but uses a different skin part:
Modal
The modal is component which wraps its children as standard dialog. As is shown in example, modal can have multiple other polymorph components:
Autocomplete
The autocomplete input is specialized to help users to select between multiple suggested words depending on entered letters:
Bubble
The bubble component will open up an absolutely positioned speech bubble. This is position in respect to it's closest relatively positioned parent.
Tooltip
The tooltip opens a bubble relative to it's children, containing text or html to display.
Radio
The radio is as simple as possible and does not have much logic:
Customizing Component Skins
Theme API
Each component has a theme API. This is a plain object which exposes the shape of a component's theme. Each property on the theme API object is a class name assigned to an element within the component's skin and a class definition within the component's theme. Below is the Button's theme API.
Every component accepts an optional themeOverrides
property intended to provide a CSS Module import object which is used by the component to assign a user's local classnames to its DOM nodes. If the component has already been passed a theme prop, the css/scss properties passed via themeOverrides will be merged with the injected theme object. This automatic composition saves the user from manually piecing together custom styles with those of the injected theme that the user may wish to retain. If you want to customize a component's theme, the themeOverrides object must contain the appropriate classname mapping to its documented theme API. In this way, you can add or override classnames on the nodes of a specific component.
Overriding a styles in a theme
For example, if you want to override the background-color of Button
's injected theme with green:
themeOverrides
ButtonTheme
Result
The user's custom background color overrides Simple theme's blue background.
Compose
Similarly, you can compose your own custom styles with an injected theme
themeOverrides
will be composed with
ButtonTheme
Result
In this case we are composing custom styles with an instance of Button
where the Simple ButtonTheme
was already injected. If a theme isn't passed to a component, a theme object implementing that component's full theme API is necessary. When implementing a component's full theme, take into account that every classname is there for a reason. You can either provide a component's theme as a prop or pass it through context as described in the next section.
ThemeProvider HOC
ThemeProvider
allows you to pass a theme to multiple instances of a component without explicitly passing them a theme prop. Wrap your component tree with ThemeProvider
at the desired level in your component hierarchy. You can maintain different themes and themeOverrides for specific portions of your app's tree.
Customizing all instances of a Component using ThemeProvider
Create a CSS Module theme file for the component you wish to customize, for example for Input
& FormField
:
input.css
formfield.css
Create a theme file that imports each component's custom styles as CSS-Modules object(s). Apply the styles according to the root theme API structure. The root theme API is simply an object whose keys are named after each component in the react-polymorph library. For example, the styles you assign to the input key will be applied to all instances of the Input
component nested within ThemeProvider
. The same goes for the formfield key and all nested instances of the FormField
component.
customInputs.js
Import your custom theme to pass ThemeProvider
's themeOverrides property. This will apply your custom css/scss to all of its nested react-polymorph components. In this example, all 3 instances of the Input
and FormField
components will have the user's custom css definitions composed with Simple InputTheme and FormFieldTheme.
You may also pass the entire SimpleTheme object to ThemeProvider
and maintain the same functionality without having to import themes specific to the components you're using.