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

reactive-widget-helper

v0.0.2

Published

Helper function to create Reactive Visualization Widgets

Downloads

118

Readme

reactive-widget

A helper function to create Reactive Visualization Widgets, web components that work like inputs holding a value and using input events to trigger changes. Useful for creating interactive visualizations and connecting them with other widgets.

To use it just pass it an html element with your visualization, and an object with the initial value and a callback function for when the value changes. It will return a reactive widget with a helper function setValue that you can call to update it's reactive value.

// 🧰 Enhance your html element with reactive value and event handling
let widget = ReactiveWidget(element, { value, showValue });

// Then on an interaction
...
.on("click", () => {
  // you can use setValue for triggering input events
  widget.setValue(newVal)
})

Full Example Usage

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Reactive Widget helper function test</title>
  </head>
  <body>
    <h1>Reactive Widget Helper Function</h1>
    <div id="target"></div>
    <div id="status"></div>
    <script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
    <script src="https://cdn.jsdelivr.net/npm/reactive-widget-helper"></script>
    <script src="./Histogram.js"></script>
    

    <script>
      const BrushableHistogram = function (
        data,
        {
          value = [],
          x = (d) => d[0],
          xLabel = "",
          width = 600,
          height = 300,
          marginBottom = 30,
          vertical = false,
        } = {}
      ) {
        // ✅ Add here the code that creates your widget
        let element = Histogram(data, {
          x,
          xLabel,
          width,
          height,
          marginBottom,
          vertical,
        });

        // 🧰 Enhance your html element with reactive value and event handling
        let widget = ReactiveWidget(element, { value, showValue });

        // Enhance the Histogram with a brush
        function brushended(event) {
          let selection = event.selection;
          if (!event.sourceEvent || !selection) return;

          const [x0, x1] = selection.map((d) => element._xS.invert(d));
          widget.setValue(vertical ? [x1, x0] : [x0, x1]);
        }
        const brush = (vertical ? d3.brushY() : d3.brushX())
          .extent([
            [element._margin.left, element._margin.top],
            [
              element._width - element._margin.right,
              element._height - element._margin.bottom,
            ],
          ])
          .on("brush end", brushended);
        const gBrush = d3.select(element).append("g").call(brush);

        // 🧰 ShowValue will display the current internalValue brush position
        function showValue() {
          // ✅ Add here the code that updates the current interaction
          const [x0, x1] = widget.value;
          // Update the brush position
          gBrush.call(
            brush.move,
            x1 > x0 ? (vertical ? [x1, x0] : [x0, x1]).map(element._xS) : null
          );
        }

        showValue();

        // 🧰 Finally return the html element
        return widget;
      };

      async function runtIt() {
        const cars = await d3.json(
          "https://cdn.jsdelivr.net/npm/vega-datasets@2/data/cars.json"
        );
        const xAttr = "Horsepower";
        const myWidget = BrushableHistogram(cars, {
          x: (d) => d[xAttr],
          xLabel: xAttr,
          height: 200,
          value: [12, 32], // initial position
        });

        // 🧰 Listen to changes in the widget
        const onInput = (e) => {
          console.log("Widget updated. value=", myWidget.value);
          document.getElementById("status").innerHTML =
            `Current Selection <pre>${JSON.stringify(myWidget.value, null, 2)}</pre>`;
        };
        myWidget.addEventListener("input", onInput);
        onInput();

        // 🧰 Append your widget to the page
        document.getElementById("target").appendChild(myWidget);
      }

      runtIt();
    </script>
  </body>
</html>
``