Note: This is a fork of postcss-nested-ancestors.
PostCSS plugin to reference any ancestor selector in nested CSS.
When writing modular nested CSS, &
current parent selector is often not enough.
PostCSS Nested Selectors 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.
.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" */
------------------- ^^^^& {} /* ^^^^& = "" */
}
}
}
}
/* Without postcss-nested-selectors */
.MyComponent
&-part{}
&:hover {
> .MyComponent-part {} /* Must manually repeat ".MyComponent" for each child */
}
}
/* With postcss-nested-selectors */
.MyComponent
&-part{}
&:hover {
> ^&-part {} /* Skip ":hover" inheritance here */
}
}
/* After postcss-nested-selectors */
.MyComponent {
&-part{}
&:hover {
> .MyComponent-part {}
}
/* After postcss-nested */
.MyComponent {}
.MyComponent-part {}
.MyComponent:hover {}
.MyComponent:hover > .MyComponent-part {} /* No ":hover" inheritance here! */
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 Selectors instead replaces special ancestor selectors, makes no use of variable assignment and produces an output ready to be unwrapped with postcss-nested.
$ npm install postcss-nested-selectors
postcss([ require('postcss-nested-selectors') ]);
Type: string
Default: ^&
Ancestor selector pattern (utility option to automatically set both levelSymbol
and parentSymbol
)
Type: string
Default: ^
Define ancestor selector fragment reative to the matching nesting level
Type: string
Default: &
Ancestor selector base symbol