All Projects β†’ porsager β†’ Bss

porsager / Bss

Licence: wtfpl
🎨 Better Style Sheets

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Bss

smoothie
A deliciously scalable and adaptable stylesheet methodology 🍹
Stars: ✭ 27 (-62.5%)
Mutual labels:  stylesheets, css-in-js
gloss
a powerful style system for building ui kits
Stars: ✭ 16 (-77.78%)
Mutual labels:  stylesheets, css-in-js
Cssobj
Runtime CSS manager, Turn CSS into dynamic JS module, Stylesheet CRUD (Create, Read, Update, Delete) in CSSOM, name space (local) class names
Stars: ✭ 253 (+251.39%)
Mutual labels:  css-in-js, stylesheets
Jss
JSS is an authoring tool for CSS which uses JavaScript as a host language.
Stars: ✭ 6,576 (+9033.33%)
Mutual labels:  css-in-js, stylesheets
Snackui
SnackUI πŸ‘ - the final React style library. With an *optimizing compiler* that lets you write views naturally, with easier DX, working on native and web at once, all while being faster than hand-rolling your own CSS.
Stars: ✭ 55 (-23.61%)
Mutual labels:  css-in-js
Postjss
Use the power of PostCSS in compiling with JSS
Stars: ✭ 40 (-44.44%)
Mutual labels:  css-in-js
Beamwind
a collection of packages to compile Tailwind CSS like shorthand syntax into CSS at runtime
Stars: ✭ 32 (-55.56%)
Mutual labels:  css-in-js
Theme Notes First
A Notes-First StyleSheet for Taskpaper
Stars: ✭ 28 (-61.11%)
Mutual labels:  stylesheets
Immutable Styles
A library for styling web interfaces with a focus on predictability and robustness. It uses immutability to remove side effects often tied to CSS, allowing UI bugs to be caught ahead of time!
Stars: ✭ 69 (-4.17%)
Mutual labels:  css-in-js
Vue Styled Components
Visual primitives for the component age. A simple port for Vue of styled-components πŸ’…
Stars: ✭ 1,114 (+1447.22%)
Mutual labels:  css-in-js
Styled Theming
Create themes for your app using styled-components
Stars: ✭ 1,067 (+1381.94%)
Mutual labels:  css-in-js
Emotion Module
πŸ’– Emotion module for Nuxt.js
Stars: ✭ 47 (-34.72%)
Mutual labels:  css-in-js
Cabana React
A design system built especially for both Sketch and React. πŸ’Žβš›οΈ
Stars: ✭ 58 (-19.44%)
Mutual labels:  css-in-js
Cattous
CSS in JSX for lazy developers, built using styled-components and styled-system
Stars: ✭ 38 (-47.22%)
Mutual labels:  css-in-js
Rosebox
CSS in Typescript
Stars: ✭ 62 (-13.89%)
Mutual labels:  css-in-js
Osmm Topography Layer Stylesheets
SQL scripts & cartographic stylesheets for OS MasterMap Topography Layer
Stars: ✭ 32 (-55.56%)
Mutual labels:  stylesheets
Twind
The smallest, fastest, most feature complete Tailwind-in-JS solution in existence.
Stars: ✭ 1,051 (+1359.72%)
Mutual labels:  css-in-js
Css In Js Utils
Useful utility functions for CSS in JS solutions
Stars: ✭ 61 (-15.28%)
Mutual labels:  css-in-js
Bankai
πŸš‰ - friendly web compiler
Stars: ✭ 1,064 (+1377.78%)
Mutual labels:  css-in-js
Treat
🍬 Themeable, statically extracted CSS‑in‑JS with near‑zero runtime.
Stars: ✭ 1,058 (+1369.44%)
Mutual labels:  css-in-js

version gzipped license

🎨 bss

A simpler way to do CSS in Javascript directly on the elements you're styling.

bss allows you to write your styles directly on the element where they matter using plenty of fun constructors.

Any group of definitions are resolved once one of the following properties are accessed / called

Installation

Using npm

npm install bss -S

Require from CDN

<script src="https://unpkg.com/bss"></script>

Or download and include - download bss.js

Example usage

Here's a quick example of using bss together with mithril - see the example live here

// Some js file
import b from 'bss'
import m from 'mithril'

let on = true

m.mount(document.body, {
  view: vnode => m('h1' + b.bc('red').c('white').fs(32).ta('center'), {
    style: b.bc(on && 'green').style,
    onclick: () => on = !on
  }, 'Hello world')
})

Creates the following in the dom, which toggles the style attribute on click.

<style>
  .bdp4f3o1 {
    background-color: red;
    color: white;
    font-size: 32px;
  }
</style>

<body>
  <h1 class="bdp4f3o1" style="background:green;">Hello world</h1>
</body>

Ways of writing CSS

In the spirit of Javscript - bss Allows you to write the same thing in many different ways.

CSS Strings

b`
  background-color: black;
  text-align: center;
`
.$hover(`
  background-color: red;
`)

Lean Strings

b`
  background-color black
  text-align center
`
.$hover`
  background-color red
`

JS Objects

b({
  backgroundColor: 'black',
  textAlign: 'center',
  ':hover': {
    backgroundColor: 'red'
  }
})

Functions

b.backgroundColor('black')
 .textAlign('center')
 .$hover(
   b.backgroundColor('red')
 )

Output

.class

The .class getter closes the current style description, creates a class name, adds the styles to a stylesheet in <head> and returns the class name

b.textAlign('center').class // Returns eg. bdp4f3o1

