Disable Loading Comments
indicator or style it?
#152
-
I got Giscus working smoothly. It uses a hook & a component to load comments. useGiscus.tsimport React from 'react'
import { useTheme } from 'next-themes'
import { siteMetadata } from '@/_data/index'
import type { IGiscusProps } from '@/types/giscus'
interface IGiscus {
mapping: IGiscusProps['mapping']
commentNodeId: string
}
const config = siteMetadata.comment.giscusConfig
const THEME_MAPPING = {
light: config.theme,
dark: config.darkTheme,
}
// username/repo format
const REPO_NAME = config.repo as string
const REPO_ID = config.repositoryId as string
const CATEGORY = config.category as string
const CATEGORY_ID = config.categoryId as string
const REACTIONS = config.reactions
const METADATA = config.metadata
export const useGiscus = ({ mapping, commentNodeId }: IGiscus) => {
const { resolvedTheme = 'light' } = useTheme()
const theme = React.useRef(resolvedTheme)
React.useEffect(() => {
const scriptParentNode = document.getElementById(commentNodeId)
if (!scriptParentNode) return
const script = document.createElement('script')
const attributes = {
src: 'https://giscus.app/client.js',
'data-repo': REPO_NAME,
'data-repo-id': REPO_ID,
'data-category': CATEGORY,
'data-category-id': CATEGORY_ID,
'data-mapping': mapping,
'data-reactions-enabled': REACTIONS,
'data-emit-metadata': METADATA,
'data-theme': THEME_MAPPING[theme.current as keyof typeof THEME_MAPPING],
crossorigin: 'anonymous',
async: '',
}
Object.entries(attributes).forEach(([name, value]) => script.setAttribute(name, value))
scriptParentNode.appendChild(script)
}, [commentNodeId, mapping])
React.useEffect(() => {
const iframe = document.querySelector<HTMLIFrameElement>('iframe.giscus-frame')
iframe?.contentWindow?.postMessage(
{
giscus: {
setConfig: {
theme: THEME_MAPPING[resolvedTheme as keyof typeof THEME_MAPPING],
},
},
},
'https://giscus.app'
)
}, [resolvedTheme])
} Giscus.tsximport React from 'react'
import { useInView } from 'react-intersection-observer'
import { useGiscus } from '@/hooks/useGiscus'
import type { IGiscusProps } from '@/types/giscus'
interface IGiscus {
mapping: IGiscusProps['mapping']
}
export const Giscus = ({ mapping }: IGiscus) => {
const COMMENTS_NODE_ID = 'comments'
const { ref, inView } = useInView({ threshold: 0, triggerOnce: true })
useGiscus({ mapping, commentNodeId: inView ? COMMENTS_NODE_ID : '' })
return (
<div ref={ref} className="min-h-[400px]">
{inView ? <div id={COMMENTS_NODE_ID} /> : null}
</div>
)
} However, I want a way to style As you can see in my code, there is no The color is first black or gray & then changes to white as I'm using dark mode but I would really like a way to style it so it shows in the center with a little padding/margin or completely remove it just like it is in Utterances. How do I do it? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You can style it by using a custom theme and styling the |
Beta Was this translation helpful? Give feedback.
-
@deadcoder0904 I tried to prevent the theme flashing on initial load in #155, which seems to have fixed this. |
Beta Was this translation helpful? Give feedback.
@deadcoder0904 I tried to prevent the theme flashing on initial load in #155, which seems to have fixed this.