• Stars
    star
    203
  • Rank 192,890 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generating CSS using JS with considerable flexibility and extensibility, at both server side and client side.

css-render · GitHub Liscense npm Total alerts codecov

Generating CSS using JS with considerable flexibility and extensibility, at both server side and client side.

It's mainly built for library builders (who wants make their library work without css import at small overhead). It's not recommend to use it in a webapp.

It is not designed to totally replace other style-related solutions, but to be a progressive tool which can just work as a supplementary of your style files or totally replace your .css files.

Docs

css-render

Why Using It

  1. You want to ship a library without css at a small price (gzip < 2kb).
  2. Reduce size compared with static css (which contains duplicate logic).
  3. You can't write sass-like or less-like css-in-js (eg. mixin in sass or less).
  4. You want to write style variables in JS.
  5. Support an simple SSR API (now only for vue3).

Comparasion with other CSS-in-JS framework

Main differences between css-render and styled-component, jss or emotion:

  1. It doesn't do the bindings between components and styles. It is more like a style generator with low level mount and unmount API.
  2. It's easier to write like a sass mixin or less mixin.

Examples

Realworld Example

Basic Example

$ npm install --save-dev css-render
import CssRender from 'css-render'
/**
 * CommonJS:
 * const { CssRender } = require('css-render')
 */

const {
  c
} = CssRender()

const style = c('body', ({ props }) => ({
  margin: 0,
  backgroundColor: props.backgroundColor
}), [
  c('&.dark', {
    backgroundColor: 'black'
  }),
  c('.container', {
    width: '100%'
  })
])

/** use it as string */
console.log(style.render({ backgroundColor: 'white' }))
/**
 * or mount on document.head. (the following lines only work in the browser.)
 */
style.mount()
// ...
style.unmount()
body {
  margin: 0;
  background-color: white;
}

body.dark {
  background-color: black;
}

body .container {
  width: 100%;
}

BEM Plugin Example

$ npm install --save-dev css-render @css-render/plugin-bem

You can use bem plugin to generate bem CSS like this:

import CssRender from 'css-render'
import bem from '@css-render/plugin-bem'
/**
 * CommonJS:
 * const { CssRender } = require('css-render')
 * const { plugin: bem } = require('@css-render/plugin-bem')
 */

const cssr = CssRender()
const plugin = bem({
  blockPrefix: '.c-'
})
cssr.use(plugin) // bind the plugin with the cssr instance
const {
  cB, cE, cM
} = plugin

const style = cB(
  'container',
  [
    cE(
      'left, right', 
      {
        width: '50%'
      }
    ),
    cM(
      'dark', 
      [
        cE(
          'left, right',
          {
            backgroundColor: 'black'
          }
        )
      ]
    )
  ]
)

/** use it as string */
console.log(style.render())
/**
 * or mount on document.head
 * the following lines only works in browser, don't call them in node.js
 */
style.mount()
// ...
style.unmount()
.c-container .c-container__left, .c-container .c-container__right {
  width: 50%;
}

.c-container.c-container--dark .c-container__left, .c-container.c-container--dark .c-container__right {
  background-color: black;
}

Packages

Name Cov
css-render codecov
@css-render/plugin-bem codecov
vue3-ssr codecov

More Repositories

1

xicons

SVG Vue/React components integrated from fluentui-system-icons, ionicons, ant-design-icons, material-design-icons, Font-Awesome, tabler-icons and carbon icons. (Vue3, Vue2, React, SVG)
JavaScript
1,264
star
2

vueuc

Util Components for Vue
TypeScript
266
star
3

treemate

Manipulating tree data structure for user interface
TypeScript
148
star
4

vfonts

CSS
72
star
5

lyla

A fully typed HTTP client with explicit behavior & error handling.
TypeScript
64
star
6

naive-ui-nuxt-demo

Vue
52
star
7

vooks

Utils Composable for Vue
TypeScript
47
star
8

naive-ui-vite-ssr

An example of naive-ui vite ssr.
JavaScript
36
star
9

evtd

Event delegation with native events and extended events.
TypeScript
23
star
10

component-tree-data-share

关于组件库树形数据的一次分享
22
star
11

seemly

TypeScript
21
star
12

nuxtjs-naive-ui

The official nuxt module for naive-ui that supports Nuxt.js SSR.
TypeScript
17
star
13

event-tracker-wrapper

Event tracker wrapper.
TypeScript
15
star
14

vdirs

Helper directives for Vue
TypeScript
14
star
15

vue3-programming-share

CSS
7
star
16

misclint

TypeScript
6
star
17

maintainable-vue-project-share

Vue
5
star
18

vue3-programming-share-2

CSS
5
star
19

naive-ui-vitepress-demo

This is a demo for using `naive-ui` in `vitepress` with SSR enabled.
TypeScript
4
star
20

demoup

TypeScript
3
star
21

irw

TypeScript
3
star
22

store4mp

A very basic data store manager library for mini program (mainly for cross-page usage)
TypeScript
3
star
23

v2s

Convert vue to script.
JavaScript
3
star
24

sstimer

A super simple timer that can start, stop & fire finish callback.
TypeScript
3
star
25

boom

JavaScript
2
star
26

07akioni

2
star
27

naming-convention-types

TypeScript
2
star
28

figma-plugin-image-info

JavaScript
2
star
29

create-merged-request

Merge scattered requests into one, distribute corresponding result to requestor.
TypeScript
2
star
30

maybe-a-bug-of-volar

Vue
1
star
31

dag-maker

A frontend for dagre & dagre-d3 that draws elegant DAG.
JavaScript
1
star
32

karma-boilerplate

JavaScript
1
star
33

taro-example

JavaScript
1
star
34

vue-create-ssr-app-issue

JavaScript
1
star
35

pkuhole-fetcher-pwa

An PWA for pkuhole-fetcher.
JavaScript
1
star
36

tailwind-color-interpolation

Linear interpolation color set for tailwind CSS
HTML
1
star
37

tex-assignment-template

TeX
1
star