Atomizer Documentation
repository·main·Indexed 23 days ago
https://github.com/acss-io/atomizerA toolset for generating and managing Atomic CSS rules. It includes a CLI for scanning files to generate stylesheets, a JavaScript API for programmatic CSS generation, and the atomizer-plugins library for integration with esbuild, Rollup, Vite, and Webpack. Additionally, it provides a VSCode extension for Atomic CSS autocomplete across multiple file types including JS, HTML, React, Svelte, and TypeScript, as well as a Grunt task via grunt-atomizer.
What's inside Atomizer
- Atomizer is a tool designed to help you create Atomic CSS rules. Atomic CSS is a methodology consisting of a collection of single-purpose styling units intended for maximum reuse. This approach is particularly effective when working with component-based frameworks like React, Ember, or Angular.
Explore the Atomizer ecosystem
mainAtomizer is maintained as a monorepo containing the core library and various integration plugins for different build tools and frameworks.
Core Library
- Atomizer: The primary library source code.
Build Tool Plugins
- Grunt:
grunt-atomizer - Gulp:
gulp-atomizer - Metalsmith:
metalsmith-atomizer - Webpack:
webpack-atomizer-loader - Boot-clj:
boot-atomizer - Ember:
ember-cli-atomizer
Integrate Atomizer with Bundlers and Frameworks
mainAtomizer provides various integrations for automated workflows. Instead of using the CLI manually, you can use dedicated plugins for your build tools or framework-specific setups.
- Bundlers: Use Atomizer plugins for tools like Rollup, Vite, or Webpack to automate CSS generation during your build process.
- Frameworks: Use specialized integrations designed for popular JavaScript frameworks.
Refer to the specific integration guides in the documentation for your chosen tool.
Structure of the Rollup Atomizer example
mainThe Rollup example demonstrates how to integrate Atomizer into a Rollup build pipeline. The key files in this example are:
index.html: The HTML entry point that executes the bundleddist/main.js.index.js: A JavaScript file that simulates creating HTML elements using Atomizer CSS classes.rollup.config.mjs: The Rollup configuration file that utilizes theatomizer-pluginspackage to process CSS.
Structure of the Atomizer Webpack example
mainThe Webpack example demonstrates how to integrate Atomizer into a standard build pipeline using the following files:
atomizer.config.mjs: The configuration file for Atomizer.webpack.config.mjs: The Webpack configuration that utilizes theatomizer-pluginspackage.index.js: A script simulating HTML creation via JavaScript, incorporating Atomizer CSS classes.index.html: The entry HTML page that executes the Webpack-bundled JavaScript.
What is Atomic CSS and how does Atomizer use it?
mainAtomic CSS is a CSS architecture consisting of a set of classes representing single-purpose styling units. Atomizer implements a specific syntax to help you generate these Atomic rulesets.
Atomizer only creates a style sheet containing the relevant declarations used in your project. These are generated from:
- Atomizer classes found within your project markup.
- Custom values defined in your Atomizer configuration file.
Benefits of the Atomic approach:
- Predictable changes: One class equals one style, making it easy to predict the impact of adding or removing classes.
- Limited scope: Styling is done via single-purpose classes rather than descendant or contextual selectors.
- Lean CSS: Reduces redundancy and eliminates dead weight by only generating used styles.
- Portability: Components styled with Atomic classes can be moved between projects that use Atomizer.
- Lower learning curve: Developers reuse existing classes instead of writing complex selectors.
What is Atomic CSS?
mainAtomic CSS is a collection of single-purpose styling units based on the single responsibility principle. These units are designed for maximum reuse and are highly compatible with component-based frameworks like React, Ember, or Angular.
Key characteristics:
- Immutable: Atomic classes and their styles are intended to be used consistently across different projects and teams.
- Content Agnostic: They act as a common "vocabulary" for styling documents regardless of the specific content or context.
- Low Specificity: Unlike semantic CSS, Atomic CSS uses generic classes to normalize selector weight and reduce scope.
Use contextual selectors and pseudo-classes
mainAtomizer supports styling elements based on their ancestors or siblings using a combinator syntax. You can also apply styles based on pseudo-classes like
:hover(represented by the:hsuffix).Example of a hover effect:
Bgc(#0280ae):hsets the background color on hover.You can also combine descendant selectors with pseudo-classes to reveal nested elements when a parent is hovered.
<!-- Hover effect on the element itself --> <div class="Bgc(#0280ae):h C(#0280ae) C(#fff):h"> Lorem ipsum </div> <!-- Reveal nested element when parent is hovered --> <div class="foo"> <p class="Op(0) foo:h>Op(1)">Lorem ipsum</p> </div>Benefits of using Atomic CSS
mainUsing Atomic CSS provides several architectural and performance advantages:
- Reduces Bloat: By using content-agnostic rules, it dramatically reduces redundancy compared to semantic selectors.
- Improves Performance: Smaller CSS files mean fewer bytes to download.
- Normalizes Specificity: Authors use generic classes via markup, reducing the need for complex contextual selectors or sandboxing.
- Removes Dependencies: Components rely on generic rules rather than being tied to specific style blocks or stylesheets.
- Easier Asset Sharing: UI patterns can be shared across projects using the same set of generic rules.
- Facilitates RTL/LTR: Directions (left/right) are abstracted, making it easier to support different script directions.
- Better Compression: Because atomic classes repeat frequently, they achieve better compression ratios with tools like Gzip (e.g., ~48% for atomic classes vs ~35% for semantic classes).
Use CSS Grid layouts with Atomizer
mainTo create a grid container, use theD(g)class fordisplay: gridorD(ig)fordisplay: inline-grid. Due to the complexity of CSS Grid, many advanced grid properties (likegrid-template) are best implemented using custom variables defined in youratomizer.config.js.Use <context> and <combinator> for relational styling
mainAtomizer allows you to style an element based on the state or presence of its ancestors or siblings using
<context>and<combinator>syntax.Contextual Selectors
<context>: A class applied to an ancestor or sibling. It is followed by a<combinator>.<combinator>: Required if a<context>is provided. It defines the relationship between the context and the current element.
Combinators
Character Name CSS Equivalent Description _Underscore Descendant combinator Styles the element if any ancestor has the context class. >Right angle bracket Child combinator Styles the element if its immediate parent has the context class. +Plus sign Adjacent sibling combinator Styles the element if its immediate previous sibling has the context class. ~Tilde sign General sibling combinator Styles the element if any previous sibling has the context class. Note: Classes containing descendant selectors are not sandboxed via the namespace; instead, Atomizer adds
!importantto these styles.<!-- Descendant (Ancestor) --> <div class="foo"> <div class="foo_D(n)"></div> </div> <!-- Child (Parent) --> <div class="foo"> <div class="foo>D(n)"></div> </div> <!-- Adjacent Sibling --> <div class="foo"></div> <div class="foo+D(n)"></div> <!-- General Sibling --> <div class="foo"></div> <div class="bar"></div> <div class="foo~D(n)"></div>Understand how descendant classes and namespaces interact
mainWhen a namespace is configured, Atomizer applies it to most rules. However, descendant classes (contextual classes) do not use the namespace to avoid selector failures in complex DOM structures (e.g., when the target node is a sibling rather than a descendant of the namespaced element).
Instead of using the namespace for these specific rules, Atomizer applies
!importantto ensure they remain effective.Note: If you are not using a namespace, Atomizer does not add
!importantto these rules. If you use a namespace, keep in mind that these!importantrules might affect how you style the same node via JavaScript.