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

Sidebar overflow and scroll to active item #51

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Sidebar overflow and automatic scroll to active item with `scrollIntoView`

## [3.1.1] - 2024-01-09

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion cypress/cypress/component/PanelSidebar/PanelSidebar.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,6 @@ describe("PanelSidebar.cy.tsx", () => {
cy.mount(PanelSidebar(sidebarItems));

// Check active entries
cy.get("#home").parent("li").should("have.class", "active");
cy.get("#home").parent("div").parent("li").should("have.class", "active");
});
});
70 changes: 40 additions & 30 deletions src/lib/Layout/PanelSideBarLayout/PanelSideBar/PanelSideBarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import classNames from "classnames";
import { ComponentType, useState } from "react";
import { ComponentType, useState, useRef, useEffect } from "react";
import { Collapse, NavItem } from "reactstrap";
import { LinkRendererProps } from "src/lib/SideBar/SideBarMenuContext";
import { PanelItem } from "./../PanelSideBar/Definitions/PanelItem";
Expand All @@ -19,52 +19,62 @@ const PanelSideBarItem = (props: PanelSideBarItemProps) => {
const { depth = 0, children: item, LinkRenderer, onClick, toggledItemIds = [], toggledSidebar } = props;

const hasitem = !!item.children?.length;
const isActive = item.children?.find((s) => s.active) || item.active;
const [isOpen, setIsOpen] = useState(toggledItemIds?.includes(item.id) || item.expanded);
if (item.display === false) {
return null;
}
const scrollToActiveItemRef = useRef<HTMLDivElement>(null);

useEffect(() => {
manni497 marked this conversation as resolved.
Show resolved Hide resolved
if (scrollToActiveItemRef.current && isActive) {
scrollToActiveItemRef.current.scrollIntoView();
}
}, []);

return (
<>
<NavItem
onClick={() => onClick && onClick(item)}
className={classNames({ "menu-open": isOpen, active: item.children?.find((s) => s.active) || item.active })}
className={classNames({ "menu-open": isOpen, active: isActive })}
style={{ paddingLeft: depth ? `${depth + 1}rem` : undefined }}
>
{hasitem ? (
<div className={classNames("d-flex flex-row", { "justify-content-between": item.collapseIconOnly })}>
{item.collapseIconOnly && (
<div ref={scrollToActiveItemRef}>
{hasitem ? (
<div className={classNames("d-flex flex-row", { "justify-content-between": item.collapseIconOnly })}>
{item.collapseIconOnly && (
<LinkRenderer item={item}>
<span className="nav-link">
{item.icon && <FontAwesomeIcon icon={item.icon} className="me-2" />}
{item.title}
</span>
</LinkRenderer>
)}

<a
role="button"
className={classNames("nav-link", { "w-100": !item.collapseIconOnly }, { "dropdown-toggle": hasitem })}
onClick={() => setIsOpen(!isOpen)}
>
{!item.collapseIconOnly && (
<span>
{item.icon && <FontAwesomeIcon className="me-2" icon={item.icon} />}
{item.title}
</span>
)}
</a>
</div>
) : (
<>
<LinkRenderer item={item}>
<span className="nav-link">
{item.icon && <FontAwesomeIcon icon={item.icon} className="me-2" />}
{item.title}
</span>
</LinkRenderer>
)}

<a
role="button"
className={classNames("nav-link", { "w-100": !item.collapseIconOnly }, { "dropdown-toggle": hasitem })}
onClick={() => setIsOpen(!isOpen)}
>
{!item.collapseIconOnly && (
<span>
{item.icon && <FontAwesomeIcon className="me-2" icon={item.icon} />}
{item.title}
</span>
)}
</a>
</div>
) : (
<>
<LinkRenderer item={item}>
<span className="nav-link">
{item.icon && <FontAwesomeIcon icon={item.icon} className="me-2" />}
{item.title}
</span>
</LinkRenderer>
</>
)}
</>
)}
</div>
</NavItem>

{hasitem && (
Expand Down
9 changes: 7 additions & 2 deletions styles/Layout/PanelSideBarLayout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,13 @@ section.content:first-of-type {
position: fixed;
display: flex;
align-items: flex-start;
overflow-y: auto;
overflow-x: hidden;
bottom: 0;
background-color: $primary;
width: $sidenav-base-width;
top: $topnav-base-height;
z-index: $zindex-fixed;
transition: $sidebar-transition;
overflow: hidden;

@include slim-scrollbar();

Expand Down Expand Up @@ -166,6 +165,8 @@ section.content:first-of-type {
height: 100%;
width: 100%;
transition: #{width $sidebar-transition};
overflow-x: hidden;
overflow-y: auto;

a {
color: inherit;
Expand Down Expand Up @@ -203,6 +204,10 @@ section.content:first-of-type {
margin-right: 0.5rem;
}

&.dropdown-toggle {
position: relative;
}

&.dropdown-toggle::after {
@include chevron-right();
}
Expand Down