KScript docs
EN

KScript.Style

Stylesheets written in KScript (.ks.css, .ks.scss, .ks.sass), with colour, size and responsive helpers.

Import it at the top of a script:

<import package="KScript.Style" />
import KScript.Style

ElementsStatements

#<css_text>css_text

CSS text in a stylesheet, with {$name} and { @fn() } filled in. kscript writes these for you around the KScript elements in a .ks.css file.

Holds text with { } values.

Example
<import package="KScript.Style" />
<css_text>font-size: {@rem(24)};</css_text>
import KScript.Style
css_text "font-size: {@rem(24)};"

Commands

Commands are called with @, in any expression: @name(arguments).

#@adjust_lightness(color, amount) → string

Adds a signed amount to HSL lightness and returns CSS colour text. Use 0.1 to add ten percentage points or '-10%' to subtract ten.

Arguments can be named: @adjust_lightness(…, amount: …).

Example (in a .ks.css file)
CSS
@adjust_lightness('red', '-10%')

#@alpha(color, opacity) → string

The colour see-through: opacity from 0 to 1 (or '50%').

Arguments can be named: @alpha(…, opacity: …).

Example (in a .ks.css file)
CSS
.overlay { background: {@alpha('black', 0.5)}; }

#@between(from, to) → string

A media query for screens from one width up to (not including) another.

Arguments can be named: @between(…, to: …).

Example (in a .ks.css file)
CSS
@media {@between('sm', 'lg')} { .grid { grid-template-columns: 1fr 1fr; } }

#@contrast_color(color, dark?, light?) → string

The text colour that reads best on a background: dark (default #000000) on light colours, light (default #ffffff) on dark ones.

Arguments can be named: @contrast_color(…, dark: …, light: …).

Example (in a .ks.css file)
CSS
.badge { background: #ffcc00; color: {@contrast_color('#ffcc00')}; }

#@css_var(name, fallback?) → string

var(--name), with a fallback when given.

Arguments can be named: @css_var(…, fallback: …).

Example (in a .ks.css file)
CSS
.card { padding: {@css_var('space-md', '16px')}; }

#@css_vars(map, prefix?) → string

Custom properties from a map, one per line: { primary: '#36f', space: { sm: '4px' } } with prefix 'theme' gives --theme-primary: #36f; --theme-space-sm: 4px;

Arguments can be named: @css_vars(…, prefix: …).

Example (in a .ks.css file)
CSS
:root {
  { @css_vars($theme.colors, 'color') }
}

#@darken(color, amount) → string

Subtracts from HSL lightness. For example, @darken('red', '10%') gives the same CSS as @adjust_lightness('red', '-10%').

Arguments can be named: @darken(…, amount: …).

Example (in a .ks.css file)
CSS
.button:active { background: {@darken('#3355ff', 0.1)}; }

#@down(width) → string

A media query for screens narrower than this: @down('md') is (max-width: 767.98px).
Example (in a .ks.css file)
CSS
@media {@down('md')} { .sidebar { display: none; } }

#@fluid(min, max, from?, to?) → string

A size that grows with the screen: min (px) on screens up to from (default 640px), max on screens from to (default 1280px), and in between it scales. Gives clamp(...) in rem.

Arguments can be named: @fluid(…, max: …, from: …).

Example (in a .ks.css file)
CSS
h1 { font-size: {@fluid(28, 48)}; }

#@lighten(color, amount) → string

Adds to HSL lightness, as @adjust_lightness(color, amount) does. The amount can be a fraction (0.1) or a percentage ('10%').

Arguments can be named: @lighten(…, amount: …).

Example (in a .ks.css file)
CSS
.button:hover { background: {@lighten('#3355ff', '10%')}; }

#@mix(color, other, amount?) → string

The colour part way to the other: amount is how far (0 to 1, default 0.5, which is halfway).

Arguments can be named: @mix(…, other: …, amount: …).

Example (in a .ks.css file)
CSS
.muted { color: {@mix('#3355ff', 'white', 0.3)}; }

#@rem(px, base?) → string

Pixels as rem: @rem(24) is 1.5rem (base 16).

Arguments can be named: @rem(…, base: …).

Example (in a .ks.css file)
CSS
.title { font-size: {@rem(24)}; margin-bottom: {@rem(12)}; }

#@stylesheet(path, vars?) → string

Compiles a stylesheet written in KScript and returns the CSS (or Sass).

Arguments can be named: @stylesheet(…, vars: …).

Example
<do>@write_text('public/theme.css', @stylesheet('theme.ks.css', { brand: '#3355ff' }))</do>
@write_text('public/theme.css', @stylesheet('theme.ks.css', { brand: '#3355ff' }))

#@up(width) → string

A media query for screens at least this wide: @up(768) or @up('md') is (min-width: 768px).
Example (in a .ks.css file)
CSS
@media {@up('md')} { .menu { display: flex; } }

Variables

VariableTypeDescription
$varsmapValues given to the stylesheet: @stylesheet(path, vars) or kscript css --var name=value.