Skip to content

Commit ec64330

Browse files
committed
fix: fixed width and scrolling issue
1 parent a0b4da9 commit ec64330

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

components/Accordion.tsx

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
2+
import { useRouter } from 'next/router';
23

34
interface AccordionItem {
45
question: string;
56
answer: string;
6-
id?: number;
7+
id: number;
78
}
89

910
interface AccordionProps {
@@ -12,37 +13,74 @@ interface AccordionProps {
1213

1314
const Accordion: React.FC<AccordionProps> = ({ items }) => {
1415
const [activeIndex, setActiveIndex] = useState<number | null>(null);
16+
const router = useRouter();
1517

1618
const handleToggle = (index: number) => {
1719
setActiveIndex((prevIndex) => (prevIndex === index ? null : index));
1820
};
1921

22+
useEffect(() => {
23+
const hash = router.asPath.split('#')[1];
24+
if (hash) {
25+
const id = parseInt(hash, 10);
26+
const index = items.findIndex((item) => item.id === id);
27+
if (index !== -1) {
28+
setActiveIndex(index);
29+
30+
setTimeout(() => {
31+
const element = document.getElementById(hash);
32+
if (element) {
33+
const navbarHeight = 150;
34+
const offset = element.offsetTop - navbarHeight;
35+
window.scrollTo({ top: offset, behavior: 'smooth' });
36+
}
37+
}, 0);
38+
}
39+
}
40+
}, [items, router.asPath]);
41+
42+
const handleLinkClick = (id: number) => {
43+
const index = items.findIndex((item) => item.id === id);
44+
setActiveIndex(index);
45+
46+
const newUrl = `#${id}`;
47+
router.push(newUrl, undefined, { shallow: true });
48+
};
49+
2050
return (
2151
<div>
2252
{items.map((item, index) => (
2353
<div
24-
key={index}
54+
key={item.id || index}
2555
className={`overflow-hidden transition-max-height border-t-2 ${
2656
activeIndex === index ? 'max-h-96' : 'max-h-20'
27-
} ${index === items.length - 1 ? 'border-b-2' : ''}`}
57+
} ${index === items.length - 1 ? 'border-b-2' : ''}`}
2858
>
29-
<div
30-
className='flex justify-between items-center p-4 cursor-pointer'
31-
onClick={() => handleToggle(index)}
32-
>
59+
<div className='flex justify-between items-center p-4 cursor-pointer'>
3360
<div className='text-[20px]'>
34-
<a href={`#${item.id}`}>{item.question}</a>
61+
<a
62+
href={`#${item.id}`}
63+
onClick={(e) => {
64+
e.preventDefault();
65+
handleLinkClick(item.id);
66+
}}
67+
>
68+
{item.question}
69+
</a>
3570
</div>
3671
<div
3772
className={`transform transition-transform text-[20px] ${
3873
activeIndex === index ? 'rotate-45' : ''
3974
}`}
75+
onClick={() => handleToggle(index)}
4076
>
4177
&#43;
4278
</div>
4379
</div>
4480
{activeIndex === index && (
45-
<div className='p-2 text-gray-500'>{item.answer}</div>
81+
<div id={`${item.id}`} className='p-2 text-gray-500'>
82+
{item.answer}
83+
</div>
4684
)}
4785
</div>
4886
))}

components/Sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const SidebarLayout = ({ children }: { children: React.ReactNode }) => {
127127
const pathWtihoutFragment = extractPathWithoutFragment(router.asPath);
128128
return (
129129
<div className='max-w-[1400px] mx-auto flex flex-col items-center'>
130-
<section>
130+
<section className='w-full'>
131131
<div className='bg-primary w-full h-12 mt-[4.5rem] z-150 flex relative flex-col justify-between items-center lg:hidden'>
132132
<div
133133
className='z-[150] flex w-full bg-primary justify-between items-center mt-2'

pages/overview/faq/index.page.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getLayout } from '~/components/Sidebar';
33
import Head from 'next/head';
44
import { SectionContext } from '~/context';
55
import Faq from '~/components/Faq';
6+
import { Headline1 } from '~/components/Headlines';
67

78
export default function Content() {
89
const newTitle = 'Frequently Asked Questions';
@@ -12,9 +13,7 @@ export default function Content() {
1213
<Head>
1314
<title>{newTitle}</title>
1415
</Head>
15-
<h1 className='group cursor-pointer hover:underline text-h1mobile md:text-h1 font-bold pt-10 mb-6'>
16-
Frequently Asked Questions
17-
</h1>
16+
<Headline1>{newTitle}</Headline1>
1817
<Faq category='general' />
1918
</SectionContext.Provider>
2019
);

0 commit comments

Comments
 (0)