1
1
import useRGS from "r18gs" ;
2
2
import type { SetStateAction } from "r18gs" ;
3
- import * as React from "react" ;
4
3
import type { ColorSchemePreference , ThemeState } from "../../constants" ;
5
4
import { DEFAULT_ID , DEFAULT_THEME_STATE } from "../../constants" ;
5
+ import { useEffect } from "react" ;
6
6
7
7
export interface ThemeSwitcherProps {
8
8
/** id of target element to apply classes to. This is useful when you want to apply theme only to specific container. */
@@ -16,7 +16,7 @@ export interface ThemeSwitcherProps {
16
16
}
17
17
18
18
const useMediaQuery = ( setThemeState : SetStateAction < ThemeState > ) => {
19
- React . useEffect ( ( ) => {
19
+ useEffect ( ( ) => {
20
20
// set event listener for media
21
21
const media = matchMedia ( "(prefers-color-scheme: dark)" ) ;
22
22
const updateSystemColorScheme = ( ) => {
@@ -42,7 +42,7 @@ const parseState = (str?: string | null) => {
42
42
let tInit = 0 ;
43
43
44
44
const useLoadSyncedState = ( { dontSync, targetId, setThemeState } : LoadSyncedStateProps ) => {
45
- React . useEffect ( ( ) => {
45
+ useEffect ( ( ) => {
46
46
if ( dontSync ) return ;
47
47
tInit = Date . now ( ) ;
48
48
const key = targetId ?? DEFAULT_ID ;
@@ -96,7 +96,7 @@ const applyClasses = ({ targets, theme, resolvedColorScheme, styles }: ApplyClas
96
96
t ?. classList . remove ( cls [ 0 ] ) ; // dark
97
97
t ?. classList . remove ( cls [ 1 ] ) ; // light
98
98
t ?. classList . forEach ( c => {
99
- if ( / (?: ^ | _ ) t h - / . exec ( c ) ) t . classList . remove ( c ) ;
99
+ if ( / (?: ^ | _ ) t h - / . test ( c ) ) t . classList . remove ( c ) ;
100
100
} ) ;
101
101
t ?. classList . add ( cls [ 2 ] ) ; // theme
102
102
t ?. classList . add ( cls [ 3 ] ) ; // resolvedColorScheme
@@ -149,7 +149,7 @@ export const ThemeSwitcher = ({
149
149
useLoadSyncedState ( { dontSync, targetId, setThemeState } ) ;
150
150
151
151
/** update DOM and storage */
152
- React . useEffect ( ( ) => {
152
+ useEffect ( ( ) => {
153
153
const restoreTransitions = modifyTransition ( themeTransition , targetId ) ;
154
154
updateDOM ( { targetId, themeState, dontSync, styles } ) ;
155
155
if ( ! dontSync && tInit < Date . now ( ) - 300 ) {
0 commit comments