Adding Animation while switching to dark mode HTML

Viewed 5247
1 Answers

You could use a css transition. Here follows a simple example, click in the snippet to transition the background color:

const rootDataset = document.documentElement.dataset;

document.onclick = () => {
    const inDarkMode = (rootDataset.theme === 'dark');
    rootDataset.theme = inDarkMode ? '' : 'dark';
}
:root {
    --primary-color: beige;    
}
[data-theme="dark"] {
    --primary-color: grey;    
}
body {
    background: var(--primary-color);
    transition: background 2s;
}

Related