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 πŸ™

Β© 2025 – Pkg Stats / Ryan Hefner

@lyonbot/interactive-blocks-react

v1.1.0

Published

This package helps you integrate [interactive-blocks](https://lyonbot.github.io/interactive-blocks/) to your βš›οΈ React app, with πŸͺ React Hooks API.

Readme

@lyonbot/interactive-blocks-react

This package helps you integrate interactive-blocks to your βš›οΈ React app, with πŸͺ React Hooks API.

Usage

All you need is

  • write a Block component
  • write a Slot Component, which lists your Blocks inside
    • a Block can also contain sub-Slots inside
  • finally, render Blocks and Slots in a <ReactInteractiveBlocksRoot>

You can integrate with your state management (Redux, Mobx, Recoil, immer, etc.):

  • In Block Component: index and data getter functions
  • In Slot Component: onCut and onPaste callbacks
  • More details can be found below.

Basic Scaffolding

Details can be found after this scaffolding code.

import { ReactInteractiveBlocksRoot, useLatestRef, useBlockHandler, useSlotHandler } from "@lyonbot/interactive-blocks-react";

// in <App>

<ReactInteractiveBlocksRoot>
  {/* put root blocks and root slots here */}
</ReactInteractiveBlocksRoot>

// -------------------------------------------------
// then write MyBlock component

function MyBlock(props) {
  const [statusClassNames, setStatusClassNames] = React.useState("");
  const { divProps, BlockWrapper } = useBlockHandler(() => ({
    index: () => ***,  // ❗ a getter function, returning index
    data: () => ***,   // ❗ a getter function, returning current block's data (for onPaste)
    onStatusChange: (block) => {
      let ans = "";
      if (block.isActive) ans += " isActive";
      if (block.hasFocus) ans += " hasFocus";

      setStatusClassNames(ans);
    },
  }));

  // ............
  // ❗ 1. Must be wrapped by <BlockWrapper>
  // ❗ 2. Must have {...divProps}

  return <BlockWrapper>
    <div
      {...divProps}
      className={`myBlock ${statusClassNames}`}
    >

      {/* render sub-slots here */}
      <MySlot .... />

    </div>
  </BlockWrapper>;
}

// -------------------------------------------------
// then write MySlot component

export function MySlot(props) {
  const [statusClassNames, setStatusClassNames] = React.useState("");
  const { divProps, SlotWrapper } = useSlotHandler(() => ({
    onStatusChange: (slot) => {
      let ans = "";
      if (slot.isActive) ans += " isActive";
      if (slot.hasFocus) ans += " hasFocus";

      setStatusClassNames(ans);
    },

    // for slot

    onCut: (action) => {
      /**
       * ❗ delete items at `action.indexes`
       */
    },

    onPaste: (action) => {
      /**
       * ❗ read `action.data.blocksData` ( from Block component's `data` getter )
       * ❗ and insert into the list, at `action.index`
       */
    },
  }));

  // ............
  // ❗ 1. Must be wrapped by <SlotWrapper>
  // ❗ 2. Must have {...divProps}

  return <SlotWrapper>
    <div
      {...divProps}
      className={`mySlot ${statusClassNames}`}
    >

      {/* render sub-block list here */}
      <MyBlock .... />

    </div>
  </SlotWrapper>;
}

Integrate with your state management

As mentioned above, you have to write...

  • In Block Component: index and data getter functions
  • In Slot Component: onCut and onPaste callbacks

Don't directly use props and state

In useBlockHandler and useSlotHandler, the initializer function only executes once, therefore, you can't directly use props and state inside it ❗. The closure captures the first props and never update!

To solve this kludge problem, you can use useLatestRef to make a ref, and keep it synchronized with the latest props and state values.

For example, in Block component, we make a propsRef and access newest prop values via propsRef.current.*:

import { useLatestRef } from "@lyonbot/interactive-blocks-react";

function MyBlock(props) {
  const propsRef = useLatestRef(props);  // πŸ‘ˆ  new
  const { handleBlockPointerUp, BlockWrapper } = useBlockHandler(() => ({
    index: () => propsRef.current.index,  // πŸ‘ˆ  always get latest "index" prop
    data: () => propsRef.current.value,   // πŸ‘ˆ  always get latest "value" prop
    ...

It's also necessary in Slot component!

Update state in Slot callbacks

You must have noticed onCut and onPaste callbacks.

  onCut: (action) => {
    /**
     * ❗ delete items at `action.indexes`
     */
  },

  onPaste: (action) => {
    /**
     * ❗ read `action.data.blocksData` ( from Block component's `data` getter )
     * ❗ and insert into the list, at `action.index`
     */
  },
  • If you are using global state management, you can use dispatch to update data here.
  • If you want to invoke callbacks from props, you can do it like propsRef.current.onChange(...)

onCut

When delete blocks (cut), you can use indexesDescending to safely remove items from the list.

const newList = oldList.slice(); // copy old list

// delete items one-by-one
action.indexesDescending.forEach(index => {
  newList.splice(index, 1);
})

*** // ❗ now, submit the newList to the state

onPaste

When insert blocks (paste), you can read Block data getter function's output, and insert them into the list.

const newList = oldList.slice(); // copy old list
const items = action.data.blocksData; // read Block `data` getter function's output
*** // ❗ process the items, if needed
newList.splice(action.index, 0, ...items); // insert items
*** // ❗ now, submit the newList to the state

Advanced: Customize Behaviors

Root Context

const handleInteractiveBlocksMount = useCallback((blockContext) => {
  // this callback only invoke once
  // you can add event listeners now.

  blockContext.on("focus", () => {
    console.log("focus");
  });

  blockContext.on("blur", () => {
    console.log("blur");
  });

  blockContext.on("paste", (action) => {
    console.log("pasting...", action);
  });

}, [])

<ReactInteractiveBlocksRoot
  options={/* see interactive-blocks document */}
  onMount={handleInteractiveBlocksMount}
  onUnmount={handleInteractiveBlocksUnmount}
>
  {/* put root blocks and root slots here */}
</ReactInteractiveBlocksRoot>

Block Handler

In your Block component, you can get blockHandler and use it like this:

const {
  divProps,
  BlockWrapper,
  blockHandler, // πŸ‘ˆ  new
} = useBlockHandler(() => ({
  /* options */
}));

// then you can call blockHandler methods like:
blockHandler.isActive
blockHandler.hasFocus

blockHandler.select();
blockHandler.focus();
blockHandler.unselect();

// and more

Slot Handler

In your Slot component, you can get slotHandler:

const {
  divProps,
  SlotWrapper,
  slotHandler, // πŸ‘ˆ  new
} = useSlotHandler(() => ({
  /* options */
}));

// then you can call slotHandler methods like:
slotHandler.isActive
slotHandler.hasFocus

slotHandler.isDescendantOfBlock(anotherBlockHandler);
slotHandler.isDescendantOfSlot(anotherSlotHandler);

slotHandler.select();
slotHandler.focus();

// and more