Rook
РусскийRU

Styles

style { ... } in a component's body is CSS for its own markup. The elements of that markup get a class of the component, and so does every selector: .track here reaches no other track on the page, and the styles of other components do not reach these elements.

// style holds CSS for the component's own markup: its elements get a class of their own, so
// the rules do not leak to the rest of the page. {expr} in a value comes from code and follows it
comp Ripeness() {
    state percent = 30

    style {
        .ripeness {
            display: grid;
            gap: 8px;
            max-width: 260px;
        }
        .track {
            height: 10px;
            border-radius: 999px;
            background: #f0e3cb;
        }
        .fill {
            width: {percent}%;
            height: 100%;
            border-radius: inherit;
            background: {percent < 70 ? "#8fcf6f" : "#ffad42"};
            transition: width 0.3s;
        }
    }

    return <div class="ripeness">
        <div class="track"><div class="fill" /></div>
        <button onClick={percent = percent >= 100 ? 0 : percent + 35}>
            Ripen: {percent < 70 ? "green" : "ripe"}
        </button>
    </div>
}

document.body.append(<Ripeness />)

The board uses these scoped styles to distinguish open, completed and high-priority tasks without leaking row styles into the surrounding page.

  • {expr} in the value of a declaration comes from code. The value becomes a CSS variable of the element the component returns, and it follows the state, as markup does: press the button.
  • The markup passed in as children and the markup of other components keep their own styles; :global(...) makes a selector one of the whole page.
  • A selector that matches no element of the markup is a warning of the compiler: usually a typo, or the element is in another component.
  • A decorator can have style too, for the markup of its wrapper.

The main way to style components is a CSS module next to them, a plain .css file. The compiler reads it: its classes get names of their own, and styles is the object of those names, typed, so styles.buton is a compile error and styles[kind] is checked by the type of kind. The CSS gets onto the page with the module, in rook build and in Vite alike; the playground has no files, so the code is shown here:

import styles from "./button.module.css"

type Kind "primary" | "ghost"

export comp Button(kind Kind = "primary", pressed bool = false, children Content) {
    return <button
        class={[styles.button, styles[kind], pressed ? styles.pressed : null]}
        aria-pressed={pressed}
    >
        {children}
    </button>
}

Several classes are a list: the nulls and empty strings of it are left out, so a class that depends on a condition is pressed ? styles.pressed : null.