Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAY-1000] Collapsible Nav in Website Sidebar #2729

Merged
merged 26 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4e25c3c
Convert sidebar to react
nidaqg Sep 5, 2023
5c8eebb
fixed dark mode, moved helper functions
nidaqg Sep 5, 2023
a1afbe0
fixed keys
nidaqg Sep 5, 2023
d74dbaf
Merge branch 'master' into PLAY-934-side-nav-to-react
nidaqg Sep 6, 2023
42d27cc
Merge branch 'master' into PLAY-934-side-nav-to-react
nidaqg Sep 6, 2023
bd23e4a
Merge branch 'master' into PLAY-934-side-nav-to-react
nidaqg Sep 6, 2023
6cd00b5
collapsible nav into sidebar
nidaqg Sep 7, 2023
579990e
fixed merge conflicts
nidaqg Sep 7, 2023
681a988
padding and margin fixes
nidaqg Sep 7, 2023
10f4803
Removed unnecessary scss
nidaqg Sep 7, 2023
55498a9
custom toggling + keep clicked item toggled open
nidaqg Sep 7, 2023
9e436b6
testing without useeffect
nidaqg Sep 7, 2023
b49294d
deleted unused code
nidaqg Sep 7, 2023
ab17ae6
if item clicked, will be toggled open on rerender
nidaqg Sep 8, 2023
15b6d03
stay toggled open if sub item active
nidaqg Sep 8, 2023
db4ca86
Do not navigatge if already on page
nidaqg Sep 8, 2023
6fd27dc
no navigation if already on page
nidaqg Sep 8, 2023
55759f7
fixed animation issue + added cursor
nidaqg Sep 10, 2023
db27c6f
Testing with commentring out link logic
nidaqg Sep 10, 2023
5a2978a
testing with commenting out currentURL logic
nidaqg Sep 10, 2023
3a7233f
extracting conditionals outside render
nidaqg Sep 11, 2023
e00dc66
adding back currentURL logic
nidaqg Sep 11, 2023
699d2f9
extracted currentURl logic out of render
nidaqg Sep 11, 2023
023064f
Fixes for icon click not toggling item
nidaqg Sep 12, 2023
dd26135
Testing with fix in collapsible to prevent default behavior
nidaqg Sep 12, 2023
a1de024
Merge branch 'master' into PLAY-1000-collapsible-nav-in-sidebar
nidaqg Sep 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 126 additions & 45 deletions playbook-website/app/javascript/components/MainSidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,135 @@
import React from "react";
import { Nav, NavItem } from "playbook-ui";
import React, {useState, useEffect} from "react";
import { Nav, NavItem, useCollapsible } from "playbook-ui";
import { linkFormat } from "../../utilities/website_sidebar_helper";

