Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reforest

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reforest

Collect ordered React render data seamlessly across the server and client.

  • 0.13.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

🌲 reforest

Collect ordered React render data seamlessly across the server and client.

Note While digital trees are cool, climate change is negatively impacting real trees at a rapid rate. Please consider planting a tree, starting a garden, or donating to an organization.

Install

npm install reforest
yarn add reforest

Why?

When building low-level components in React for accessibility, styling, and animation purposes, the orchestration for everything can become painful for both the library author and consumer. In general, the problem boils down to a component needing to use render data from another component[s]. This library aims to solve this problem by managing a tree of data built from other component renders in an easy API that works on the server and client.

Usage

Warning The API is still in development and may change without notice. Docs coming soon..

Please note the following example is for demo purposes only and you should use a more robust solution that is fully accessible.

import * as React from "react"
import { useTree, useTreeNode } from "reforest"

const SelectContext = React.createContext<any>(null)

function Select({ children }: { children: React.ReactNode }) {
  const highlightedIndexState = React.useState<number | null>(null)
  const [highlightedIndex, setHighlightedIndex] = highlightedIndexState
  const [selectedValue, setSelectedValue] = React.useState<React.ReactElement | null>(null)
  const tree = useTree(children)
  const moveHighlightedIndex = (amountToMove: number) => {
    setHighlightedIndex((currentIndex) => {
      if (currentIndex === null) {
        return 0
      } else {
        const nextIndex = currentIndex + amountToMove

        if (nextIndex >= tree.maxIndex) {
          return 0
        } else if (nextIndex < 0) {
          return maxIndex - 1
        }

        return currentIndex + amountToMove
      }
    })
  }

  return (
    <div
      tabIndex={0}
      onKeyDown={(event) => {
        if (event.key === "ArrowUp") {
          moveHighlightedIndex(-1)
        } else if (event.key === "ArrowDown") {
          moveHighlightedIndex(1)
        }
      }}
    >
      <strong>{selectedValue ? <>Selected: {selectedValue}</> : `Select an option below`}</strong>
      <SelectContext.Provider value={{ highlightedIndexState, selectedValue }}>
        {indexedChildren}
      </SelectContext.Provider>
    </div>
  )
}

function Option({ children, value }: { children: React.ReactNode; value: any }) {
  const { indexPath, index } = useTreeNode()
  const selectContext = React.useContext(SelectContext)
  const [highlightedIndex, setHighlightedIndex] = selectContext.highlightedIndexState
  const isHighlighted = index === highlightedIndex
  const isSelected = selectContext.selectedValue ? selectContext.selectedValue === value : false

  return (
    <div
      onMouseOver={() => setHighlightedIndex(index)}
      onMouseOut={() => setHighlightedIndex(null)}
      onClick={() => selectContext.selectIndex(indexPath)}
      style={{ backgroundColor: isHighlighted ? "yellow" : "white" }}
    >
      {children} {isSelected && "✅"}
    </div>
  )
}

const fruits = ["Apple", "Orange", "Pear", "Kiwi", "Banana", "Mango"]

export default function App() {
  return (
    <Select>
      {fruits.map((fruit) => (
        <Option key={fruit} value={fruit}>
          {fruit}
        </Option>
      ))}
    </Select>
  )
}

Keywords

FAQs

Package last updated on 12 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc