Giter Site home page Giter Site logo

trevorblades / transform-css Goto Github PK

View Code? Open in Web Editor NEW
12.0 3.0 4.0 16.83 MB

๐Ÿ  Transforms CSS into beautifully formatted styles ready to use with a preprocessor

Home Page: https://transform-css.trevorblades.com

JavaScript 97.79% HTML 2.21%
css less sass stylus transform

transform-css's Introduction

Transform CSS

Build Status

This library transforms CSS into beautifully formatted styles ready to use with a preprocessor like LESS, SASS, or Stylus. Check out a working example here!

Installation

$ npm install transform-css

Usage

transformCss(css, options)

Takes a string of CSS as input and returns a string of LESS/SASS. Descendant selectors, subclasses, and pseudo-classes will be nested where it makes sense. Sets of rules will be spaced out for increased readability. Pass options as a second argument to customize the format of the returned styles.

import transformCss from 'transform-css';

const css = `
  #id {
    width: 420px;
    color: green;
  }

  #id .child-class {
    overflow: hidden;
  }

  #id .child-class p {
    margin: 0 1em;
  }

  #id .child-class p:last-child {
    margin-bottom: 0;
  }
`;

transformCss(css);

This will produce the following output:

#id {
  width: 420px;
  color: green;

  .child-class {
    overflow: hidden;

    p {
      margin: 0 1em;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

Options

spaces

By default, two spaces will be used as indentation for the transformed styles. Use the spaces option to change this behaviour.

Specify a number to control the number of spaces used, or false to use tabs.

omitBracketsAndSemicolons

By default, transform-css will produce code that follows LESS or SCSS syntax, which use brackets to separate selectors from rule declarations and semicolons to separate declarations from one another.

#container {
  height: 50px;
  color: red;

  .header {
    position: sticky;
    top: 24px;
  }
}

On the other hand, SASS and Stylus use newlines and tabs to separate stylesheet elements from one another. Set the omitBracketsAndSemicolons option to true to use this syntax.

transformCss(css, {omitBracketsAndSemicolons: true})

Turns into...

#container
  height: 50px
  color: red

  .header
    position: sticky
    top: 24px

transform-css's People

Contributors

dependabot[bot] avatar dr-dimitru avatar trevorblades avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

transform-css's Issues

Selector lost when block applies to two selectors with comma

Hey,

The following generates wrong nested Less:

.arg-header,
.social-header{
    padding: 40px 0 20px;
}


.arg-header h2,
.social-header h2{
    font-size: 28px;
}
.social-header {
  padding: 40px 0 20px;
  h2 {
    font-size: 28px;
  }
}

.arg-header should be preserved.

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.