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

[MOL-16700][AM] Add local nav dropdown and local nav menu components #605

Merged
merged 44 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7656e09
[MOL-16700][AM] Add local nav dropdown and local nav menu components
Oct 25, 2024
5c30ca9
[MOL-16700][AM] remove mastheadheight since its not used. (we can use…
Oct 28, 2024
66932fe
[MOL-16700][AM] remove ISectionItem. update sections to titleList
Oct 28, 2024
183a7d3
[MOL-16700][AM]Added className and ids
Oct 28, 2024
2ba8fbc
[MOL-16700][AM]Added stories for local nav dropdown
Oct 28, 2024
e50c4c5
[MOL-16700][AM]Added stories for local nav menu component
Oct 28, 2024
bd9e5ae
[MOL-16700][AM]Added className and ids for localnavMenu
Oct 28, 2024
87f2958
[MOL-16700][AM]update dropdown story
Oct 28, 2024
4188127
[MOL-16700][AM]update dropdown ids
Oct 28, 2024
dbdd954
[MOL-16700][AM]update dropdwon story to cater for scroll behaviour
Oct 28, 2024
19706a2
[MOL-16700][RL] Update LocalNav documentation
qroll Oct 31, 2024
526f343
[MOL-16700][AM]update visibleSectioindex when the dropdown item is se…
Nov 1, 2024
c8dc240
[MOL-16700][AM] update file names
Nov 1, 2024
3d7a034
[MOL-16700][AM] update file names
Nov 1, 2024
d9aec3b
[MOL-16700][AM] update types
Nov 1, 2024
0b55aa8
[MOL-16700][AM] update styles
Nov 1, 2024
f379bb3
[MOL-16700][AM] rename props
Nov 1, 2024
be24fe2
[MOL-16700][AM] update styles
Nov 1, 2024
16a32e1
[MOL-16700][AM] update stories
Nov 1, 2024
a953ddc
[MOL-16700][AM] update chevron icon based on the expaned state
Nov 1, 2024
d95e47e
[MOL-16700][AM] move LocalNavDropdownProps to seperate file
Nov 4, 2024
d032207
[MOL-16700][AM] delete unused stories
Nov 4, 2024
3d4808f
[MOL-16700][AM] update renderItem logic
Nov 4, 2024
1ce80da
[MOL-16700][AM] update stories and props-table
Nov 4, 2024
8d242bb
[MOL-16700][AM] update type fro handle Click.
Nov 7, 2024
922306f
[MOL-16700][AM] remove unnecessary container wrapper added in the dro…
Nov 7, 2024
7909c0d
[MOL-16700][AM] Add section headers
Nov 7, 2024
7c3f381
[MOL-16700][AM] update handclick in stories fro dropdown
Nov 8, 2024
e3638b1
[MOL-16700][AM] update items in props-table
Nov 8, 2024
ff5890c
[MOL-16700][AM] Add prefix $ for style props to mark them as transient
Nov 11, 2024
cb40ffb
[MOL-16700][AM] update order in stories
Nov 11, 2024
fadaa9a
[MOL-16700][AM] reorder imports
Nov 11, 2024
a16b17c
[MOL-16700][AM] remove unwanted styles
Nov 13, 2024
0f4904b
[MOL-16700][AM] update the margin values dynamically
Nov 13, 2024
13a6eb1
[MOL-16700][AM] remove default id and move text body to styles file
Nov 14, 2024
b1735f6
[MOL-16700][AM] remove unused imports
Nov 14, 2024
9517601
[MOL-16700][AM] remove border radius from navlabel
Nov 15, 2024
674ef68
[MOL-16700][AM] update border radius and padding when the item is sel…
Nov 18, 2024
9944dac
[MOL-16700][AM] update dynamicPadding to use ref instead of query sel…
Nov 18, 2024
c6f7651
[MOL-16700][AM] update border radius when the dropdown is not expanded
Nov 19, 2024
241b171
[MOL-16700][AM] update border radius when the dropdown is not expanded
Nov 19, 2024
5a1803f
[MOL-16700][AM] use ref from forwardRef
Nov 19, 2024
06aaad6
[MOL-16700][AM] use client width instead of inner width to cater for …
Nov 20, 2024
591f7f6
[MOL-16700][RL] Update docs
qroll Nov 20, 2024
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
3 changes: 3 additions & 0 deletions src/local-nav/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./local-nav-dropdown/local-nav-dropdown";
export * from "./local-nav-menu/local-nav-menu";
export * from "./types";
13 changes: 13 additions & 0 deletions src/local-nav/internal-types.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LocalNavItemProps } from "./types";

