Atomizer Documentation

repository·main·Indexed 23 days ago

https://github.com/acss-io/atomizer

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

Tokens
39.7K
Snippets
140
Records
216
Agent score
75%

What's inside Atomizer

  1. What is Atomizer

    main
    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.
  2. Explore the Atomizer ecosystem

    main

    Atomizer 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
  3. Integrate Atomizer with Bundlers and Frameworks

    main

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

  4. Structure of the Rollup Atomizer example

    main

    The 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 bundled dist/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 the atomizer-plugins package to process CSS.
  5. Structure of the Atomizer Webpack example

    main

    The 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 the atomizer-plugins package.
    • 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.
  6. What is Atomic CSS and how does Atomizer use it?

    main

    Atomic 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:

    1. Atomizer classes found within your project markup.
    2. 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.
  7. What is Atomic CSS?

    main

    Atomic 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.
  8. Use contextual selectors and pseudo-classes

    main

    Atomizer 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 :h suffix).

    Example of a hover effect: Bgc(#0280ae):h sets 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>
  9. Benefits of using Atomic CSS

    main

    Using 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).
  10. Use <context> and <combinator> for relational styling

    main

    Atomizer 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

    CharacterNameCSS EquivalentDescription
    _UnderscoreDescendant combinatorStyles the element if any ancestor has the context class.
    >Right angle bracketChild combinatorStyles the element if its immediate parent has the context class.
    +Plus signAdjacent sibling combinatorStyles the element if its immediate previous sibling has the context class.
    ~Tilde signGeneral sibling combinatorStyles the element if any previous sibling has the context class.

    Note: Classes containing descendant selectors are not sandboxed via the namespace; instead, Atomizer adds !important to 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>
  11. Understand how descendant classes and namespaces interact

    main

    When 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 !important to ensure they remain effective.

    Note: If you are not using a namespace, Atomizer does not add !important to these rules. If you use a namespace, keep in mind that these !important rules might affect how you style the same node via JavaScript.