Mutation Mutation Layer (--mutation)
- Definition
- Mutation Capabilities
- 1. Block-Level Control
- 2. Reduced Verbosity
- 3. Easy Variants
- 4. Clear Intent
- 5. Dynamic State Management
- Implementation Guide
- Basic Mutation
- Pre-loaded Mutations
- Combining Multiple Mutations
- JavaScript-driven Mutations
- Mutations vs. Contexts: Conceptual Comparison
- Pattern Validation
- Valid Patterns
- Invalid Patterns
- CSS Implementation
- Key Takeaways
mutation
is a concept that allows for dynamic, block-specific modifications. Inspired by command-line flags, mutations offer a concise way to create block variants without the verbosity often associated with traditional modifier classes.
Definition
Copy link to this sectionIn Blocktail, mutations create specific state changes within individual blocks, similar to flags that modify behavior. A mutation:
- Applies changes only to its block
- Can combine with other mutations
- Maintains block identity while changing its state
Mutation Capabilities
Copy link to this section1. Block-Level Control
Copy link to this section- Mutations keep modifications where they belong - at the block level
- Allows for specific changes to individual blocks without affecting others
2. Reduced Verbosity
Copy link to this section- We don't need to write long class names
- Mutations are concise and clear and improve code readability
3. Easy Variants
Copy link to this section- Create multiple versions of a block without duplicating code
- Enables variations without cluttering the base block structure
4. Clear Intent
Copy link to this section- Mutations clearly communicate the state or variant of a block
- Improves code understandability for developers
5. Dynamic State Management
Copy link to this section- Can be pre-loaded on page load based on server-side calculations
- Facilitates real-time changes through JavaScript
Implementation Guide
Copy link to this sectionBasic Mutation
Copy link to this section<article class="product_card --featured">
<!-- Featured product card content -->
</article>
Pre-loaded Mutations
Copy link to this section<article class="product_card <?php echo $is_featured ? '--featured' : ''; ?>">
<!-- Product card content -->
</article>
Combining Multiple Mutations
Copy link to this section<article class="product_card --featured --on_sale">
<!-- Featured product on sale -->
</article>
JavaScript-driven Mutations
Copy link to this sectiondocument.querySelector('.product_card').classList.add('--featured');
Mutations vs. Contexts: Conceptual Comparison
Copy link to this sectionWhile contexts provide broad, environmental changes, mutations offer granular, block-specific modifications:
- Contexts: Applied to high-level elements, influencing multiple blocks
- Mutations: Applied to individual blocks or tails for specific modifications
Example:
<main class="context--sales_event">
<article class="product_card --featured">
<!-- Featured product card in a sales event context -->
</article>
</main>
Pattern Validation
Copy link to this sectionValid Patterns
Copy link to this section✓ class="--featured" <!-- Single mutation -->
✓ class="--on_sale" <!-- Compound name -->
✓ class="--featured --on_sale" <!-- Multiple mutations -->
Invalid Patterns
Copy link to this section✗ class="-featured" <!-- Single dash -->
✗ class="featured--" <!-- Wrong prefix position -->
✗ class="--on-sale" <!-- Using hyphen instead of underscore -->
CSS Implementation
Copy link to this section.product_card {
/* Base styles */
&.--featured {
/* Featured mutation styles */
}
&.--on_sale {
/* Sale mutation styles */
}
/* Combined mutations */
&.--featured.--on_sale {
/* Styles for featured items on sale */
}
}
Key Takeaways
Copy link to this section- Mutations provide block-specific modifications
- Use double dash prefix (
--
) - Use descriptive, action-oriented names
- Use underscores for compound names
- Can be combined for complex states
- Each mutation should represent a distinct state change