Solid Primitives

repository·main·Indexed 23 days ago

https://github.com/solidjs-community/solid-primitives

A community-driven collection of high-quality reactive utilities and building blocks designed to extend the core SolidJS library with tertiary primitives for client and server-side functionality. Includes packages for managing active elements, audio playback, autofocus, element bounds tracking, broadcast channels, clipboard interactions, and connectivity status.

Tokens
168.5K
Snippets
515
Records
900
Agent score
80%

What's inside solid-primitives

  1. Overview of Solid Primitives

    main

    Solid Primitives is a collection of high-quality, community-contributed utilities designed to extend the SolidJS ecosystem. It provides a set of 'tertiary primitives' that wrap client and server-side functionality into a fully reactive API layer.

    While not officially maintained by the SolidJS Core Team, the project is managed by members of the SolidJS core and ecosystem teams to ensure it remains in-sync with the framework's evolution. The goal is to provide well-tested, stable, and tree-shakable foundations that improve application readability and performance.

  2. Overview of Solid Primitives packages

    main

    Solid Primitives is a collection of specialized utility packages for SolidJS. Instead of a single monolithic library, it is organized into modular packages that you can install individually based on your needs.

    Key categories include:

    • State & Reactivity: @solid-primitives/memo, @solid-primitives/mutable, @solid-primitives/resource, @solid-primitives/signal-builders, @solid-primitives/state-machine, @solid-primitives/static-store, and @solid-primitives/trigger.
    • Data Structures: @solid-primitives/set.
    • Lifecycle & Scoping: @solid-primitives/rootless.
    • UI Patterns: @solid-primitives/marker and @solid-primitives/masonry.
  3. Overview of @solid-primitives/filesystem adapters

    main

    The @solid-primitives/filesystem package provides a reactive interface for managing different file system access methods through various adapters.

    Available Adapters

    • makeNoFileSystem: Synchronous mock file system.
    • makeNoAsyncFileSystem: Asynchronous mock file system.
    • makeVirtualFileSystem: Virtual file system that can be converted to a FsMap for typescript-vfs via .toMap().
    • makeWebAccessFileSystem (client only): Accesses the actual browser file system via directory picker or webkitdirectory input.
    • makeNodeFileSystem (server only): Abstracts the Node.js fs/promises module.
    • makeTauriFileSystem: Connects to the tauri fs module (requires tauri with fs access enabled).
    • makeChokidarWatcher (experimental): Uses chokidar to watch for file system changes and trigger reactive updates.

    Reactive Creation Methods

    • createFileSystem: A convenience method that calls the correct wrapper for a given adapter.
    • createSyncFileSystem: Wraps a synchronous adapter (like makeNoFileSystem) in a reactive interface.
    • createAsyncFileSystem: Adds a reactive layer to an asynchronous adapter.
  4. Use @solid-primitives/pagination for pagination and infinite scroll

    main

    The @solid-primitives/pagination package provides utilities for managing paginated data and infinite scrolling.

    Key APIs include:

    • createPagination: For standard pagination logic.
    • createSegment: For segment-based navigation.
    • createInfiniteScroll: For implementing infinite scroll behavior.
  5. Use @solid-primitives/websocket to manage connections

    main

    The @solid-primitives/websocket package provides several primitives to establish, maintain, and operate WebSocket connections in SolidJS.

    All primitives return an extended WebSocket instance that includes a .message property. This property is an accessor for the last received message, allowing you to react to incoming data easily.

    Connection Lifecycle Management

    • createWS(url, protocols?): Sets up a connection that automatically disconnects when the Solid component/scope is cleaned up.
    • makeWS(url, protocols?): Sets up a connection with a buffered send (allows calling .send() before the connection is fully open).
    • createReconnectingWS(url, protocols?, options?): A connection that automatically reconnects if it is involuntarily closed, but disconnects on cleanup.
    • makeReconnectingWS(url, protocols?, options?): Similar to createReconnectingWS, but requires manual cleanup.
    • makeHeartbeatWS(reconnectingWS, options?): Wraps a reconnecting WebSocket to send periodic heartbeat messages and reconnect if the server fails to respond.
    const ws = createWS("ws://localhost:5000");
    const state = createWSState(ws);
    const states = ["Connecting", "Connected", "Disconnecting", "Disconnected"];
    
    ws.send("it works");
    
    // Accessing the last message via the .message accessor
    createEffect(on(ws.message, msg => console.log(msg), { defer: true }));
    
    return <p>Connection: {states[state()]}</p>;
  6. Use @solid-primitives/transition-group for list and switch transitions

    main

    The @solid-primitives/transition-group package provides utilities for managing transitions when elements are added, removed, or swapped in a group.

    Key APIs include:

    • createSwitchTransition: For transitions when switching between different elements.
    • createListTransition: For transitions within a list of elements.
  7. Use @solid-primitives/spring for spring physics animations

    main

    The @solid-primitives/spring package provides spring-based physics animations for smooth transitions.

    Key APIs include:

    • createSpring: Creates a spring-based animation.
    • createDerivedSpring: Creates a spring that is derived from another value.
  8. Explore Solid Primitives packages

    main

    Solid Primitives is a collection of modular utility packages for SolidJS. Each package is scoped under @solid-primitives/ and focuses on a specific domain (e.g., Inputs, Display & Media).

    Available Packages by Category

    Inputs

    • @solid-primitives/active-element: createActiveElement, createFocusSignal
    • @solid-primitives/autofocus: autofocus, createAutofocus
    • @solid-primitives/input-mask: createInputMask, createMaskPattern
    • @solid-primitives/keyboard: useKeyDownList, useCurrentlyHeldKey, useKeyDownSequence, createKeyHold, createShortcut
    • @solid-primitives/mouse: createMousePosition, createPositionToElement
    • @solid-primitives/pointer: createPointerListeners, createPerPointerListeners, createPointerPosition, createPointerList
    • @solid-primitives/scroll: createScrollPosition, useWindowScrollPosition
    • @solid-primitives/selection: createSelection

    Display & Media

    • @solid-primitives/audio: makeAudio, makeAudioPlayer, createAudio
    • @solid-primitives/bounds: createElementBounds
    • @solid-primitives/devices: createDevices, createMicrophones, createSpeakers, createCameras, createAccelerometer, createGyroscope
  9. Use @solid-primitives/virtual for virtualized lists

    main

    The @solid-primitives/virtual package provides tools for rendering large lists efficiently by only rendering items currently in view.

    Key APIs include:

    • createVirutalList: A hook for managing virtual list state.
    • VirtualList: A component for rendering virtualized lists.
  10. Use @solid-primitives/gestures for gesture recognition

    main

    The @solid-primitives/gestures package provides helpful directives to react to common user touch and mouse gestures in SolidJS applications. It supports the following gesture types:

    • pan: Detects movement of a finger or pointer across a surface.
    • pinch: Detects zooming in or out using two fingers.
    • rotate: Detects rotation gestures.
    • swipe: Detects quick directional movements.
    • tap: Detects single clicks or taps.