Skip to content

Commit

Permalink
dropdown create for docs sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatnema committed Nov 17, 2023
1 parent deeb2a5 commit 33321d4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
7 changes: 7 additions & 0 deletions components/icons/DocsArrow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function DocsArrow({ isDropDown, activeDropDown, onClick, className }) {
return (
<div className={`w-6 my-auto p-2 rounded-md cursor-pointer ${isDropDown && 'hover:bg-gray-100'}`} onClick={isDropDown ? onClick : () => { }}>
{isDropDown && <img src='/img/illustrations/icons/arrow.svg' className={`transition-transform w-fit m-auto duration-200 transform ${className} ${activeDropDown ? 'rotate-90 translate-x-0.5' : ''}`} />}
</div>
);
}
2 changes: 1 addition & 1 deletion components/layout/DocsLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default function DocsLayout({ post, navItems = {}, children }) {
className="hidden lg:flex lg:flex-shrink-0"
data-testid="DocsLayout-main"
>
<div className="flex flex-col w-64 border-r border-gray-200 bg-white py-2">
<div className="flex flex-col w-72 border-r border-gray-200 bg-white py-2">
<div className="flex-1 flex flex-col md:overflow-y-auto md:sticky md:top-20 md:max-h-(screen-14)">
<SearchButton
className="mt-8 mb-4 mr-2 flex items-center text-left text-sm space-x-3 px-3 py-1.5 bg-white hover:bg-secondary-100 border-gray-300 hover:border-secondary-500 border text-gray-700 hover:text-secondary-500 shadow-sm transition-all duration-500 ease-in-out rounded-md"
Expand Down
44 changes: 28 additions & 16 deletions components/navigation/DocsNav.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, { useState } from 'react';
import DocsNavItem from './DocsNavItem';
import IconHome from '../icons/Home';
import SubCategoryDocsNav from './SubCategoryDocsNav';
import DocsArrow from '../icons/DocsArrow';

import { buckets } from '../data/buckets';
import { useEffect } from 'react';


const serializedBuckets = buckets.reduce((acc, bucket) => {
acc[bucket.name] = {
Expand All @@ -19,28 +24,35 @@ const serializedBuckets = buckets.reduce((acc, bucket) => {
export default function DocsNav({
item,
active,
onClick = () => {},
onClick = () => { },
}) {
const subCategories = item.children;
const bucket = serializedBuckets[item.item.rootSectionId];

const [openSubCategory, setOpenSubCategory] = useState(active.startsWith(item.item.slug));

const onClickHandler = () => {
setOpenSubCategory(!openSubCategory);
onClick();
}

useEffect(() => {
setOpenSubCategory(active.startsWith(item.item.slug));
}, [active])

return (
<li className='mb-4' key={item.item.title} data-testid="DocsNav-item">
<DocsNavItem {...item.item} activeSlug={active} defaultClassName='font-body text-sm text-black hover:font-semibold' inactiveClassName='font-regular' activeClassName='font-semibold' bucket={bucket} onClick={onClick} />
<ul className='border-l border-gray-200 pl-4 ml-3 mt-1'>
{Object.values(subCategories).map((subCategory) => (
<li key={subCategory.item.title} data-testid="DocsNav-subitem">
<DocsNavItem {...subCategory.item} activeSlug={active} defaultClassName={`font-body text-sm text-black leading-8 ${subCategory.children ? 'hover:font-semibold' : 'hover:text-secondary-600'}`} inactiveClassName='font-regular' activeClassName={subCategory.children ? 'font-semibold' : 'text-secondary-600'} onClick={onClick} />
<ul className='border-l border-gray-200 pl-4'>
{subCategory.children && subCategory.children.map(subItem => (
<li key={subItem.title}>
<DocsNavItem {...subItem} activeSlug={active} defaultClassName='font-body text-sm text-gray-700 leading-7 hover:text-secondary-600' inactiveClassName='font-regular' activeClassName='text-secondary-600' onClick={onClick} />
</li>
))}
</ul>
</li>
))}
</ul>
<div className='flex gap-2'>
<DocsArrow isDropDown={Object.values(subCategories).length > 0} activeDropDown={openSubCategory} onClick={() => setOpenSubCategory(!openSubCategory)} />
<DocsNavItem {...item.item} activeSlug={active} defaultClassName='font-body text-sm text-black hover:font-semibold' inactiveClassName='font-regular' activeClassName='font-semibold' bucket={bucket} onClick={onClickHandler} />
</div>
{openSubCategory && (
<ul className='border-l border-gray-200 pl-4 ml-3 mt-1'>
{Object.values(subCategories).map((subCategory) => (
<SubCategoryDocsNav key={subCategory.item.title} subCategory={subCategory} active={active} onClick={onClick} />
))}
</ul>
)}
</li>
);
}
34 changes: 34 additions & 0 deletions components/navigation/SubCategoryDocsNav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useState, useEffect } from "react";
import DocsNavItem from "./DocsNavItem";
import DocsArrow from "../icons/DocsArrow";

export default function SubCategoryDocsNav({ subCategory, active, onClick }) {
const [openSubCategoryChildren, setOpenSubCategoryChildren] = useState(active.startsWith(subCategory.item.slug));

const onClickHandler = () => {
setOpenSubCategoryChildren(!openSubCategoryChildren);
onClick();
}

useEffect(() => {
setOpenSubCategoryChildren(active.startsWith(subCategory.item.slug));
}, [active])

return (
<li key={subCategory.item.title} data-testid="DocsNav-subitem">
<div className='flex gap-2'>
<DocsArrow isDropDown={subCategory.children} activeDropDown={openSubCategoryChildren} onClick={() => setOpenSubCategoryChildren(!openSubCategoryChildren)} />
<DocsNavItem {...subCategory.item} activeSlug={active} defaultClassName={`font-body text-sm text-black leading-8 ${subCategory.children ? 'hover:font-semibold' : 'hover:text-secondary-600'}`} inactiveClassName='font-regular' activeClassName={subCategory.children ? 'font-semibold' : 'text-secondary-600'} onClick={onClickHandler} />
</div>
{openSubCategoryChildren && (
<ul className='border-l ml-8 border-gray-200 pl-4'>
{subCategory.children && subCategory.children.map(subItem => (
<li key={subItem.title}>
<DocsNavItem {...subItem} activeSlug={active} defaultClassName='font-body text-sm text-gray-700 leading-7 hover:text-secondary-600' inactiveClassName='font-regular' activeClassName='text-secondary-600' onClick={onClick} />
</li>
))}
</ul>
)}
</li>
)
}

0 comments on commit 33321d4

Please sign in to comment.