Module spacing

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

TokenMultiplierPixels
SpacingXs1×4px
SpacingSm2×8px
SpacingMd3×12px
SpacingLg4×16px
SpacingXl6×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§

SpacingValue
A runtime spacing value for when const generics aren’t suitable.

Constants§

SPACING_BASE
The base unit for spacing in pixels.

Type Aliases§

SpacingLg
Large spacing (16px).
SpacingMd
Medium spacing (12px).
SpacingSm
Small spacing (8px).
SpacingXl
Extra-large spacing (24px).
SpacingXs
Extra-small spacing (4px).