Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

rocx · Roc Solid's markup

Write pages and components in Roc. Render them on the server. Run them in the browser.

A .rocx file is Roc with markup wherever an expression can go; pages, layouts and components are all written this way. rocx build turns it into plain Roc, line for line, for the Roc compiler to check. The server renders a page to HTML, and the browser runs the same Roc, compiled to WebAssembly, to keep it live.

Learn the syntaxComponents

Why rocx#

Checked by Roc

Every {hole} is a Roc expression the compiler checks: a hole shows text, a number or markup; a list hole takes a List(Html), an attribute a Str, a handler a function of the right type. Errors point at your .rocx line, quoting your code.

Safe by construction

Html is opaque: no string can become markup. Text and attribute values are escaped, URLs that could run script are replaced, and handlers are Roc functions, never script in an attribute (safety).

Line for line

Line N of the generated Roc is line N of your file, so compiler reports, dbg and crash messages point to the right place, with no source maps to break.

Fine-grained, end to end

As in SolidJS, a component runs once. A change reruns only the holes that read it, each patching its own node; nothing is re-rendered to find what changed.

Pages, components, endpoints#

Pages

A page is a file, routes/…/+page.rocx, at the URL its folders spell. It is a component the server renders for each request, inside the layouts above it, and the browser hydrates. What it wraps in <NoHydration> stays the server's HTML.

Routing →
view : { name : Str, link : Str } -> Html
view = |{ name, link }|
	<p class="greeting">
		Hello, <a href={link} title="Their profile">{name}</a>!
	</p>

Components

SolidJS 2.0's model: a component runs once and may make signals, values that change, and the holes that read them rerun. A page places a component like an element.

Components →
view! = |{ start_value }| {
	(count!, set_count!) = create_signal!(start_value)
	<div class="counter" class:negative={count!() < 0}>
		<button type="button" on:click={|| set_count!(count!() - 1)} aria-label="One less">−</button>
		<output>{count!().to_str()}</output>
		<button type="button" on:click={|| set_count!(count!() + 1)} aria-label="One more">+</button>
	…
	</div>
}

Endpoints

A route's +server.roc holds its endpoints: a function per HTTP method at a URL, typed at both ends. A component reads a GET as a plain value; the server answers it while rendering, and the page carries the answer to the browser. The handler runs only on the server and isn't in the browser's download.

Talking to the server →
get! = |{ params, .. }| Ok("Echo: ${params.message}")

echo! = |message| Api.get_api_echo_message({ message: message }).read!()

A file, and the Roc it becomes#

The counter on the front page is this file:

site/Demos/Counter.rocx
import pf.Html
import pf.Solid exposing [create_signal!]

## The smallest component: a number, and buttons that change it. The number is a signal; what reads it
## (the text, the class, the Reset button's `disabled`) runs again when it changes, and patches its
## own node. The component itself runs once. Its styles are its own: the `<style>` in its block is
## scoped to its markup, so `.counter button` here is no other component's button.
Counter := [].{
	view! : { start_value : I64 } => Html
	view! = |{ start_value }| {
		(count!, set_count!) = create_signal!(start_value)
		<div class="counter" class:negative={count!() < 0}>
			<button type="button" on:click={|| set_count!(count!() - 1)} aria-label="One less">−</button>
			<output>{count!().to_str()}</output>
			<button type="button" on:click={|| set_count!(count!() + 1)} aria-label="One more">+</button>
			<button type="button" class="reset" on:click={|| set_count!(0)} disabled?={count!() == 0}>
				Reset
			</button>
		</div>
	}

	<style>
		/* The number between its two buttons, and Reset under the three, quieter: all in the middle
		   of where it is put. A grid, so the markup stays the three and Reset in a row. */
		.counter {
			display: grid;
			grid-template-columns: auto auto auto;
			justify-content: center;
			align-items: center;
			gap: var(--space-4) var(--space-5);
		}

		.counter button {
			width: 3rem;
			height: 3rem;
			padding: 0;
			border: 1px solid var(--fill-strong);
			border-radius: 50%;
			background: var(--fill);
			color: var(--text);
			font-size: 1.375rem;
			font-weight: 500;
			line-height: 1;
			cursor: pointer;
			transition:
				background-color 120ms,
				border-color 120ms,
				color 120ms;
		}

		.counter button:hover:not(:disabled) {
			border-color: color-mix(in srgb, var(--accent) 45%, transparent);
			background: var(--accent-soft);
			color: var(--accent);
		}

		.counter button:focus-visible {
			outline: 2px solid var(--accent);
			outline-offset: 2px;
		}

		.counter button:disabled {
			opacity: 0.35;
			cursor: default;
		}

		/* Under the three, as wide as its word. */
		.counter .reset {
			grid-column: 1 / -1;
			justify-self: center;
			width: auto;
			height: 2rem;
			padding: 0 var(--space-4);
			border-radius: 999px;
			background: transparent;
			color: var(--text-muted);
			font-size: var(--size-sm);
			letter-spacing: 0.02em;
		}

		/* Wide enough for -99 to 999, so the buttons stay where they are as it counts. */
		.counter output {
			min-width: 3ch;
			font: 700 3rem/1 var(--font);
			font-variant-numeric: tabular-nums;
			letter-spacing: -0.03em;
			text-align: center;
		}

		.counter.negative output {
			color: var(--bad);
		}
	</style>
}

rocx build writes Counter.roc beside it. Its lines 9 to 20, the same lines as above, with the markup turned into calls to the platform's Html module:

Counter.roc, as rocx build writes it
view! : { start_value : () => I64 } => Html
view! = |{ start_value: start_value! }| {
	(count!, set_count!) = create_signal!(start_value!())
	Html.element("div", [Html.attr_literal("class", "counter rx-49lxy6"), Html.class_of("negative", || count!() < 0),], [
		Html.element("button", [Html.attr_literal("type", "button"), Html.on("click", || set_count!(count!() - 1)), Html.attr_literal("aria-label", "One less"), Html.attr_literal("class", "rx-49lxy6"),], [Html.text("−"),]), Html.text(" "),
		Html.element("output", [Html.attr_literal("class", "rx-49lxy6"),], [Html.hole_of(|| Html.hole(count!().to_str())),]), Html.text(" "),
		Html.element("button", [Html.attr_literal("type", "button"), Html.on("click", || set_count!(count!() + 1)), Html.attr_literal("aria-label", "One more"), Html.attr_literal("class", "rx-49lxy6"),], [Html.text("+"),]), Html.text(" "),
		Html.element("button", [Html.attr_literal("type", "button"), Html.attr_literal("class", "reset rx-49lxy6"), Html.on("click", || set_count!(0)), Html.flag_of("disabled", || count!() == 0),], [
			Html.text("Reset"),
		]),
	])
}

A route file's Roc goes into the app's Generated/ folder instead. The syntax page covers everything you can write in markup, tooling the commands, and safety what rocx refuses to build, and why.

rocx's pages#