npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sveltebase/state

v0.4.6

Published

Small state helpers for Svelte 5.

Downloads

1,969

Readme

@sveltebase/state

Small state helpers for Svelte 5.

This package exports two classes:

  • State<T>: simple reactive in-memory state
  • PersistentState<TSchema>: reactive state backed by cookies and validated with a Standard Schema-compatible validator

Install

bun add @sveltebase/state svelte

If you want to use zod as your schema library:

bun add zod

Exports

  • State
  • PersistentState

State

A tiny wrapper around a value.

import { State } from "@sveltebase/state";

const count = new State(0);

count.current = 1;
count.set((value) => value + 1);

console.log(count.current); // 2

API

  • new State(initialValue)
  • state.current
  • state.set(updater)

PersistentState

Cookie-backed state with schema validation.

It:

  • reads from cookies
  • validates with a Standard Schema-compatible validator
  • writes updates back to cookies
  • can load the initial value during SSR
import { z } from "zod";
import { PersistentState } from "@sveltebase/state";

const themeSchema = z.enum(["light", "dark"]).default("light");

export const theme = new PersistentState("theme", themeSchema);

theme.current = "dark";
theme.set((value) => (value === "dark" ? "light" : "dark"));

SSR setup

For SSR, return all cookies from +layout.server.ts, then initialize the state with state.init(() => data.cookies) so it loads the server value first.

src/routes/+layout.server.ts

export async function load({ cookies }) {
  return {
    cookies: cookies.getAll()
  };
}

src/routes/+layout.svelte

<script lang="ts">
  import type { LayoutData } from "./$types";
  import { locale } from "$lib/state";

  let { data }: { data: LayoutData } = $props();

  locale.init(() => data.cookies);
</script>

<slot />

src/lib/state.ts

import { z } from "zod";
import { PersistentState } from "@sveltebase/state";

const localeSchema = z.enum(["en", "uz"]).default("en");

export const locale = new PersistentState("locale", localeSchema);

With this setup:

  • on the server, init reads from data.cookies
  • the initial SSR render uses that cookie value
  • in the browser, updates keep syncing back to cookies

API

new PersistentState(key, schema)

Creates a persistent state value.

  • key: cookie name
  • schema: Standard Schema-compatible validator used for parsing and validation, such as a zod schema

persistentState.current

Gets or sets the current value.

persistentState.set(updater)

Updates the current value.

persistentState.init(cookies)

Loads the value from server cookies.

It accepts either:

  • a cookie array
  • a getter function like () => data.cookies

Expected cookie shape:

type Cookie = {
  name: string;
  value: string;
};

Notes

  • PersistentState stores JSON in cookies
  • invalid cookie data falls back to the schema result
  • init(...) is for server-side initialization
  • any Standard Schema-compatible library can be used here, including zod
  • this package is designed for Svelte 5