-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web): Extend Tooltip Floating UI example, deprecate CSS modifier…
…s and side corners names #DS-618
- Loading branch information
Showing
7 changed files
with
323 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 78 additions & 5 deletions
83
packages/web/src/scss/components/Tooltip/floating-ui-example.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,96 @@ | ||
// To fully understand Floating UI and its options, please consult Floating UI docs: | ||
// @see https://floating-ui.com | ||
|
||
import { autoUpdate, computePosition, flip } from 'https://cdn.skypack.dev/@floating-ui/[email protected]'; | ||
import { | ||
arrow, | ||
autoUpdate, | ||
computePosition, | ||
flip, | ||
offset, | ||
limitShift, | ||
shift, | ||
size, | ||
} from 'https://cdn.skypack.dev/@floating-ui/[email protected]'; | ||
|
||
const button = document.getElementById('my-button'); | ||
const tooltip = document.getElementById('my-advanced-tooltip'); | ||
const result = document.getElementById('my-advanced-result'); | ||
const select = document.getElementById('my-advanced-select'); | ||
const viewport = document.getElementById('my-advanced-viewport'); | ||
const content = document.getElementById('my-advanced-content'); | ||
const arrowEl = tooltip.querySelector('[data-spirit-element="arrow"]'); | ||
|
||
const cleanup = autoUpdate(button, tooltip, () => { | ||
const tooltipComputedStyle = window.getComputedStyle(tooltip); | ||
const tooltipMaxWidth = parseInt(tooltipComputedStyle.maxWidth, 10); | ||
const tooltipOffset = parseInt(tooltipComputedStyle.getPropertyValue('--tooltip-offset'), 10); | ||
const arrowCornerOffset = parseInt( | ||
window.getComputedStyle(arrowEl).getPropertyValue('--tooltip-arrow-corner-offset'), | ||
10, | ||
); | ||
|
||
function updateTooltipPosition() { | ||
computePosition(button, tooltip, { | ||
placement: 'top', | ||
middleware: [flip()], | ||
}).then(({ x, y, placement }) => { | ||
placement: tooltip.dataset.spiritPlacement, | ||
middleware: [ | ||
offset(tooltipOffset), | ||
flip({ | ||
crossAxis: false, | ||
}), | ||
shift({ | ||
limiter: limitShift({ | ||
offset: ({ rects }) => ({ | ||
mainAxis: rects.reference.height, | ||
}), | ||
}), | ||
}), | ||
size({ | ||
apply({ availableWidth }) { | ||
Object.assign(tooltip.style, { | ||
maxWidth: `${tooltipMaxWidth < availableWidth ? tooltipMaxWidth : availableWidth}px`, | ||
}); | ||
}, | ||
}), | ||
arrow({ element: arrowEl, padding: arrowCornerOffset }), // arrow() should be placed at the end | ||
], | ||
}).then(({ x, y, middlewareData, placement }) => { | ||
Object.assign(tooltip.style, { | ||
top: `${y}px`, | ||
left: `${x}px`, | ||
}); | ||
|
||
const side = placement.split('-')[0]; | ||
|
||
const staticSide = { | ||
top: 'bottom', | ||
right: 'left', | ||
bottom: 'top', | ||
left: 'right', | ||
}[side]; | ||
|
||
if (middlewareData.arrow) { | ||
const divider = staticSide === 'top' || staticSide === 'bottom' ? 2 : 4; | ||
const { x, y } = middlewareData.arrow; | ||
Object.assign(arrowEl.style, { | ||
left: x != null ? `${x}px` : '', | ||
top: y != null ? `${y}px` : '', | ||
[staticSide]: `${-arrowEl.offsetWidth + arrowCornerOffset / divider}px`, | ||
}); | ||
} | ||
|
||
tooltip.dataset.spiritPlacement = placement; | ||
result.value = placement; | ||
}); | ||
} | ||
|
||
window.onload = () => { | ||
viewport.scrollLeft = (content.offsetWidth - viewport.offsetWidth) / 2; | ||
}; | ||
|
||
const cleanup = autoUpdate(button, tooltip, updateTooltipPosition); | ||
|
||
select.addEventListener('change', () => { | ||
tooltip.dataset.spiritPlacement = select.value; | ||
updateTooltipPosition(); | ||
}); | ||
|
||
// Call cleanup function when tooltip is removed from DOM. | ||
|
Oops, something went wrong.