-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
8 changed files
with
136 additions
and
108 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useEffect, useState } from 'react' | ||
import { createContextState } from 'foxact/create-context-state' | ||
|
||
import { clsxm } from '~/utils/helper' | ||
|
||
const [ | ||
ArticleElementContextProviderInternal, | ||
useArticleElement, | ||
useSetArticleElement, | ||
] = createContextState<HTMLDivElement | null>(null) | ||
|
||
const ArticleElementContextProvider: Component = ({ children, className }) => { | ||
return ( | ||
<ArticleElementContextProviderInternal> | ||
<Content className={className}>{children}</Content> | ||
</ArticleElementContextProviderInternal> | ||
) | ||
} | ||
|
||
const Content: Component = ({ children, className }) => { | ||
const [contentRef, setContentRef] = useState<HTMLDivElement | null>(null) | ||
const setter = useSetArticleElement() | ||
useEffect(() => { | ||
setter(contentRef) | ||
console.log(contentRef) | ||
}, [contentRef, setter]) | ||
return ( | ||
<div className={clsxm('relative', className)} ref={setContentRef}> | ||
{children} | ||
</div> | ||
) | ||
} | ||
|
||
export { | ||
ArticleElementContextProvider, | ||
useSetArticleElement, | ||
useArticleElement, | ||
} |
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,30 +1,32 @@ | ||
'use client' | ||
|
||
import React, { useEffect } from 'react' | ||
import { createPortal } from 'react-dom' | ||
import { atom, useAtomValue, useSetAtom } from 'jotai' | ||
import type { FC } from 'react' | ||
|
||
const rightSideAtom = atom<FC | JSX.Element | null>(null) | ||
import { useIsClient } from '~/hooks/common/use-is-client' | ||
|
||
const useSetNoteLayoutRightSideElement = () => useSetAtom(rightSideAtom) | ||
const rightSideElementAtom = atom<null | HTMLDivElement>(null) | ||
export const NoteLayoutRightSideProvider: Component = ({ className }) => { | ||
const setElement = useSetAtom(rightSideElementAtom) | ||
|
||
export const NoteLayoutRightSideProvider = () => { | ||
const ReactNodeOrComponent = useAtomValue(rightSideAtom) | ||
|
||
if (!ReactNodeOrComponent) return null | ||
useEffect(() => { | ||
return () => { | ||
// GC | ||
setElement(null) | ||
} | ||
}, []) | ||
|
||
if (React.isValidElement(ReactNodeOrComponent)) return ReactNodeOrComponent | ||
else if (typeof ReactNodeOrComponent === 'function') | ||
return <ReactNodeOrComponent /> | ||
else return null | ||
return <div ref={setElement} className={className} /> | ||
} | ||
|
||
export const NoteLayoutRightSidePortal: Component = ({ children }) => { | ||
const setter = useSetNoteLayoutRightSideElement() | ||
const rightSideElement = useAtomValue(rightSideElementAtom) | ||
|
||
useEffect(() => { | ||
setter(<>{children}</>) | ||
}, [children]) | ||
const isClient = useIsClient() | ||
if (!isClient) return null | ||
|
||
if (!rightSideElement) return null | ||
|
||
return null | ||
return createPortal(children, rightSideElement) | ||
} |
Oops, something went wrong.
c4f80a3
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-git-main-innei.vercel.app
springtide.vercel.app
springtide-innei.vercel.app