Preact Signals

repository·main·Indexed 26 days ago

https://github.com/preactjs/signals

A high-performance state management library providing fine-grained reactivity for web applications, designed to work with Preact and React. The ecosystem includes @preact/signals-debug for console tracking, @preact/signals-devtools-adapter for communication logic, and @preact/signals-devtools-ui for visualizing signal updates and dependency graphs via a Chrome extension or embedded UI.

Tokens
20.4K
Snippets
47
Records
142
Agent score
87%

What's inside preact-signals

  1. Overview of Signals

    main

    Signals is a performant state management library designed for two primary goals:

    1. Simplified Business Logic: It allows for easy state management from small to complex applications. Updates are automatically optimized to trigger the fewest necessary re-renders. Signals are lazy by default and automatically skip updates if no listeners are present.
    2. Seamless Framework Integration: Signals are designed to integrate into frameworks (like Preact and React) as if they were native primitives. You can access signals directly, and components will automatically re-render when the signal's value changes without requiring manual selectors or wrapper functions.
  2. Debug features of @preact/signals-debug

    main

    When enabled, @preact/signals-debug provides the following debugging capabilities:

    • Value Changes: Tracks and logs all signal value changes.
    • Effect Tracking: Monitors effect executions and their dependencies.
    • Computed Values: Tracks computed value recalculations and dependencies.
    • Update Grouping: Groups related updates for better visualization in the console.
    • Performance Stats: Provides real-time statistics including active trackers and subscription counts.
  3. Install @preact/signals-agent-vite

    main

    Install the Vite plugin as a development dependency. If you want to use the built-in Babel transform integration, you must also install the matching transform package for your framework.

    # Install the agent
    pnpm add -D @preact/signals-agent-vite
    
    # Install the matching transform (choose one)
    # React
    pnpm add -D @preact/signals-react-transform
    
    # Preact
    pnpm add -D @preact/signals-preact-transform
  4. Control and filter signal updates in DevTools

    main

    The Preact Signals extension provides several controls within the DevTools panel to manage how you view data:

    • Pause/Resume: Temporarily stop receiving real-time updates.
    • Clear: Reset the current update history.
    • Filter: Use regex patterns to filter the view and show only specific signals.
    • Settings: Configure grouping, rate limiting, and other monitoring options.
  5. Configure @preact/signals-agent-vite in Vite

    main

    Import signalsVite from @preact/signals-agent-vite and add it to your vite.config.ts plugins array. You must specify the framework option (either "react" or "preact") to enable the built-in transform layer.

    import { defineConfig } from "vite";
    import { signalsVite } from "@preact/signals-agent-vite";
    
    export default defineConfig({
    	plugins: [signalsVite({ framework: "preact" })],
    });
  6. Configure @preact/signals-preact-transform in Babel

    main

    To use the transform plugin, add module:@preact/signals-preact-transform to your Babel configuration plugins array.

    Note: Since this is a development plugin, it is recommended to remove it from your configuration when building for production to minimize bundle size.

    // babel.config.js
    module.exports = {
    	plugins: [["module:@preact/signals-preact-transform"]],
    };
  7. Quick Start: Embed DevTools in a page

    main

    To embed the DevTools directly into a web page (e.g., for demos or blog posts), use createDirectAdapter to connect directly to signals on the page and call mount to render the UI into a container element.

    Important: Ensure @preact/signals-debug is imported in an entry point to ensure signals are registered as early as possible.

    import { mount } from "@preact/signals-devtools-ui";
    import { createDirectAdapter } from "@preact/signals-devtools-adapter";
    import "@preact/signals-devtools-ui/styles";
    
    // Create a direct adapter (connects directly to signals on the page)
    const adapter = createDirectAdapter();
    
    // Mount the DevTools UI
    const unmount = await mount({
    	adapter,
    	container: document.getElementById("devtools-container")!,
    });
    
    // Later, to cleanup:
    unmount();
  8. Quick Start: Use DevTools in an iframe

    main

    To use the DevTools inside an iframe, use the createPostMessageAdapter. You must specify the sourceWindow, sourceOrigin, targetWindow, and targetOrigin to facilitate communication between the iframe and the host page.

    import { mount } from "@preact/signals-devtools-ui";
    import { createPostMessageAdapter } from "@preact/signals-devtools-adapter";
    import "@preact/signals-devtools-ui/styles";
    
    const adapter = createPostMessageAdapter({
    	sourceWindow: window,
    	sourceOrigin: "https://your-app.com",
    	targetWindow: window.parent,
    	targetOrigin: "https://your-app.com",
    });
    
    await mount({
    	adapter,
    	container: document.getElementById("devtools")!,
    });