Skip to content

@param

The @param directive declares a parameter inside a @block. Parameters control which CSS rules are applied to the component.

Syntax

ecss
@param --name Type;
@param --name? Type;
@param --name? Type: default;

@param is declared at the start of a @block body.

Parameter name

A parameter name is a CSS custom property (--name). In JS it is converted to camelCase:

ECSSJS
--variantvariant
--is-disabledisDisabled
--bg-colorbgColor

Required and optional

A parameter without ? is required: it must be passed on every call.

ecss
@param --variant Variant;
@param --size unit;

A ? after the name makes the parameter optional. If no value is passed, null is used (or an explicit default):

ecss
@param --label? string;
@param --label? string: "Submit";

INFO

A default value can be set only for an optional parameter.

Parameter type

All supported types are described in the data types reference. The type is always specified explicitly.

boolean

ecss
@param --disabled boolean;
@param --elevated? boolean: true;

In TypeScript: boolean.

string

ecss
@param --label? string: "Submit";
@param --href string;

In TypeScript: string.

number

ecss
@param --columns number;
@param --opacity? number: 1;

In TypeScript: number.

unit

ecss
@param --size unit;
@param --gap? unit: 8px;

In TypeScript: a string literal of the form "16px", "1.5rem" — a number with a valid unit.

color

ecss
@param --bg color;
@param --accent? color: #646cff;

In TypeScript: string.

Reference to @enum

The parameter accepts one of the enum's values. The type is the @enum name:

ecss
@enum Variant {
  values: "primary", "danger", "ghost";
}

@block Button {
  @param --variant Variant;
}

In TypeScript: a union type of the enum's values.

Default value

A default is specified with : and is used when the parameter is not passed. It requires ?:

ecss
@param --variant? Variant: "primary";
@param --disabled? boolean: false;
@param --size? unit: 16px;

An optional parameter without an explicit default gets null by default.

TypeScript types

SyntaxTypeScript typeDefault
@param --p Type;Type
@param --p? Type;Type | null | undefinednull
@param --p? Type: value;Type | null | undefinedvalue

Usage

Parameters can be used in @if conditions and as CSS property values:

ecss
@block Badge {
  @param --status Status;
  @param --outlined? boolean: false;
  @param --gap? unit: 8px;

  padding: 8px 16px;
  column-gap: --gap; // no var() needed

  @if (--outlined) {
    border: 1px solid currentColor;
  }

  @if (--status == "error") {
    background: #ffe0e0;
    color: #cc0000;
  }
}

Constraints

  • @param is declared only inside a @block, not inside @element
  • Parameter names are unique within a block and all its descendants — a child block cannot declare a parameter with a name that already exists on the parent

See also