react-faq-toggler
v1.4.1
Published
React FAQ Toggler component
Downloads
58
Readme
React FAQ Toggler Component
Simple FAQ component with individual question/answer toggle, with smooth icon rotate on click.
Usage:
Pass in the props of faqs, containing an array of objects, with the keys of 'question' and 'answer':
import FaqComponent from "react-faq-toggler";
function App() {
const data = [
{ question: "Hello there?", answer: "Boo" },
{ question: "You what?", answer: "Meh" },
{ question: "A third question, I have", answer: "Well, do you now!" },
];
return (
<div className="App">
<h1>My Super App</h1>
<div className="faq-cont">
<FaqComponent faqs={data} />
</div>
</div>
);
}
Default Width
By default, the FaqComponent will have a width of 100% of its parent container.
Paragraphs
Use '\n' to break individual FAQ question/answer into paragraphs (uses CSS rule, "white-space:pre-wrap")
Styling
Uses the following class names to style the question/answers:
<p class="faq-question-style"></p>
<p class="faq-answer-style"></p>
Recommended default styles:
.faq-question-style {
width: 100%;
background-color: rgb(255, 102, 0);
color: white;
border-radius: 5px;
padding: 1em;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 1.25em;
margin: 0.25em 0;
white-space: pre-wrap;
}
.faq-question-style:hover {
cursor: pointer;
}
.faq-cont {
width: 70%;
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
padding-top: 1.5em;
-webkit-user-select: none; /* Chrome/Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ */
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.faq-answer-style {
display: none;
font-weight: 300;
width: 100%;
color: black;
border-radius: 5px;
padding: 0 1em;
text-align: left;
font-size: 1.25em;
margin: 0.5em 0;
white-space: pre-wrap;
}
.faq-show-animation {
display: block;
height: fit-content;
animation: faq-answer-slide 0.8s;
}
.faq-rotate-forwards {
transform: rotate(180deg);
transition: 0.2s transform;
}
.faq-rotate-back {
transform: rotate(0deg);
transition: 0.2s transform;
}
@keyframes faq-answer-slide {
0% {
opacity: 0;
max-height: 0;
}
100% {
opacity: 1;
max-height: 500px;
}
}
Custom Icon
Uses FontAwesome icons for responsiveness - default icon is a chevron, but you can pass in your own FontAwesome icon by importing and passing it through the 'icon' props:
import FaqComponent from "react-faq-toggler";
import { SomeOtherIcon } from "@fortawesome/free-solid-svg-icons";
function App() {
const data = [
{ question: "Hello there?", answer: "Boo" },
{ question: "You what?", answer: "Meh" },
{ question: "A third question, I have", answer: "Well, do you now!" },
];
return (
<div className="App">
<h1>My Super App</h1>
<div className="faq-cont">
<FaqComponent faqs={data} icon={someOtherIcon} />
</div>
</div>
);
}