Skip to content

feat: comptime CSS-in-Zig — type-safe styles with dead code elimination #41

@justrach

Description

@justrach

Problem

Page CSS is currently raw multiline strings with no validation:

const page_css = 
    \\.card { background: var(--bg2); }
    \\.card:hover { opacity: 0.88; }
;

Typos in class names, unused styles, and invalid properties are never caught.

Proposed Solution

A comptime CSS builder that validates at compile time:

const styles = mer.css(.{
    .card = .{ .background = "var(--bg2)", .border_radius = "8px" },
    .card_hover = .{ .opacity = 0.88 },
    .title = .{ .font_size = "clamp(28px, 4vw, 42px)", .font_family = "serif" },
});

// In render:
h.div(.{ .class = styles.card }, .{ ... })
// ^^^ compile error if you reference styles.nonexistent

Benefits:

  • Dead style elimination — only emit CSS for classes actually used
  • Property validation at comptime (catch backgroud typos)
  • Scoped class names to prevent collisions between pages
  • CSS output is a single minified string

Complexity

Medium — Zig's comptime is powerful enough to build this as a library, no compiler changes needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions