What is jss?
JSS (JavaScript Style Sheets) is a library for generating CSS styles with JavaScript. It allows you to define styles in a JavaScript object and apply them to your components, making it easier to manage and maintain styles in a JavaScript-centric development environment.
What are jss's main functionalities?
Creating Styles
This feature allows you to create styles using JavaScript objects. The `createStyleSheet` method generates a stylesheet from the provided styles and attaches it to the document.
const styles = {
button: {
color: 'blue',
background: 'white',
border: '1px solid blue'
}
};
const { classes } = jss.createStyleSheet(styles).attach();
// Usage in a component
const button = document.createElement('button');
button.className = classes.button;
button.textContent = 'Click me';
document.body.appendChild(button);
Dynamic Styles
This feature allows you to create dynamic styles that can change based on props or state. The `update` method is used to update the styles with new values.
const styles = {
button: {
color: props => props.color,
background: 'white',
border: '1px solid blue'
}
};
const sheet = jss.createStyleSheet(styles);
const { classes } = sheet.update({ color: 'red' }).attach();
// Usage in a component
const button = document.createElement('button');
button.className = classes.button;
button.textContent = 'Click me';
document.body.appendChild(button);
Theming
This feature allows you to create themes that can be applied to your styles. The styles can reference theme variables, making it easy to switch themes or update theme values.
const theme = {
primaryColor: 'blue',
secondaryColor: 'green'
};
const styles = theme => ({
button: {
color: theme.primaryColor,
background: 'white',
border: `1px solid ${theme.primaryColor}`
}
});
const sheet = jss.createStyleSheet(styles(theme)).attach();
const { classes } = sheet;
// Usage in a component
const button = document.createElement('button');
button.className = classes.button;
button.textContent = 'Click me';
document.body.appendChild(button);
Other packages similar to jss
styled-components
Styled-components is a library for styling React components using tagged template literals. It allows you to write actual CSS code to style your components and supports theming and dynamic styling. Compared to JSS, styled-components is more tightly integrated with React and uses a different syntax for defining styles.
emotion
Emotion is a library designed for writing CSS styles with JavaScript. It provides both a styled API similar to styled-components and a css API for defining styles as objects. Emotion is known for its performance and flexibility, offering a similar feature set to JSS but with a different API and additional performance optimizations.
aphrodite
Aphrodite is a library for styling React components with JavaScript. It allows you to define styles as JavaScript objects and provides support for media queries and pseudo-selectors. Aphrodite is simpler and more lightweight compared to JSS, but it may lack some of the advanced features and flexibility that JSS offers.
Dynamic stylesheets for web components.
Modern web applications need expressive language for styles description. This project takes a fresh look at the idea of writing stylesheets in javascript. It solves some major problems with css:
-
Cascading style sheets do not scale. There are some solutions like bem which solve this problem, however they introduce an overhead of writing long class names. Here is how true namespaces can look like in jss.
-
We often need to calculate layouts in javascript and we need to access properties defined in stylessheets. Jss allows us to do it easy and without DOM round trip by giving us a direct access to css values.
-
Its up to you whether to put some styles via style tag or to apply them directly to the element.
-
Optimize your app performance by detaching unused stylesheets from render tree.
-
Use full power of expressive full featured language. Any features you might know from stylus or sass and some more are already available.
-
Evil mixins are not a problem any more.
-
No need to precompile, but you can if you want to.
Take a look at examples directory.
Built in preprocessors
Jss styles are just plain javascript objects. They map 1:1 to css rules, except of those modified by preprocessors.
Nested Rules
Put an ampersand before a selector within a rule and it will be converted to a separate rule with a nested selector.
{
'.container': {
padding: '20px',
'&.clear': {
clear: 'both'
},
'& .button': {
background: 'red'
}
}
}
.container {
padding: 20px;
}
.container.clear {
clear: both;
}
.container .button {
background: red;
}
Extend keyword
Add extend
keyword to a rule and set any style or array of styles as value to extend some style definition with the current style object. See example.
var rules = {}
var button1 = {
padding: '20px',
background: 'blue'
}
rules['.button-1'] = button1
rules['.button-2'] = {
extend: button1,
padding: '30px'
}
.button-1 {
padding: 20px;
background: blue;
}
.button-2 {
padding: 30px;
background: blue;
}
API
Access the jss namespace
var jss = window.jss
var jss = require('jss')
Create stylesheet
jss.createStylesheet([rules], [named], [attributes])
rules
is an object, where keys are selectors if named
is not truenamed
rule keys are not used as selectors, but as names, will cause auto generated class names and selectors. It will also make class names accessible via stylesheet.classes
.attributes
allows to set any attributes on style element.
var stylesheet = jss.createStylesheet({
'.selector': {
width: '100px'
}
}, {media: 'print'}).attach()
<style type="text/css" media="print" title="Generated by jss.">
.selector {
width: 100px;
}
</style>
Create a stylesheet with namespaced rules.
var stylesheet = jss.createStylesheet({
myButton: {
width: '100px',
height: '100px'
}
}, true).attach()
console.log(stylesheet.classes.myButton)
<style type="text/css" media="screen" title="Generated by jss.">
.jss-0 {
width: 100px;
height: 100px;
}
</style>
Attach stylesheet
stylesheet.attach()
Insert stylesheet into render tree.
stylesheet.attach()
Detach stylesheet
stylesheet.detach()
Remove stylesheet from render tree for performance optimization.
stylesheet.detach()
Add a rule
stylesheet.addRule([selector], rule)
You might want to add rules dynamically.
var button = stylesheet.addRule('.my-button', {
padding: '20px',
background: 'blue'
})
Generated namespace.
In case you have an element reference or you create elements in javascript you might want to write styles and attach them later to the element using a generated class name.
var button = stylesheet.addRule({
padding: '20px',
background: 'blue'
})
document.body.innerHTML = '<button class="' + button.className + '">Button</button>'
Get a rule
stylesheet.getRule(selector)
var rule = stylesheet.getRule('.my-button')
var rule = stylesheet.getRule('myButton')
Add a rules
stylesheet.addRules(rules)
Add a list of rules.
stylesheet.addRules({
'.my-button': {
float: 'left',
},
'.something': {
display: 'none'
}
})
Create a rule without a stylesheet.
jss.createRule([selector], rule)
var rule = jss.createRule({
padding: '20px',
background: 'blue'
})
$('.container').css(rule.style)
Install
npm i jss
Run tests
npm i
open test/index.html
License
MIT