oxen-website/components/navigation/SideMenuRow.tsx

70 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-02-12 01:06:54 +01:00
import React, { useContext, useRef } from 'react';
import { ISideMenuItem } from '@/components/navigation/SideMenu';
import Link from 'next/link';
import { ScreenContext } from '@/contexts/screen';
import { ReactComponent as TriangleOutlinedSVG } from '@/assets/svgs/triangle-outlined.svg';
import { UI } from '@/constants';
import classNames from 'classnames';
import { collapseSideMenu } from '@/state/navigation';
2021-02-08 00:25:27 +01:00
import { useDispatch } from 'react-redux';
2021-02-12 01:06:54 +01:00
import { useHoverDirty } from 'react-use';
2021-02-04 04:34:23 +01:00
interface SideMenuRowProps {
item: ISideMenuItem;
isActive: boolean;
}
2021-02-08 00:25:27 +01:00
export function SideMenuRow({ item, isActive }: SideMenuRowProps) {
const { isMobile, isTablet, isDesktop, isHuge, isEnormous } = useContext(
ScreenContext,
);
2021-02-04 04:34:23 +01:00
const isCollapsible = isTablet || isMobile;
2021-02-08 00:25:27 +01:00
const dispatch = useDispatch();
const handleOnClick = () => {
dispatch(collapseSideMenu());
};
2021-02-04 04:34:23 +01:00
2021-02-12 01:06:54 +01:00
const ref = useRef(null);
const isHovering = useHoverDirty(ref);
const isExcited = !isCollapsible && (isActive || isHovering);
2021-03-18 05:44:59 +01:00
const inner = (
<div
ref={ref}
onClick={handleOnClick}
style={{
maxHeight: '5rem',
padding:
isMobile || isTablet
? `0 ${UI.PAGE_CONTAINED_PADDING_VW}vw`
: 'unset',
}}
className={classNames(
'flex flex-1 space-x-6 justify-between text-primary items-center cursor-pointer border-b border-black py-4 hover:bg-secondary duration-300 whitespace-nowrap',
(isHuge || isEnormous) ? 'text-2xl' : isDesktop ? 'text-xl' : 'text-xl',
2021-03-18 05:44:59 +01:00
isActive ? 'bg-secondary' : 'bg-transparent',
item.shouldHide ? 'hidden' : '',
2021-03-18 05:44:59 +01:00
)}
>
<span className="pl-6 whitespace-no-wrap">{item.label}</span>
2021-02-04 06:42:15 +01:00
2021-03-18 05:44:59 +01:00
{!isMobile && !isTablet && (
<TriangleOutlinedSVG
className={classNames(
'h-4 pr-6 duration-300 fill-current transform',
isExcited ? 'text-primary scale-y-75' : 'text-transparent',
2021-03-18 05:44:59 +01:00
)}
/>
)}
</div>
);
return item.isExternal ? (
<a href={item.href}>{inner}</a>
) : (
<Link href={item.href}>{inner}</Link>
2021-02-04 04:34:23 +01:00
);
}