3.2.0 • Published 8 months ago

@cssfn/cssfn v3.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

cssfn - Writes CSS in JavaScript way

A lib for generating Style Sheet (css) with JavaScript function.
Similar to React Hooks but for generating css dynamically.

By underlying JavaScript language, the css can be easily exported/imported as a regular JavaScript module.

The generated css can be exported to css toString() or attached to DOM directly .attach().

Preview

export const usesAwesomeButton = () => composition([
    imports([
        stripoutControl(), // clear browser's default styles
        
        usesButtonBase(), // imports css from a generic button
        
        // imports any stuff here...
    ]),
    layout({
        display       : 'flex',
        flexDirection : 'row',
        background    : 'pink',
        color         : 'darkred',
        
        // writes the css declaration similar to regular css
        
        ...children(['span', '.logo'], [ // target to <span> and class="logo"
            imports([
                // imports any stuff here...
            ]),
            layout({
                // writes the css declaration similar to regular css
            }),
        ]),
    }),
    variants([
        rule('.big', [
            layout({
                fontSize: 'xx-large',
                // ....
            }),
        ]),
        rule('.dark', [
            // ...
        ]),
    ]),
    states([
        rule([':disabled', '.disabled'], [
            // ....
        ]),
    ]),
]);

// attach the css to DOM:
createSheet(() => [
    globalDef([
        rule('.awesome-btn', [
            imports([
                usesAwesomeButton(),
            ]),
        ]),
        rule('.awesome-checkbox', [
            // ....
        ]),
        // ....
    ]),
])
.attach();

Then we can consume our generated css like this:

<script src="button-style.js">
</script>

<button class="awesome-btn">Awesome!</button>

Features

  • Includes all Vanilla & ES6 JavaScript features.
  • Lazy execution (your function will be executed on demand).
  • Cached - your function only be executed once (or never if not needed).
  • IntelliSense supported - Our cssfn is written in TypeScript (superset of JavaScript).
  • CSS Variable Management - Never write variable name in plain string.
  • CSS Config Management - Shares a common setting to many components.

Installation

Using npm:

npm i @cssfn/cssfn

Support Us

If you feel our lib is useful for your projects,
please make a donation to avoid our project from extinction.

We always maintain our projects as long as we're still alive.

[Make a donation]

3.2.0

8 months ago

3.0.2

10 months ago

3.1.0

10 months ago

3.0.0

1 year ago

2.5.1

1 year ago

2.4.1

1 year ago

2.5.0

1 year ago

2.2.0

1 year ago

2.4.0

1 year ago

2.0.33

1 year ago

2.3.0

1 year ago

2.3.1

1 year ago

2.1.0

1 year ago

2.0.28

1 year ago

2.0.29

1 year ago

2.0.15

2 years ago

2.0.16

2 years ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.31

1 year ago

2.0.32

1 year ago

2.0.30

1 year ago

2.0.19

2 years ago

2.0.17

2 years ago

2.0.18

2 years ago

2.0.26

1 year ago

2.0.27

1 year ago

2.0.24

1 year ago

2.0.25

1 year ago

2.0.22

1 year ago

2.0.23

1 year ago

2.0.20

2 years ago

2.0.21

2 years ago

2.0.11

2 years ago

2.0.12

2 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.8

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.12

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago