Skip to content

Commit

Permalink
refactor(website): Improve navigation on menu and submenu rendering (#…
Browse files Browse the repository at this point in the history
…1480)

* refactor(website): Make the submenus always rendered but hidden

* refactor(website): improve keyboard navigating on the menu
  • Loading branch information
massao authored Nov 4, 2021
1 parent 08ccb57 commit 5362019
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions packages/forma-36-website/src/components/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const styles = {
margin-top: 0;
`,

hidden: css`
display: none;
`,

link: css`
display: flex;
justify-content: space-between;
Expand Down Expand Up @@ -100,6 +104,12 @@ const MenuListItem = React.forwardRef(
setIsExpanded(!isExpanded);
};

const handleKeyDown = (event) => {
if (event.nativeEvent.code === 'Enter') {
handleToggle(event);
}
};

const itemOffset = { paddingLeft: `${1 + hierarchyLevel}rem` };

return (
Expand All @@ -109,6 +119,9 @@ const MenuListItem = React.forwardRef(
<div
css={[styles.link, styles.linkGroup, itemOffset]}
onClick={handleToggle}
onKeyDown={handleKeyDown}
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={0}
>
{isCategory ? (
<SectionHeading>{item.name}</SectionHeading>
Expand All @@ -123,13 +136,12 @@ const MenuListItem = React.forwardRef(
icon={isExpanded ? 'ChevronDown' : 'ChevronRight'}
/>
</div>
{isExpanded && (
<MenuList
menuItems={item.menuLinks}
currentPath={currentPath}
hierarchyLevel={hierarchyLevel + 1}
/>
)}
<MenuList
isHidden={!isExpanded}
menuItems={item.menuLinks}
currentPath={currentPath}
hierarchyLevel={hierarchyLevel + 1}
/>
</>
) : (
<Link
Expand Down Expand Up @@ -161,15 +173,20 @@ MenuListItem.defaultProps = {
hierarchyLevel: 0,
};

const MenuList = ({ menuItems, currentPath, hierarchyLevel }) => {
const MenuList = ({
menuItems,
currentPath,
hierarchyLevel,
isHidden = false,
}) => {
const activeRef = useRef(null);
useEffect(() => {
if (activeRef.current) {
activeRef.current.scrollIntoView({ block: 'center' });
}
}, []);
return (
<ul css={styles.list}>
<ul css={[styles.list, isHidden && styles.hidden]}>
{menuItems.map((item, index) => {
const active = checkActive(item, currentPath);
return (
Expand Down

0 comments on commit 5362019

Please sign in to comment.