|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { useCallback } from 'react'; |
| 3 | +import { useDispatch, useSelector } from 'react-redux'; |
| 4 | +import styled from 'styled-components'; |
| 5 | +import { |
| 6 | + LAYOUT_FOOTER_HEIGHT, |
| 7 | + LAYOUT_HEADER_HEIGHT, |
| 8 | + MARGIN, |
| 9 | + MIDDLE_COLUMN_SIZE, |
| 10 | + POST_MARGIN_BOTTOM, |
| 11 | +} from '../const/sizes'; |
| 12 | +import { getIsOutline } from '../logic/store/post.selector'; |
| 13 | +import { postSlice } from '../logic/store/post.slice'; |
| 14 | + |
| 15 | +export const Outline: React.FC = () => { |
| 16 | + const dispatch = useDispatch(); |
| 17 | + const [outLineTop, setOutLineTop] = React.useState(0); |
| 18 | + const [outlineHeight, setOutlineHeight] = useState(0); |
| 19 | + const ref = useRef<HTMLDivElement>(null); |
| 20 | + const items = useSelector(getIsOutline); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + setOutlineHeight(ref.current?.clientHeight || 0); |
| 24 | + dispatch(postSlice.actions.reset()); |
| 25 | + }, [dispatch]); |
| 26 | + |
| 27 | + const handleScroll = useCallback(() => { |
| 28 | + const { scrollY, innerHeight } = window; |
| 29 | + const clientHeight = document.body.clientHeight; |
| 30 | + const isReachedTop = scrollY > LAYOUT_HEADER_HEIGHT - MARGIN; |
| 31 | + |
| 32 | + if (isReachedTop) { |
| 33 | + const visibleFooterPart = Math.max( |
| 34 | + scrollY + innerHeight - clientHeight + LAYOUT_FOOTER_HEIGHT + POST_MARGIN_BOTTOM, |
| 35 | + 0 |
| 36 | + ); |
| 37 | + const top = Math.min(innerHeight - outlineHeight - visibleFooterPart, MARGIN); |
| 38 | + |
| 39 | + setOutLineTop(top); |
| 40 | + } else { |
| 41 | + setOutLineTop(0); |
| 42 | + } |
| 43 | + }, [outlineHeight]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + window.addEventListener('scroll', handleScroll); |
| 47 | + return () => window.removeEventListener('scroll', handleScroll); |
| 48 | + }, [handleScroll]); |
| 49 | + |
| 50 | + const handleOnItemClick = useCallback( |
| 51 | + (y: number) => () => { |
| 52 | + window.scrollTo(0, y); |
| 53 | + }, |
| 54 | + [] |
| 55 | + ); |
| 56 | + |
| 57 | + return ( |
| 58 | + <OutlineSticker outLineTop={outLineTop} ref={ref}> |
| 59 | + <OutlineContainer> |
| 60 | + <OutlineItems> |
| 61 | + {items?.map((item, index) => ( |
| 62 | + <OutlineItem key={index} onClick={handleOnItemClick(item.offsetTop)}> |
| 63 | + {item.isReached ? <strong>{item.title}</strong> : item.title} |
| 64 | + </OutlineItem> |
| 65 | + ))} |
| 66 | + </OutlineItems> |
| 67 | + </OutlineContainer> |
| 68 | + </OutlineSticker> |
| 69 | + ); |
| 70 | +}; |
| 71 | + |
| 72 | +const OutlineSticker = styled.div<{ outLineTop: number }>` |
| 73 | + ${({ outLineTop }) => |
| 74 | + outLineTop |
| 75 | + ? ` |
| 76 | + position: fixed; |
| 77 | + top: ${outLineTop}px; |
| 78 | + width: calc((100% - ${MIDDLE_COLUMN_SIZE}) / 2); |
| 79 | + ` |
| 80 | + : ''} |
| 81 | +`; |
| 82 | + |
| 83 | +const OutlineContainer = styled.div` |
| 84 | + display: flex; |
| 85 | + justify-content: center; |
| 86 | + margin: 0 ${MARGIN}px; |
| 87 | +`; |
| 88 | + |
| 89 | +const OutlineItems = styled.div` |
| 90 | + font-size: 12px; |
| 91 | + font-weight: 100; |
| 92 | +`; |
| 93 | + |
| 94 | +const OutlineItem = styled.div` |
| 95 | + margin-bottom: 5px; |
| 96 | + cursor: pointer; |
| 97 | +`; |
0 commit comments