d3 / d3-transition

Animated transitions for D3 selections.
https://d3js.org/d3-transition
ISC License
224 stars 64 forks source link

transition.style(name, function<null>) doesn’t remove style on end. #49

Closed mbostock closed 5 years ago

mbostock commented 8 years ago

We do remove the style on transition end in this case:

transition.style("foo", null);

But we don’t in this case:

transition.style("foo", function() { return null; });
mbostock commented 7 years ago

When the target value is specified as a function, the target value is not known until the tween is initialized and the function is called. And a transition’s tweens are initialized immediately after the transition starts; at that point, it’s too late to register an end event listener, as is done in the constant case. Per the README:

The transition then dispatches a start event to registered listeners. This is the last moment at which the transition may be modified: after starting, the transition’s timing, tweens, and listeners may no longer be changed.

So either: (1) we need to defer the state change from STARTING to STARTED until after the tweens are initialized, allowing them to add or remote transition event listeners (which is a bit confusing since at that point the start event was already dispatched… but probably okay) or (2) we need to use a different mechanism for removing the style after the transition ends, say by registering a tween that removes the style when t === 1 (which is icky because due to easing that could occur at times other than the exact end of the transition) or perhaps a tween that checks that state === ENDING.