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 🙏

© 2024 – Pkg Stats / Ryan Hefner

store2state-dom

v0.0.3

Published

An extension of the `store2state` library that provides a straightforward way to bind application state changes to DOM elements in vanilla JavaScript

Downloads

5

Readme

Store2State DOM Subscriber

The Store2State DOM Subscriber is an extension of the store2state library that provides a straightforward way to bind application state changes to DOM elements in vanilla JavaScript. This utility allows you to manage UI updates efficiently by subscribing to state changes and reacting accordingly.

Features

  • DOM Binding: Easily bind application state to DOM elements.
  • Custom Actions: Define and execute actions when the state changes.
  • Event Handling: Attach event listeners to DOM elements for interactive applications.
  • Dynamic Updates: Automatically update the DOM in response to state changes.

Installation

Ensure store2state is installed in your project:

# Install store2state if not already installed
npm install store2state

Install store2state-dom package in your project:

npm install store2state-dom

Usage

Creating a Store

Begin by creating a store with an initial state using store2state:

import { createStore } from 'store2state';

const initialState = {
  count: 0,
  message: 'Hello, World!',
};

const store = createStore(initialState);

Creating a Subscriber

Use the createSubscriber function to create a new subscriber for a DOM element:

import { createSubscriber } from './path-to-subscriber-file';

const displayElement = document.getElementById('display');

const subscriber = createSubscriber(store, displayElement);

Adding Subscribers

Add actions to be executed when the state changes:

subscriber.addSubscriber((ele, state) => {
  ele.textContent = `Count: ${state.count}, Message: ${state.message}`;
}).subscribe();

Initializing the Subscriber

Initialize the DOM element with the current state:

subscriber.init((ele, state) => {
  ele.textContent = `Initial Count: ${state.count}, Message: ${state.message}`;
});

Handling Events

Add event listeners to DOM elements:

const incrementButton = document.getElementById('increment');

subscriber.addEventListener('click', (event, store) => {
  store.set((state) => ({ count: state.count + 1 }));
});

Dispatching Actions

Execute custom actions and dispatch events to update subscribers:

subscriber.action((store) => {
  // Perform any state updates or other logic here
  store.set((state) => ({ message: 'State Updated!' }));
});

API Reference

CreateSubscriber Methods

  • addSubscriber(action: Action<State>): Adds an action to be executed on state changes. Returns the CreateSubscriber instance for method chaining.
  • getLastSubscriberId(): Returns the ID of the last added subscriber.
  • removeSubscriber(id: string): Removes a subscriber action by ID. Returns the CreateSubscriber instance.
  • addEventListener(eventName: string, listener: (event: Event, store: Store<State>) => void): Adds an event listener to the DOM element, automatically executing the provided listener in response to events. Returns the CreateSubscriber instance.
  • subscribe(): Subscribes all added actions to state changes. Returns the CreateSubscriber instance.
  • init(initAction: Action<State>): Initializes the DOM element with the current state. Returns the CreateSubscriber instance.
  • action(actionMethod: (store: Store<State>) => void): Executes a custom action and dispatches a domChange event to update subscribers.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.