Skip to content

Commit

Permalink
Merge pull request #378 from baoagency/tooltips
Browse files Browse the repository at this point in the history
Tooltips
  • Loading branch information
forsbergplustwo authored Feb 10, 2024
2 parents 45a397c + c17c8f7 commit a99615f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 37 deletions.
52 changes: 38 additions & 14 deletions app/assets/javascripts/polaris_view_components.js
Original file line number Diff line number Diff line change
Expand Up @@ -2432,11 +2432,12 @@ class Tooltip extends Controller {
let tooltip = document.createElement("span");
tooltip.className = "Polaris-Tooltip";
tooltip.innerHTML = this.templateTarget.innerHTML;
this.tooltip = element.appendChild(tooltip);
const arrowElement = element.querySelector("[data-tooltip-arrow]");
document.body.appendChild(tooltip);
this.tooltip = tooltip;
const arrowElement = this.tooltip.querySelector(".Polaris-Tooltip-Arrow");
computePosition(element, this.tooltip, {
placement: this.positionValue,
middleware: [ offset(this.offsetValue), flip(), shift({
middleware: [ offset(10), flip(), shift({
padding: 5
}), arrow({
element: arrowElement
Expand All @@ -2446,20 +2447,43 @@ class Tooltip extends Controller {
left: `${x}px`,
top: `${y}px`
});
const {x: arrowX, y: arrowY} = middlewareData.arrow;
const staticSide = {
top: "bottom",
right: "left",
bottom: "top",
left: "right"
}[placement.split("-")[0]];
Object.assign(arrowElement.style, {
left: arrowX != null ? `${arrowX}px` : "",
top: arrowY != null ? `${arrowY}px` : "",
left: "",
top: "",
right: "",
bottom: "",
[staticSide]: "-4px"
bottom: ""
});
const {x: arrowX, y: arrowY} = middlewareData.arrow || {};
const primaryPlacement = placement.split("-")[0];
switch (primaryPlacement) {
case "top":
Object.assign(arrowElement.style, {
left: arrowX ? `${arrowX}px` : "",
bottom: "-4px"
});
break;

case "bottom":
Object.assign(arrowElement.style, {
left: arrowX ? `${arrowX}px` : "",
top: "-4px"
});
break;

case "left":
Object.assign(arrowElement.style, {
top: arrowY ? `${arrowY}px` : "",
right: "-4px"
});
break;

case "right":
Object.assign(arrowElement.style, {
top: arrowY ? `${arrowY}px` : "",
left: "-4px"
});
break;
}
}));
}
hide() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,65 @@ export default class extends Controller {
const element = event.currentTarget;

let tooltip = document.createElement("span");
tooltip.className = "Polaris-Tooltip"
tooltip.className = "Polaris-Tooltip";
tooltip.innerHTML = this.templateTarget.innerHTML;
this.tooltip = element.appendChild(tooltip);
document.body.appendChild(tooltip); // Append tooltip to body for better positioning control
this.tooltip = tooltip;

const arrowElement = element.querySelector("[data-tooltip-arrow]");
const arrowElement = this.tooltip.querySelector(".Polaris-Tooltip-Arrow");

computePosition(element, this.tooltip, {
placement: this.positionValue,
middleware: [
offset(this.offsetValue),
offset(10),
flip(),
shift({ padding: 5 }),
arrow({ element: arrowElement })
]
}).then(({x, y, placement, middlewareData}) => {
arrow({ element: arrowElement }),
],
}).then(({ x, y, placement, middlewareData }) => {
Object.assign(this.tooltip.style, {
left: `${x}px`,
top: `${y}px`,
})

const {x: arrowX, y: arrowY} = middlewareData.arrow;

const staticSide = {
top: 'bottom',
right: 'left',
bottom: 'top',
left: 'right',
}[placement.split('-')[0]];
});

// Reset any previously set styles on the arrow
Object.assign(arrowElement.style, {
left: arrowX != null ? `${arrowX}px` : '',
top: arrowY != null ? `${arrowY}px` : '',
left: '',
top: '',
right: '',
bottom: '',
[staticSide]: '-4px',
});
})

const { x: arrowX, y: arrowY } = middlewareData.arrow || {};
const primaryPlacement = placement.split('-')[0];

switch (primaryPlacement) {
case 'top':
Object.assign(arrowElement.style, {
left: arrowX ? `${arrowX}px` : '',
bottom: '-4px', // Aligns arrow to the bottom edge of the tooltip
});
break;
case 'bottom':
Object.assign(arrowElement.style, {
left: arrowX ? `${arrowX}px` : '',
top: '-4px', // Aligns arrow to the top edge of the tooltip
});
break;
case 'left':
Object.assign(arrowElement.style, {
top: arrowY ? `${arrowY}px` : '',
right: '-4px', // Aligns arrow to the right edge of the tooltip
});
break;
case 'right':
Object.assign(arrowElement.style, {
top: arrowY ? `${arrowY}px` : '',
left: '-4px', // Aligns arrow to the left edge of the tooltip
});
break;
}
});
}

hide() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ html.Polaris-Summer-Editions-2023 {
--p-space-1_5: var(--p-space-1_5-experimental);

--pc-stack-spacing: var(--p-space-300);

}
4 changes: 3 additions & 1 deletion app/assets/stylesheets/polaris_view_components.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
position: absolute;
top: 0;
left: 0;
z-index: var(--p-z-index-2);
opacity: 1;
box-shadow: var(--p-shadow-lg);
filter: drop-shadow(rgba(0, 0, 0, 0.3) 0 2px 5px);
border-radius: var(--p-border-radius-2);
pointer-events: none;
-webkit-backface-visibility: hidden;
Expand Down

0 comments on commit a99615f

Please sign in to comment.