All Projects β†’ toomuchdesign β†’ Postcss Nested Ancestors

toomuchdesign / Postcss Nested Ancestors

Licence: mit
πŸ‘©β€πŸ‘§β€πŸ‘¦ PostCSS plugin to reference any parent/ancestor selector in nested CSS.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Postcss Nested Ancestors

Rucksack
A little bag of CSS superpowers, built on PostCSS
Stars: ✭ 1,861 (+2255.7%)
Mutual labels:  postcss-plugins
react-cli
基于 create-react-app ζ­ε»Ίηš„ε‰η«―θ„šζ‰‹ζžΆ
Stars: ✭ 18 (-77.22%)
Mutual labels:  postcss-plugins
Postcss Cssnext
`postcss-cssnext` has been deprecated in favor of `postcss-preset-env`.
Stars: ✭ 5,388 (+6720.25%)
Mutual labels:  postcss-plugins
postcss-stylestats
[Unmainted] PostCSS plugin for StyleStats.
Stars: ✭ 13 (-83.54%)
Mutual labels:  postcss-plugins
postcss-clip-path-polyfill
PostCSS plugin which add SVG hack for clip-path property to make it work in Firefox
Stars: ✭ 24 (-69.62%)
Mutual labels:  postcss-plugins
postcss-copy
An async postcss plugin to copy all assets referenced in CSS files to a custom destination folder and updating the URLs.
Stars: ✭ 39 (-50.63%)
Mutual labels:  postcss-plugins
Postcss Instagram
πŸ“· PostCSS plugin for using Instagram filters in CSS
Stars: ✭ 111 (+40.51%)
Mutual labels:  postcss-plugins
Postcss Selector Not
PostCSS plugin to transform :not() W3C CSS level 4 pseudo class to more compatible CSS (multiple css3 :not() selectors)
Stars: ✭ 49 (-37.97%)
Mutual labels:  postcss-plugins
postcss-namespace
PostCSS plugin that prefix a namespace to a selector
Stars: ✭ 16 (-79.75%)
Mutual labels:  postcss-plugins
Postcss Sorting
PostCSS plugin to keep rules and at-rules content in order.
Stars: ✭ 438 (+454.43%)
Mutual labels:  postcss-plugins
postcss-if-media
A PostCSS plugin to inline or nest media queries within CSS rules & properties.
Stars: ✭ 35 (-55.7%)
Mutual labels:  postcss-plugins
postcss-gtk
Processes GTK+ CSS into browser CSS
Stars: ✭ 23 (-70.89%)
Mutual labels:  postcss-plugins
Postcss Utilities
PostCSS plugin to add a collection of mixins, shortcuts, helpers and tools for your CSS
Stars: ✭ 293 (+270.89%)
Mutual labels:  postcss-plugins
Stylefmt
stylefmt is a tool that automatically formats stylesheets.
Stars: ✭ 2,123 (+2587.34%)
Mutual labels:  postcss-plugins
Postcss Start To End
PostCSS plugin that lets you control your layout (LTR or RTL) through logical rather than physical rules
Stars: ✭ 18 (-77.22%)
Mutual labels:  postcss-plugins
Postcss Aspect Ratio Mini
A PostCSS plugin to fix an element's dimensions to an aspect ratio
Stars: ✭ 123 (+55.7%)
Mutual labels:  postcss-plugins
usedcss
PostCSS plugin which helps you extract only used styles.
Stars: ✭ 52 (-34.18%)
Mutual labels:  postcss-plugins
Postcss At Rules Variables
PostCss plugin to use CSS Custom Properties in at-rule @each, @for, @if, @else and more...
Stars: ✭ 52 (-34.18%)
Mutual labels:  postcss-plugins
Stencil Postcss
Autoprefixer plugin for Stencil
Stars: ✭ 19 (-75.95%)
Mutual labels:  postcss-plugins
Autoprefixer
Parse CSS and add vendor prefixes to rules by Can I Use
Stars: ✭ 20,001 (+25217.72%)
Mutual labels:  postcss-plugins

PostCSS Nested ancestors Build Status

PostCSS plugin to reference any parent ancestor selector in nested CSS.

Getting ancestor selectors

When writing modular nested CSS, & current parent selector is often not enough.

PostCSS Nested ancestors introduces ^& selector which let you reference any parent ancestor selector with an easy and customizable interface.

This plugin should be used before a PostCSS rules unwrapper like postcss-nested.

See PostCSS docs for examples for your environment.