const MainSidebar = ({ dark, type, category, kit, kits }) => {
return (
<>
<Nav
dark={dark}
link={`/kits${type ? `?type=${type}` : ""}`}
paddingY="sm"
title="Kits"
variant="subtle"
>
{kits.map((link, i) =>
typeof link === "object" ? (
<div key={`${Object.keys(link)[0]}-${i}`} className="category_section">
<NavItem
active={category === Object.keys(link)[0]}
className="category"
dark={dark}
iconRight="angle-down"
key={`${Object.keys(link)[0]}-${i}`}
link={`/kit_category/${Object.keys(link)}?type=${type}`}
text={linkFormat(Object.keys(link))}
/>

{link[Object.keys(link)[0]].map((sublink, i) => (
<NavItem
active={kit === sublink}
dark={dark}
key={`${sublink}-${i}`}
marginLeft="xl"
link={`/kits/${sublink}/${type}`}
text={linkFormat(sublink)}
/>
))}
</div>
) : (
//hook into collapsible logic for all nested nav items
const collapsibles = kits.map(() => useCollapsible());

const currentURL = window.location.pathname + window.location.search;

//set up custom toggling
const handleMainClick = (index) => {
collapsibles.forEach(([, , setCollapsed], idx) => {
if (idx === index) {
setCollapsed(false);
} else {
setCollapsed(true);
}
});
return true
};


const renderNavItem = (link, i) => {
const [collapsed] = collapsibles[i];

const generateLink = (categoryKey, sublink, type) => {
if (sublink) {
const link = `/kits/${sublink}/${type}`;
return currentURL === link ? "" : link;
} else {
const link = `/kit_category/${categoryKey}?type=${type}`;
return currentURL === link ? "" : link;
}
};

if (typeof link === "object") {
//useState for handling collapsed state
const [toggleNav, setToggleNav] = useState(false);
//useEffect to handle toggle to consolidate logic
useEffect(() => {
setToggleNav(isActiveCategory || hasActiveSublink ? false : collapsed);
}, [collapsed]);

//click event for right icon
const handleIconClick = (index) => {
collapsibles.forEach(([, ,], idx) => {
if (idx === index) {
toggleNav === true ? setToggleNav(false) : setToggleNav(true)
}
});
};

const categoryKey = Object.keys(link)[0];
const sublinks = link[categoryKey];
const isActiveCategory = category === categoryKey;

const hasActiveSublink = link[Object.keys(link)[0]].some(
(sublink) => sublink === kit
);
return (
<NavItem
active={isActiveCategory}
collapsed={toggleNav}
collapsible
collapsibleTrail
cursor="pointer"
dark={dark}
fontSize="small"
iconRight={["plus", "minus"]}
key={`${categoryKey}-${i}`}
link={generateLink(categoryKey, null, type)}
marginBottom="none"
marginTop="xxs"
onClick={() => handleMainClick(i)}
onIconRightClick={() => handleIconClick(i)}
paddingY="xxs"
text={linkFormat(categoryKey)}
>
{sublinks.map((sublink, j) => (
<NavItem
active={kit === link}
className="category"
active={kit === sublink}
cursor="pointer"
dark={dark}
key={`${link}-${i}`}
text={linkFormat(link)}
link={`/kits/${link}?type=${type}`}
fontSize="small"
key={`${sublink}-${j}`}
link={generateLink(categoryKey, sublink, type)}
marginY="none"
paddingY="xxs"
text={linkFormat(sublink)}
/>
)
)}
</Nav>
</>
))}
</NavItem>
);
} else {
return (
<NavItem
active={kit === link}
cursor="pointer"
dark={dark}
fontSize="small"
key={`${link}-${i}`}
link={generateLink(null, link, type)}
marginBottom="none"
marginTop="xxs"
text={linkFormat(link)}
paddingY="xxs"
/>
);
}
};

return (
<Nav dark={dark} variant="bold" paddingTop="xxs">
<NavItem
collapsed={false}
collapsible
collapsibleTrail
cursor="pointer"
dark={dark}
fontSize="small"
fontWeight="bolder"
iconRight={["plus", "minus"]}
key="top-nav-item"
link={currentURL === `/kits${type ? `?type=${type}` : ""}` ? "" : `/kits${type ? `?type=${type}` : ""}`}
marginY="none"
paddingY="xxs"
text="Components"
>
{kits.map((link, i) => renderNavItem(link, i))}
</NavItem>
</Nav>
);
};

Expand Down
12 changes: 0 additions & 12 deletions playbook-website/app/javascript/site_styles/_site-style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,6 @@ body {
&--sideNav {
height: calc(100vh - 89px);
overflow: auto;
.category_section {
&.active {
padding: $space_xs 0;
background: $bg_light;
}
.sub_category {
padding-left: $space_md;
}
}
&[class*=dark]{
border-color: $border_dark;
}
.mobile-sidebar-pages {
display: none;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ const CollapsibleMain = ({
const mainSpacing = globalProps(props, { cursor })

const handleCollapsibleClick = () => {
onClick && onClick();
//To disable default toggling behavior return "true" in the onClick()
const disableToggle = onClick && onClick();
if (disableToggle !== true) {
toggle();
onClick && onClick()
}
}

return (
Expand Down