Documentation

JSSheet logo

What is JSSheet

⚠️NOTE this is a prototype project and still a WIP - consider to test it before using in production!
JSSheet is a new way to create stylesheets for your websites, it uses JavaScript to create minified CSS files. The goal is to handle styles and logic with a single language, and to create some cool shortcuts to handle complex flexbox and css grids layouts

How does it works

The logic behind is pretty simple, we define a main .js file called styles.js, wich exports a stylesheets object:
/**
* HELLO WORLD EXAMPLE
*
* @author nicolacastellanidev@gmail.com
* @desc the root stylesheet, don't rename stylesheets export
*/

const { generics } = require('./partials/generics');

/**
* Core stylesheet
*/
const stylesheets = {
  root: {
    selector: "body",
    padding: 0,
    margin: 0,
    width: '100vw',
    height: '100vh',
    align: 'center column',
    children: [
      ...generics
    ]
  }
};

module.exports = {
  stylesheets,
};
          
As you can see the main stylesheets component defines a :root selector, wich is the starting point for JSSheet.

JSSheet advantages

JSSheets PROS

  1. Unique styling and logic language
  2. Simple-to-recycle variables and methods
  3. Layout and other css shortcuts!
  4. Style documentation with js-docs syntax (or custom)
  5. Better code completition with custom or built in Javascript IDE plugin
  6. Custom css shortcuts creations by gulpfile custom edits

Hello JSSheet!

js-sheet hello world tutorial
This is a simple introductive tutorial to start with JSSheet, you will see how to install the dependencies and start the parser.

Step 0. prerequisites

You need to install NodeJS and Gulp to use JSSheet, yarn is optional, but suggested as default package manager.

Step 1. clone the repository and install dependencies

Clone the repository:
git clone https://github.com/NicolaLC/js-sheet.git
Navigate to the hello-world tutorial:
cd js-sheet/tutorials/hello-world
Install dependencies:
Note: if yarn is not installed run npm i -g yarn
yarn 
Run the parser and the server:
yarn dev 
Now the example is running @ http://localhost:4040, you can copy-cat this example to start your own website!

The main styles.js file

The project has a jss core folder, in there we can define your JSS styles starting from the styles.js core file. The main styles.js file is the starting point of any JSS based application:
const stylesheets = {
  root: {
    selector: "body",
    ...bodyProps
    children: [...childrens]
  }
};

module.exports = {
  stylesheets
};
            
As you can see it defines a root selector, wich is the main selector to starts with, and exports a stylesheets variable wich will be used for JSS parsing. Remember to start from this file, and include (by imports) your partials!

Partials

The partials are separated JSS files to use for separate stylings, for example if you have different layout parts (header, body, footer) you can separate their styles into partials:
/// Get partials
const { body } = require("./partials/body");
const { header } = require("./partials/header");
const { footer } = require("./partials/footer");
const stylesheets = {
  root: {
    selector: "body",
    ...bodyProps
    children: [
      body,
      header,
      footer
    ]
  }
};

module.exports = {
  stylesheets
};
            
By this way you can organize your styles into different files, to improve your styles readability.

Partial example

A partial file is the same of the styles file, except for the root selector, wich must be defined inside the core styles.js file:
/**
* HEADER PARTIAL
*/

const header = {
  selector: ".Header",
  ...headerProps,
  children: [
    ...headerChildrens
  ]
};

module.exports = {
  header
};

            
As you can see this partials is the same of the main styles.js file, except for root selector. Remember to include your partials into the styles.js file to see your properties in action.

Shared variables and methods

With JSSheet you can define global variables and methods to recycle the same code and properties between partials. So like advanced css or precompiled css you can define global variables and methods (mixins in scss).

Global shared Variables

For example we want to define a global colors variable mapping, so we can create a variables.js file (name as you like), and import it in your partials:
-- variables.js
[...]
const colors = {
  primary: "#323232",
  secondary: "lawngreen",
  tertiary: "rgba(255,255,255,1)",
  dark: "rgb(0,0,0)"
};
[...]
module.exports = {
  colors,
  otherVars
};
As you can see we have defined and exported a colors variable in your variables.js file. Now we can use this variables in your JSSheets:
-- styles.js
[...]
// import the variables first in your stylesheet
const {colors} = require('path/to/variables.js');
[...]
// then use it in your styles:
const stylesheets = {
  root: {
    selector: "body",
    backgroundColor: colors.primary
    [...]
  }
};
[...]
By this way you can edit the colors once in your variables.js file to propagate the edit to all the stylesheets!

Global shared Methods

You can define global (or local) shared utilities methods like the variables. This could be useful to handle custom global behaviors. For example, we want to define a widthByPadding method, wich returns a dynamic css property:
const widthByPadding = (padding) => `calc(100% - ${padding}*2)`;
This method returns a dynamic css property, a 100% width minus the padding passed. We can use this method in our styles:
[...]
width: widthByPadding('1rem'),
[...]
You can now define a global shared file (e.g. mixins.js) wich defines and exports the global method, and reuse it in your stylesheets!

The align property shortcut

In JSSheet you can define flexbox driven layout in a seconds with the align property shortcut!
The align property shortcut lets you define complex or simple flexbox layouts:
align: 'center';
This shortcut will create a flexbox layout item:

align: 'center'
Cool, I'm on center!
align: 'center'

Cool, I'm on center!


Me too!
align: 'center column'

Cool, I'm on center!


Me too! but in column!

Property CSS relative code
row flex-direction: row
column flex-direction: column
top align-items: flex-start
bottom align-items: flex-end
center align-items: center + justify-content: center
left justify-content: flex-start
right justify-content: flex-end

The gridTemplate shortcut

Like the align shortcut, you can define grid layout by the gridTemplate shortcut:
gridTemplate: {
  columns: 'repeat( 33.33%, minmax(250px, 1fr) );'
  rows: 'repeat( 33.33%, minmax(250px, 1fr) );'
},
Result:

1-1

1-2

1-3

2-1

2-2

2-3

3-1

3-2

3-3

camelCased properties

In JSSheet you can use any kind of css property by double quotes wrap, but some of them are converted in camelCased format to simplify the sheet definition and to prevent lint errors on JSON objects.

camelCased properties

Property CSS relative code
fontFamily font-family
fontSize font-size
fontWeight font-weight
overflowX overflow-x
overflowY overflow-y
zIndex z-index
boxShadow box-shadow
textShadow text-shadow
whiteSpace white-space
borderBottom border-bottom
borderTop border-top
borderLeft border-left
borderRight border-right
letterSpacing letter-spacing
maxWidth max-width
maxHeight max-height
textAlign text-align
textTransform text-transform
backgroundColor background-color
textDecoration text-decoration

Note: each of this properties could be defined with the classic syntax, e.g "text-decoration"
mobile
tablet
desktop