-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtippy.ts
39 lines (31 loc) · 1.02 KB
/
tippy.ts
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
import tippy, {Props} from 'tippy.js'
import shadowCss from 'bundle-text:./shadow.css'
import {lazy} from './common/lazy'
export const getShadowRoot = lazy(initShadowRoot)
export const createTippy = (
link: HTMLAnchorElement | HTMLAreaElement,
previewElement: HTMLElement,
options: Partial<Props> = {},
) => tippy(link, {
content: previewElement,
placement: 'bottom',
arrow: true,
// in shadow dom to avoid affecting the page styles
appendTo: () => getShadowRoot() as unknown as Element,
animation: 'fade',
interactive: true,
theme: 'light',
maxWidth: '55em',
delay: [0, 400],
...options,
})
function initShadowRoot() {
const shadowContainer = document.createElement('div')
shadowContainer.className = 'transclude-shadow-container'
const shadowRoot = shadowContainer.attachShadow({mode: 'open'})
const style = document.createElement('style')
style.innerText = shadowCss
shadowRoot.append(style)
document.body.append(shadowContainer)
return shadowRoot
}