Repository navigation
Expand file tree
/
Copy pathshadow-dom.tsx
More file actions
51 lines (41 loc) · 1.43 KB
/
Copy pathshadow-dom.tsx
File metadata and controls
51 lines (41 loc) · 1.43 KB
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
43
44
45
46
47
48
49
50
51
import React, { useMemo, useRef, useEffect } from 'react'
import { createRoot } from 'react-dom/client'
import { createEditor, Descendant } from 'slate'
import { Slate, Editable, withReact } from 'slate-react'
import { withHistory } from 'slate-history'
const ShadowDOM = () => {
const container = useRef<HTMLDivElement>(null)
useEffect(() => {
if (container.current!.shadowRoot) return
// Create a shadow DOM
const outerShadowRoot = container.current!.attachShadow({ mode: 'open' })
const host = document.createElement('div')
outerShadowRoot.appendChild(host)
// Create a nested shadow DOM
const innerShadowRoot = host.attachShadow({ mode: 'open' })
const reactRoot = document.createElement('div')
innerShadowRoot.appendChild(reactRoot)
// Render the editor within the nested shadow DOM
const root = createRoot(reactRoot)
root.render(<ShadowEditor />)
})
return <div ref={container} data-cy="outer-shadow-root" />
}
const ShadowEditor = () => {
const editor = useMemo(() => withHistory(withReact(createEditor())), [])
return (
<Slate editor={editor} initialValue={initialValue}>
<Editable
aria-label="Editor in shadow DOM"
placeholder="Enter some plain text..."
/>
</Slate>
)
}
const initialValue: Descendant[] = [
{
type: 'paragraph',
children: [{ text: 'This Editor is rendered within a nested Shadow DOM.' }],
},
]
export default ShadowDOM