content logo

React Accordions:

Simple React Accordion with Images

You can use this code to better sort items on your website. If you need to put tables below each other, you can put these tables inside the accordions to be more organized or like this code, put the photos inside the accordion. This accordion has a white background and there is a black separating line between the two items.

#

Simple Accordion Code

#

React Accordion with Image

#

HTML Accordion Code Example

#

White Accordion Code


<!-- This script got from frontendfreecode.com -->
<div id='root'>
</div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
@charset "UTF-8";
.accordion {
  width: 100%;
  padding: 1rem 2rem;
  display: flex;
  flex-direction: column;
  border-radius: 10%;
  overflow-y: auto;
}
.fold {
  border-bottom: 1px solid rgba(34, 36, 38, 0.35);
}
.fold .fold_trigger {
  text-align: start;
  width: 100%;
  padding: 1rem;
  border: none;
  outline: none;
  background: none;
  cursor: pointer;
  border-bottom: 1px solid rgba(34, 36, 38, 0.35);
}
.fold .fold_trigger:before {
  font-family: FontAwesome;
  content: "";
  display: block;
  float: left;
  padding-right: 1rem;
  transition: transform 400ms;
  transform-origin: 20%;
  color: rgba(34, 36, 38, 0.35);
}
.fold .fold_trigger.open:before {
  transform: rotateZ(-180deg);
}
.fold .fold_content {
  display: none;
  max-height: 0;
  opacity: 0;
  transition: max-height 400ms linear;
}
.fold .fold_content.open {
  display: block;
  max-height: 400px;
  opacity: 1;
}
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}
class Accordion extends React.Component {
  constructor(props) {
    super(props);_defineProperty(this, "selectFold",
    foldNum => {
      const current = this.state.active === foldNum ? -1 : foldNum;
      this.setState(() => ({ active: current }));
    });this.state = { active: -1 };} /***
   * Selects the given fold, and deselects if it has been clicked again by setting "active to -1" 
   * */render() {
    return /*#__PURE__*/(
      React.createElement("div", { className: "accordion" },
      this.props.contents.map((content, i) => {
        return /*#__PURE__*/(
          React.createElement(Fold, {
            key: `${i}-${content.title}`,
            content: content,
            handle: () => this.selectFold(i),
            active: i === this.state.active }));
      })));
  }}
class Fold extends React.Component {
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { className: "fold" }, /*#__PURE__*/
      React.createElement("button", {
        className: `fold_trigger ${this.props.active ? "open" : ""}`,
        onClick: this.props.handle },
      this.props.content.title), /*#__PURE__*/
      React.createElement("div", {
        key: "content",
        className: `fold_content ${this.props.active ? "open" : ""}` },
      this.props.active ? this.props.content.inner : null)));
  }}
const pictures = [
"http://unsplash.it/200",
"http://unsplash.it/200",
"http://unsplash.it/200"];
var test = (title, text, imageURLs) => {
  const images = /*#__PURE__*/
  React.createElement("div", { className: "test-images" },
  imageURLs.map((url, i) => /*#__PURE__*/React.createElement("img", { key: i, src: url })));
  const inner = /*#__PURE__*/
  React.createElement("div", { className: "test-content" }, /*#__PURE__*/
  React.createElement("p", null, text, " "),
  images);
  return { title, inner };
};
const testData = [
test('Title', 'Content', pictures),
test('Title', 'Content', pictures),
test('Title', 'Content', pictures),
test('Title', 'Content', pictures),
test('Title', 'Content', pictures)];
ReactDOM.render( /*#__PURE__*/React.createElement(Accordion, { contents: testData }), document.getElementById('root'));
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js'></script>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css'>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js'></script>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css'>
<style>
@charset "UTF-8";
.accordion {
  width: 100%;
  padding: 1rem 2rem;
  display: flex;
  flex-direction: column;
  border-radius: 10%;
  overflow-y: auto;
}
.fold {
  border-bottom: 1px solid rgba(34, 36, 38, 0.35);
}
.fold .fold_trigger {
  text-align: start;
  width: 100%;
  padding: 1rem;
  border: none;
  outline: none;
  background: none;
  cursor: pointer;
  border-bottom: 1px solid rgba(34, 36, 38, 0.35);
}
.fold .fold_trigger:before {
  font-family: FontAwesome;
  content: "";
  display: block;
  float: left;
  padding-right: 1rem;
  transition: transform 400ms;
  transform-origin: 20%;
  color: rgba(34, 36, 38, 0.35);
}
.fold .fold_trigger.open:before {
  transform: rotateZ(-180deg);
}
.fold .fold_content {
  display: none;
  max-height: 0;
  opacity: 0;
  transition: max-height 400ms linear;
}
.fold .fold_content.open {
  display: block;
  max-height: 400px;
  opacity: 1;
}
</style>

</head>
<body>
<div id='root'>
</div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}
class Accordion extends React.Component {
  constructor(props) {
    super(props);_defineProperty(this, "selectFold",
    foldNum => {
      const current = this.state.active === foldNum ? -1 : foldNum;
      this.setState(() => ({ active: current }));
    });this.state = { active: -1 };} /***
   * Selects the given fold, and deselects if it has been clicked again by setting "active to -1" 
   * */render() {
    return /*#__PURE__*/(
      React.createElement("div", { className: "accordion" },
      this.props.contents.map((content, i) => {
        return /*#__PURE__*/(
          React.createElement(Fold, {
            key: `${i}-${content.title}`,
            content: content,
            handle: () => this.selectFold(i),
            active: i === this.state.active }));
      })));
  }}
class Fold extends React.Component {
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { className: "fold" }, /*#__PURE__*/
      React.createElement("button", {
        className: `fold_trigger ${this.props.active ? "open" : ""}`,
        onClick: this.props.handle },
      this.props.content.title), /*#__PURE__*/
      React.createElement("div", {
        key: "content",
        className: `fold_content ${this.props.active ? "open" : ""}` },
      this.props.active ? this.props.content.inner : null)));
  }}
const pictures = [
"http://unsplash.it/200",
"http://unsplash.it/200",
"http://unsplash.it/200"];
var test = (title, text, imageURLs) => {
  const images = /*#__PURE__*/
  React.createElement("div", { className: "test-images" },
  imageURLs.map((url, i) => /*#__PURE__*/React.createElement("img", { key: i, src: url })));
  const inner = /*#__PURE__*/
  React.createElement("div", { className: "test-content" }, /*#__PURE__*/
  React.createElement("p", null, text, " "),
  images);
  return { title, inner };
};
const testData = [
test('Title', 'Content', pictures),
test('Title', 'Content', pictures),
test('Title', 'Content', pictures),
test('Title', 'Content', pictures),
test('Title', 'Content', pictures)];
ReactDOM.render( /*#__PURE__*/React.createElement(Accordion, { contents: testData }), document.getElementById('root'));
</script>

</body>
</html>
Preview