KScript docs
EN

Stylesheets

Writing stylesheets

Expressions

Write a KScript expression in braces wherever CSS takes a value: {$name}, {@rem(24)}. A { is KScript only when $name, @name( or ! comes next. Every other brace is CSS, and Sass's own #{$var} is left alone.

Inside a CSS string, a < is plain text, so content: "<b>" and an inline SVG in url("data:image/svg+xml,<svg …>") are left as written.

Control tags

<let>, <if> and <for> work as they do in a script. Keep each on its own line between CSS rules. A tag alone on its line leaves no blank line behind:

CSS
<let name="space" value="{ sm: 4, md: 8, lg: 16 }" />
<for each="name, px" in="$space">
.p-{$name} { padding: {@rem($px)}; }
</for>
<if $vars.dark ?? false>
@media (prefers-color-scheme: dark) { :root { --surface: #111; } }
</if>

Values from data

A stylesheet may read its values from anywhere a script can:

  • A variable: {$name}.
  • A JSON file: <let name="theme" value="@read_json(./theme.json)" />.
  • Project configuration: {$config.theme.accent}.
  • Build arguments: {$vars.dark}, set by kscript css --var dark=true.

Here a theme file drives the whole stylesheet:

JSON
{ "colors": { "primary": "#3355ff", "surface": "#ffffff" }, "space": { "sm": 4, "md": 8, "lg": 16 } }
CSS
<let name="theme" value="@read_json(./theme.json)" />
:root {
  { @css_vars($theme.colors, 'color') }        /* --color-primary: #3355ff; --color-surface: #ffffff; */
}

<for each="name, px" in="$theme.space">
.p-{$name} { padding: {@rem($px)}; }
</for>

.button {
  background: var(--color-primary);
  color: {@contrast_color($theme.colors.primary)};
}