This content is over 8 years old. It may be obsolete and may not reflect the current opinion of the author.
It is quite nature to code CSS classes as “states” in a web applications. Say if you have three
<div class="view">s within the
<body> tag, you can easily toggle the class of the body to show/hide the views, with one-off CSS animations as view-to-view transitions, right? Wrong.
It turns out, it works with two states even with transition/animation, but not so much if there are three states. For two states like A & B, there is only one possible route to get into each state, A→B for B, and B→A for A. However, if there are three states, A would have routes B→A and C→A, and so on. It’s basically n*(n-1) for n states.
How do you specify two different CSS animations for the two routes? This doesn’t sounds like a common question, but I have personally encountered twice in different projects, and I bet sooner or later this would happen to everyone as we moved from DOM animation to CSS and rely more and more on browser to run the animation smoothly.
One way to look at it would be that this is a design flaw of the CSS language. There should be some pseudo-class like
:was() to specify what exact route to match against. However, inventing a pseudo-class just for the
animation property doesn’t sound like a great idea.
The other way, and a more practical approach is to look for solution with what we have right now. Inevitably, to distinguish route B→A and route C→A, one should introduce immediate states, making the routes B→A’→A and C→A”→A. Yet, this further complicate things if one would like the scripting part stay as a clean finite-state machine: First, the transition of A’→A will depend on