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

@posit-dev/shiny-bindings-react

v0.1.0

Published

React helpers for building Shiny app extensions

Downloads

20

Readme

@posit-dev/shiny-bindings-react

This package contains helpers for creating custom input and output bindings using React components. Under the hood it encapsulates the react components in web copmponents and handles the communication between the two.

Want to use plain webcomponents instead of React? See @posit-dev/shiny-bindings-core

Installing

npm install @posit-dev/shiny-bindings-react

Importing/Using

import { makeReactInput } from "@posit-dev/shiny-bindings-react";

Functions

makeReactInput()

Convenience function for creating a custom input binding for a React component. Takes care of registering the input binding to Shiny.

Arguments:

  • name: The name of the input binding to register with Shiny.
  • initialValue: The initial value of the input.
  • renderComp: A function that sets up the react component. Arguments:
    • onNewValue: A function that should be called whenever the value of the input binding changes. The value should be passed as the first argument to the function.
  • priority: Should the value be immediately (immediate) updated wait to the next even loop ( "deferred")? Typically left at the default of "immediate".
  • selector (Optional) A css selector for finding the element to render the input into in the UI. Defaults to selecting an element with the class of name. I.e. .${name}.

Example:

makeReactInput<number>({
  name: "custom-react-input",
  initialValue: 0,
  renderComp: ({ initialValue, onNewValue }) => (
    <MyInput value={initialValue} onNewValue={onNewValue} />
  ),
});

makeReactOutput()

Make a custom Shiny input binding using a react component. Takes care of registering the output binding to Shiny.

Arguments:

  • name: The name of the output binding to register with Shiny.
  • renderComp: Function to render the react component given a provided value. This function will be re-rendered every time the value of the output binding changes. Arguments:
    • value: The value of the output binding.
  • selector (Optional) A css selector for finding the element to render the output into in the UI. Defaults to selecting an element with the class of name. I.e. .${name}.
makeReactOutput<{ value: string }>({
  name: "custom-react-output",
  renderComp: ({ value }) => (
    <div>
      I'm a react output with value <strong>{value}</strong>
    </div>
  ),
});

Webcomponent helpers

makeReactInput()

Convenience function for creating a custom input binding for a React component. Takes care of registering the input binding to Shiny.

Arguments:

  • tagName: The name of the custom element that will be used to render the React component.
  • initialValue: The initial value of the input.
  • renderComp: A function that setsup the react component.
  • priority: Should the value be immediately (immediate) updated wait to the next even loop ( "deferred")? Typically left at the default of "immediate".

Example:

// Generates a new input binding that renders the supplied react component
// into the root of the webcomponent.
makeReactInput({
  tagName: "react-demo",
  initialValue: "#fff",
  renderComp: ({ onNewValue }) => (
    <ColorPickerReact
      initialValue="#fff"
      onNewValue={(color) => onNewValue(color)}
    />
  ),
});

makeReactOutput()

Make a custom Shiny input binding using a react component. Takes care of registering the output binding to Shiny. Encapsulates the React component in a webcomponent internally so the UI generation function just needs to be a return an HTMLTools Tag with the name of the custom element.

Arguments:

  • tagName: The name of the custom element that will be used to render the React component.
  • renderComp: A function that setsup the react component. Is a function that takes as an argument the payload sent over from the server. This is set by the user and can/should be typed to match the payload sent from the server on the TS side with the generic argument. Ex.
makeReactOutput<{ value: number }>({
  tagName: "testing-output-comp",
  renderComp: ({ value }) => {
    return <div>My value is: {value}</div>;
  },
});

Development

To build the package locally run

npm run build

This runs tsc and generates the production code in the dist folder.

Deploying

To deploy this package to NPM simply run

npm publish

This will auto build the project and publish it to NPM. Make sure to update the version number in package.json before publishing.