package tw

All checked builds pass
  1. Overview
  2. Docs
Type-safe Tailwind CSS v4 in OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

tw-1.2.0.tbz
sha256=c6f2f55bdbafd557e7716e4ab2daa012beb2ab2548090fae4a6e53c8016a3b10
sha512=13c066b3b233227d620a28abb724c8aea13c08f8f4b4657f7462d5806aa227568b921523e2675f3cd8ec8597fbe23a0f512e98f143adcf49b64a80ac95e6b740

doc/tw/Tw/Scheme/index.html

Module Tw.SchemeSource

Theme scheme configuration for customizing CSS output.

A scheme defines theme overrides that affect how utilities generate CSS. This allows matching Tailwind's test expectations which use custom @theme definitions with hex colors and explicit spacing variables.

Sourcetype color_value =
  1. | Hex of string
    (*

    e.g., "#ef4444"

    *)
  2. | Oklch of {
    1. l : float;
    2. c : float;
    3. h : float;
    }
    (*

    e.g., oklch(63.7% 0.237 25.331)

    *)

Color value - either hex string or oklch components

Sourcetype custom_variant = {
  1. values : (string * string) list;
  2. template : string;
}

A \@custom-variant registered with a value map (the DEFAULT value under key "") and a selector template with {} where the value is substituted. & in the template denotes the element's own class.

Sourcetype variant_branch = {
  1. media : Cascade.Css.Media.t option;
  2. selector : Cascade.Css.Selector.t;
}

One rule of a variant's block body: the element's selector, where & (Css.Selector.t.Nesting) stands for the element, inside \@media media when media is Some. The branch { media = Some m; selector = s } is the body \@media m { s { \@slot; } } of a Tailwind \@custom-variant.

Sourcetype t = {
  1. colors : (string * color_value) list;
    (*

    Color overrides. Key is color name like "red-500". When a color is Hex, opacity modifiers use hex+alpha fallback. When Oklch, opacity modifiers use color-mix fallback.

    *)
  2. spacing : (int * Cascade.Css.length) list;
    (*

    Explicit spacing variables. Key is the multiplier (e.g., 4 for --spacing-4). When defined, utilities use var(--spacing-N) instead of calc(var(--spacing) * N).

    *)
  3. radius : (string * Cascade.Css.length) list;
    (*

    Explicit radius variables. Key is the radius name (e.g., "none", "full", "sm"). When defined, utilities use var(--radius-NAME) instead of raw values.

    *)
  4. default_ring_width : int;
    (*

    Default ring width in pixels for bare ring utility. Corresponds to Tailwind's @theme \{ --default-ring-width: Npx \}. Default: 1.

    *)
  5. default_border_width : int;
    (*

    Default border width in pixels for bare border utility. Corresponds to Tailwind's @theme \{ --default-border-width: Npx \}. Default: 1.

    *)
  6. default_outline_width : int;
    (*

    Default outline width in pixels for bare outline utility. Corresponds to Tailwind's @theme \{ --default-outline-width: Npx \}. Default: 1.

    *)
  7. breakpoints : (string * float) list;
    (*

    Explicit breakpoint values in px. Key is breakpoint name (e.g., "sm"). When defined, responsive media queries use @media (min-width: Xpx) instead of rem-based values.

    *)
  8. token_overrides : (string * string) list;
    (*

    Per-render theme token overrides (from a @theme block). Key is the variable name without the leading --; value is the CSS string. Threaded replacement for the global Var.theme_value_overrides.

    *)
  9. inline_tokens : string list;
    (*

    Names of the tokens a project declared in an \@theme inline block. Such a token has no declaration of its own: a utility reading it inlines the value instead of referencing var(--name).

    *)
  10. reference_tokens : string list;
    (*

    Names of the tokens a project declared in an \@theme reference block. The block declares the token elsewhere, so the theme layer emits no declaration for it and a reader spells the value as the fallback of its own reference.

    *)
  11. reference_theme : bool;
    (*

    Whether the stylesheet only references the package, with \@reference "tailwindcss", rather than importing it. Every token but the ones the stylesheet's own \@theme declares is then a reference token.

    *)
  12. static_tokens : string list;
    (*

    Names of the tokens a project declared in an \@theme static block. The theme layer declares each of them whether or not a utility reads it, with the value the block gave it.

    *)
  13. static_theme : bool;
    (*

    Whether the package was imported with theme(static), which emits every theme variable rather than only the ones a utility used.

    *)
  14. important : bool;
    (*

    Whether the package was imported with important, which marks every declaration a utility emits !important, dressed in variants or not. The author's own CSS, and what an \@apply pulls into it, are not utilities and keep their declarations as written.

    *)
  15. prefix : string option;
    (*

    The prefix prefix(tw) on the import asks for. Every candidate is then spelled tw:p-4, and every theme token is declared and read as --tw-spacing. The --tw-* variables a utility sets for itself are not theme tokens and keep their names, whatever the prefix is.

    *)
  16. custom_variants : (string * custom_variant) list;
    (*

    The \@custom-variants this \@theme declared, each a value map and a selector template. A variant belongs to the theme that declared it, so two stylesheets built in one process cannot see each other's.

    *)
  17. container_variants : (string * Cascade.Css.Container.t) list;
    (*

    The \@custom-variants this \@theme declared with a container-query body (e.g. has-a -> \@container style(--a)). Kept apart from custom_variants because the condition is structural, so the not- prefix negates it soundly.

    *)
  18. dark : variant_branch list option;
    (*

    The body of the dark variant when the project redeclares it, as Tailwind's \@custom-variant dark { ... } does: Tw.dark and the dark: prefix then write one rule per branch, in order, each under the class dark: names. None, the default, is the built-in \@media (prefers-color-scheme: dark).

    A dark theme that follows the reader's choice as well as the system one is two branches: the media query with a selector that a data-theme=light root turns off, and a selector for data-theme=dark.

    *)
}

