content logo

React Accordions:

React Accordion with Separated Shadowy Items

These accordions can be used to categorize the steps of a task or to better categorize the content. These accordions have a triangular icon to the left of the title of each accordion. Click on each accordion to turn the icon down, and normally this triangle is to the right. The titles and texts on this accordion are black. The background of the accordion is white. A dividing line separates the title from the accordion texts.

#

React Accordion Shadow Effect

#

HTML Accordion with Icons

#

Simple Accordion Code

#

React Accordion Separated Items

<!-- This script got from frontendfreecode.com -->
<div id="app"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
body {
	height: 100vh;
	padding: 2rem;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
	background-color: #eff2f7;
	font-size: 16px;
}
#app {
	max-width: 30rem;
	margin: 0 auto;
}
.accordion-item {
	margin-bottom: 1rem;
	border-radius: 4px;
	background-color: white;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}
.accordion-header {
	margin: 0;
	padding: 1rem;
	line-height: 0;
}
.accordion-header button {
	width: 100%;
	padding: 0;
	border: 0;
	outline: 0;
	background-color: transparent;
	font-size: 16px;
	text-align: left;
	cursor: pointer;
}
.accordion-panel {
	display: none;
}
.accordion-panel.expanded {
	display: block;
	padding: 1rem;
	border-top: 1px solid #eee;
}
const Accordion = props => {
  return /*#__PURE__*/(
    React.createElement("div", { className: "accordion" }, props.children));
};
const AccordionItemContext = React.createContext({
  expanded: false,
  toggleExpansion: () => {} });
class AccordionItem extends React.Component {
  constructor(props) {
    super(props);
    this.toggleExpansion = () => {
      this.setState({ expanded: !this.state.expanded });
    };
    this.state = {
      expanded: false,
      toggleExpansion: this.toggleExpansion };
  }
  render() {
    return /*#__PURE__*/(
      React.createElement(AccordionItemContext.Provider, { value: this.state }, /*#__PURE__*/
      React.createElement("div", { className: "accordion-item" },
      this.props.children)));
  }}
const AccordionHeader = props => {
  return /*#__PURE__*/(
    React.createElement(AccordionItemContext.Consumer, null,
    ({ expanded, toggleExpansion }) => /*#__PURE__*/
    React.createElement("h2", { className: "accordion-header" }, /*#__PURE__*/
    React.createElement("button", { onClick: toggleExpansion },
    expanded ? '▼ ' : '► ',
    props.children))));
};
const AccordionPanel = props => {
  return /*#__PURE__*/(
    React.createElement(AccordionItemContext.Consumer, null,
    ({ expanded }) => /*#__PURE__*/React.createElement("div", { className: "accordion-panel " + (expanded ? 'expanded' : '') }, props.children)));
};
class App extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return /*#__PURE__*/(
      React.createElement(Accordion, null, /*#__PURE__*/
      React.createElement(AccordionItem, null, /*#__PURE__*/
      React.createElement(AccordionHeader, null, "Section 1"), /*#__PURE__*/
      React.createElement(AccordionPanel, null, "Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.")), /*#__PURE__*/
      React.createElement(AccordionItem, null, /*#__PURE__*/
      React.createElement(AccordionHeader, null, "Section 2"), /*#__PURE__*/
      React.createElement(AccordionPanel, null, "Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.")), /*#__PURE__*/
      React.createElement(AccordionItem, null, /*#__PURE__*/
      React.createElement(AccordionHeader, null, "Section 3"), /*#__PURE__*/
      React.createElement(AccordionPanel, null, "Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.", /*#__PURE__*/
      React.createElement("ul", null, /*#__PURE__*/
      React.createElement("li", null, "List item one"), /*#__PURE__*/
      React.createElement("li", null, "List item two"), /*#__PURE__*/
      React.createElement("li", null, "List item three"))))));
  }}
ReactDOM.render( /*#__PURE__*/
React.createElement(App, null),
document.getElementById('app'));
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.development.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.0/umd/react-dom.development.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.development.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.0/umd/react-dom.development.js'></script>
<style>
body {
	height: 100vh;
	padding: 2rem;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
	background-color: #eff2f7;
	font-size: 16px;
}
#app {
	max-width: 30rem;
	margin: 0 auto;
}
.accordion-item {
	margin-bottom: 1rem;
	border-radius: 4px;
	background-color: white;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}
.accordion-header {
	margin: 0;
	padding: 1rem;
	line-height: 0;
}
.accordion-header button {
	width: 100%;
	padding: 0;
	border: 0;
	outline: 0;
	background-color: transparent;
	font-size: 16px;
	text-align: left;
	cursor: pointer;
}
.accordion-panel {
	display: none;
}
.accordion-panel.expanded {
	display: block;
	padding: 1rem;
	border-top: 1px solid #eee;
}
</style>

</head>
<body>
<div id="app"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
const Accordion = props => {
  return /*#__PURE__*/(
    React.createElement("div", { className: "accordion" }, props.children));
};
const AccordionItemContext = React.createContext({
  expanded: false,
  toggleExpansion: () => {} });
class AccordionItem extends React.Component {
  constructor(props) {
    super(props);
    this.toggleExpansion = () => {
      this.setState({ expanded: !this.state.expanded });
    };
    this.state = {
      expanded: false,
      toggleExpansion: this.toggleExpansion };
  }
  render() {
    return /*#__PURE__*/(
      React.createElement(AccordionItemContext.Provider, { value: this.state }, /*#__PURE__*/
      React.createElement("div", { className: "accordion-item" },
      this.props.children)));
  }}
const AccordionHeader = props => {
  return /*#__PURE__*/(
    React.createElement(AccordionItemContext.Consumer, null,
    ({ expanded, toggleExpansion }) => /*#__PURE__*/
    React.createElement("h2", { className: "accordion-header" }, /*#__PURE__*/
    React.createElement("button", { onClick: toggleExpansion },
    expanded ? '▼ ' : '► ',
    props.children))));
};
const AccordionPanel = props => {
  return /*#__PURE__*/(
    React.createElement(AccordionItemContext.Consumer, null,
    ({ expanded }) => /*#__PURE__*/React.createElement("div", { className: "accordion-panel " + (expanded ? 'expanded' : '') }, props.children)));
};
class App extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return /*#__PURE__*/(
      React.createElement(Accordion, null, /*#__PURE__*/
      React.createElement(AccordionItem, null, /*#__PURE__*/
      React.createElement(AccordionHeader, null, "Section 1"), /*#__PURE__*/
      React.createElement(AccordionPanel, null, "Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.")), /*#__PURE__*/
      React.createElement(AccordionItem, null, /*#__PURE__*/
      React.createElement(AccordionHeader, null, "Section 2"), /*#__PURE__*/
      React.createElement(AccordionPanel, null, "Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.")), /*#__PURE__*/
      React.createElement(AccordionItem, null, /*#__PURE__*/
      React.createElement(AccordionHeader, null, "Section 3"), /*#__PURE__*/
      React.createElement(AccordionPanel, null, "Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.", /*#__PURE__*/
      React.createElement("ul", null, /*#__PURE__*/
      React.createElement("li", null, "List item one"), /*#__PURE__*/
      React.createElement("li", null, "List item two"), /*#__PURE__*/
      React.createElement("li", null, "List item three"))))));
  }}
ReactDOM.render( /*#__PURE__*/
React.createElement(App, null),
document.getElementById('app'));
</script>

</body>
</html>
Preview