Ancestor selectors schema

    .level-1 {
|   |   .level-2 {
|   |   |   .level-3 {
|   |   |   |   .level-4 {
|   |   |   |   |
|   |   |   |   --- & {}        /*      & = ".level-1 .level-2 .level-3 .level-4" */
|   |   |   ------- ^& {}       /*     ^& = ".level-1 .level-2 .level-3"          */
|   |   ----------- ^^& {}      /*    ^^& = ".level-1 .level-2"                   */
|   --------------- ^^^& {}     /*   ^^^& = ".level-1"                            */
------------------- ^^^^& {}    /*  ^^^^& = ""                                    */
                }
            }
        }
    }

A real example

/* Without postcss-nested-ancestors */
.MyComponent
    &-part{}
    &:hover {
        > .MyComponent-part {} /* Must manually repeat ".MyComponent" for each child */
    }
}

/* With postcss-nested-ancestors */
.MyComponent
    &-part{}
    &:hover {
        > ^&-part {} /* Skip ":hover" inheritance here */
    }
}

/* After postcss-nested-ancestors */
.MyComponent {
    &-part{}
    &:hover {
        > .MyComponent-part {}
}

/* After postcss-nested */
.MyComponent {}
.MyComponent-part {}
.MyComponent:hover {}
.MyComponent:hover > .MyComponent-part {} /* No ":hover" inheritance here! */

Why?

Currently another plugin - postcss-current-selector - has tried to solve the problem of referencing ancestors selector. It works great, but its approach involves assigning ancestor selectors to special variables to be later processed by a further postcss plugin postcss-simple-vars.

PostCSS Nested ancestors instead replaces special ancestor selectors, makes no use of variable assignment and produces an output ready to be unwrapped with postcss-nested.

Installation

$ npm install postcss-nested-ancestors

Usage

postcss([ require('postcss-nested-ancestors') ]);

Options

placeholder

Type: string Default: ^&

Ancestor selector pattern (utility option to automatically set both levelSymbol and parentSymbol)

levelSymbol

Type: string Default: ^

Define ancestor selector fragment reative to the matching nesting level

parentSymbol

Type: string Default: &

Ancestor selector base symbol

replaceDeclarations (experimental)

Type: boolean Default: false

If this is true then this plugin will look through your declaration values for the placeholder symbol and replace them with specified selector.

An use case for this if enabling postcss-ref to work with dynamic @ref selectors. Read discussion here.

/* Before */
.foo {
    &:last-child {
        border-top: ref(^&, border-bottom);
    }
}

/* After PostCSS Nested ancestors and PostCSS Nested */
.foo {
}

.foo:last-child {
    border-top: ref(.foo, border-bottom);
}

Known issues

Multiple ancestor placeholders in same selector

This plugin currently fails when trying to replace more than one different ancestor pleceholder in a single rule selector. This scenario has not been considered in order to not bloat the code with a remote use case.

More precisely, all ancestor placeholders are replaced, but processed as if they where the equal to the first ancestor placeholder found in selector.

In general, do not use more than one ancestor placeholder in a single rule selector. Anyway, this use case can be rewritten by splitting the selectors in multiple nested rules (see edge case 2).

Edge case 1 (success)

/* 2 equal ancestor placeholders in single rule selector */
.a{
    &:hover{
        ^&^&-b{}
    }
}

/* Output: It works but casts a warning */
.a{
    &:hover{
        .a.a-b{}
    }
}

Edge case 2 (failing)

/* 2 different ancestor placeholders in single rule selector */
.a{
    &-b{
        &:hover{
            /* Will be processed as ^&^&-c{}, sorry! */
            ^&^^&-c{}
        }
    }
}

/* Wrong output: All placeholder replaced with the value of the first one */
.a{
    &-b{
        &:hover{
            /* Expected output: .a-b.a-c{}*/
            .a-b.a-b-c{}
        }
    }
}

/* This use case can be rewritten as: */
.a{
    &-b{
        &:hover{
            ^&{
                &^^^&-c{}
            }
        }
    }
}

Replace declaration values in complex nesting scenarios

replaceDeclarations options used in a complex nesting scenario might have undesired outputs because of the different nature of CSS selectors and and declaration values.

In general, avoid replacing declaration values when inside a rule with multiple selectors (but why should you?). In other words don't get yourself into trouble!

Here is an example of what you don't want to do.

/* Don't replace declaration value inside multiple selector rules */
.a1,.a2
    { &:hover
        { &:before
            { content: "^^&";
        }
    }
}

/* Output */
.a1,.a2{
    &:hover {
        &:before {
            content: ".a1,.a2";
        }
    }
}

Contributing

Contributions are super welcome, but please follow the conventions below if you want to do a pull request:

  • Create a new branch and make the pull request from that branch
  • Each pull request for a single feature or bug fix
  • If you are planning on doing something big, please discuss first with @toomuchdesign about it
  • Follow current code formatting
  • Update tests (test.js) covering new features

Todo's

  • Better comment source code
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].