feat(x-anchor): allow dynamic reference to be used with x-anchor #4735
+56
−20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses: #4079
Problem
Currently using
x-anchorwith a reference to an element does not allow for reactivity / changes to the reference. The element withx-anchoris permanently anchored to the initial reference throughout its lifecycle.In the below example, the header element with id
bazis anchored to the button with idfooon init. The button with idbarhas a@clickdirective to change thereferencevariable to reference itself.The expected behavior is that when clicking
barbutton, the header element re-anchors tobar. However, the actual behavior is that the header remains anchored tofoobutton.The current behavior is detrimental in certain use-cases, such as where a dropdown menu has multiple detached triggers that can each open and anchor to itself the same content.
Ideally,
x-anchorshould react to changes to the reference, and immediately re-calculate the CSS positioning.Investigation
The expression is evaluated once at mount and then passed to @floating-ui/dom
autoUpdate:This means changes to the expression are not captured and re-evaluated to form an updated reference, which can be passed to @floating-ui/dom API.
Solution
Introduce the
effectAPI to the directive and store the previous reference, such that if thex-anchorexpression changes and does not evaluate to the previous reference, we release the autoUpdate attached to the previous reference and create a new one.