Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support dark mode for safari < 14 using matchMedia().addListener #495

Merged
merged 2 commits into from
Oct 30, 2024

Conversation

yunsteel
Copy link
Contributor

@yunsteel yunsteel commented Oct 15, 2024

Issue:

Closes #494

What has been done:

To support Safari, a try-catch block was added around addEventListener.
matchMedia().addEventListener is supported starting from Safari 14, while the addListener method should be used for versions prior to Safari 14. As a result, some users are now experiencing errors.

Screenshots/Videos:

N/A

Copy link

vercel bot commented Oct 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sonner ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 15, 2024 2:54am

@yunsteel yunsteel changed the title fix: support matchMedia for safari fix: support dark mode for safari < 14 using matchMedia().addListener Oct 15, 2024
@yunsteel
Copy link
Contributor Author

I solved the problem by adding polyfill as below.

        if (typeof window !== 'undefined' && !window.matchMedia('').addEventListener) {
        const originalMatchMedia = window.matchMedia;

        window.matchMedia = (query) => {
          const mediaQueryList = originalMatchMedia(query);

          if (!mediaQueryList.addEventListener) {
            mediaQueryList.addEventListener = (e, handler) => mediaQueryList.addListener(handler);
            mediaQueryList.removeEventListener = (e, handler) => mediaQueryList.removeListener(handler);
          }

          return mediaQueryList;  
        };
      } 

@emilkowalski emilkowalski merged commit 6fca6ab into emilkowalski:main Oct 30, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: (Safari 12.1.2) window.matchMedia("(prefers-color-scheme: dark)").addEventListener is not a function.
2 participants