Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tooltip position and allow tooltip mouseover | Fixes #995 #996

Merged
merged 6 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix tooltip position and allow tooltip mouseover | Fixes #995
  • Loading branch information
Carlos Lancha committed Jun 12, 2018
commit b0ca8c74f5a2f5c26dec3aff0e50045cf7d9b24c
165 changes: 123 additions & 42 deletions packages/clay-tooltip/src/ClayTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ class ClayTooltip extends Component {
this._eventHandler = new EventHandler();
}

attached() {
this.addListener('transitionend', this._handleTransitionEnd, true);
}

rendered() {
if (this._target) {
const alignedPosition = Align.align(this.element, this._target, this.position);

if (this.alignedPosition !== alignedPosition) {
this.alignedPosition = alignedPosition;
}
}
}

/**
* @inheritDoc
*/
Expand All @@ -73,70 +87,125 @@ class ClayTooltip extends Component {
}

/**
* Handles mouseenter events.
* Handles mouseenter event.
* @memberof ClayTooltip
* @param {Object} event The event object.
* @protected
* @param {!Element} event
* @return {!String}
* @private
*/
_handleMouseEnter(event) {
const target = event.delegateTarget;

const titleAttribute = target.getAttribute('title');
_getContent(element) {
const titleAttribute = element.getAttribute('title');

if (titleAttribute) {
target.setAttribute('data-title', titleAttribute);
target.setAttribute('data-restore-title', 'true');
target.removeAttribute('title');
} else if (target.tagName === 'svg') {
let titleTag = target.querySelector('title');
element.setAttribute('data-title', titleAttribute);
element.setAttribute('data-restore-title', 'true');
element.removeAttribute('title');
} else if (element.tagName === 'svg') {
let titleTag = element.querySelector('title');

if (titleTag) {
target.setAttribute('data-title', titleTag.innerHTML);
target.setAttribute('data-restore-title', 'true');
element.setAttribute('data-title', titleTag.innerHTML);
element.setAttribute('data-restore-title', 'true');
titleTag.remove();
}
}

this._content = target.getAttribute('data-title');
return element.getAttribute('data-title');
}

/**
* Handles click event.
* @memberof ClayTooltip
* @param {!Event} event
* @private
*/
_handleMouseClick(event) {
this._restoreTitle(event.delegateTarget);

this.alignedPosition = Align.align(this.element, target, this.position);
this._showTooltip = true;
this._isTransitioning = true;
this.visible = false;
}

/**
* Handles mouseenter event.
* @memberof ClayTooltip
* @param {!Event} event
* @private
*/
_handleMouseEnter(event) {
const content = this._getContent(event.delegateTarget);
this._target = event.delegateTarget;

this._content = content;

this._isTransitioning = true;
this.visible = true;
}

/**
* Handles tooltip element mouseenter event.
* @memberof ClayTooltip
* @param {!Event} event
* @private
*/
_handleMouseEnterTooltip(event) {
if (this._isTransitioning) {
this.visible = true;
}
}

/**
* Handles mouseleave events.
* @memberof ClayTooltip
* @param {Object} event The event object.
* @protected
* @param {!Event} event
* @private
*/
_handleMouseLeave(event) {
const target = event.delegateTarget;
this._restoreTitle(event.delegateTarget);

const title = target.getAttribute('data-title');
const restoreTitle = target.getAttribute('data-restore-title');
this._isTransitioning = true;
this.visible = false;
}

/**
* Handles transionend event.
* @memberof ClayTooltip
* @private
*/
_handleTransitionEnd() {
this._isTransitioning = false;
}

/**
* Restores the title attribute to an element
* @memberof ClayTooltip
* @param {Element} element
* @private
*/
_restoreTitle(element) {
const title = element.getAttribute('data-title');
const restoreTitle = element.getAttribute('data-restore-title');

if (title && restoreTitle === 'true') {
if (target.tagName === 'svg') {
if (element.tagName === 'svg') {
let titleTag = document.createElement('title');
titleTag.innerHTML = title;

target.appendChild(titleTag);
element.appendChild(titleTag);
} else {
target.setAttribute('title', title);
element.setAttribute('title', title);
}

target.removeAttribute('data-restore-title');
element.removeAttribute('data-restore-title');
}

this._showTooltip = false;
}

/**
* The setter function for the `classMap` staet.
* @memberof ClayTooltip
* @param {Object} val
* @return {!Object}
* @protected
* @private
*/
setterClassMapFn_(val) {
return object.mixin(this.valueClassMapFn_(), val);
Expand All @@ -147,7 +216,7 @@ class ClayTooltip extends Component {
* @memberof ClayTooltip
* @param {Array.<string>} newValue The new value of `this.selectors`.
* @param {Array.<string>} prevValue The previous value of `this.selectors`.
* @protected
* @private
*/
syncSelectors(newValue, prevValue) {
if (newValue) {
Expand All @@ -170,6 +239,12 @@ class ClayTooltip extends Component {
selector,
this._handleMouseLeave.bind(this)
),
dom.delegate(
document,
'click',
selector,
this._handleMouseClick.bind(this)
),
dom.delegate(
document,
'focus',
Expand All @@ -193,10 +268,17 @@ class ClayTooltip extends Component {
}
}

/**
* @inheritDoc
*/
syncVisible() {
//This is needed to make fade transition work
}

/**
* Gets the default value for the `classMap` state.
* @return {!Object}
* @protected
* @private
*/
valueClassMapFn_() {
return {
Expand Down Expand Up @@ -229,17 +311,6 @@ ClayTooltip.STATE = {
.value('')
.internal(),

/**
* A flag indicating if the tooltip should be shown.
* @default false
* @instance
* @memberof ClayTooltip
* @type {boolean}
*/
_showTooltip: Config.bool()
.value(false)
.internal(),

/**
* The current position of the tooltip after being aligned via `Align.align`.
* @default undefined
Expand Down Expand Up @@ -293,6 +364,16 @@ ClayTooltip.STATE = {
* @type {!Array.<string>}
*/
selectors: Config.array().value(['[data-title]', '[title]']),

/**
* Tooltip visible when show is called.
* @default false
* @instance
* @memberof ClayTooltip
* @private
* @type {?bool}
*/
visible: Config.bool().value(false),
};

Soy.register(ClayTooltip, templates);
Expand Down
9 changes: 6 additions & 3 deletions packages/clay-tooltip/src/ClayTooltip.soy
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
*/
{template .render}
{@param? _content: html|string}
{@param? _showTooltip: bool}
{@param? alignedPosition: number}
{@param? classMap: ?}
{@param? elementClasses: string}
{@param? position: number}
{@param? visible: bool}

{let $classes: $classMap ? $classMap : ['clay-tooltip-top-left', 'clay-tooltip-top', 'clay-tooltip-top-right', 'clay-tooltip-bottom-left', 'clay-tooltip-bottom', 'clay-tooltip-bottom-right', 'clay-tooltip-right', 'clay-tooltip-left'] /}
{let $currentPosition: $alignedPosition ?: $position /}
{let $positionClass: isNonnull($currentPosition) ? $classes[$currentPosition] : 'clay-tooltip-bottom' /}

{let $tooltipAttributes kind="attributes"}
class="tooltip
class="tooltip fade
{sp}{$positionClass}

{if $elementClasses}
{sp}{$elementClasses}
{/if}

{if $_showTooltip}
{if $visible}
{sp}show
{/if}
"

data-onmouseenter="_handleMouseEnterTooltip"
data-onmouseleave="_handleMouseLeave"
role="tooltip"
{/let}

Expand Down