Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
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.
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);
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 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 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.
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.
Jss styles are just plain javascript objects. They map 1:1 to css rules, except of those modified by preprocessors.
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',
// Will result in .container.clear
'&.clear': {
clear: 'both'
},
// Will result in .container .button
'& .button': {
background: 'red'
}
}
}
.container {
padding: 20px;
}
.container.clear {
clear: both;
}
.container .button {
background: red;
}
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, // can be an array of styles
padding: '30px'
}
.button-1 {
padding: 20px;
background: blue;
}
.button-2 {
padding: 30px;
background: blue;
}
// Pure js
var jss = window.jss
// Commonjs
var jss = require('jss')
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) // .jss-0
<style type="text/css" media="screen" title="Generated by jss.">
.jss-0 {
width: 100px;
height: 100px;
}
</style>
stylesheet.attach()
Insert stylesheet into render tree.
stylesheet.attach()
stylesheet.detach()
Remove stylesheet from render tree for performance optimization.
stylesheet.detach()
stylesheet.addRule([selector], rule)
var button = stylesheet.addRule('.my-button', {
padding: '20px',
background: 'blue'
})
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>'
stylesheet.getRule(selector)
// Using selector
var rule = stylesheet.getRule('.my-button')
// Using name, if named rule was added.
var rule = stylesheet.getRule('myButton')
stylesheet.addRules(rules)
Add a list of rules.
stylesheet.addRules({
'.my-button': {
float: 'left',
},
'.something': {
display: 'none'
}
})
jss.createRule([selector], rule)
var rule = jss.createRule({
padding: '20px',
background: 'blue'
})
// Apply styles directly using jquery.
$('.container').css(rule.style)
npm i jss
# print help
jss
# convert css
jss source.css -p > source.jss
To make some realistic assumptions about performance overhead, I have converted bootstraps css to jss. In bench/bootstrap
folder you will find jss and css files. You need to try more than once to have some average value.
In my tests overhead is 10-15ms.
npm i
open test/local.html
MIT
FAQs
A lib for generating Style Sheets with JavaScript.
The npm package jss receives a total of 1,344,951 weekly downloads. As such, jss popularity was classified as popular.
We found that jss demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.