Skip to content

Commit

Permalink
fix: support for media query event listener in older safari versions
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed May 6, 2024
1 parent 6c51bc8 commit 55644f2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,21 @@ const Toaster = (props: ToasterProps) => {

if (typeof window === 'undefined') return;

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', ({ matches }) => {
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');
const handleChange = ({ matches }: MediaQueryListEvent) => {
if (matches) {
setActualTheme('dark');
} else {
setActualTheme('light');
}
});
};

if ('addEventListener' in mediaQueryList) {
mediaQueryList.addEventListener('change', handleChange);
} else {
// @ts-expect-error: Deprecated API
mediaQueryList.addListener(handleChange);
}
}, [theme]);

React.useEffect(() => {
Expand Down

0 comments on commit 55644f2

Please sign in to comment.