0.1.3 • Published 3 years ago

react-accordion-with-header v0.1.3

Weekly downloads
258
License
MIT
Repository
github
Last release
3 years ago

react-accordion-with-header

React accordion component with customizable flexbox header

NPM

"You've got 5 seconds... and 3 are up." - Steven Seagal

Check out the demo NOW

Install via NPM:

npm install react-accordion-with-header

Import the modules:

import {
  AccordionWithHeader,
  AccordionNode,
  AccordionHeader,
  AccordionPanel
} from 'react-accordion-with-header';

Items can be passed in to <AccordionHeader /> and <AccordionPanel />:

  • as children (preferred)
  • as a component passed into the template prop (deprecated)

The elements passed in to <AccordionHeader /> can be horizontally justified and vertically aligned via their respective props :tada: :boom: :beers:

  • horizontalAlignment
  • verticalAlignment

Pass in a component as a child to AccordionHeader and AccordionPanel:

(or plain html of course)

// note: due to the warning "Stateless function components cannot be given refs. Attempts to access this ref will fail."
// the components passed into <AccordionPanel> must be class components
// this allows to measure the height of the element via refs
class BodyTpl extends React.Component {
  render() {
    return <div>Look at this {this.props.item}</div>;
  }
}

class MyAccordion extends React.Component {
  render() {
    return (
      <AccordionWithHeader>
        {[1, 2, 3, 4].map((item, i) => {
          return (
            <AccordionNode key={i}>
              <AccordionHeader
                horizontalAlignment="centerSpaceAround"
                verticalAlignment="center"
              >
                <div>This is the header</div>
                <div>It has flexbox layout</div>
              </AccordionHeader>
              <AccordionPanel>
                <BodyTpl item={item} />
              </AccordionPanel>
            </AccordionNode>
          );
        })}
      </AccordionWithHeader>
    );
  }
}

…

actionCallback

…
  actionCallback = (panels, state) => {
    // fires any time headers are clicked and panels change state
    // receives array of panels: [{ panel: 3, open: true }, { panel: 6, open: true }]
    // and the AccordionWithHeader state
    console.log('panels, state', panels, state);
  }

  render() {
    return (
      <AccordionWithHeader actionCallback={this.actionCallback}>
        // ... stuff
      </AccordionWithHeader>
    );
  }
…

Props

(all components accept a className and style prop per usual convention)

<AccordionWithHeader />

PropertyTypeDescriptionDefault
firstOpenBooleanDetermines if the first panel should be expanded on initfalse
activeArrayDetermines which panels are open (on componentDidMount or to control externally)[]
multipleOkayBooleanTrue allows multiple panels to be expanded at the same time. False allows only one panel to be expanded at any time.false
actionCallbackFunctionCallback function fired when a header is clicked and panel is opened or closed. Returns an array representing panels and the AccordionWithHeader statenone
styleObjectstyle objectnone
classNameStringCSS classnamenone

<AccordionNode />

PropertyTypeDescriptionDefault
styleObjectstyle objectnone
classNameStringCustom classname applied to root item divnone

<AccordionHeader />

PropertyTypeDescriptionDefault
titleStringFor simple headers, a title will render an <h1> and disallow child elementsnone
titleColorStringsome valid CSS color or rgb or hexblack
horizontalAlignmentStringOne of: 'centerSpaceBetween', 'spaceBetween', 'centerSpaceAround', 'spaceAround', 'spaceEvenly', 'center', 'left', 'right'. Maps to corresponding flex-box CSS propertycenterSpaceAround
verticalAlignmentStringOne of: 'top', 'center', 'bottom'center
styleObjectstyle objectnone
classNameStringCSS classnamenone
templateElementComponent to be rendered as a templatenone

<AccordionPanel />

PropertyTypeDescriptionDefault
templateElementComponent to be rendered as a templatenone
speedNumberSpeed in milliseconds to apply to CSS transition of open/close effect250
styleObjectstyle objectnone
classNameStringCSS classnamenone

What about styling?

You can styles to any component with a style prop or className prop

For example: <AccordionHeader style={{border: '1px solid'}}>

Or: <AccordionHeader className="myCssClass">


0.1.3

3 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.9

6 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.33

8 years ago

0.0.32

8 years ago

0.0.31

8 years ago

0.0.30

8 years ago

0.0.29

8 years ago

0.0.28

8 years ago

0.0.27

8 years ago

0.0.26

8 years ago

0.0.25

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago