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

@replit/clui-session

v0.0.1

Published

A utility library to manage a list of react components

Downloads

218

Readme

@replit/clui-session

A utility for manipulating a list of React children.

When building a CLI-style interfaces this can be useful for adding and removing lines when the prompt is submitted. Each child receives an item prop that contains methods and properties related to navigating and transforming the list. By default only the first child is rendered. It's up to the child elements to call a method on props.item to update the list. The child elements can be insered dynamically, defined up-front or a mix of both.

Basic Prompt/Output example

Here's an exmaple that renders an input and a button. When the button is clicked, 2 components are added (not rendered) to the list by calling item.insert(/* ... */).next(). By chaining next() the next child is rendered, which is <Output value={value} />. By passing value to Output it can render something based on it. The only requirement for Output is to call props.item.next() at some point to show the next Prompt. This could be after fetching data, a user interaction, or right when the component mounts (useEffect(() => props.item.next(), []).

import React, { useState } from 'react'
import { render } from 'react-dom'
import Session from '@replit/clui-session';

// Substitute for somehting more interesting!
const useFetchData = (value) => {
  return value;
}

const Output = (props) => {
  // Do something interesting with prompt input value
  const data = useFetchData(props.value);

  useEffect(() => {
    if (data) {
      // After data has loaded, call `next` to show next child (which is another Prompt)
      props.item.next();
    }
  }, [data]);

  if (!data) {
    return <div>Loading...</div>;
  }

  // Render output data
  return <div>output: ${data}</div>,
}

const Prompt = (props) => {
  const [value, setValue] = useState('');

  const onClick = () => {
    props.item.insert(
      <Output value={value} />,
      <Prompt />,
    ).next();
  }

  return (
    <div>
      <input value={value} onChange={e => setValue(e.target.value)}/>
      <button onClick={onClick}>run</button>
    <div>;
  );
}

render(
  <Session>
    <Prompt />
  </Session>,
  document.getElementById('root'),
);


/* After typing "hello session" and clicking run on the output
 * the component tree would look like
 *
 * <Session>
 *   <Prompt />
 *   <Output value="hello session" />
 *   <Prompt />
 * </Session>
 */

Components

Step is a utility component that automatically shows the next child by calling item.next when the component mounts

<Session>
  <Step><div>step 1</div></Step>
  <Step wait={2000}><div>step 2 (paused for 1 second)</div></Step>
  <Step wait={2000}><div>step 3 (paused for 1 second)</div></Step>
  <Step><div>step 4</div></Step>
</Session>

Do is a utility component the gives you access to item inline as a render prop.

<Session>
  <Do>
    {item => <button onClick={item.next}>next 1</button>}
  </Do>
  <Do>
    {item => <button onClick={item.next}>next 2</button>}
  </Do>
  <Do>
    {item => <button onClick={item.next}>next 3</button>}
  </Do>
</Session>