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

react-simple-expand

v0.0.2

Published

React Component for creating accessible and performant expandable UI

Downloads

2,399

Readme

Build Status Code Coverage version downloads MIT License

PRs Welcome Code of Conduct

Watch on GitHub Star on GitHub Tweet

The problem

We create similar info panels and accordion components that collapse and expand, to and from a height of zero. The logic is always the same but the look may change between use cases. Additionally, we want to have the component accessible to all users, at all times.

This solution

This is a WAI-ARIA compliant React component. It controls how panels/accordions/etc collapse and expand. ReactSimpleExpand uses render props to reduce overhead and give you flexibility in styling.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save react-simple-expand

Usage

class Accordion extends React.Component {
  state = {
    open: 0,
  }

  onClickItem = index => () => {
    this.setState({open: index})
  }

  render() {
    const {items} = this.props
    const {open} = this.state

    return items.map((item, index) => (
      <ReactSimpleExpand
        key={item.title}
        onToggle={this.onClickItem(index)}
        isOpen={open === index}
      >
        ({(getRootProps, getHeaderProps, getContentProps)}) => (
        <div {...getRootProps()}>
          <button {...getHeaderProps()}>{item.title}</button>
          <div {...getContentProps()}>{item.description}</div>
        </div>
        )}
      </ReactSimpleExpand>
    ))
  }
}

// Using the accordion
const App = () => (
  <Accordion
    items={[
      {title: 'First item', description: 'This is just a demo.'},
      {
        title: "I'm the second item",
        description:
          'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque scelerisque mi sed sapien mollis tempor. Donec ut pretium mi, et mollis dolor. Donec fermentum, augue tristique dapibus facilisis, velit nisi mattis mi, sit amet ultrices arcu libero sed odio. Donec id nisl facilisis, luctus turpis vitae, egestas velit.',
      },
      {
        title: 'Yup, this is the last item...',
        description: 'Last but not least!',
      },
    ]}
  />
)

Props

id required

id: PropTypes.string.isRequired

Unique html id.

isOpen

isOpen: PropTypes.bool

Controls the visibilty of the content. If the value changes after the first render, the component will change with an animation.

onToggle

onToggle: PropTypes.func

onToggle is passed to the header element and called when clicked. This is the ideal place to trigger a state change.

duration

duration: PropTypes.number

If a value is supplied, the component will animate the toggle.

Note: This API is likely to change.

Render Prop

This is where you pass your own components to render the state of ReactSimpleExpand. You can either use a prop called render, or use the children prop.

The render prop function is passed an object that has the following properties:

Prop Getters

These functions are used to apply props to the elements that you render. The functions take care composing event handlers, merging style objects and applying other important props and attributes to your components. Their API is all as follows:

@params {Object} props - optional props you want to add.
@returns {Object} - the props that you need to render the component.

Note: If you are rendering a composite component, that component will need a prop called refKey. The refKey is used to forward a required ref function that ReactSimpleExpand uses internally to the root DOM element of the composite component. Commonly, folks call this innerRef. So you'd call: getRootProps({ refKey: 'innerRef' }) and your composite component would forward like: <div ref={props.innerRef} />

getRootProps()

getHeaderProps()

getContentProps()

State

| property | type | description | | ----------- | ------- | ------------------------------------------------ | | isOpen | boolean | Indicates the current visibility of the content. | | isAnimating | boolean | Is true during state changes. |

LICENSE

MIT