Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
Ben Rohlfs | 94fcbbc | 2022-05-27 10:45:03 +0200 | [diff] [blame] | 3 | * Copyright 2016 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 5 | */ |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 6 | import {sharedStyles} from '../../../styles/shared-styles'; |
| 7 | import {LitElement, css, html} from 'lit'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 8 | import {customElement, property} from 'lit/decorators.js'; |
| 9 | import {styleMap} from 'lit/directives/style-map.js'; |
Becky Siegel | c9e4a34 | 2017-03-10 16:37:17 -0800 | [diff] [blame] | 10 | |
Dhruv Srivastava | 0c8f769 | 2020-07-30 14:03:34 +0200 | [diff] [blame] | 11 | declare global { |
| 12 | interface HTMLElementTagNameMap { |
| 13 | 'gr-tooltip': GrTooltip; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 14 | } |
| 15 | } |
| 16 | |
Dhruv Srivastava | 0c8f769 | 2020-07-30 14:03:34 +0200 | [diff] [blame] | 17 | @customElement('gr-tooltip') |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 18 | export class GrTooltip extends LitElement { |
Dhruv Srivastava | 0c8f769 | 2020-07-30 14:03:34 +0200 | [diff] [blame] | 19 | @property({type: String}) |
| 20 | text = ''; |
| 21 | |
| 22 | @property({type: String}) |
| 23 | maxWidth = ''; |
| 24 | |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 25 | @property({type: String}) |
| 26 | arrowCenterOffset = '0'; |
| 27 | |
| 28 | @property({type: Boolean, reflect: true, attribute: 'position-below'}) |
Dhruv Srivastava | 0c8f769 | 2020-07-30 14:03:34 +0200 | [diff] [blame] | 29 | positionBelow = false; |
| 30 | |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 31 | 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 Musin | 3869c20 | 2023-08-24 14:20:57 +0200 | [diff] [blame] | 73 | .text { |
Kamil Musin | d1f241f | 2023-08-28 14:14:18 +0200 | [diff] [blame] | 74 | white-space: pre-wrap; |
Kamil Musin | 3869c20 | 2023-08-24 14:20:57 +0200 | [diff] [blame] | 75 | } |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 76 | `, |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | override render() { |
| 81 | this.style.maxWidth = this.maxWidth; |
| 82 | |
Gerrit Code Review | 222cda7 | 2024-02-01 19:05:08 +0000 | [diff] [blame] | 83 | return html` <div class="tooltip" aria-live="polite" role="tooltip"> |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 84 | <i |
| 85 | class="arrowPositionBelow arrow" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 86 | style=${styleMap({marginLeft: this.arrowCenterOffset})} |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 87 | ></i> |
Kamil Musin | 3869c20 | 2023-08-24 14:20:57 +0200 | [diff] [blame] | 88 | <div class="text">${this.text}</div> |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 89 | <i |
| 90 | class="arrowPositionAbove arrow" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 91 | style=${styleMap({marginLeft: this.arrowCenterOffset})} |
Paladox none | 421c5f6 | 2021-08-07 22:42:35 +0000 | [diff] [blame] | 92 | ></i> |
| 93 | </div>`; |
Dhruv Srivastava | 0c8f769 | 2020-07-30 14:03:34 +0200 | [diff] [blame] | 94 | } |
| 95 | } |