matchMedia().addListener marked as deprecated, addEventListener equivalent?
From the doc - https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/addListener
A function or function reference representing the callback function you want to run when the media query status changes.
It should be change
event. https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/onchange.
const mql = window.matchMedia("(prefers-color-scheme: dark)");
mql.addEventListener("change", () => {
this.checkNative();
});
Chrome and Firefox handle it differently than Safari, I solved it with way:
const darkMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
try {
// Chrome & Firefox
darkMediaQuery.addEventListener('change', (e) => {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark;
});
} catch (e1) {
try {
// Safari
darkMediaQuery.addListener((e) => {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark;
});
} catch (e2) {
console.error(e2);
}
}
If you're interested in how to support Dark Mode with your website, read this blogpost.