.style

The .style getter also closes the current style description and return a JS object with the styles

b.textAlign('center').style // Returns { textAlign: 'center' }

valueOf()

.valueOf() will be called if b is used like 'div' + b because javascript casts automatically using .valueOf(). Casting b to a string will call .class and prepend a period for easy use in vdom libraries.

'div' + b.textAlign('center') // Returns eg. div.bdp4f3o1

You can also override .valueOf if you set classNames instead of selectors

b.valueOf = function() { return this.class + ' ' }

`<div class="${ b.textAlign('center') }`"></div>` // Returns eg. <div class="bdp4f3o1"></div>

Short property names

Short property names can also be used and are the acronyms of full css properties with collisions handpicked by popularity

b.bc('black')
 .ta('center')
 .$hover(
   b.bc('red')
 )

.helper

Define your own helpers to work in a fashion similar to tachyons, or simply to make your life easier.

Tachyon style helpers

b.f1.p1 // { font-size: '3rem'; padding: '0.25rem'; }

// Created like this:
b.helper('f1', b.fontSize('3rem'))
b.helper('p1', b.padding('0.25rem'))

// Or like this:
b.helper({
  f1: b.fontSize('3rem'),
  p1: b.padding('0.25rem')
})

Helpers can also take values like this:

b.size('100%').align('center') // Fills an element in it's parent and centers all children on both axes.

// Created like this:
b.helper('size', (width, height) =>
  b.width(width).height(height || width)
)

b.helper('align', (x, y) =>
  b.display('flex').justifyContent(x).alignItems(y || x)
)

They can even be easy to use media query groupers like this:

b.desktop(
  b.fontSize(128)
)

// Equally valid is 
b.desktop(`
  fs 128
`)

// Created like this:
b.helper('desktop', style => b.$media('(min-width:801px)', style))

Helpers can be called in a couple different ways:

b.helper('size', (width, height) =>
  b.width(width).height(height || width)
)

// Can be invoked by css/lean strings:
b`
  color green // you can mix in normal styles
  size 50 100
`

// Or functions:
b.color('green').size(50, 100)

When invoking a helper through a css/lean string arguments are converted to strings, whereas functions can accept object arguments.

Pixel values and Numbers

Properties accepting pixel values will automatically have px added if a number type is passed.

b.fontSize(32) // font-size: 32px;
b.width(200) // width: 200px;

.$hover :pseudo selectors

All of the different css pseudo selectors normally used with a colon : is added with the dollor $ for ease of use in js.

b(`
  color: red;
`).$hover(`
  color: blue;
`)

b.color('red').$hover(b.color('blue'))

.$nest nested selectors

Targeting nested children is sometimes useful, and is done by using $nest and supplying the first argument with a regular child css selector, and then supply styling as the second argument for that selector.

b.color('red').$nest('li', b.color('blue'))
b.color('red').$nest(':hover li', b.color('blue'))
b.color('red').$nest('> &', b.margin(10))

& is a placeholder for the generated class like in sass/less. Once you use & in $nest you take full responsibility of adding the generated class in the correct places to avoid creating global selectors. One case where this could happen is if using the comma seperator to make multiple selectores, like doing $nest('th &, tr', ...). This would create a global tr style. To do this right the code would need to be $nest('th &, tr &, ...).

Normally bss handles this for you such that $nest('th, tr', ...) will be scoped locally, but using & overrides this behavior.

.$media @media queries

b.color('red').$media('(max-width: 600px)', b.color('blue'))

.$keyframes @keyframes

Animation in CSS is usually a mixture of transition and animation / @keyframes. Transition is handled as usual css properties by bss, but @keyframes are a bit different.

Creating a keyframe animation is done using b.$keyframes and will return a generated name for the animation specified.

const fadeIn = b.$keyframes({
  from: b.o(0).style,
  to: b.o(1).style
})

// To use the animation do:
b.animation(fadeIn + ' 1s')

.$animate

Often it might not be necessary to consider the animation name so a built in helper method called $animate comes in handy. It takes a regular animation shorthand value as the first argument, excluding the animation name. The second parameter is the animation definition itself.

b.$animate('1s linear', {
  from: b.o(0).style,
  to: b.o(1).style
})

.css

A way to add regular css properties to a selector and prepend to the generated stylesheet

b.css('html', b.boxSizing('border-box'))
b.css('*, *:before, *:after', b.boxSizing('inherit'))

.setDebug

Since chrome dev tools doesn't allow changing styles applied using CSSOM you can activate debugging mode which doesn't apply the styles using CSSOM.

b.setDebug(true)

The only caveat here is that animations and applied styles can result in small quicks like blinking fonts and styles.

Specificity

To enhance developer expectations bss uses double class names for regular definitions to give higher specificity than nested items, which might have rules added from regular css, or from $nest. To support this $nest will not increase specificity by only adding the class name once.

Browser support

bss is tested and works in ie9+ and the other major browsers.

TODO - Create browser support table

Prefixes

When using bss in the browser it automatically adds only the necessary prefixes, so you can go ahead and use the raw property and expect it to work in browsers that only has the prefixed version.

Prefixes for css property values like linear-gradient are not supported yet, so if you want to support older browsers you'll need to add them manually like

b`
  background -webkit-linear-gradient(45deg, red, blue)
  background -moz-linear-gradient(45deg, red, blue)
  background linear-gradient(45deg, red, blue)
  ...etc
`

Server support

TODO - If using it on the server you can specify the prefixes that you'd like to be generated when generating the css.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].