Theme scheme configuration

Sourceval pp : t -> string

pp t returns a string representation of a scheme configuration.

Sourceval default : t

default is the default scheme using oklch colors and calc-based spacing (matches Tailwind v4 default).

Sourceval or_default : t option -> t

or_default theme is the scheme an optionally-threaded theme names: itself, or default when none was threaded.

Sourceval register_default_token : string -> string -> unit

register_default_token name css registers the v4.3.1 baseline default CSS for theme token name (without --). It updates a lock-free immutable snapshot and is called once at module initialisation by the utility that owns the token.

Sourceval all_default_tokens : unit -> (string * string) list

all_default_tokens () is every token a family has published through register_default_token. theme(static) emits them all.

Sourceval token_default : string -> string option

token_default name returns the registered baseline default for name.

Sourceval is_removed : t -> string -> bool

is_removed t name is whether the \@theme block removed name. Tailwind reads --name: initial as "remove this token", --namespace-*: initial as "remove the whole namespace" and the bare --*: initial as "remove every token", and a candidate that needed the token stops resolving. A token the block declares in its own right survives a reset, and so does a nested scale that merely shares the prefix: --font-*: initial leaves --font-weight-* alone.

Sourceval token_override : t -> string -> string option

token_override t name returns the per-render override for name, if any. A removed token has none.

Sourceval theme_value : t option -> string -> string option

theme_value theme name looks up a per-render token override from the optionally-threaded theme (None when no theme is threaded). Threaded replacement for the global Var.theme_value.

Sourceval token : t -> string -> string option

token t name resolves a theme token: override (if any) else default, or nothing at all when the \@theme block removed it.

Sourceval removes_tokens : t -> bool

removes_tokens t is whether the \@theme block took any token away, so a candidate has to be held against is_removed_token at all.

Sourceval is_removed_token : t -> string -> bool

is_removed_token t name is is_removed narrowed to a name that was a theme token to begin with: a registered default or one the block declared. A --tw-* channel is neither, so the bare --*: initial leaves it be.

Sourceval with_overrides : ?inline:string list -> ?reference:string list -> ?static:string list -> t -> (string * string) list -> t

with_overrides ?inline ?reference ?static t overrides applies overrides on top of t's existing token overrides (new entries win). inline names the tokens that came from an \@theme inline block, reference those from an \@theme reference one, and static those from an \@theme static one.

Sourceval is_inline_token : t -> string -> bool

is_inline_token t name is whether name was declared in an \@theme inline block, so a utility reading it inlines the value rather than referencing var(--name).

Sourceval is_reference_token : t -> string -> bool

is_reference_token t name is whether name was declared in an \@theme reference block, so nothing declares it in the generated sheet and a reader carries the value as its own var() fallback.

Sourceval is_static_token : t -> string -> bool

is_static_token t name is whether name was declared in an \@theme static block, so the theme layer declares it whether or not a utility reads it.

Sourceval color : t -> string -> color_value option

color t name looks up a color in the scheme.

Sourceval declares_color : t option -> string -> bool

declares_color theme name is whether theme binds --color-<name>, through color or a token override.

Sourceval spacing : t -> int -> Cascade.Css.length option

spacing t n looks up a spacing value in the scheme.

Sourceval is_hex_color : t -> string -> bool

is_hex_color t name checks if a color is defined as hex in the scheme.

Sourceval hex_color : t -> string -> string option

hex_color t name returns the hex value for a color if defined as hex.

Sourceval has_explicit_spacing : t -> int -> bool

has_explicit_spacing t n checks if spacing has an explicit variable.

Sourceval radius : t -> string -> Cascade.Css.length option

radius t name looks up a radius value in the scheme.

Sourceval has_explicit_radius : t -> string -> bool

has_explicit_radius t name checks if radius has an explicit variable.

Sourceval breakpoint : t -> string -> float option

breakpoint t name looks up a breakpoint px value in the scheme.

Sourceval breakpoint_length : t -> string -> Cascade.Css.length option

breakpoint_length t name looks up a breakpoint as its exact CSS length. A breakpoint-NAME token override takes precedence over the legacy px-only breakpoint field.

Sourceval all_breakpoints : t -> (string * Cascade.Css.length) list

all_breakpoints t is every breakpoint t defines, keyed by name and sorted by name: the registered defaults, the --breakpoint-* tokens a \@theme block set, and the legacy px-only breakpoints field. A breakpoint the block removed is left out.

Sourceval has_breakpoint : t -> string -> bool

has_breakpoint t name is whether t still defines the breakpoint name, reading the same set as all_breakpoints. A breakpoint the \@theme block removed is gone for the variants that name it.

Sourceval breakpoint_names : t -> string list

breakpoint_names t returns the custom breakpoint names available while parsing variants.