Giter Site home page Giter Site logo

component-css-extractor's Introduction

๐Ÿ‘จโ€๐ŸŽจ component-css-extractor

Extract CSS from any scoped styles in a component framework. In the browser.

Try it in a CodeSandbox ๐Ÿ‘‡

CodeSandbox thumbnail

How to use

$ yarn add component-css-extractor

To extract CSS from any component, use the extractCSS method with a DOM node reference.

import extractCSS from 'component-css-extractor'

// ...
const styles = extractCSS(node);

Tested with React and styled-componets, but should work on anything that renders to the DOM and puts class-based CSS in <style> tags in your HTML header. Works on CodeSandbox and create-react-app. Probably others. โœŒ๏ธ

Happy CSS extracting! โค๏ธ

PS: ๐Ÿ‘จโ€๐ŸŽจ component-css-extractor won't work with server-side-rendering. This is purely a client-side library. There are plenty of built-in solutions for the server.

But why?

I built this for a project where I needed to export parts of my UI to raw HTML. Getting the HTML is easy, but you need styles as well or it doesn't work that great.

Turns out extracting the styles isn't so trivial. Styled-components supports it when you're doing server-side-rendering, but not in the browser.

Using element.getComputedStyle works okay, but produces too much code. You get values for every CSS property in the standard, not just what you defined.

If you wanna see all the approaches I tried before landing on this one, you can watch this video.

Contributions welcome :)

MIT License

component-css-extractor's People

Contributors

swizec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

component-css-extractor's Issues

Extract scoped media query styles

Hi @Swizec, great job on this library! Very helpful here!

Is there a way we can extract the media queries styles as well? My specific need here is the media print.

Thanks!
Henry

Bookmarklet

Hi!

Please, do you know how to convert this project into a bookmarklet?

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.