diff --git a/components/Tabs.js b/components/Tabs.js index 6f08aada..eb1e2e69 100644 --- a/components/Tabs.js +++ b/components/Tabs.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react' +import { useState } from 'react'; /** * Tabs切换标签 @@ -6,59 +6,37 @@ import React, { useState } from 'react' * @returns */ const Tabs = ({ className, children }) => { - const [currentTab, setCurrentTab] = useState(0) + const [currentTab, setCurrentTab] = useState(0); - if (!children) { - return <> + const validChildren = children.filter(c => c); + + if (validChildren.length === 0) { + return <>; } - children = children.filter(c => c && c !== '') - - let count = 0 - children.forEach(e => { - if (e) { - count++ - } - }) - - if (count === 0) { - return <> - } - - if (count === 1) { - return
- {children} -
- } - - function tabClickHandle(i) { - setCurrentTab(i) - } - - return
- -
- {children.map((item, index) => { - return
- {currentTab === index && item} -
- })} -
+ return ( +
+ + {/* 标签切换的时候不销毁 DOM 元素,使用 CSS 样式进行隐藏 */} +
+ {validChildren.map((item, index) => ( +
+ {item} +
+ ))} +
-} + ); +}; -export default Tabs +export default Tabs; diff --git a/components/Utterances.js b/components/Utterances.js index 3524c646..34c4f09c 100644 --- a/components/Utterances.js +++ b/components/Utterances.js @@ -1,5 +1,6 @@ +import { useEffect, useState } from 'react' import { siteConfig } from '@/lib/config' -import { useEffect } from 'react' +import { useGlobal } from '@/lib/global' /** * 评论插件 @@ -9,28 +10,48 @@ import { useEffect } from 'react' * @constructor */ const Utterances = ({ issueTerm, layout }) => { + const { isDarkMode } = useGlobal() + + const [isLoading, setLoading] = useState(true); + useEffect(() => { - const theme = - siteConfig('APPEARANCE') === 'auto' - ? 'preferred-color-scheme' - : siteConfig('APPEARANCE') === 'light' - ? 'github-light' - : 'github-dark' - const script = document.createElement('script') - const anchor = document.getElementById('comments') - script.setAttribute('src', 'https://utteranc.es/client.js') - script.setAttribute('crossorigin', 'anonymous') - script.setAttribute('async', true) - script.setAttribute('repo', siteConfig('COMMENT_UTTERRANCES_REPO')) - script.setAttribute('issue-term', 'title') - script.setAttribute('theme', theme) - anchor.appendChild(script) + const script = document.createElement('script'); + const anchor = document.getElementById('comments'); + script.onload = () => setLoading(false); + script.setAttribute('src', 'https://utteranc.es/client.js'); + script.setAttribute('crossorigin', 'anonymous'); + script.setAttribute('async', true); + script.setAttribute('repo', siteConfig('COMMENT_UTTERRANCES_REPO')); + script.setAttribute('issue-term', 'title'); + // 初始主题 + script.setAttribute('theme', isDarkMode ? 'github-dark' : 'github-light'); + anchor.appendChild(script); + return () => { - anchor.innerHTML = '' + // anchor.innerHTML = '' + }; + }, []); + + useEffect(() => { + // 直接设置 iframe 的类来改变主题,不重新加载脚本 + const iframe = document.querySelector('iframe.utterances-frame'); + if (iframe) { + iframe.contentWindow.postMessage({ + type: 'set-theme', + theme: isDarkMode ? 'github-dark' : 'github-light' + }, 'https://utteranc.es'); } - }) - return
-
+ }, [isDarkMode]); + + return ( +
+ {isLoading && ( +
+
+
+ )} +
+ ); } export default Utterances diff --git a/styles/globals.css b/styles/globals.css index 67644891..65057e3e 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -160,6 +160,11 @@ nav { @apply text-blue-700 } +/* twikoo 内置的 element-ui 加载样式 */ +.el-loading-spinner { + @apply flex justify-center items-center; +} + /* Webmention style */ .webmention-block { background: rgba(0, 116, 222, .2);