Socket
Socket
Sign inDemoInstall

postcss-custom-selectors

Package Overview
Dependencies
7
Maintainers
4
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    postcss-custom-selectors

Use Custom Selectors in CSS


Version published
Maintainers
4
Install size
2.15 MB
Created

Package description

What is postcss-custom-selectors?

The postcss-custom-selectors npm package is a plugin for PostCSS, a tool for transforming CSS with JavaScript. This package allows developers to define custom selectors in their CSS, which can be reused throughout the stylesheet. This helps in writing more maintainable and readable CSS by reducing repetition and promoting clearer structure.

What are postcss-custom-selectors's main functionalities?

Defining Custom Selectors

This feature allows you to define a custom selector, :--heading, which targets h1, h2, and h3 tags. Any style rules applied to :--heading will be applied to these elements, making it easier to manage styles for all headings in one place.

@custom-selector :--heading h1, h2, h3; 

:--heading { 
  font-weight: bold; 
}

Reusing Custom Selectors in Media Queries

Custom selectors can also be reused within media queries. This example shows a custom selector :--button used within a media query to apply specific styles to all elements with the class .btn on screens wider than 768 pixels.

@custom-selector :--button .btn; 

@media (min-width: 768px) { 
  :--button { 
    padding: 10px 15px; 
  } 
}

Other packages similar to postcss-custom-selectors

Readme

Source

PostCSS Custom Selectors PostCSS

NPM Version CSS Standard Status Build Status Support Chat

PostCSS Custom Selectors lets you use Custom Selectors in CSS, following the CSS Extensions specification.

@custom-selector :--heading h1, h2, h3;

article :--heading + p {
  margin-top: 0;
}

/* becomes */

article h1 + p, article h2 + p, article h3 + p {}

Usage

Add PostCSS Custom Selectors to your project:

npm install postcss-custom-selectors --save-dev

Use PostCSS Custom Selectors to process your CSS:

const postcssCustomSelectors = require('postcss-custom-selectors');

postcssCustomSelectors.process(YOUR_CSS /*, processOptions, pluginOptions */);

Or use it as a PostCSS plugin:

const postcss = require('postcss');
const postcssCustomSelectors = require('postcss-custom-selectors');

postcss([
  postcssCustomSelectors(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

PostCSS Custom Selectors runs in all Node environments, with special instructions for:

NodePostCSS CLIWebpackCreate React AppGulpGrunt

Options

preserve

The preserve option determines whether custom selectors and rules using custom selectors should be preserved in their original form.

@custom-selector :--heading h1, h2, h3;

article :--heading + p {
  margin-top: 0;
}

/* becomes */

article h1 + p, article h2 + p, article h3 + p {}

article :--heading + p {}

importFrom

The importFrom option specifies sources where custom selectors can be imported from, which might be CSS, JS, and JSON files, and directly passed objects.

postcssCustomSelectors({
  importFrom: 'path/to/file.css' // => @custom-selector h1, h2, h3
});
article :--heading + p {
  margin-top: 0;
}

/* becomes */

article h1 + p, article h2 + p, article h3 + p {}

Multiple files can be passed into this option, and they will be parsed in the order they are received. JavaScript files, JSON files, and objects will need to namespace custom selectors using the customProperties or custom-properties key.

postcssCustomSelectors({
  importFrom: [
    'path/to/file.css',
    'and/then/this.js',
    'and/then/that.json',
    {
      customSelectors: {
        ':--heading': 'h1, h2, h3'
      }
    }
  ]
});

Keywords

FAQs

Last updated on 08 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc