All Projects → dbtedman → postcss-prefixwrap

dbtedman / postcss-prefixwrap

Licence: MIT license
A PostCSS plugin that is used to wrap css styles with a css selector to constrain their affect on parent elements in a page.

Programming Languages

typescript
32286 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to postcss-prefixwrap

postcss-trash-killer
It is a postcss plugin which wil be remove all unused css
Stars: ✭ 20 (-62.96%)
Mutual labels:  postcss, postcss-plugin
postcss-import-url
PostCSS plugin inlines remote files.
Stars: ✭ 47 (-12.96%)
Mutual labels:  postcss, postcss-plugin
postcss-clearfix
PostCSS plugin that adds a 'fix' argument to the 'clear' property
Stars: ✭ 47 (-12.96%)
Mutual labels:  postcss, postcss-plugin
abilitysheet
This app is ability sheet for beatmania iidx music of level 12.
Stars: ✭ 38 (-29.63%)
Mutual labels:  yarn, postcss
postcss-critical-css
PostCSS plugin to define and output critical CSS using custom atRules, and/or custom CSS properties. Critical CSS may be output to one or more files, as defined within the plugin options or within the CSS.
Stars: ✭ 84 (+55.56%)
Mutual labels:  postcss, postcss-plugin
Xity Starter
A blog-ready 11ty starter based on PostCSS, with RSS feed and Native Elements!
Stars: ✭ 184 (+240.74%)
Mutual labels:  yarn, postcss
postcss-center
PostCSS plugin to center elements.
Stars: ✭ 44 (-18.52%)
Mutual labels:  postcss, postcss-plugin
iridium
💎 Growing collection of VS Code extensions with a fancy name
Stars: ✭ 39 (-27.78%)
Mutual labels:  yarn, pnpm
postcss-styl
PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
Stars: ✭ 15 (-72.22%)
Mutual labels:  postcss, postcss-plugin
postcss-inline-media
Media queries shortcut, built on PostCSS, example: font-size: 20px @1200 18px @480 16px;
Stars: ✭ 47 (-12.96%)
Mutual labels:  postcss, postcss-plugin
Verdaccio
📦🔐 A lightweight Node.js private proxy registry
Stars: ✭ 12,667 (+23357.41%)
Mutual labels:  yarn, pnpm
postcss
No description or website provided.
Stars: ✭ 59 (+9.26%)
Mutual labels:  postcss, postcss-plugin
Barebones
A barebones boilerplate for getting started on a bespoke front end.
Stars: ✭ 127 (+135.19%)
Mutual labels:  yarn, postcss
Core
Native HTML Elements with CSS superpowers. 🕶
Stars: ✭ 237 (+338.89%)
Mutual labels:  yarn, postcss
React Boilerplate
This project is deprecated. Please use CRA instead.
Stars: ✭ 88 (+62.96%)
Mutual labels:  yarn, postcss
postcss-input-style
PostCSS plugin that adds new pseudo-elements for easily styling the inner elements of inputs
Stars: ✭ 16 (-70.37%)
Mutual labels:  postcss, postcss-plugin
Postcss Rtl
PostCSS plugin for RTL-adaptivity
Stars: ✭ 143 (+164.81%)
Mutual labels:  postcss, postcss-plugin
Postcss Spiffing
PostCSS plugin to use British English
Stars: ✭ 158 (+192.59%)
Mutual labels:  postcss, postcss-plugin
ci
Run npm ci using the appropriate Node package manager (npm, yarn, pnpm)
Stars: ✭ 39 (-27.78%)
Mutual labels:  yarn, pnpm
postcss-rename
Replace class names based on a customizable renaming scheme.
Stars: ✭ 77 (+42.59%)
Mutual labels:  postcss, postcss-plugin

PostCSS Prefix Wrap

CI GitHub Pipeline Test Code Coverage SAST GitHub Pipeline Release GitHub Pipeline NPM Downloads Per Week

A PostCSS plugin which prepends a selector to CSS styles to constrain their effect on parent elements in a page.

Supports Versions
NodeJS v14, v15, v16, v17, v18
PostCSS v7, v8

How to use this plugin?

⚠️ These instructions are only for this plugin. See the PostCSS website for framework information.

Install

Package Manager Command
NPM npm install postcss-prefixwrap --save-dev --save-exact
PNPM pnpm add postcss-prefixwrap --save-dev --save-exact
Yarn yarn add postcss-prefixwrap --dev --exact

Configure

Add to your PostCSS configuration.

const PostCSS = require("gulp-postcss");
const PrefixWrap = require("postcss-prefixwrap");

PostCSS([PrefixWrap(".my-custom-wrap")]);

Container

Add the container to your markup.

<div class="my-custom-wrap"><!-- Your existing markup. --></div>

View

View your CSS, now prefix-wrapped.

Before

p {
    color: red;
}

body {
    font-size: 16px;
}

After

.my-custom-wrap p {
    color: red;
}

.my-custom-wrap {
    font-size: 16px;
}

What options does it have?

Minimal

The minimal required configuration is the prefix selector, as shown in the above example.

PrefixWrap(".my-custom-wrap");

Ignored Selectors

You may want to exclude some selectors from being prefixed, this is enabled using the ignoredSelectors option.

PrefixWrap(".my-custom-wrap", {
    ignoredSelectors: [":root", "#my-id", /^\.some-(.+)$/],
});

Prefix Root Tags

You may want root tags, like body and html to be converted to classes, then prefixed, this is enabled using the prefixRootTags option.

PrefixWrap(".my-container", {
    prefixRootTags: true,
});

With this option, a selector like html will be converted to .my-container .html, rather than the default .my-container.

File Whitelist

In certain scenarios, you may only want PrefixWrap() to wrap certain CSS files. This is done using the whitelist option.

⚠️ Please note that each item in the whitelist is parsed as a regular expression. This will impact how file paths are matched when you need to support both Windows and Unix like operating systems which use different path separators.

PrefixWrap(".my-custom-wrap", {
    whitelist: ["editor.css"],
});

File Blacklist

In certain scenarios, you may want PrefixWrap() to exclude certain CSS files. This is done using the blacklist option.

⚠️ Please note that each item in the blacklist is parsed as a regular expression. This will impact how file paths are matched when you need to support both Windows and Unix like operating systems which use different path separators.

If whitelist option is also included, blacklist will be ignored.

PrefixWrap(".my-custom-wrap", {
    blacklist: ["colours.css"],
});

Nesting

When writing nested css rules, and using a plugin like postcss-nested to compile them, you will want to ensure that the nested selectors are not prefixed. This is done by defining the nested property and setting the value to the selector prefix being used to represent nesting, this is most likely going to be "&".

PrefixWrap(".my-custom-wrap", {
    nested: "&",
});

As an example, in the following CSS that contains nested selectors.

.demo {
    &--lite {
        color: red;
    }
}

Without the nested configuration option defined:

.my-custom-wrap .my-custom-wrap .demo--lite {
    color: red;
}

With the tested configuration defined:

.my-custom-wrap .demo--lite {
    color: red;
}

What problems can it solve?

💡 Hi there! If you have a problem that you have used this plugin for, I would like to hear so I can list it here to share with the community.

Embedding content within an existing site

You may be asked to develop a piece of interactivity that needs to live within a content management system that you do not control. You may find that your styles are impacted by the CSS already on the site, or that your newly included CSS now impacts the rest of the page it is embedded in.

PostCSS Prefix Wrap solves this problem by prefixing your CSS selectors so that they only apply to HTML contained within a parent containing element. Your styles will now take precedence over those of the parent page.

On the flip side, your styles wont negatively impact the site your content is hosted on as its scoped to that parent container.

This is in fact the origin story for this plugin, for developing interactive content to live within the Blackboard LMS.

🤔 You might wonder why its necessary to use this plugin when you could just prefix your styles yourself. Yes, this is correct, but does not apply to any 3rd party code you include such as a CSS framework like Bootstrap.

How to contribute?

Read our Contributing Guide to learn more about how to contribute to this project.

Is this project secure?

Read our Security Guide to learn how security is considered during the development and operation of this plugin.

License

The MIT License is used by this project.

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].