-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathct-tooltip.ts
More file actions
172 lines (160 loc) · 4.05 KB
/
ct-tooltip.ts
File metadata and controls
172 lines (160 loc) · 4.05 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { CtLit, css, customElement, html, property } from "./ct-lit.js";
export const tooltipStyles = css`
/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
cursor: pointer;
}
/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
opacity: 0;
pointer-events: none;
backdrop-filter: saturate(180%) blur(15px);
transition: all 200ms;
}
/* Position tooltip above the element */
[data-tooltip]:before {
font-family: var(--font-family, "Google Sans", Ubuntu, Roboto, arial, sans-serif);
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 7px;
width: 160px;
border-radius: 8px;
background-color: rgba(0, 0, 0, 0.3);
color: #fff;
content: attr(data-tooltip);
text-align: center;
font-size: 14px;
line-height: 1.2;
}
[data-toleft]:before {
margin-left: -140px;
}
[data-toright]:before {
margin-left: -24px;
}
/* Triangle */
[data-tooltip]:after {
position: absolute;
bottom: 150%;
left: 50%;
margin-left: -5px;
width: 0;
border-top: 5px solid rgba(0, 0, 0, 0.3);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
content: "";
font-size: 0;
line-height: 0;
}
[data-tobottom]:after {
bottom: -37%;
border-top: 0;
border-bottom: 5px solid rgba(0, 0, 0, 0.3);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
}
[data-tobottom]:before {
bottom: -150%;
}
/* Show tooltip content on hover */
[data-tooltip]:not([data-tooltip=""]):hover:before,
[data-tooltip]:not([data-tooltip=""]):hover:after {
visibility: visible;
opacity: 1;
z-index: 150;
}
`;
@customElement("ct-tooltip")
export class CtTooltip extends CtLit {
static styles = [
css`
:host {
background-color: var(--color-surface);
border-radius: var(--border-radius);
bottom: calc(100% + 8px);
box-shadow: 0 0 16px 0 var(--color-disable);
inline-size: max-content;
left: 50%;
max-inline-size: min(calc(100vw - 48px), 400px);
padding: 16px;
position: absolute;
text-align: initial;
top: auto;
writing-mode: horizontal-tb;
z-index: 550;
font-size: 13px;
outline: none;
}
:host(:not([open])) {
opacity: 0;
transform: translateY(8px) translateX(calc(-50% + 0px));
pointer-events: none;
visibility: hidden;
transition:
opacity 0.5s cubic-bezier(0.5, 0, 0, 0.75),
transform 0.5s cubic-bezier(0.5, 0, 0, 0.75),
visibility 0s 0.5s;
}
:host([open]) {
transform: translateY(0px) translateX(calc(-50% + 0px));
opacity: 1;
pointer-events: inherit;
visibility: inherit;
transition:
opacity 0.5s cubic-bezier(0.5, 0, 0, 0.75),
transform 0.5s cubic-bezier(0.5, 0, 0, 0.75),
visibility 0s 0s;
}
`
];
@property({ type: String }) placement = "top";
@property({ type: String }) for = "";
@property({ type: Boolean, reflect: true }) open = false;
public context!: HTMLElement | ShadowRoot;
render() {
return html`<slot></slot>`;
}
connectedCallback(): void {
super.connectedCallback();
if (!this.for) return;
if (this.parentElement) {
this.parentElement.style.position = "relative";
}
let cnx = this.context || this.parentElement;
let el = cnx.querySelector(this.for) as HTMLElement;
if (el) {
el.tabIndex = -1;
el.addEventListener("mouseenter", () => {
this.open = true;
});
el.addEventListener("mouseleave", () => {
// if focus is on the tooltip, don't close it
let isTooltipFocused = document.activeElement === el || (cnx as ShadowRoot).activeElement === el;
if (isTooltipFocused) return;
this.close();
});
// if context active element changes, close the tooltip
el.addEventListener("blur", () => {
this.close();
});
}
}
close() {
let cnx = this.context || this.parentElement;
this.open = false;
this.blur();
let el = cnx.querySelector(this.for) as HTMLElement;
if (el) el.blur();
}
}
declare global {
interface HTMLElementTagNameMap {
"ct-tooltip": CtTooltip;
}
}