H
H
Hazrat Hajikerimov2017-01-24 21:42:27
css
Hazrat Hajikerimov, 2017-01-24 21:42:27

PostCSS check for class existence &?

Started using PostCSS on a small project, used to use the Less preprocessor.
I often face the task that if an element has such a class, then apply such styles, for example:

.link {
   color: blue;

   // В Less можно использовать амперсанд (&) для решения такой задачи
   &.active {
       color: red; 
   }

}

How to do something like this in PostCSS?
The ampersand in PostCSS is a link to the parent element, and after building the project with Webpack, the above code does not work, the ampersand inserts the name of the parent class.
Here is the result of PostCSS with the postcss-nested plugin:
/* Профиль пользователя */
.app-components-Aside-__user__1gy.app-components-Aside-__wrapper__1n4 {
  height: 42px;
  width: 100%;
  background-color: red;
}

and component render
<div class="app-components-Aside-__user__1gy">
      <div class="app-components-Aside-__user__1gy-wrapper"></div>
</div>

the component itself
import React from 'react';
import { Link } from 'react-router';

/* component styles */
import s from './styles.css';

function Aside() {
    return (
        <aside className={s.root}>
            <div className={s.user}>
                <div className={s.user + '-wrapper'}></div>
            </div>
             
        </aside>
    );
}

export default Aside;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Berezhnoy, 2017-01-24
@hazratgs

Use precss for postcss
https://github.com/jonathantneal/precss

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question