From aa2522ba0ff193ab6c131f04bd712ff93a77b7ec Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 6 Sep 2022 16:00:28 +0300 Subject: [PATCH] overlay and navigation changes --- src/components/Navigation/index.tsx | 56 ++++++++++++++--------------- src/components/Overlay/index.tsx | 28 ++++++--------- src/style/style.css | 4 --- 3 files changed, 38 insertions(+), 50 deletions(-) diff --git a/src/components/Navigation/index.tsx b/src/components/Navigation/index.tsx index 77ca7fc2..5138600d 100644 --- a/src/components/Navigation/index.tsx +++ b/src/components/Navigation/index.tsx @@ -2,10 +2,11 @@ import { MenuToggle } from "../MenuToggle" import { useMobile } from "src/hooks/useMobile" import { Menu } from "../Menu" import { Overlay } from "../Overlay" -import { useState, useCallback, useContext } from "react" +import { useContext } from "react" import { ConfigContext } from "src/context/ConfigContext" import { Block } from "../Block" import { Spacer } from "../Spacer" +import { useToggle } from "react-use" export interface INavigationProps { style?: React.CSSProperties @@ -13,26 +14,32 @@ export interface INavigationProps { export const Navigation: React.FC = props => { const collapseMenu = useMobile() - const [open, setOpen] = useState(false) - const onClick = useCallback( - () => { - setOpen(!open) - }, - [open] - ) + const [open, toggleOpen] = useToggle(false) const { menu } = useContext(ConfigContext) + if (!collapseMenu) { + return ( + + ) + } + return ( <> = props => { - {collapseMenu ? ( - - - - - ) : ( - - )} + + + + ) } diff --git a/src/components/Overlay/index.tsx b/src/components/Overlay/index.tsx index 6149251d..1b1e5947 100644 --- a/src/components/Overlay/index.tsx +++ b/src/components/Overlay/index.tsx @@ -2,30 +2,20 @@ import s from "./styles.module.css" import { Transition } from "./Transition" import { useReducedMotion } from "../../hooks/useReducedMotion" -import { useCallback, useEffect } from "react" +import { useLockBodyScroll } from "react-use" -export interface IOverlayProps { +export type OverlayProps = { show: boolean onClickOverlay: () => void style?: React.CSSProperties + duration: number } -export const Overlay: React.FC = props => { +export const Overlay: React.FC = props => { const motionDisabled = useReducedMotion() - const onClick = useCallback((event: React.MouseEvent) => { - if (event.target === event.currentTarget) { - props.onClickOverlay() - } - }, [props.onClickOverlay]) - const duration = motionDisabled ? 0 : 250 + const duration = motionDisabled ? 0 : props.duration - useEffect(() => { - if (props.show) { - document.body.classList.add("noscroll") - } else { - document.body.classList.remove("noscroll") - } - }, [props.show]) + useLockBodyScroll(props.show) return ( = props => { show={props.show} >
{ + if (event.target === event.currentTarget) { + props.onClickOverlay() + } + }} className={s.overlay} style={props.style} > diff --git a/src/style/style.css b/src/style/style.css index e9d22303..b38b0389 100644 --- a/src/style/style.css +++ b/src/style/style.css @@ -136,10 +136,6 @@ a.invisible { border-bottom-color: var(--color-text-opposite); } -.noscroll { - overflow: hidden; -} - /* RC TOOLTIP OVERRIDE */ .hudozka.rc-tooltip {