Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 9b26af9

Browse files
hughesgoonjbee37142
authored andcommitted
🎁 feat: Enhance the scroll feature in useCategory hook
Modify the hooks that scroll to tag-container for respond any width Change DEST_POS to the state value that based on bio.div using forwardRef
1 parent 1e4dc1c commit 9b26af9

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/components/bio/index.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import React from 'react'
1+
import React, { forwardRef } from 'react'
22
import { StaticQuery, graphql, Link } from 'gatsby'
33
import Image from 'gatsby-image'
44

55
import './index.scss'
66

7-
export const Bio = () => (
8-
<StaticQuery
7+
export const Bio = forwardRef((props, ref) => {
8+
return <StaticQuery
99
query={bioQuery}
1010
render={data => {
1111
const { author, social, introduction } = data.site.siteMetadata
1212

1313
return (
14-
<div className="bio">
14+
<div ref={ref} className="bio">
1515
<div className="author">
1616
<div className="author-description">
1717
<Image
@@ -63,7 +63,7 @@ export const Bio = () => (
6363
)
6464
}}
6565
/>
66-
)
66+
})
6767

6868
const bioQuery = graphql`
6969
query BioQuery {

src/hooks/useCategory.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import qs from 'query-string'
33
import { CATEGORY_TYPE } from '../constants'
44
import * as ScrollManager from '../utils/scroll'
55

6-
const DEST_POS = 316
6+
let DEST_POS
77

8-
export function useCategory() {
8+
export function useCategory(DEST) {
99
const [category, setCategory] = useState(CATEGORY_TYPE.ALL)
10+
DEST_POS = DEST
1011
const adjustScroll = () => {
1112
if (window.scrollY > DEST_POS) {
1213
ScrollManager.go(DEST_POS)

src/pages/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { graphql } from 'gatsby'
22
import _ from 'lodash'
3-
import React, { useMemo } from 'react'
3+
import React, { useMemo, useRef, useEffect, useState } from 'react'
44
import { Bio } from '../components/bio'
55
import { Category } from '../components/category'
66
import { Contents } from '../components/contents'
@@ -28,8 +28,14 @@ export default ({ data, location }) => {
2828
() => _.uniq(posts.map(({ node }) => node.frontmatter.category)),
2929
[]
3030
)
31+
const bioRef = useRef(null)
32+
const [DEST, setDEST] = useState(316)
3133
const [count, countRef, increaseCount] = useRenderedCount()
32-
const [category, selectCategory] = useCategory()
34+
const [category, selectCategory] = useCategory(DEST)
35+
36+
useEffect( tabRef => {
37+
setDEST(!bioRef.current ? 316 : bioRef.current.getBoundingClientRect().bottom + window.pageYOffset + 24 )
38+
}, [bioRef.current])
3339

3440
useIntersectionObserver()
3541
useScrollEvent(() => {
@@ -47,7 +53,7 @@ export default ({ data, location }) => {
4753
return (
4854
<Layout location={location} title={siteMetadata.title}>
4955
<Head title={HOME_TITLE} keywords={siteMetadata.keywords} />
50-
<Bio />
56+
<Bio ref={bioRef} />
5157
<Category
5258
categories={categories}
5359
category={category}

0 commit comments

Comments
 (0)