-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDynamicLayout.tsx
42 lines (33 loc) · 1.03 KB
/
DynamicLayout.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { FC, useState } from 'react'
import { useAppSelector, useObserverAncestor } from '@/hooks'
import { selectOptions } from '../global/optionsSlice'
import Timer from './Timer'
import { getRoot } from './utils'
const DynamicLayout: FC<{ beta?: boolean }> = () => {
const options = useAppSelector(selectOptions)
const [timerRoot, setTimerRoot] = useState<HTMLElement>()
const showTimer = !!options?.problemsPage.timer
useObserverAncestor(
async state => {
if (!showTimer) return
const parent = await getRoot(true)
// 创建「计时器」按钮根元素
if (!parent || !state.isMount) return
const root = document.createElement('div')
parent.style.display = 'flex'
parent.append(root)
setTimerRoot(root)
state.unmount.push(() => root && root.remove())
return root!
},
[showTimer]
)
return (
<>
{showTimer && timerRoot && (
<Timer beta={true} root={timerRoot} dynamicLayout={true} />
)}
</>
)
}
export default DynamicLayout