From 522a1cdbaf0f478fb23a9f770b9caf732ea13803 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 29 Oct 2019 17:11:07 +0300 Subject: Hide header on scroll (mobile only) --- vnext/src/App.js | 115 +++++++++++++++++++++++++------------------------------ 1 file changed, 53 insertions(+), 62 deletions(-) (limited to 'vnext/src/App.js') diff --git a/vnext/src/App.js b/vnext/src/App.js index f39875c1..011e5cbd 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -1,5 +1,6 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom'; +import { useScroll, useRafState } from 'react-use'; import qs from 'qs'; import svg4everybody from 'svg4everybody'; @@ -10,18 +11,23 @@ import { Friends, Readers } from './ui/Users'; import Settings from './ui/Settings'; import Contacts from './ui/Contacts'; import Chat from './ui/Chat'; +import Header from './ui/Header'; import Post from './ui/Post'; import Thread from './ui/Thread'; import Login from './ui/Login'; -import { UserLink } from './ui/UserInfo'; -import SearchBox from './ui/SearchBox'; import cookie from 'react-cookies'; import { me } from './api'; +const elClassHidden = 'header--hidden'; + +const elClassFull = 'content--full'; + export default function App() { + let contentRef = useRef(null); + useEffect(() => { svg4everybody(); }, []); @@ -49,6 +55,48 @@ export default function App() { }); }; + const [scrollState, setScrollState] = useRafState({ + hidden: false, + bottom: false, + prevScroll: 0 + }); + + let { x, y } = useScroll(contentRef); + + useEffect(() => { + let dHeight = contentRef.current.scrollHeight; + let wHeight = contentRef.current.clientHeight; + setScrollState((scrollState) => { + let wScrollDiff = scrollState.prevScroll - y; + let hidden = scrollState.hidden; + let bottom = scrollState.bottom; + if (y <= 0) { + // scrolled to the very top; element sticks to the top + hidden = false; + bottom = false; + } else if ((wScrollDiff > 0) && hidden) { + // scrolled up; element slides in + hidden = false; + bottom = false; + } else if (wScrollDiff < 0) { + // scrolled down + if ((y + wHeight) >= dHeight && hidden) { + // scrolled to the very bottom; element slides in + hidden = false; + bottom = true; + } else { + // scrolled down; element slides out + hidden = true; + bottom = false; + } + } + return { + hidden: hidden, + bottom: bottom, + prevScroll: y + }; + }); + }, [x, y, setScrollState]); const [hash, setHash] = useState(cookie.load('hash')); const [eventSource, setEventSource] = useState({}); @@ -111,66 +159,9 @@ export default function App() { return ( <> - +
-
+
} /> } /> -- cgit v1.2.3