From 1a4a78e59f0ab26366a6989d40696db652ee964e Mon Sep 17 00:00:00 2001 From: Ovidio Henriquez Date: Fri, 4 Jan 2019 10:32:40 -0800 Subject: [PATCH] Allow a manual target to be set This change adds the ability to set a manual target that the tooltip should be anchored to. A test is added to ensure that the target can be set manually and that the tooltip is updated appropriately to be anchored correctly. --- paper-tooltip.js | 16 ++++++++++-- test/basic.html | 65 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/paper-tooltip.js b/paper-tooltip.js index 9476bc8..36f28be 100644 --- a/paper-tooltip.js +++ b/paper-tooltip.js @@ -314,12 +314,15 @@ Polymer({ /** * Returns the target element that this tooltip is anchored to. It is - * either the element given by the `for` attribute, or the immediate parent - * of the tooltip. + * the element given by the `for` attribute, the element manually specified + * through the `target` attribute, or the immediate parent of the tooltip. * * @type {Node} */ get target() { + if (this._manualTarget) + return this._manualTarget; + var parentNode = dom(this).parentNode; // If the parentNode is a document fragment, then we need to use the host. var ownerRoot = dom(this).getOwnerRoot(); @@ -334,6 +337,15 @@ Polymer({ return target; }, + /** + * Sets the target element that this tooltip will be anchored to. + * @param {Node} target + */ + set target(target) { + this._manualTarget = target; + this._findTarget(); + }, + /** * @return {void} */ diff --git a/test/basic.html b/test/basic.html index fe86f40..a284df1 100644 --- a/test/basic.html +++ b/test/basic.html @@ -22,11 +22,16 @@ margin: 0; padding: 0; } - #target { + #target, #target1 { width: 100px; height: 20px; background-color: red; } + #target2 { + width: 120px; + height: 30px; + background-color: blue; + } paper-tooltip { width: 70px; height: 30px; @@ -118,6 +123,18 @@ + + + +