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

y-zustand

v1.1.2

Published

A simple and efficient middleware to synchronize a [Zustand](https://github.com/pmndrs/zustand) store with a [Yjs](https://github.com/yjs/yjs) document.

Readme

y-zustand

A simple and efficient middleware to synchronize a Zustand store with a Yjs document.

This middleware enables real-time collaboration by connecting your Zustand state to a shared Yjs YDoc, allowing state to be seamlessly synced between multiple clients.

Features

  • Connects a Zustand store to a Yjs Y.Doc.
  • Synchronizes state changes in both directions (Zustand -> Yjs and Yjs -> Zustand).
  • Efficiently handles updates, only transmitting changes for modified properties.
  • Correctly handles complex data types like arrays and their methods.
  • Ignores functions in the store, only syncing data properties.
  • Supports partial state syncing with the partialize option.

Installation

npm install y-zustand yjs zustand
# or
yarn add y-zustand yjs zustand
# or
pnpm add y-zustand yjs zustand

Basic Usage

  1. Import the middleware and your Yjs document.
  2. Wrap your Zustand store creator with the syncYjsMiddleware.
import { create } from "zustand";
import * as Y from "yjs";
import { syncYjsMiddleware } from "y-zustand";

// 1. Create a Yjs document
const ydoc = new Y.Doc();

// (Optional) Connect to a provider for network synchronization
// import { WebsocketProvider } from 'y-websocket';
// new WebsocketProvider('ws://localhost:1234', 'my-room-name', ydoc);

// 2. Define your store's interface and initial state
interface MyState {
  count: number;
  name: string;
  increment: () => void;
}

// 3. Create your store, wrapping the creator with the middleware
export const useStore = create<MyState>()(
  syncYjsMiddleware<MyState>(
    ydoc,
    "shared"
  )((set) => ({
    count: 0,
    name: "Alice",
    increment: () => set((state) => ({ count: state.count + 1 })),
  }))
);

Partial State Syncing

You can selectively sync only specific parts of your state using the partialize option. This is useful when you have local-only state that shouldn't be shared with other clients.

import { create } from "zustand";
import * as Y from "yjs";
import { syncYjsMiddleware } from "y-zustand";

const ydoc = new Y.Doc();

interface MyState {
  count: number;
  name: string;
  localOnly: string; // This won't be synced
  increment: () => void;
}

export const useStore = create<MyState>()(
  syncYjsMiddleware<MyState>(ydoc, "shared", {
    // Only sync count and name, omit localOnly
    partialize: (state) => {
      const { localOnly, ...rest } = state;
      return rest;
    },
  })((set) => ({
    count: 0,
    name: "Alice",
    localOnly: "This won't be synced",
    increment: () => set((state) => ({ count: state.count + 1 })),
  }))
);

You can also use a more concise approach with Object.fromEntries:

export const useStore = create<MyState>()(
  syncYjsMiddleware<MyState>(ydoc, "shared", {
    // Omit specific keys from syncing
    partialize: (state) =>
      Object.fromEntries(
        Object.entries(state).filter(([key]) => !["localOnly"].includes(key))
      ),
  })((set) => ({
    count: 0,
    name: "Alice",
    localOnly: "This won't be synced",
    increment: () => set((state) => ({ count: state.count + 1 })),
  }))
);