-
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-twig): Introduce Full Floating UI demo to Tooltip, update in…
…ternal implementation #DS-1084 Switch to data attribute placement instead of modification class based.
- Loading branch information
Showing
6 changed files
with
195 additions
and
119 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
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
186 changes: 142 additions & 44 deletions
186
packages/web-twig/src/Resources/components/Tooltip/stories/TooltipFloatingUI.twig
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,62 +1,160 @@ | ||
<p class="mb-0"> | ||
The following example is using external library <a href="https://floating-ui.com">Floating UI</a>. | ||
Try scrolling the frame or resizing the window to see how the Tooltip behaves. The Floating UI | ||
library is trying to keep the Tooltip in the viewport and it is also flipping, shifting and | ||
resizing the Tooltip when it is not possible to keep it in the viewport. | ||
</p> | ||
|
||
<Alert color="informative"> | ||
Please note that the Floating UI library is trying to point the arrow to the center | ||
of the trigger element. This is not possible to achieve in CSS only so our behavior | ||
is slightly different for tooltips not using Floating UI. | ||
</Alert> | ||
|
||
<Select id="my-advanced-select" label="Suggested placement"> | ||
<option value="top-start" selected>top-start</option> | ||
<option value="top">top</option> | ||
<option value="top-end">top-end</option> | ||
<option value="right-start">right-start</option> | ||
<option value="right">right</option> | ||
<option value="right-end">right-end</option> | ||
<option value="bottom-start">bottom-start</option> | ||
<option value="bottom">bottom</option> | ||
<option value="bottom-end">bottom-end</option> | ||
<option value="left-start">left-start</option> | ||
<option value="left">left</option> | ||
<option value="left-end">left-end</option> | ||
</Select> | ||
|
||
<style> | ||
.example-viewport { | ||
width: 40rem; | ||
max-width: 100%; | ||
height: 10rem; | ||
overflow: auto; | ||
} | ||
.example-viewport { | ||
width: 100%; | ||
max-width: 100%; | ||
height: 30rem; | ||
overflow: auto; | ||
} | ||
.example-content { | ||
position: relative; | ||
width: 100%; | ||
height: 20rem; | ||
padding-block: 7rem; | ||
text-align: center; | ||
} | ||
.example-content { | ||
position: relative; | ||
width: 300%; | ||
height: 60rem; | ||
padding-block: 12rem; | ||
text-align: center; | ||
} | ||
</style> | ||
|
||
<p> | ||
The following example is using external library <a href="https://floating-ui.com">Floating UI</a>. | ||
</p> | ||
<p>🖱 Try scrolling the example to see how Tooltip placement is updated.</p> | ||
|
||
<div class="example-viewport bg-cover"> | ||
<div class="example-content"> | ||
<Button | ||
type="button" | ||
id="my-button" | ||
UNSAFE_className="Button Button--primary Button--medium" | ||
aria-describedby="my-advanced-tooltip" | ||
> | ||
I have a flipping tooltip! | ||
</Button> | ||
<Tooltip id="my-advanced-tooltip" UNSAFE_className="Tooltip" data-spirit-placement="top"> | ||
Hello there! | ||
<span class="Tooltip__arrow"></span> | ||
</Tooltip> | ||
</div> | ||
<div class="example-viewport bg-cover" id="my-advanced-viewport"> | ||
<div class="example-content" id="my-advanced-content"> | ||
<Button | ||
id="my-button" | ||
aria-describedby="my-advanced-tooltip" | ||
> | ||
I have a Floating tooltip! | ||
</Button> | ||
<Tooltip id="my-advanced-tooltip" placement="top-start" data-spirit-placement-controlled> | ||
This long tooltip is flipping, resizing and shifting to stay in the viewport. | ||
Also its arrow is always trying to point to the center of the trigger. | ||
</Tooltip> | ||
</div> | ||
</div> | ||
|
||
<script type="module"> | ||
// 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 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 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, | ||
); | ||
const cleanup = autoUpdate(button, tooltip, () => { | ||
computePosition(button, tooltip, { | ||
placement: 'top', | ||
middleware: [flip()], | ||
}).then(({ x, y, placement }) => { | ||
Object.assign(tooltip.style, { | ||
top: `${y}px`, | ||
left: `${x}px`, | ||
function updateTooltipPosition() { | ||
computePosition(button, tooltip, { | ||
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 offset = | ||
staticSide === 'top' || staticSide === 'bottom' | ||
? arrowEl.offsetHeight | ||
: (arrowEl.offsetHeight + arrowEl.offsetWidth) / 2; | ||
const { x, y } = middlewareData.arrow; | ||
Object.assign(arrowEl.style, { | ||
left: x != null ? `${x}px` : '', | ||
top: y != null ? `${y}px` : '', | ||
bottom: '', | ||
right: '', | ||
[staticSide]: `-${offset}px`, | ||
}); | ||
} | ||
tooltip.dataset.spiritPlacement = placement; | ||
}); | ||
tooltip.dataset.spiritPlacement = 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.