react-outline was designed to more easly manage inline styles and better support server side rendering with complete styling.
Feathers:
- Cleaner JSX markup (without the styles)
- Easy creation of standered and custom element combining styles into the element
- Easer debugging of element with injected name attributes
- Support a UI Color Palette
- Dynamically add vendor prefixes
- Cache calculated style
- Named elements (Generate element mapped to name prop in DOM)
- Creating and applying a style
- Generate a element from a style
- Combine style attribute
- Logic functions for run-time control of styles
- Setting the options
- Using the options
- Comparisons
The simplest way to create styled components:
importoutlinefrom'react-outline'// Inline style schema - no nesting required!conststyles=outline({title: {fontSize: 25,backgroundColor: "red"}});constTitle=styles.title`div`exportdefault<Title>Hello World</Title>// Renders: <div style={{ fontSize: 25, backgroundColor: "red" }}>Hello World</div>importoutlinefrom'react-outline'conststyles=outline({title: {fontSize: "25px"}});exportdefault(props)=><divstyle={styles.title()}>{props.text}</div>// Renders: <div style={{ fontSize: "25px" }}>"hello"</div>importoutlinefrom'react-outline'conststyles=outline({title: {fontSize: "25px"}});constTitle=styles.title`div`exportdefault(props)=><Title>{props.text}</Title>// Renders: <div style={{ fontSize: "25px" }}>"hello"</div>Use base for default styles and add variants as sibling keys:
importoutlinefrom'react-outline'conststyles=outline({title: {base: {fontSize: "25px"},error: {color: "#f00"}}});constTitle=styles.title`div`// Using a prop flag - cleanest!exportdefault<Titleerror>Something went wrong</Title>// Renders: <div style={{ fontSize: "25px", color: "#f00" }}>...</div>Alternative syntaxes:
// Via style prop<Titlestyle={{error: true}}>Error</Title>// Via style function (inline)<divstyle={styles.title({error: true})}>Error</div>Add functions to compute styles dynamically at runtime:
importoutlinefrom'react-outline'conststyles=outline({content: {backgroundColor: "gray"},cell: {fontSize: 10}},{// 1 arg: receives the passed valuecontent: (count)=>({height: `${count*50}px`}),// 2 args: receives (baseStyle, passedValue)cell: (style,important)=>({fontSize: style.fontSize+(important ? 5 : -5)})});constdata=[{name: "foo",important: true},{name: "bar",important: false},{name: "baz"}]exportdefault(<divstyle={styles.content(data.length)}>{data.map((item,i)=>(<spankey={i}style={styles.cell(item.important)}>{item.name}</span>))}</div>)With generated elements - the style prop becomes the function argument:
constGroup=styles.content`div`constCell=styles.cell`span`exportdefault(<Groupstyle={data.length}>{data.map((item,i)=>(<Cellkey={i}style={item.important}>{item.name}</Cell>))}</Group>)There three mechanisms
setOptionsis used to set the default options of all calls tooutline(...)withOptionsis to custom the options for a specific instance ofoutline(...)- Pass an options object as the 2nd parameters to outline
import{setOptions}from'react-outline'setOptions({caching:true,named:true})import{withOptions}from'react-outline'constoutline=withOptions({caching:true,named:true})When enable, will case styled per element based on deep equal check
default to:false
use:{caching : true}(boolean)
When enable, will case styled per element based on deep equal check
default to:undefined
use:{colors : { red500:#F44336, indigo500:#3F51B5 }}(object)
import{withOptions}from'react-outline'// Using material-ui colors codesimport{colors}from'material-ui/styles';constoutline=withOptions({colors})letstyles={base : {foo:{color:"deepPurple900"}}}styles=outline(styles)exportdefault<divstyle={styles.foo}/>/* <div style={{ backgroundColor:"#311B92" }} />*/Helpful for debugging. Will add a
nameattribute Dom element if you use a generated element
default to:true
use:{named : true}(boolean)
// Using the named optionimportoutlinefrom'react-outline'letstyles={base : {page:{backgroundColor:"#eee"}}styles=outline(styles,{named:true})// enable named elementsconstPage=styles.page`div`// create a div elemet with the 'page' styleexportdefaultPage// export the elemet/* <div name="page" style={{ backgroundColor:"#eee" }} />*/I created react-outline becase of some shortcoming I found when trying to use styled-components in a new project. The problem's I addressed as:
- Elements where replaced and the new css class name was changed on each render. This was a problem with using animate.css + ReactCSSTransitionGroup -> leaveActive. The element would enter fine but just disappear immediately on leave.
React-outline supports ReactCSSTransitionGroup Example
- You cant style exiting react of elements, only create new ones.
With React-outline you can create any element you what Example or style an existing one Example
- No vendor prefixes support.
vendor prefixes support is provided by inline-style-prefixer under the covers
- The style must be in a CSS string. Then makes moving existing inline style object to CSS is time time consuming
Checkout any of the code here examples
Feature requests can be made using Github issues
Pull requests are totally encouraged and you are welcome to contribute to the development of react-outline. Please do raise an issue before making a pull request so as to determine if a particular feature is already being worked on or is currently out of the scope of this project.
- Fork react-outline
- Create a feature branch (git checkout -b my-new-feature)
- Write tests
- Ensure the code is covered
- Add story to the example storybook
- Commit your changes
- Push to your branch
- Make a pull request
The project requires 100% code coverage before publishing to npm.
- New features must include a Storybook example. These examples automatically become tests.
- Edge cases are validated in
cornerCases.test.jsto ensure comprehensive coverage. - Run
npm run test:coverto check coverage before submitting a PR.
