Tooltip
Hover-triggered tooltip portaled to body for correct positioning.
Live Demo
Hover over the highlighted text to see a tooltip. Also try this one.
Source
HTML + JavaScript
<div data-component="my-tooltip">
<span class="has-tooltip"
data-action="mouseover:showTip mouseout:hideTip"
data-tip="Helpful information here">hover me</span>
<!-- Portal ensures tooltip escapes overflow:hidden parents -->
<div data-show="tipVisible" data-portal="body">
<div class="tooltip-popup" data-bind-style="tipStyle"
data-bind="tipText"></div>
</div>
</div>
<script>
wildflower.component('my-tooltip', {
state: {
tipVisible: false,
tipText: '',
tipStyle: {}
},
showTip(e) {
const rect = e.target.getBoundingClientRect();
this.tipText = e.target.dataset.tip;
this.tipStyle = {
position: 'fixed',
left: rect.left + 'px',
top: (rect.top - 32) + 'px'
};
this.tipVisible = true;
},
hideTip() { this.tipVisible = false; }
});
</script>
Key Points
data-action="mouseover:showTip mouseout:hideTip"binds two events on one elementdata-portal="body"teleports the tooltip outside anyoverflow: hiddencontainer. It renders at the body leveldata-bind-style="tipStyle"positions the tooltip dynamically usinggetBoundingClientRect()- The tooltip content comes from a
data-tipattribute on the trigger, read in the action method viae.target.dataset.tip