export interface LocalNavItemComponentProps {
handleClick: React.MouseEventHandler<HTMLLIElement>;
isSelected: boolean;
item: LocalNavItemProps;
renderItem?:
| ((
item: LocalNavItemProps,
renderProps: { selected: boolean }
) => React.ReactNode)
| undefined;
}
127 changes: 127 additions & 0 deletions src/local-nav/local-nav-dropdown/local-nav-dropdown.styles.tsx
qroll marked this conversation as resolved.
Show resolved Hide resolved
AnuMutyala marked this conversation as resolved.
Show resolved Hide resolved
AnuMutyala marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { ChevronDownIcon } from "@lifesg/react-icons/chevron-down";
import { TickIcon } from "@lifesg/react-icons/tick";
import styled, { css } from "styled-components";
import { Color } from "../../color";
import { Text } from "../../text/text";

// =============================================================================
// STYLE INTERFACES, transient props are denoted with $
// See more https://styled-components.com/docs/api#transient-props
// =============================================================================
interface DropdownNavStyleProps {
$isStickied?: boolean;
$stickyOffset: number;
$sideMargin?: number;
}
interface NavItemListStyleProps {
$viewportHeight?: number;
}

interface NavItemStyleProps {
$isSelected?: boolean;
}
interface DropdownExpandedProps {
$isDropdownExpanded: boolean;
}

interface NavIconStyleProps extends DropdownExpandedProps {}
interface NavLabelStyleProps extends DropdownExpandedProps {}
// =============================================================================
// STYLING
// =============================================================================
// use #rrggbbaa format for color (D9 is 0.85 alpha)
// LINK: https://rgbacolorpicker.com/rgba-to-hex
export const Backdrop = styled.div`
position: fixed;
top: 0;
right: 0;
left: 0;
bottom: 0;
background-color: ${Color.Neutral[1]}D9;
z-index: -1;
`;

export const LabelText = styled(Text.BodySmall)<NavItemStyleProps>`
margin: 0;
${(props) =>
props.$isSelected &&
css`
color: ${Color.Primary};
`}
`;

export const StyledTickIcon = styled(TickIcon)`
color: ${Color.Primary};
margin: 0 8px;
`;

export const NavIcon = styled(ChevronDownIcon)<NavIconStyleProps>`
color: ${Color.Primary};
transition: transform 250ms ease-in-out;
transform: rotate(${(props) => (props.$isDropdownExpanded ? 180 : 0)}deg);
`;

export const NavLabel = styled.div<NavLabelStyleProps>`
cursor: pointer;
background: ${Color.Neutral[8]};
padding: 12px 16px;
box-shadow: 0px 0px 1px 1px ${Color.Neutral[5]};
overflow: hidden;
border-radius: ${(props) => (props.$isDropdownExpanded ? "0" : "4px")};
qroll marked this conversation as resolved.
Show resolved Hide resolved
display: flex;
justify-content: space-between;
align-items: center;
transition: all 200ms linear;
`;

export const NavItem = styled.li<NavItemStyleProps>`
padding: ${(props) =>
props.$isSelected ? "12px 8px 12px 0" : "12px 8px 12px 32px"};
background: ${(props) =>
props.$isSelected ? Color.Accent.Light[5] : Color.Neutral[8]};
position: relative; /* Ensures that the tick mark is positioned relative to the selected item */
display: flex;
align-items: center; /* Vertically align text and tick */
`;

export const NavItemList = styled.ul<NavItemListStyleProps>`
transition: all 300ms;
transform-origin: top;
list-style-type: none;
padding: 0px 8px 0px 8px;
margin: 0;
background: ${Color.Neutral[8]};
cursor: pointer;
box-shadow: 0px 0px 1px 1px ${Color.Neutral[5]};
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
overflow-y: auto; /* Enables vertical scrolling */
max-height: ${(props) =>
props.$viewportHeight}px; /* Set a max height for the dropdown list */
`;

export const NavWrapper = styled.nav<DropdownNavStyleProps>`
display: block;
position: sticky;
top: ${(props) => props.$stickyOffset}px;
width: 100%;
z-index: 10;

${(props) =>
props.$isStickied &&
`${NavLabel} {
${props.$sideMargin && `margin: 0 -${props.$sideMargin}px;`}
padding: 12px 16px;
border-radius: 0;
}

${NavItemList} {
${props.$sideMargin && `margin-left: -${props.$sideMargin}px;`}
${props.$sideMargin && `margin-right: -${props.$sideMargin}px;`}
border-radius-bottom-left: 4px;
border-radius-bottom-right: 4px;

}

`}
`;
213 changes: 213 additions & 0 deletions src/local-nav/local-nav-dropdown/local-nav-dropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/* eslint-disable react/display-name */
import React, { useEffect, useRef, useState } from "react";
import { LocalNavItemComponentProps } from "../internal-types";
import { LocalNavDropdownProps, LocalNavItemProps } from "../types";
import {
Backdrop,
LabelText,
NavIcon,
NavItem,
NavItemList,
NavLabel,
NavWrapper,
StyledTickIcon,
} from "./local-nav-dropdown.styles";

