blob: 74cc9f7e7c51fba4423b496b3979224c62cf3297 [file] [log] [blame]
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04001/**
2 * @license
Ben Rohlfs94fcbbc2022-05-27 10:45:03 +02003 * Copyright 2016 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04005 */
Paladox none421c5f62021-08-07 22:42:35 +00006import {sharedStyles} from '../../../styles/shared-styles';
7import {LitElement, css, html} from 'lit';
Frank Borden42c1a452022-08-11 16:27:20 +02008import {customElement, property} from 'lit/decorators.js';
9import {styleMap} from 'lit/directives/style-map.js';
Becky Siegelc9e4a342017-03-10 16:37:17 -080010
Dhruv Srivastava0c8f7692020-07-30 14:03:34 +020011declare global {
12 interface HTMLElementTagNameMap {
13 'gr-tooltip': GrTooltip;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010014 }
15}
16
Dhruv Srivastava0c8f7692020-07-30 14:03:34 +020017@customElement('gr-tooltip')
Paladox none421c5f62021-08-07 22:42:35 +000018export class GrTooltip extends LitElement {
Dhruv Srivastava0c8f7692020-07-30 14:03:34 +020019 @property({type: String})
20 text = '';
21
22 @property({type: String})
23 maxWidth = '';
24
Paladox none421c5f62021-08-07 22:42:35 +000025 @property({type: String})
26 arrowCenterOffset = '0';
27
28 @property({type: Boolean, reflect: true, attribute: 'position-below'})
Dhruv Srivastava0c8f7692020-07-30 14:03:34 +020029 positionBelow = false;
30
Paladox none421c5f62021-08-07 22:42:35 +000031 static override get styles() {
32 return [
33 sharedStyles,
34 css`
35 :host {
36 --gr-tooltip-arrow-size: 0.5em;
37
38 background-color: var(--tooltip-background-color);
39 box-shadow: var(--elevation-level-2);
40 color: var(--tooltip-text-color);
41 font-size: var(--font-size-small);
42 position: absolute;
43 z-index: 1000;
44 }
45 :host .tooltip {
46 padding: var(--spacing-m) var(--spacing-l);
47 }
48 :host .arrowPositionBelow,
49 :host([position-below]) .arrowPositionAbove {
50 display: none;
51 }
52 :host([position-below]) .arrowPositionBelow {
53 display: initial;
54 }
55 .arrow {
56 border-left: var(--gr-tooltip-arrow-size) solid transparent;
57 border-right: var(--gr-tooltip-arrow-size) solid transparent;
58 height: 0;
59 position: absolute;
60 left: calc(50% - var(--gr-tooltip-arrow-size));
61 width: 0;
62 }
63 .arrowPositionAbove {
64 border-top: var(--gr-tooltip-arrow-size) solid
65 var(--tooltip-background-color);
66 bottom: calc(-1 * var(--gr-tooltip-arrow-size));
67 }
68 .arrowPositionBelow {
69 border-bottom: var(--gr-tooltip-arrow-size) solid
70 var(--tooltip-background-color);
71 top: calc(-1 * var(--gr-tooltip-arrow-size));
72 }
Kamil Musin3869c202023-08-24 14:20:57 +020073 .text {
Kamil Musind1f241f2023-08-28 14:14:18 +020074 white-space: pre-wrap;
Kamil Musin3869c202023-08-24 14:20:57 +020075 }
Paladox none421c5f62021-08-07 22:42:35 +000076 `,
77 ];
78 }
79
80 override render() {
81 this.style.maxWidth = this.maxWidth;
82
Gerrit Code Review222cda72024-02-01 19:05:08 +000083 return html` <div class="tooltip" aria-live="polite" role="tooltip">
Paladox none421c5f62021-08-07 22:42:35 +000084 <i
85 class="arrowPositionBelow arrow"
Ben Rohlfs8003bd32022-04-05 18:24:42 +020086 style=${styleMap({marginLeft: this.arrowCenterOffset})}
Paladox none421c5f62021-08-07 22:42:35 +000087 ></i>
Kamil Musin3869c202023-08-24 14:20:57 +020088 <div class="text">${this.text}</div>
Paladox none421c5f62021-08-07 22:42:35 +000089 <i
90 class="arrowPositionAbove arrow"
Ben Rohlfs8003bd32022-04-05 18:24:42 +020091 style=${styleMap({marginLeft: this.arrowCenterOffset})}
Paladox none421c5f62021-08-07 22:42:35 +000092 ></i>
93 </div>`;
Dhruv Srivastava0c8f7692020-07-30 14:03:34 +020094 }
95}