A component can hold its own CSS, and those rules style only the elements in its 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-, and so does its rule. Markless makes that class from the file's path and adds it to the file's elements and to the end of each of its rules.
The look changes when the state does
Give class, or an attribute of your own such as data-costume, a value from state. When the state changes, Markless updates the attribute, and the CSS rule for the new value applies.
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);}
/* The booth is 240 x 236 units of --u. */.quick-change { --u: calc(100cqw / 240); container-type: inline-size; position: relative; width: 100%; max-width: 28rem; margin: 0 auto; background: 0 0 / 100% no-repeat url(/examples/ink/st-quick-change-booth.svg); font-family: 'Shantell Sans', sans-serif; text-align: center; user-select: none;}/* He and his shadow stand in the same spot. */.quick-change .actor,.quick-change .shadow { position: absolute; top: calc(50 * var(--u)); left: calc(70 * var(--u)); width: calc(100 * var(--u)); height: calc(170 * var(--u)); margin: 0;}/* Four costumes on one sheet: --look picks which one. */.quick-change .actor { background: url(/examples/ink/st-quick-change-actor.svg) calc(var(--look) * 100% / 3) 0 / 400% 100% no-repeat;}.quick-change .curtain { position: absolute; z-index: 1; top: calc(18 * var(--u)); left: calc(42 * var(--u)); width: calc( (36 + 120 * var(--pull, 0)) * var(--u) ); height: calc(198 * var(--u)); padding: 0; border: 0; background: url(/examples/ink/st-quick-change-curtain.svg) 0 0 / 100% 100% no-repeat; cursor: grab; touch-action: none;}.quick-change .curtain:focus-visible { outline: 2px dashed; outline-offset: 3px;}.quick-change.shut .curtain { cursor: wait;}/* Seen through the cloth while he changes. */.quick-change .shadow { z-index: 2; background: url(/examples/ink/st-quick-change-shadow.svg) 0 0 / 300% 100% no-repeat; opacity: 0; pointer-events: none;}.quick-change.shut .shadow { animation: quick-change-shadow 0.6s;}.quick-change p { margin: calc(240 * var(--u)) 0 0; font-weight: 700;}@keyframes quick-change-shadow { 15%, 85% { opacity: 0.3; }}@media (prefers-reduced-motion: no-preference) { .quick-change:not(.pulling) .curtain { transition: width 0.2s ease-out; } .quick-change.shut .shadow { animation-name: quick-change-scramble; }}/* He hauls, hops and ducks into the next one. */@keyframes quick-change-scramble { 15%, 85% { opacity: 0.3; } 0%, 32% { background-position-x: 0; translate: 0 0; } 33%, 65% { background-position-x: 50%; translate: calc(-6 * var(--u)) calc(-4 * var(--u)); } 66%, 100% { background-position-x: 100%; translate: calc(4 * var(--u)) 0; }}
Resize code and preview
Now playing: the king
Try this
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
Put a value from state in style as a CSS variable, a property whose name starts with --, and your CSS can turn it into a colour, a size or a position. The element stays on the page while the value changes, so a CSS transition plays.
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 colour 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
A .css file you import is not limited to one file's elements. Import it once in document.tsrx for fonts, colours on :root and classes every page shares.
import './global.css';
A component inside yours keeps its own look
Your rules stop at the elements of a component from another file, even when you put it inside yours. A CSS variable you set on an element around it does reach it.
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, and give it a string. style={{ marginTop: 8 }} works as it does in React. Like CSS modules, each file gets one added class. There is no :global: put rules for the whole page in a CSS file, and give a component its own file to give it its own styles.
Components shows how to put one component inside another.