const Component = ({
qroll marked this conversation as resolved.
Show resolved Hide resolved
defaultLabel,
stickyOffset = 0,
onNavItemSelect,
items,
selectedItemIndex,
id,
"data-testid": testId,
className,
renderItem,
}: LocalNavDropdownProps): JSX.Element => {
// =============================================================================
// CONST, STATE, REF
// =============================================================================
const detectStickyRef = useRef<HTMLSpanElement>(null);
const dropdownRef = useRef<HTMLDivElement>(null);
const [isStickied, setIsStickied] = useState<boolean>(false);
const navWrapperRef = useRef<HTMLElement>(null);
const [isDropdownExpanded, setIsDropdownExpanded] =
useState<boolean>(false);
const [viewportHeight, setViewportHeight] = useState(0);
const [dropdowntHeight, setDropdownHeight] = useState(0);
const [dynamicMargin, setDynamicMargin] = useState(0);
const navTestId = testId || "local-nav-dropdown";

const labelText =
selectedItemIndex >= 0 && isStickied
? items[selectedItemIndex].title
: defaultLabel;

// =============================================================================
// EFFECTS, EVENT LISTENERS
// ============================================================================

useEffect(() => {
if (dropdownRef.current) {
const dropdownHeight =
dropdownRef.current.getBoundingClientRect().height;
setDropdownHeight(dropdownHeight);
}
}, []);

useEffect(() => {
setViewportHeight(window.innerHeight);
const handleResize = () => {
setViewportHeight(window.innerHeight);
};
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);

useEffect(() => {
const callback = (entries: IntersectionObserverEntry[]) => {
const [entry] = entries;
setIsStickied(!entry.isIntersecting);
};

const observer = new IntersectionObserver(callback, {
threshold: 1,
rootMargin: `-${stickyOffset}px 0px 0px 0px`,
});

if (detectStickyRef.current) {
observer.observe(detectStickyRef.current);
}

return () => observer.disconnect();
}, [stickyOffset]);

useEffect(() => {
document.body.style.overflow =
isDropdownExpanded && isStickied ? "hidden" : "auto";
}, [isDropdownExpanded, isStickied]);

useEffect(() => {
const adjustPadding = () => {
const dropdown = navWrapperRef?.current;
if (dropdown) {
const dropdownRect = dropdown.getBoundingClientRect();
const spaceToRight = window.innerWidth - dropdownRect.right;
const spaceToLeft = dropdownRect.left;
// Calculate the padding needed to balance the dropdown in the viewport
const sidePadding = Math.max(spaceToRight, spaceToLeft);
setDynamicMargin(sidePadding);
}
};
adjustPadding();

window.addEventListener("resize", adjustPadding);

return () => {
window.removeEventListener("resize", adjustPadding);
};
}, []);

// =============================================================================
// EVENT HANDLERS
// =============================================================================

const handleToggleDropdown = () => {
setIsDropdownExpanded((prev) => !prev);
};

const handleDismissBackdrop = () => {
setIsDropdownExpanded(false);
};

const handleNavItemClick = (
e: React.MouseEvent,
item: LocalNavItemProps,
index: number
) => {
if (onNavItemSelect) {
onNavItemSelect(e, item, index);
}
setIsDropdownExpanded(false);
};

// =============================================================================
// RENDER FUNCTIONS
// =============================================================================

const renderDropdownNavItem = ({
handleClick,
isSelected,
item,
renderItem,
}: LocalNavItemComponentProps) => {
const { id, title } = item;

const renderTitle = () => {
if (renderItem) {
return renderItem(item, { selected: isSelected });
}
return (
<>
{isSelected && <StyledTickIcon />}
<LabelText $isSelected={isSelected}>{title}</LabelText>
</>
);
};

return (
<NavItem id={id} $isSelected={isSelected} onClick={handleClick}>
{renderTitle()}
</NavItem>
);
};

return (
<>
<span ref={detectStickyRef} data-testid={"sticky-ref"} />
<NavWrapper
$isStickied={isStickied}
$sideMargin={dynamicMargin}
$stickyOffset={stickyOffset}
ref={navWrapperRef}
id={id}
data-testid={navTestId}
className={className}
>
<NavLabel
ref={dropdownRef}
onClick={handleToggleDropdown}
data-testid={`${navTestId}-label`}
$isDropdownExpanded={isDropdownExpanded}
>
<LabelText weight="bold">{labelText}</LabelText>
<NavIcon $isDropdownExpanded={isDropdownExpanded} />
</NavLabel>
{isDropdownExpanded && (
<NavItemList
data-testid={`${navTestId}-dropdown-list`}
$viewportHeight={
viewportHeight - dropdowntHeight - stickyOffset
}
>
{items.map((item, i) =>
renderDropdownNavItem({
handleClick: (e) =>
handleNavItemClick(e, item, i),
isSelected:
i === selectedItemIndex && isStickied,
item,
renderItem,
})
)}
</NavItemList>
)}
{isDropdownExpanded && isStickied && (
<Backdrop onClick={handleDismissBackdrop} />
)}
</NavWrapper>
</>
);
};

export const LocalNavDropdown = React.forwardRef(Component);
Loading