Add scroll to top button

This commit is contained in:
Sakura Knoll
2022-03-06 17:06:29 +09:00 Unverified
parent f204492786
commit a17fb2d584
2 changed files with 57 additions and 6 deletions
+54 -4
View File
@@ -1,18 +1,42 @@
/** @jsxImportSource theme-ui */
import { mdiMenu } from '@mdi/js'
import { mdiArrowUp, mdiMenu } from '@mdi/js'
import Icon from '@mdi/react'
import React, { useCallback, useState } from 'react'
import React, { useCallback, useRef, useState } from 'react'
import { Box, Flex, IconButton } from 'theme-ui'
import Honkai3rdNavigator from '../organisms/Honkai3rdNavigator'
const Honkai3rdLayout: React.FC = ({ children }) => {
const [hiddenMobileNav, setHiddenMobileNav] = useState(true)
const contentBoxRef = useRef<HTMLDivElement>(null)
const [scrolled, setScrolled] = useState(false)
const close = useCallback(() => {
setHiddenMobileNav(true)
}, [])
const handleScroll = useCallback(() => {
if (contentBoxRef.current == null) {
return
}
if (contentBoxRef.current.scrollTop === 0) {
return setScrolled(false)
}
if (scrolled) {
return
}
setScrolled(true)
}, [scrolled])
const scrollToTop = useCallback(() => {
if (contentBoxRef.current == null) {
return
}
contentBoxRef.current.scrollTop = 0
}, [])
return (
<Flex
sx={{
@@ -33,14 +57,40 @@ const Honkai3rdLayout: React.FC = ({ children }) => {
>
<Honkai3rdNavigator close={close} />
</Box>
<Box sx={{ flex: 1, overflowY: 'auto', overflowX: 'hidden' }}>
<Box
sx={{
flex: 1,
overflowY: 'auto',
overflowX: 'hidden',
scrollBehavior: 'smooth',
}}
onScroll={handleScroll}
ref={contentBoxRef}
>
<IconButton
sx={{
width: 50,
height: 50,
position: 'absolute',
bottom: 1,
right: 1,
zIndex: 1000,
pointerEvents: scrolled ? 'auto' : 'none',
opacity: scrolled ? 1 : 0,
transition: 'opacity 200ms ease-in-out',
backgroundColor: 'transparent',
}}
onClick={scrollToTop}
>
<Icon path={mdiArrowUp} size={1} />
</IconButton>
<Flex
sx={{
position: 'sticky',
display: ['block', 'none'],
top: 0,
height: 50,
bg: 'rgba(255,255,255,0.8)',
bg: 'transparent',
}}
>
<IconButton