GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - postcss/postcss-js: PostCSS for React Inline Styles, Free Style and other CSS-in-JS

BranchesTags
Open more actions menu

Latest commit

 

History

197 Commits
197 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
test
test
 
 
.devcontainer.json
.devcontainer.json
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
.npmignore
.npmignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
async.d.ts
async.d.ts
 
 
async.js
async.js
 
 
eslint.config.mjs
eslint.config.mjs
 
 
index.d.mts
index.d.mts
 
 
index.d.ts
index.d.ts
 
 
index.js
index.js
 
 
index.mjs
index.mjs
 
 
objectifier.d.ts
objectifier.d.ts
 
 
objectifier.js
objectifier.js
 
 
package.json
package.json
 
 
parser.d.ts
parser.d.ts
 
 
parser.js
parser.js
 
 
pnpm-lock.yaml
pnpm-lock.yaml
 
 
process-result.d.ts
process-result.d.ts
 
 
process-result.js
process-result.js
 
 
sync.d.ts
sync.d.ts
 
 
sync.js
sync.js
 
 

Repository files navigation

PostCSS JS

PostCSS for CSS-in-JS and styles in JS objects.

For example, to use Stylelint or RTLCSS plugins in your workflow.


  PostCSS JS Parser is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.


Usage

Processing

const postcssJs = require('postcss-js')
const autoprefixer = require('autoprefixer')

const prefixer = postcssJs.sync([autoprefixer])

const style = prefixer({
  userSelect: 'none'
})

style //=> {
//     WebkitUserSelect: 'none',
//        MozUserSelect: 'none',
//         msUserSelect: 'none',
//           userSelect: 'none'
//   }

Compile CSS-in-JS to CSS

const postcss = require('postcss')
const postcssJs = require('postcss-js')

const style = {
  'top': 10,
  '&:hover': {
    top: 5
  }
}

postcss()
  .process(style, { parser: postcssJs })
  .then(result => {
    result.css //=> top: 10px;
    //   &:hover { top: 5px; }
  })

Compile CSS to CSS-in-JS

const postcss = require('postcss')
const postcssJs = require('postcss-js')

const css =
  '--text-color: #DD3A0A; @media screen { z-index: 1; color: var(--text-color) }'
const root = postcss.parse(css)

postcssJs.objectify(root) //=> {
//     '--text-color': '#DD3A0A',
//     '@media screen': {
//       zIndex: '1',
//       color: 'var(--text-color)'
//     }
//   }

API

sync(plugins): function

Create PostCSS processor with simple API, but with only sync PostCSS plugins support.

Processor is just a function, which takes one style object and return other.

async(plugins): function

Same as sync, but also support async plugins.

Returned processor will return Promise.

parse(obj): Root

Parse CSS-in-JS style object to PostCSS Root instance.

It converts numbers to pixels and parses [Free Style] like selectors and at-rules:

{
    '@media screen': {
        '&:hover': {
            top: 10
        }
    }
}

This methods use Custom Syntax name convention, so you can use it like this:

postcss().process(obj, { parser: postcssJs })

objectify(root): object

Convert PostCSS Root instance to CSS-in-JS style object.

Troubleshoot

Webpack may need some extra config for some PostCSS plugins.

Module parse failed

Autoprefixer and some other plugins need a json-loader to import data.

So, please install this loader and add to webpack config:

loaders: [
  {
    test: /\.json$/,
    loader: 'json-loader'
  }
]

About

PostCSS for React Inline Styles, Free Style and other CSS-in-JS

Resources

Readme
MIT license
Activity
Custom properties

Stars

668 stars

Watchers

14 watching

Forks

24 forks
Report repository

Sponsor this project

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.