Styling

4 min·Assumes: state, events

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 function Laundry() @{
	let load = state('white');

	<section class={load}>
		<style>
			.pink {
				background: pink;
			}
		</style>
		<button onClick={() => (load = 'pink')}>
			Add one red sock
		</button>
	</section>
}

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.

Mira HoltTeapot$24
<div class="card mk-u2nn94">…</div><div class="card mk-1aip96a">…</div>
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.

PlaygroundRestored your edit

Now playing: the king

Try this
  1. To do: Pull the curtain across the booth. He comes back out as the cow.
  2. To do: Keep pulling it until the ghost comes out.
  3. 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).

PlaygroundRestored your edit

Lights: 100%

Try this
  1. To do: Slide the lights all the way down.
  2. 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 style={{ 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.

Next, call an element's own methods, like focus() and play().

Button sounds are from Josh Comeau's Button Sounds library.