Styling

4 min·Assumes: state, events

A component can hold its own CSS, and those rules style only the elements in its 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-, 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.

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

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.

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 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 style={{ 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.