Home

Awesome

Build Status npm semantic-release style: styled-components

:gem:Styless:gem:

Styless enables less syntax in your styled-components

Installation

$ yarn add --dev babel-plugin-styless

.babelrc

{
  "plugins": ["babel-plugin-styless"]
}

Note that styless should appear before babel-plugin-styled-components if used.

Key features

    @size: if(@small, 4px, 10px);
    color: darken(@highlight, 5%);
There is no need to import `darken`.
    color: hsv(90, 100%, 50%);
    const Button = styled.button`
        @highlight: blue;                           // can be overwritten by theme or props
        background: darken(@highlight, 5%);         // make green darken by 5%
    `;
    <ThemeProvider theme={{highlight: "red"}}>
        <Button highlight="green">click me</Button> // green (set in props) overwrites red (set in theme)
    </ThemeProvider>
    const Button = styled.button`
        @import (reference) "variables";
        .bg-light-blue;
    `;
    <button css="color: @color;"/>
    <button css={`color: @color;`}/>
    <button css={css`color: @color;`}/>
    `${props => props.main}`

Your first Styless component

const Button = styled.button`
    @main: palevioletred;
    @size: 1em;
    
    font-size: @size;
    margin: @size;
    padding: @size / 4 @size;
    border-radius: @size / 2;
    
    color: @main;
    border: 2px solid @main;
    background-color: white;
`;
<Button>Normal</Button>
<Button main="mediumseagreen">Themed</Button>
<Button main="mediumseagreen" size="1.5em">Themed large</Button>

This is what you'll see in your browser :tada:, play with it on codesandbox

Advanced Styless component example

const Button = styled.button`
    @faded: fade(black, 21%);
    @size: if(@small, 4px, 10px);
    cursor: pointer;
    cursor: if(@disabled, not-allowed);
    color: hsv(0, 0%, 99%);
    padding: @size @size * 3;
    border: 1px solid @faded;
    border-bottom: 4px solid @faded;
    border-radius: ${4}px;
    text-shadow: 0 1px 0 @faded;
    background: linear-gradient(@highlight 0%, darken(@highlight, 5%) 100%);
    &:active {
        background: darken(@highlight, 10%);
    }
`;
// Notice that the @highlight variable is resolved from the theme, and overwritten from a props in the second button.
<ThemeProvider theme={{highlight: "palevioletred"}}>
    <Button>Click me</Button>
    <Button highlight="hsl(153, 100%, 33%)">Or me</Button>
    <Button disabled small>But not me</Button>
</ThemeProvider>

This is what you'll see in your browser :tada:, play with it on codesandbox

Note that with webstorm-styled-components, we get syntax highlighting, color preview and ctrl+click access to variables!

FAQ

const Button = styled.button`
  ${hover}
  color: red;
`;
[
  "styless",
  {
    "cwd": "babelrc",
    "import": "../less/common.less"
  }
]
[
  "styless",
  {
    "import": "~antd/lib/style/themes/default.less",
    "lessOptions": {
      "javascriptEnabled": true
    }
  }
]