Skip to content

React component for creating accessible, expandable and performant UI

License

Notifications You must be signed in to change notification settings

aranja/react-simple-expand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-simple-expand

React Component for creating accessible and performant expandable UI


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