Skip to content

Commit c5e5106

Browse files
committed
Add useAccordion hook
1 parent d11a65e commit c5e5106

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

src/assets/css/_css/docs.less

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,75 @@
162162
}
163163
}
164164

165+
.faq-content {
166+
h3 {
167+
font-size: 16px;
168+
padding: 40px 0;
169+
// box-shadow: 0px -1px 0px #242424;
170+
margin: 0;
171+
user-select: none;
172+
cursor: pointer;
173+
position: relative;
174+
175+
&.open:after {
176+
transform: rotate(-90deg);
177+
}
178+
179+
&:after {
180+
content: '';
181+
height: 14px;
182+
width: 9px;
183+
background-image: url('data:image/svg+xml,%3Csvg width="9" height="14" viewBox="0 0 9 14" fill="none" xmlns="http://www.w3.org/2000/svg"%3E%3Cpath d="M6.67512e-07 7.00014L9 1.03507e-06L9 14L6.67512e-07 7.00014Z" fill="%23343434"/%3E%3C/svg%3E%0A');
184+
position: absolute;
185+
transition: transform 200ms ease;
186+
right: 0;
187+
188+
@media screen and (max-width: 768px) {
189+
display: none;
190+
}
191+
}
192+
193+
a {
194+
display: none
195+
}
196+
}
197+
198+
ul {
199+
max-height: 0;
200+
opacity: 0;
201+
transition: all 200ms ease;
202+
font-style: normal;
203+
font-weight: normal;
204+
font-size: 16px;
205+
line-height: 23px;
206+
color: #999999;
207+
margin-bottom: 0;
208+
209+
&.show {
210+
max-height: 800px;
211+
opacity: 1;
212+
margin-bottom: 1.0875rem;
213+
}
214+
}
215+
p {
216+
font-style: normal;
217+
font-weight: normal;
218+
font-size: 16px;
219+
line-height: 23px;
220+
color: #999999;
221+
max-height: 0;
222+
opacity: 0;
223+
transition: all 200ms ease;
224+
margin: 0;
225+
226+
&.show {
227+
opacity: 1;
228+
max-height: 400px;
229+
margin-bottom: 1.0875rem;
230+
}
231+
}
232+
}
233+
165234
.faq-questions::before {
166235
content: "\A";
167236
white-space: pre;

src/pages/faq.tsx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,70 @@
1-
import React from "react"
1+
import React, { useEffect } from "react"
22
import Layout from "../components/Layout"
33
import FAQSection from "../components/FAQSection"
44
import { graphql } from "gatsby"
55

6+
export const useAccordion = () => {
7+
const toggleChildrenClass = (element: HTMLElement) => {
8+
console.log(nextUntil(element, 'h3'))
9+
Array.from(nextUntil(element, 'h3')).map(p =>
10+
p.classList.toggle('show')
11+
);
12+
};
13+
14+
var nextUntil = function (elem: HTMLElement, selector: string) {
15+
16+
// Setup siblings array
17+
var siblings = [];
18+
19+
// Get the next sibling element
20+
let nextElement = elem.nextElementSibling;
21+
22+
// As long as a sibling exists
23+
while (nextElement) {
24+
25+
// If we've reached our match, bail
26+
if (nextElement.matches(selector)) break;
27+
28+
// Otherwise, push it to the siblings array
29+
siblings.push(nextElement);
30+
31+
// Get the next sibling element
32+
nextElement = nextElement.nextElementSibling;
33+
34+
}
35+
36+
return siblings;
37+
38+
};
39+
40+
useEffect(() => {
41+
const hash = location.hash ? location.hash.split('#')[1] : '';
42+
43+
if (hash) {
44+
const parent = document && document.getElementById(hash);
45+
46+
document.getElementById(hash).classList.toggle('open');
47+
toggleChildrenClass(parent);
48+
}
49+
50+
const toggleClasses = (e: Event) => {
51+
if (e.target.localName !== 'h3') return;
52+
history.replaceState({}, '', '#' + e.target.getElementsByTagName('a')[0].id);
53+
history.scrollRestoration = 'manual';
54+
55+
e.target.classList.toggle('open');
56+
toggleChildrenClass(e.target);
57+
};
58+
59+
document.addEventListener('click', toggleClasses);
60+
61+
return () => document.removeEventListener('click', toggleClasses);
62+
}, []);
63+
};
64+
665
export default ({ pageContext, data }: any) => {
66+
useAccordion()
67+
768
const sections = data.allMarkdownRemark.edges
869
.map((e: any) => e.node)
970
.sort((a: any, b: any) => {

0 commit comments

Comments
 (0)