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

@opencookies/svelte

v0.0.2

Published

Svelte adapter for OpenCookies. Runes-first for Svelte 5; ships a `Readable<ConsentState>` fallback at `@opencookies/svelte/stores` for Svelte 4. Wraps [`@opencookies/core`](../core/).

Downloads

31

Readme

@opencookies/svelte

Svelte adapter for OpenCookies. Runes-first for Svelte 5; ships a Readable<ConsentState> fallback at @opencookies/svelte/stores for Svelte 4. Wraps @opencookies/core.

Install

bun add @opencookies/core @opencookies/svelte

Peer dependencies: svelte >= 4.

Setup (Svelte 5 runes)

Call setOpenCookiesContext once in a root component (e.g., +layout.svelte for SvelteKit):

<script lang="ts">
  import { setOpenCookiesContext } from "@opencookies/svelte";
  import type { Category } from "@opencookies/core";

  const categories: Category[] = [
    { key: "essential", label: "Essential", locked: true },
    { key: "analytics", label: "Analytics" },
    { key: "marketing", label: "Marketing" },
  ];

  setOpenCookiesContext({ config: { categories } });

  let { children } = $props();
</script>

{@render children?.()}

You can pass a pre-created store with setOpenCookiesContext({ store }) instead.

API

getConsent()

Returns a reactive object whose properties are tracked via $state. Read directly in markup — no destructuring required to keep reactivity.

<script lang="ts">
  import { getConsent } from "@opencookies/svelte";

  const consent = getConsent();
</script>

{#if consent.route === "cookie"}
  <div class="banner">
    <button onclick={consent.acceptNecessary}>Necessary only</button>
    <button onclick={consent.acceptAll}>Accept all</button>
    <button onclick={() => consent.setRoute("preferences")}>Customize</button>
  </div>
{/if}

getCategory(key)

Granular per-category access.

<script lang="ts">
  import { getCategory } from "@opencookies/svelte";

  const analytics = getCategory("analytics");
</script>

<label>
  <input type="checkbox" checked={analytics.granted} onchange={analytics.toggle} />
  Analytics
</label>

<ConsentGate>

Renders the children snippet when an expression is satisfied; renders fallback snippet otherwise.

<script lang="ts">
  import { ConsentGate } from "@opencookies/svelte";
  import Chart from "./Chart.svelte";
  import EnablePrompt from "./EnablePrompt.svelte";
</script>

<ConsentGate requires="analytics">
  {#snippet children()}
    <Chart />
  {/snippet}
  {#snippet fallback()}
    <EnablePrompt />
  {/snippet}
</ConsentGate>

<ConsentGate requires={{ and: ["analytics", "marketing"] }}>
  {#snippet children()}
    <PersonalizedPromo />
  {/snippet}
</ConsentGate>

SvelteKit (SSR + hydration)

Call setOpenCookiesContext from your root layout. It uses Svelte's setContext, so it hydrates safely:

<!-- src/routes/+layout.svelte -->
<script lang="ts">
  import { setOpenCookiesContext } from "@opencookies/svelte";
  import { categories } from "$lib/cookies";

  setOpenCookiesContext({ config: { categories } });

  let { children } = $props();
</script>

{@render children()}

Svelte 4 stores fallback

For Svelte 4 codebases (or when you prefer $store syntax), import from the /stores subpath:

<script>
  import { createConsentReadable } from "@opencookies/svelte/stores";

  const consent = createConsentReadable({
    config: { categories: [/* ... */] },
  });

  $: route = $consent.route;
</script>

{#if route === "cookie"}
  <button on:click={consent.acceptAll}>Accept all</button>
{/if}

createConsentReadable returns a Readable<ConsentState> augmented with the same action methods as getConsent() (acceptAll, toggle, save, has, etc.).

Shared concepts

Categories, GPC handling, jurisdiction resolvers, re-consent triggers, script gating (gateScript), and storage adapters all live in @opencookies/core — the Svelte adapter is a thin reactivity wrapper. A working example is in examples/svelte.

See also

License

Apache-2.0