use requestAnimationFrame and MutationObservers to ensure smooth class swapping