Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS committed Apr 11, 2024
1 parent 41d7c20 commit ce1a79d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/components/theme/getDarkMediaMatch.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const supportsMatchMedia =
typeof window !== 'undefined' && typeof window.matchMedia === 'function';
export const getDarkMediaMatch = () => window.matchMedia('(prefers-color-scheme: dark)');
4 changes: 2 additions & 2 deletions src/components/theme/getSystemTheme.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getDarkMediaMatch} from './getDarkMediaMatch';
import {getDarkMediaMatch, supportsMatchMedia} from './getDarkMediaMatch';

export function getSystemTheme() {
if (typeof window === 'object') {
if (supportsMatchMedia) {
return getDarkMediaMatch().matches ? 'dark' : 'light';
} else {
return 'light';
Expand Down
6 changes: 5 additions & 1 deletion src/components/theme/useSystemTheme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {getDarkMediaMatch} from './getDarkMediaMatch';
import {getDarkMediaMatch, supportsMatchMedia} from './getDarkMediaMatch';
import {getSystemTheme} from './getSystemTheme';
import type {ThemeType} from './types';

Expand Down Expand Up @@ -29,6 +29,10 @@ export function useSystemTheme(): ThemeType {
const [theme, setTheme] = React.useState<ThemeType>(getSystemTheme());

React.useEffect(() => {
if (!supportsMatchMedia) {
return undefined;
}

function onChange(event: MediaQueryListEvent) {
setTheme(event.matches ? 'dark' : 'light');
}
Expand Down

0 comments on commit ce1a79d

Please sign in to comment.