Skip to content

Commit e999ad0

Browse files
authored
Merge pull request #431 from danice/tooltip-content
Tooltip content
2 parents aee5d8e + 876a2e4 commit e999ad0

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

pug/contents/tooltips_content.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
<p>Tooltips are small, interactive, textual hints for mainly graphical elements. When using icons for actions you can
88
use a tooltip to give people clarification on its function.</p>
99
<div class="row">
10-
<a href="#!" class="btn tooltipped col s4 offset-s4 l2 offset-l1" data-html="true" data-position="bottom" data-tooltip="I am a tooltip">
11-
Bottom</a>
10+
<a href="#!" class="btn tooltipped col s4 offset-s4 l2 offset-l1" data-html="true" data-position="bottom" data-tooltip-id="tooltip-content"> Bottom</a>
11+
<div id="tooltip-content" style="display: none;">
12+
This is a tooltip with a <a href="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/materializecss">link</a> and a <i class="material-icons icon-demo">insert_chart</i>
13+
</div>
1214
<a href="#!" class="btn tooltipped col s4 offset-s4 l2 offset-l1" data-position="top" data-tooltip="I am a tooltip"> Top</a>
1315
<a href="#!" class="btn tooltipped col s4 offset-s4 l2 offset-l1" data-position="left" data-tooltip="I am a tooltip"> Left</a>
1416
<a href="#!" class="btn tooltipped col s4 offset-s4 l2 offset-l1" data-position="right" data-tooltip="I am a tooltip"> Right</a>

src/tooltip.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Utils } from "./utils";
44
import { Bounding } from "./bounding";
55
import { Component, BaseOptions, InitElements, MElement } from "./component";
66

7+
export type TooltipPosition = 'top' | 'right' | 'bottom' | 'left';
8+
79
export interface TooltipOptions extends BaseOptions {
810
/**
911
* Delay time before tooltip disappears.
@@ -15,6 +17,11 @@ export interface TooltipOptions extends BaseOptions {
1517
* @default 0
1618
*/
1719
enterDelay: number;
20+
/**
21+
* Element Id for the tooltip.
22+
* @default ""
23+
*/
24+
tooltipId?: string;
1825
/**
1926
* Text string for the tooltip.
2027
* @default ""
@@ -45,7 +52,7 @@ export interface TooltipOptions extends BaseOptions {
4552
* Set the direction of the tooltip.
4653
* @default 'bottom'
4754
*/
48-
position: 'top' | 'right' | 'bottom' | 'left';
55+
position: TooltipPosition;
4956
/**
5057
* Amount in px that the tooltip moves during its transition.
5158
* @default 10
@@ -60,7 +67,7 @@ const _defaults: TooltipOptions = {
6067
margin: 5,
6168
inDuration: 250,
6269
outDuration: 200,
63-
position: 'bottom',
70+
position: 'bottom' as TooltipPosition,
6471
transitionMovement: 10,
6572
opacity: 1
6673
};
@@ -90,6 +97,7 @@ export class Tooltip extends Component<TooltipOptions> {
9097

9198
this.options = {
9299
...Tooltip.defaults,
100+
...this._getAttributeOptions(),
93101
...options
94102
};
95103

@@ -139,15 +147,22 @@ export class Tooltip extends Component<TooltipOptions> {
139147
this.tooltipEl = document.createElement('div');
140148
this.tooltipEl.classList.add('material-tooltip');
141149

142-
const tooltipContentEl = document.createElement('div');
150+
const tooltipContentEl = this.options.tooltipId
151+
? document.getElementById(this.options.tooltipId)
152+
: document.createElement('div');
153+
this.tooltipEl.append( tooltipContentEl);
154+
tooltipContentEl.style.display = "";
155+
143156
tooltipContentEl.classList.add('tooltip-content');
144157
this._setTooltipContent(tooltipContentEl);
145158
this.tooltipEl.appendChild(tooltipContentEl);
146159
document.body.appendChild(this.tooltipEl);
147160
}
148161

149162
_setTooltipContent(tooltipContentEl: HTMLElement) {
150-
tooltipContentEl.innerText = this.options.text;
163+
if (this.options.tooltipId)
164+
return;
165+
tooltipContentEl.innerText = this.options.text;
151166
}
152167

153168
_updateTooltipContent() {
@@ -331,16 +346,21 @@ export class Tooltip extends Component<TooltipOptions> {
331346
this.close();
332347
}
333348

334-
_getAttributeOptions() {
335-
const attributeOptions = {};
349+
_getAttributeOptions(): Partial<TooltipOptions> {
350+
let attributeOptions: Partial<TooltipOptions> = { };
336351
const tooltipTextOption = this.el.getAttribute('data-tooltip');
352+
const tooltipId = this.el.getAttribute('data-tooltip-id');
337353
const positionOption = this.el.getAttribute('data-position');
338354
if (tooltipTextOption) {
339-
(attributeOptions as any).text = tooltipTextOption;
355+
attributeOptions.text = tooltipTextOption;
340356
}
341357
if (positionOption) {
342-
(attributeOptions as any).position = positionOption;
358+
attributeOptions.position = positionOption as TooltipPosition;
359+
}
360+
if (tooltipId) {
361+
attributeOptions.tooltipId = tooltipId;
343362
}
363+
344364
return attributeOptions;
345365
}
346366
}

0 commit comments

Comments
 (0)