forked from atomiks/tippyjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.ts
152 lines (127 loc) · 3.84 KB
/
template.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
ARROW_CLASS,
BACKDROP_CLASS,
BOX_CLASS,
CONTENT_CLASS,
SVG_ARROW_CLASS,
} from './constants';
import {div, isElement} from './dom-utils';
import {Instance, PopperElement, Props} from './types';
import {PopperChildren} from './types-internal';
import {arrayFrom} from './utils';
// Firefox extensions don't allow .innerHTML = "..." property. This tricks it.
const innerHTML = (): 'innerHTML' => 'innerHTML';
function dangerouslySetInnerHTML(element: Element, html: string): void {
element[innerHTML()] = html;
}
function createArrowElement(value: Props['arrow']): HTMLDivElement {
const arrow = div();
if (value === true) {
arrow.className = ARROW_CLASS;
} else {
arrow.className = SVG_ARROW_CLASS;
if (isElement(value)) {
arrow.appendChild(value);
} else {
dangerouslySetInnerHTML(arrow, value as string);
}
}
return arrow;
}
export function setContent(content: HTMLDivElement, props: Props): void {
if (isElement(props.content)) {
dangerouslySetInnerHTML(content, '');
content.appendChild(props.content);
} else if (typeof props.content !== 'function') {
if (props.allowHTML) {
dangerouslySetInnerHTML(content, props.content);
} else {
content.textContent = props.content;
}
}
}
export function getChildren(popper: PopperElement): PopperChildren {
const box = popper.firstElementChild as HTMLDivElement;
const boxChildren = arrayFrom(box.children);
return {
box,
content: boxChildren.find((node) => node.classList.contains(CONTENT_CLASS)),
arrow: boxChildren.find(
(node) =>
node.classList.contains(ARROW_CLASS) ||
node.classList.contains(SVG_ARROW_CLASS)
),
backdrop: boxChildren.find((node) =>
node.classList.contains(BACKDROP_CLASS)
),
};
}
export function render(
instance: Instance
): {
popper: PopperElement;
onUpdate?: (prevProps: Props, nextProps: Props) => void;
} {
const popper = div();
const box = div();
box.className = BOX_CLASS;
box.setAttribute('data-state', 'hidden');
box.setAttribute('tabindex', '-1');
const content = div();
content.className = CONTENT_CLASS;
content.setAttribute('data-state', 'hidden');
setContent(content, instance.props);
popper.appendChild(box);
box.appendChild(content);
onUpdate(instance.props, instance.props);
function onUpdate(prevProps: Props, nextProps: Props): void {
const {box, content, arrow} = getChildren(popper);
if (nextProps.theme) {
box.setAttribute('data-theme', nextProps.theme);
} else {
box.removeAttribute('data-theme');
}
if (typeof nextProps.animation === 'string') {
box.setAttribute('data-animation', nextProps.animation);
} else {
box.removeAttribute('data-animation');
}
if (nextProps.inertia) {
box.setAttribute('data-inertia', '');
} else {
box.removeAttribute('data-inertia');
}
box.style.maxWidth =
typeof nextProps.maxWidth === 'number'
? `${nextProps.maxWidth}px`
: nextProps.maxWidth;
if (nextProps.role) {
box.setAttribute('role', nextProps.role);
} else {
box.removeAttribute('role');
}
if (
prevProps.content !== nextProps.content ||
prevProps.allowHTML !== nextProps.allowHTML
) {
setContent(content, instance.props);
}
if (nextProps.arrow) {
if (!arrow) {
box.appendChild(createArrowElement(nextProps.arrow));
} else if (prevProps.arrow !== nextProps.arrow) {
box.removeChild(arrow);
box.appendChild(createArrowElement(nextProps.arrow));
}
} else if (arrow) {
box.removeChild(arrow!);
}
}
return {
popper,
onUpdate,
};
}
// Runtime check to identify if the render function is the default one; this
// way we can apply default CSS transitions logic and it can be tree-shaken away
render.$$tippy = true;