-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add title of the article (still pending)
ref #651
- Loading branch information
1 parent
f71093f
commit e99cce2
Showing
3 changed files
with
76 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useLayoutEffect, useRef } from 'react' | ||
import { useSpring, a } from '@react-spring/web' | ||
import '../../styles/components/Article2/ArticleToCTitle.scss' | ||
|
||
function reduceTitleWithEllipsis(title = '') { | ||
const maxTitleLength = 50 | ||
if (typeof title === 'string' && title.length > maxTitleLength) { | ||
return title.slice(0, title.lastIndexOf(' ', maxTitleLength)) + '...' | ||
} | ||
return title | ||
} | ||
|
||
const ArticleToCTitle = ({ children, plainTitle = '', delay = 500 }) => { | ||
const ref = useRef() | ||
const isCollapsed = useRef(true) | ||
const [style, api] = useSpring(() => ({ | ||
height: 0, | ||
opacity: 0, | ||
transform: 'translate3d(0, 0, 0)', | ||
delay, | ||
})) | ||
|
||
useLayoutEffect(() => { | ||
const collapse = () => { | ||
if (isCollapsed.current) { | ||
return | ||
} | ||
isCollapsed.current = true | ||
api.start({ | ||
height: 0, | ||
opacity: 0, | ||
}) | ||
} | ||
const expand = () => { | ||
if (!isCollapsed.current) { | ||
return | ||
} | ||
isCollapsed.current = false | ||
api.start({ | ||
height: 50, | ||
opacity: 1, | ||
}) | ||
} | ||
// if window scroll is more than 100px; show the title | ||
const onScroll = () => { | ||
if (!ref.current) return | ||
if (window.scrollY < 100) { | ||
collapse() | ||
return | ||
} | ||
expand() | ||
} | ||
if (window.scrollY > 100) { | ||
expand() | ||
} | ||
window.addEventListener('scroll', onScroll, { passive: true }) | ||
return () => window.removeEventListener('scroll', onScroll) | ||
}, []) | ||
|
||
return ( | ||
<a.div style={style} ref={ref} className="ArticleToCTitle text-end"> | ||
<h1 className=" h5 text-dark mb-2 pe-3">{reduceTitleWithEllipsis(plainTitle)}</h1> | ||
{children} | ||
</a.div> | ||
) | ||
} | ||
|
||
export default ArticleToCTitle |