-
-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <tukon479@gmail.com>
- Loading branch information
Showing
6 changed files
with
68 additions
and
40 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
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 |
---|---|---|
@@ -1,37 +1,70 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useEffect, useRef } from 'react' | ||
import { createContextState } from 'foxact/create-context-state' | ||
|
||
import { clsxm } from '~/utils/helper' | ||
|
||
const [ | ||
ArticleElementContextProviderInternal, | ||
ArticleElementProviderInternal, | ||
useArticleElement, | ||
useSetArticleElement, | ||
] = createContextState<HTMLDivElement | null>(undefined as any) | ||
|
||
const ArticleElementContextProvider: Component = ({ children, className }) => { | ||
const [ | ||
IsEOArticleElementProviderInternal, | ||
useIsEOArticleElement, | ||
useSetIsEOArticleElement, | ||
] = createContextState<boolean>(false) | ||
|
||
const ArticleElementProvider: Component = ({ children, className }) => { | ||
return ( | ||
<ArticleElementContextProviderInternal> | ||
<Content className={className}>{children}</Content> | ||
</ArticleElementContextProviderInternal> | ||
<ArticleElementProviderInternal> | ||
<IsEOArticleElementProviderInternal> | ||
<Content className={className}>{children}</Content> | ||
</IsEOArticleElementProviderInternal> | ||
</ArticleElementProviderInternal> | ||
) | ||
} | ||
|
||
const Content: Component = ({ children, className }) => { | ||
const [contentRef, setContentRef] = useState<HTMLDivElement | null>(null) | ||
const setter = useSetArticleElement() | ||
useEffect(() => { | ||
setter(contentRef) | ||
}, [contentRef, setter]) | ||
|
||
return ( | ||
<div className={clsxm('relative', className)} ref={setContentRef}> | ||
<div className={clsxm('relative', className)} ref={setter}> | ||
{children} | ||
<EOADetector /> | ||
</div> | ||
) | ||
} | ||
|
||
const EOADetector: Component = () => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
const setter = useSetIsEOArticleElement() | ||
useEffect(() => { | ||
if (!ref.current) return | ||
const $el = ref.current | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
const entry = entries[0] | ||
setter(entry.isIntersecting) | ||
}, | ||
{ | ||
rootMargin: '0px 0px 10% 0px', | ||
}, | ||
) | ||
|
||
observer.observe($el) | ||
return () => { | ||
observer.unobserve($el) | ||
observer.disconnect() | ||
} | ||
}, []) | ||
|
||
return <div ref={ref} /> | ||
} | ||
|
||
export { | ||
ArticleElementContextProvider, | ||
ArticleElementProvider, | ||
useSetArticleElement, | ||
useArticleElement, | ||
useIsEOArticleElement, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8c9b062
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
springtide – ./
springtide.vercel.app
springtide-git-main-innei.vercel.app
springtide-innei.vercel.app