Storage

3 min·Assumes: state, conditionals

Storage keeps a choice after a reload, and the next time the user comes back.
import { storage } from '@markless/core';

export default function Intro() @{
	let seen = storage('seen-intro', 'no');

	@if (seen === 'no') {
		<section>
			<video src="/intro.mp4" autoplay />

			<button onClick={() => (seen = 'yes')}>
				Skip intro. Forever.
			</button>
		</section>
	} @else {
		<p>Straight to the good part.</p>
	}
}

storage() is state the browser keeps. It takes two things:

  • A key, 'seen-intro': the name the value is saved under.
  • A starting value, 'no': what a first-time user gets.

Read and change seen like any state.

Can I leave the storage key out?
import { storage } from '@markless/core';

export default function Checkout() @{
	let shown = storage('checkout-help', 'on');
	let tips = storage('on');

	<p>Help {shown}, tips {tips}</p>
}

The second has no key, so Markless names it after the variable: markless:tips. Rename the variable and users lose what they saved, so write the key yourself for anything you ship.

The key and the starting value are text in quotes; anything else stops the build.

A storage value is still there after a reload

Reloading a page starts it over. What happens to a value depends on how you made it:

  • state() goes back to its starting value.
  • storage() comes back as the user left it.

Play Snake, then reload this page. The score is gone, and the best is still there.

PlaygroundRestored your edit

Score 0 Best 0

Press an arrow to play.

Try this
  1. To do: Play until you set a best score.
  2. To do: Reload this page to switch the console off and on: the score is gone, the best is still on the card.

A first-time user gets the starting value

Nothing is saved until the user changes the value. After that, every visit opens with theirs.

This radio is new, so it is on its factory station. Tune it to a station, then press Reset to make it new again.

PlaygroundRestored your edit

Factory station: 87.5, static.

Try this
  1. To do: Turn the tuning knob to 94.2, Harbour Jazz.
  2. To do: Press Reset. The radio is back on its factory station, as on a first visit.

A saved value is ready before the page is shown

A user saved night mode. Read that too late, and the page shows day first, then snaps to night. storage() reads it before the page appears.

Press Play. The left page uses storage(); the right one reads too late.

Two browser windows show the same small site. The left one reads its theme with storage('mode', 'day'); the right one reads it after the page shows. Under them, a time slider covers the first 500 milliseconds after a reload, with a Play button that runs it at real speed. A sun and moon switch saves the theme.

How do I style the page by a saved value?

Use it like any other value. Here it is a class name:

let mode = storage('mode', 'day');

<main class={mode}>
	<h1>The Pondside Post</h1>
</main>

Then style that class:

.night {
	background: black;
}

One key is one saved value, on every page

A value saved on one page can be read on any other page. Use the same key.

Both pages of this newspaper ask about cookies with the same key. Press Accept, then open Sport: it does not ask again.

PlaygroundRestored your edit

pondside-post.example/

The Pondside Post

Goose wins seat on town council

Cheese wins the cheese race, again

Try this
  1. To do: Accept on the front page, then open Sport: no banner.
  2. To do: In pages.tsrx, change Sport's key to 'sport-cookies'. Press Cookies: ask again, accept on the front page, then open Sport: it asks again.
What if the page is open in two tabs?

Every tab shows the same value. You write nothing for this.

  • Change it in tab A: tab B shows the new value straight away.
  • Change it in both: the last change wins, in every tab.
  • The user is typing in tab B: the field keeps their text until they leave it.
Can storage keep a number or a list?

Storage keeps text. Turn the value into text to save it, and back to read it.

A number:

best = String(12);
Number(best) + 1; // 13

A list:

saved = JSON.stringify(['milk', 'eggs']);
JSON.parse(saved); // ['milk', 'eggs']

Skip the step back, and best + 1 is '121'.

Coming from React?

In React:

const [mode, setMode] = useState('day');

useEffect(() => {
	const saved = localStorage.getItem('mode');
	if (saved) setMode(saved);
}, []);

useEffect(() => {
	localStorage.setItem('mode', mode);
}, [mode]);

Here:

let mode = storage('mode', 'day');

The React page shows 'day' until the first effect runs.

When several components need the same value while the page is open, share it with shared().

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