A .tsrx file can hold its own CSS, and those rules style only the elements in that file.
import { state } from '@markless/core';export default functionLaundryfunction Laundry(): Element() @{@{ · Statement containerHolds instructions: setup lines, then the element to show. letloadlet load: string=statestate · Watched valueDeclares a variable the page watches. You read and assign it like any other variable, and the page keeps up.('white'); <section<section> · HTML elementA section of a page, usually with its own heading.class(property) class?: string | undefined={load}> <stylestyle · Scoped stylesA <style> block inside a component. The build gives the file its own class and adds it to every selector, so the rules reach only the elements this file renders.> .pink { background: pink; } </stylestyle · Scoped stylesA <style> block inside a component. The build gives the file its own class and adds it to every selector, so the rules reach only the elements this file renders.> <button<button> · HTML elementA button a person can press.onClickonClick · Event handlerRuns when this element gets the DOM event named after on, with the browser's own event object.={() => (load = 'pink')}> Add one red sock </button<button> · HTML elementA button a person can press.> </section<section> · HTML elementA section of a page, usually with its own heading.>}
The button changes load to 'pink'. class={load} puts that class on the <section>, and the .pink rule in the <style> block turns it pink.
Two files can use the same class name
In plain CSS, two files that both write a .card rule fight over every card on the page. In Markless a file's rules reach only the elements in that file, so each card keeps its own look.
Pick a card, as you would with the browser's inspector.
A shop page with a profile card and a product card, and below it the browser's element inspector: an elements tree on the left and the rules for the picked element on the right. Both cards have the class card. Press a card, or its row in the tree, to pick it: the inspector shows the class Markless added to that card and the one rule that reaches it, with the file it came from.
Each card has a second class starting mk-, unique to its file. The file's .card rule became .card.mk-…, so it only matches that file's card.
Where does the mk- class come from?
Markless makes it from the file's path, so every file gets a different one. It adds the class to each element in the file and to the end of each rule in the file's <style> block.
The look changes when the state does
Put state in class, or in an attribute of your own like data-costume, and the look changes whenever the state does.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { element, state } from '@markless/core';import { next } from './costumes.ts';import type { Costume } from './costumes.ts';import * as curtain from './curtain.ts';export default function QuickChange() @{ let costume = state<Costume>('king'); const booth = element<HTMLElement>(); <sectionclass="quick-change" el={booth}> <style> [data-costume='king'] { --look: 0; } [data-costume='cow'] { --look: 1; } [data-costume='tree'] { --look: 2; } [data-costume='ghost'] { --look: 3; } </style> <iclass="actor" data-costume={costume} /> <iclass="shadow"onAnimationEnd={() => { costume = next(costume); curtain.open(booth); }} /> <buttonclass="curtain" aria-label="Curtain"onPointerDown={(event) => curtain.grab(event, booth)}onPointerMove={curtain.move}onPointerUp={curtain.drop}onClick={() => curtain.close(booth)} /> <p>Now playing: the {costume}</p> </section>}
export const costumes = [ 'king', 'cow', 'tree', 'ghost',] as const;export type Costume = (typeof costumes)[number];// After the last part, the play starts again.export function next(costume: Costume): Costume { const at = costumes.indexOf(costume) + 1; return costumes[at % costumes.length] ?? 'king';}
type Booth = HTMLElement | undefined;// Where the pull started, how far it has gone,// and the booth it belongs to.let grip: { x: number; unit: number } | undefined;let pulled = 0;let dragged = false;let scene: Booth;// The curtain is 36 units wide drawn back and 156// across the booth.const travel = 120;function pull(booth: HTMLElement, to: number) { pulled = Math.min(1, Math.max(0, to)); booth.style.setProperty('--pull', `${pulled}`);}function shut(booth: HTMLElement) { pull(booth, 1); booth.classList.add('shut');}export function open(booth: Booth) { if (!booth) return; booth.classList.remove('shut'); pull(booth, 0);}export function grab( event: PointerEvent, booth: Booth,) { const edge = event.currentTarget; if (!(edge instanceof HTMLElement)) return; if (!booth) return; if (booth.classList.contains('shut')) return; edge.setPointerCapture(event.pointerId); const { width } = booth.getBoundingClientRect(); grip = { x: event.clientX, unit: width / 240 }; dragged = false; scene = booth;}export function move(event: PointerEvent) { if (!grip || !scene) return; if (!event.buttons) return drop(); const moved = event.clientX - grip.x; const units = moved / grip.unit; if (Math.abs(units) > 3) dragged = true; if (!dragged) return; scene.classList.add('pulling'); pull(scene, units / travel);}// Let go past halfway and it closes; short of// that, it falls back open.export function drop() { if (!grip || !scene) return; grip = undefined; scene.classList.remove('pulling'); if (!dragged) return; if (pulled > 0.5) return shut(scene); pull(scene, 0);}// A press with no drag, or Enter, closes it.export function close(booth: Booth) { if (dragged) { dragged = false; return; } if (booth) shut(booth);}
To do: Pull the curtain across the booth. He comes back out as the cow.
To do: Keep pulling it until the ghost comes out.
To do: In the <style> block, change the cow's --look: 1 to --look: 3. The cow now comes out in a sheet.
Send a number to your CSS
When a look depends on a number, like how bright or how far, pass the number to your CSS as a variable: style={'--light:' + light + '%'}. Your CSS reads it with var(--light).
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
/* The drawing is 240 x 180 units. */.cantina { position: relative; container-type: inline-size; width: 100%; max-width: 26rem; margin: 0 auto; font-family: 'Shantell Sans', sans-serif; text-align: center; user-select: none;}.cantina i,.cantina i::before,.cantina i::after,.cantina::after { position: absolute; top: 0; left: 0; width: 100%; aspect-ratio: 240 / 180; background: 0 0 / 100% no-repeat;}.cantina .room { position: relative; display: block; background-image: url(/examples/ink/st-cantina-room.svg);}/* The hunter sits over the booth, upright before the lights go out, slumped after. */.cantina .room::before { content: ''; background-image: url(/examples/ink/st-cantina-hunter.svg);}.cantina .room::after { content: ''; background-image: url(/examples/ink/st-cantina-slumped.svg); opacity: 0;}.cantina[data-shot='true'] .room::before { opacity: 0;}.cantina[data-shot='true'] .room::after { opacity: 1;}.cantina .glow { background-image: url(/examples/ink/st-cantina-glow.svg);}.cantina .lamp { background-image: url(/examples/ink/st-cantina-lamp.svg);}.cantina .eyes { background-image: url(/examples/ink/st-cantina-his-eyes.svg);}.cantina .eyes::after { content: ''; background-image: url(/examples/ink/st-cantina-hunter-eyes.svg);}/* Afterwards, one pair of eyes fewer. */.cantina[data-shot='true'] .eyes::after { opacity: 0;}.cantina[data-shot='true'] p::after { content: '. Nobody saw a thing.';}/* The slider's plate stays lit, so you can find it in the dark. */.cantina::after { content: ''; background-image: url(/examples/ink/st-cantina-plate.svg); pointer-events: none;}/* The slider runs up and down the plate. */.cantina .dimmer { position: absolute; z-index: 1; top: 21.67cqw; left: 80.83cqw; width: 16.67cqw; height: 31.67cqw; margin: 0; writing-mode: vertical-lr; direction: rtl; appearance: none; background: none; cursor: grab; touch-action: none;}.cantina .dimmer:active { cursor: grabbing;}.cantina .dimmer::-webkit-slider-thumb { width: 14.17cqw; height: 6.67cqw; border: 0; appearance: none; background: center / contain no-repeat url(/examples/ink/st-cantina-knob.svg);}.cantina .dimmer::-moz-range-thumb { width: 14.17cqw; height: 6.67cqw; border: 0; border-radius: 0; background: center / contain no-repeat url(/examples/ink/st-cantina-knob.svg);}.cantina .dimmer:focus-visible { outline: 2px dashed currentColor; outline-offset: 2px;}.cantina p { margin: 0.4rem 0 0;}@media (prefers-reduced-motion: no-preference) { .cantina .room { transition: filter 0.2s; } .cantina .glow { transition: opacity 0.2s; } /* A beat in the dark, one flash, and then one pair of eyes is gone. */ .cantina[data-shot='true'] .lamp::after { content: ''; background-image: url(/examples/ink/st-cantina-flash.svg); opacity: 0; animation: cantina-flash 0.25s 0.5s ease-out; } .cantina[data-shot='true'] .eyes::after, .cantina[data-shot='true'] .room::before, .cantina[data-shot='true'] .room::after { transition: opacity 0s 0.6s; }}@keyframes cantina-flash { 30% { opacity: 1; }}
Resize code and preview
Lights: 100%
Try this
To do: Slide the lights all the way down.
To do: In the <style> block, change brightness to saturate. Now the slider drains the color from the room instead.
Can style take an object?
Yes. Write property names in camelCase. A bare number gets px unless the property takes plain numbers, like opacity. --name keys stay as they are, and null leaves a property out.
<p<p> · HTML elementA paragraph.stylestyle · Scoped stylesA <style> block inside a component. The build gives the file its own class and adds it to every selector, so the rules reach only the elements this file renders.={{ marginTop: 8, opacity: faded ? 0.5 : 1, '--hue': hue }} />
Styles for every page go in a CSS file
For styles the whole site shares, like fonts and colors, write an ordinary .css file and import it once in document.tsrx:
import './global.css';
Another file's elements keep their own look
Santa comes from another file, santa.tsrx. Two things in the globe's CSS treat him differently:
A rule stops at the file's own elements..roof paints the cottage blue and leaves Santa's workshop alone.
A CSS variable passes through.--snow reaches Santa too.
Shake the globe by dragging it.
An ink drawing of a snow globe on a shelf, its base plaque reading globe.tsrx. Inside stand the maker's cottage, whose roof is painted blue, and a fir tree. Glued in beside them is Santa, in his red suit with a sack over his shoulder, and his little wooden workshop, on their own round stand labelled santa.tsrx. Both roofs carry the class .roof, but only the cottage roof is blue. Drag the globe side to side, or focus it and press Space or an arrow key, to shake it: the flakes swirl and fall, --snow goes up, and snow settles on everything, Santa and his workshop too.
What about dark mode?
Markless has no theme setting of its own. Start a rule with the class your site puts on <html>, as in .dark .card, or wrap it in @media (prefers-color-scheme: dark). Both work inside a <style> block.
Coming from React or CSS modules?
Write class, not className. It takes a string.
style={{ marginTop: 8 }} works as it does in React.
A file's styles reach only that file, like CSS modules, with nothing to import.
There is no :global. Site-wide rules go in a .css file.