Skip to content

Commit ebe7d24

Browse files
committed
Fix initial bugs with accordion
1 parent c5e5106 commit ebe7d24

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/components/FAQSection/index.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
import React from "react"
2-
import { Link } from "gatsby"
32
import Marked from "../Marked"
4-
import { toSlug } from '../../utils/slug'
53

64
interface Props {
75
title: string
86
permalink: string
9-
questions: string
107
rawMarkdownBody: string
118
pageContext: any
129
}
1310

1411
const FAQSection = ({
1512
title,
16-
questions,
1713
rawMarkdownBody,
1814
pageContext
1915
}: Props) => (
2016
<section className="inner-content">
2117
<h2>{title}</h2>
22-
{questions
23-
.split(',')
24-
.map(question => (
25-
<Link className="faq-questions" key={question} to={`#${toSlug( question )}`}>
26-
{question}
27-
</Link>
28-
))
29-
}
3018
<Marked pageContext={pageContext}>{rawMarkdownBody}</Marked>
3119
</section>
3220
)

src/pages/faq.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import FAQSection from "../components/FAQSection"
44
import { graphql } from "gatsby"
55

66
export const useAccordion = () => {
7-
const toggleChildrenClass = (element: HTMLElement) => {
7+
const toggleChildrenClass = (element: React.ReactNode) => {
88
console.log(nextUntil(element, 'h3'))
99
Array.from(nextUntil(element, 'h3')).map(p =>
1010
p.classList.toggle('show')
1111
);
1212
};
1313

14-
var nextUntil = function (elem: HTMLElement, selector: string) {
14+
var nextUntil = function (elem: any, selector: string) {
1515

1616
// Setup siblings array
1717
var siblings = [];
@@ -41,13 +41,16 @@ export const useAccordion = () => {
4141
const hash = location.hash ? location.hash.split('#')[1] : '';
4242

4343
if (hash) {
44-
const parent = document && document.getElementById(hash);
44+
const anchor = document && document.getElementById(hash)
45+
const heading: any = anchor && anchor.parentNode;
4546

46-
document.getElementById(hash).classList.toggle('open');
47-
toggleChildrenClass(parent);
47+
if (heading) {
48+
heading.classList.toggle('open');
49+
toggleChildrenClass(heading);
50+
}
4851
}
4952

50-
const toggleClasses = (e: Event) => {
53+
const toggleClasses = (e: any) => {
5154
if (e.target.localName !== 'h3') return;
5255
history.replaceState({}, '', '#' + e.target.getElementsByTagName('a')[0].id);
5356
history.scrollRestoration = 'manual';
@@ -72,10 +75,8 @@ export default ({ pageContext, data }: any) => {
7275
const bPosition = b.frontmatter.position
7376
if (aPosition < bPosition) {
7477
return -1
75-
} else if (aPosition > bPosition) {
76-
return 1
7778
}
78-
return 0
79+
return 1
7980
})
8081

8182
return (
@@ -88,7 +89,7 @@ export default ({ pageContext, data }: any) => {
8889
{sections.map(
8990
(
9091
{
91-
frontmatter: { title, permalink, questions },
92+
frontmatter: { title, permalink },
9293
rawMarkdownBody,
9394
}: any,
9495
i
@@ -97,7 +98,6 @@ export default ({ pageContext, data }: any) => {
9798
key={i}
9899
title={title}
99100
permalink={permalink}
100-
questions={questions}
101101
rawMarkdownBody={rawMarkdownBody}
102102
pageContext={pageContext}
103103
/>

0 commit comments

Comments
 (0)