@composi/styler
v1.0.10
Published
Create virtual stylesheets scoped to a component.
Downloads
23
Readme
styler
This is an ES6 module that lets you create a virtual stylesheet for a component.
- Unsupported Features
- base
- styles
- Install
- Creating a Styles Object
- Semicolons
- Camel Case or Quoted
- Pixel Values
- Nested Elements
- Media Queries
- Keyframe Animation
- BEM
- @composi/core Components
Unsupported Features
Styler provides support for a wide range of CSS styles for components.
Styler is ideal for components that will be rendered a single time. Trying to create custom stylesheets for something that repeats, such as list items, is extremely wasteful.
Styler exposes one function: addstyles
. This takes two arguments: base
and styles
.
base
base
is a valid CSS selector for the element to which the stylesheet will be attached. This should be unique in the document to prevent style leaks. So, best to use an id for base
. If your component will be used multiple times, give it a class and use that as the base. Or you may use the container that the component will be rendered in as base.
styles
styles
is an object defining the stylesheet you want to create.
Install
If you're created a project using @composi/create-composi-app
then it's already and install. All you have to do is import it into your project to use:
import { addStyles } from '@composi/styler'
If you want to use @composi/styler with another library, you can install it from NPM:
npm i -g styler
Then import it into your project. Styler is an ES6 module, so use standard ES6 import syntax:
import {addStyles} from '@composi/styler'
After importing styler, you need to use it in a lifecycle event. For React, Preact or Inferno: componentDidMount
, for Vue: created
, Angular: ngOnInit
and Composi: componentWasCreated
. If you are using it with a library that does not have lifecycle events, use it during a DOM ready event.
Creating a Styles Object
When creating the object to define the stylesheet there are several rules to be aware of. Because this is a JavaScript object, it must follow JavaScript naming conventions. That means any CSS properties with hyphens must be converted to camel case or quoted. Any properties with non-alphabetic characters, such as :
must be quoted. An all values must be quoted.
Semicolons
Because this is a JavaScript object, you can't put semicolons at the end of a definition. Instead use a comma:
// Correct usage:
addStyles('#list', {
// Use comma to separate from next property:
border: 'solid 1px #ccc',
margin: '20px'
})
// Incorrect:
addStyles('#list', {
// Do not put semicolons at end!
// This will generate an error:
border: 'solid 1px #ccc';
margin: '20px';
})
Camel Case or Quoted
Below are some examples of camel case and quoted properties:
// Camel case:
addStyles('#list', {
backgroundColor: '#ff0000',
fontFamily: 'sans-serif',
textAlign: 'center'
})
// Or quoted:
addStyles('#list', {
'background-color': '#ff0000',
'font-family': 'sans-serif',
'text-align': 'center'
})
Pixel Values
If you're using a pixel value for a property, you can leave off the length identifier and provide just a raw number:
addStyles('#list', {
// Define margin of 20px:
margin: 20,
// Define width of 300px:
width: 300
})
Nested Elements
Like LESS and SASS, you can nest child elements to define their relationship to parents. This also works for pseudo-elements.
addStyles('#main', {
ul: {
margin: 10,
width: 350,
border: 'solid 1px #ccc',
li: {
padding: 10,
borderBottom: 'solid 1px #ccc',
transition: 'all .25s ease-out',
':hover': {
cursor: 'pointer',
backgroundColor: '#333',
color: '#fff'
},
':last-of-type': {
border: 'none'
}
}
}
})
This will produce the following stylesheet:
#main ul {
margin: 10px;
width: 350px;
border: solid 1px #ccc;
}
#main ul li {
padding: 10px;
border-bottom: solid 1px #ccc;
transition: all .25s ease-out;
}
#main ul li:hover {
cursor: pointer;
background-color: #333;
color: #fff;
}
#main ul li:last-of-type {
border: none;
}
Media Queries
You can add media queries to your component's stylesheet. Just make sure you quote the entire media query. To create a media query rule you need to quote the entire query, and then the entire value. Notice how this is done below:
addStyles('h1', {
padding: '2rem',
fontSize: '13pt'
}
'@media only screen and (max-width: 500px)': `{
h1: {
padding: '1rem',
fontSize: '11pt'
}
}`)
Keyframe Animation
You can add keyframe animations. To do so youo need to quote the keyframe and name property together, and then quote the entire animation rule. Notice how we do that below:
addStyles(nav, {
h1: {
'animation-duration': '3s',
'animation-timing-function': 'ease-out',
'animation-fill-mode': 'forwards',
'text-shadow': '0 5px 5px white',
':hover': {
'animation': 'animate-out'
}
},
'@keyframes animate-out': `{
0% {
transform: translateX(0);
opacity: 1;
height: 40px;
}
100% {
transform: translateX(1000px);
opacity: 0;
height: 0px;
}
}`
})
BEM
This will also work with BEM. When doing so, best to just use the generic body tag as the base for the stylesheet:
<ul class="list">
<li class="list__item">
<h3 class="item__title">Joe Bodoni</h3>
</li>
<li class="list__item">
<h3 class="item__title-selected">Ellen Vanderbilt</h3>
</li>
<li class="list__item">
<h3 class="item__title">Sam Anderson</h3>
</li>
</ul>
Define BEM CSS for above markup:
addStyles('body', {
'.list': {
margin: '20px 0',
listStyle: 'none',
border: 'solid 1px #ccc'
},
'.list__item': {
padding: 0,
borderBottom: 'solid 1px #ccc',
':last-of-type': {
border: 'none'
}
},
'.item__title': {
margin: 0,
padding: 10,
':hover': {
backgroundColor: '#333',
color: '#fff',
cursor: 'pointer'
}
},
'item__title-selected': {
backgroundColor: '#333',
color: '#fff'
}
})
@composi/core Components
It's easy to style your @composi/core components with @composi/styler. The easiest way is to do it right before the return statement in a component:
import { h, render } from '@composi/core'
import { addStyles } from '@composi/styler'
// Create list with associated stylesheet:
function List({data}) {
// Base stylesheet rule on class 'list':
addStyles('.list', {
listStyles: 'none',
padding: 0,
margin: 0,
border: 'solid 1px #ccc',
li: {
padding: '5px 10px'
borderBottom: 'solid 1px #ccc'
':last-of-type': {
border: 'none'
}
}
})
// Give list class of 'list':
return (
<ul class='list'>
{
data.map(item => <li key={item.key}>{item.value}</li>)
}
</ul>
)
}