Expand description
§Scale-Based Spacing Tokens
This module provides spacing tokens that follow a defined scale, preventing arbitrary pixel values and ensuring visual consistency.
§Design Rationale
Arbitrary spacing values create visual inconsistency:
ⓘ
// ❌ Bad: Arbitrary values
button.padding = 7; // Why 7? Looks off.
card.margin = 13; // Not on any scale.Scale-based spacing ensures everything aligns:
ⓘ
// ✅ Good: Scale-based values
button.padding = SpacingSm::pixels(); // 8px, on 4px scale
card.margin = SpacingMd::pixels(); // 12px, on 4px scale§The 4px Base Unit
All spacing in Fern is based on a 4px unit. This creates a consistent rhythm and ensures elements align to a pixel grid.
| Token | Multiplier | Pixels |
|---|---|---|
SpacingXs | 1× | 4px |
SpacingSm | 2× | 8px |
SpacingMd | 3× | 12px |
SpacingLg | 4× | 16px |
SpacingXl | 6× | 24px |
§Const Generics
Spacing uses const generics to encode the multiplier in the type:
use fernctl::domain::tokens::spacing::*;
// The multiplier is part of the type, not a runtime value
let _sm: Spacing<2> = Spacing::new();
assert_eq!(Spacing::<2>::pixels(), 8);
// Type aliases provide semantic names
assert_eq!(SpacingSm::pixels(), 8);§Compile-Time Validation
The type system ensures spacing values are always on the scale:
use fernctl::domain::tokens::spacing::*;
// These are the only valid spacing values
let xs = SpacingXs::pixels(); // 4
let sm = SpacingSm::pixels(); // 8
let md = SpacingMd::pixels(); // 12
let lg = SpacingLg::pixels(); // 16
let xl = SpacingXl::pixels(); // 24
// There's no way to express "7px" — it's not a type!Structs§
- Spacing
- A spacing value that is a multiple of the base unit.
Enums§
- Spacing
Value - A runtime spacing value for when const generics aren’t suitable.
Constants§
- SPACING_
BASE - The base unit for spacing in pixels.