Use Window.matchMedia()
with the appropriate media query to check the user color scheme preference.
const prefersDarkColorScheme = () =>
window &&
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches;
// Example
prefersDarkColorScheme(); // true
Source
https://www.30secondsofcode.org/js/s/prefers-dark-color-scheme