@@ -16,14 +16,41 @@ export interface NextJsSSRThemeSwitcherProps extends HTMLProps<HTMLElement> {
16
16
forcedPages ?: ForcedPage [ ] ;
17
17
/** id of target element to apply classes to. This is useful when you want to apply theme only to specific container. */
18
18
targetId ?: string ;
19
+ /** provide styles object imported from CSS/SCSS modules, if you are using CSS/SCSS modules. */
20
+ styles ?: Record < string , string > ;
21
+ }
22
+
23
+ function getDataProps ( resolvedData : UpdateProps , styles ?: Record < string , string > ) {
24
+ const dataProps : DataProps = { className : "" } ;
25
+ let classeNames = [ ] ;
26
+ if ( resolvedData . resolvedColorScheme !== undefined ) {
27
+ dataProps [ "data-color-scheme" ] = resolvedData . resolvedColorScheme ;
28
+ classeNames . push ( resolvedData . resolvedColorScheme ) ;
29
+ }
30
+ if ( resolvedData . resolvedTheme !== undefined ) {
31
+ dataProps [ "data-theme" ] = resolvedData . resolvedTheme ;
32
+ classeNames . push ( `theme-${ resolvedData . resolvedTheme } ` ) ;
33
+ }
34
+ if ( resolvedData . th ) {
35
+ dataProps [ "data-th" ] = resolvedData . th ;
36
+ classeNames . push ( `th-${ resolvedData . th } ` ) ;
37
+ }
38
+ if ( resolvedData . resolvedColorSchemePref !== undefined ) {
39
+ dataProps [ "data-csp" ] = resolvedData . resolvedColorSchemePref ;
40
+ classeNames . push ( `csp-${ resolvedData . resolvedColorSchemePref } ` ) ;
41
+ }
42
+ if ( styles ) classeNames = classeNames . map ( cls => styles [ cls ] ?? cls ) ;
43
+ dataProps . className = classeNames . join ( " " ) ;
44
+ return dataProps ;
19
45
}
20
46
21
47
function sharedServerComponentRenderer (
22
- { children, tag, forcedPages, targetId, ...props } : NextJsSSRThemeSwitcherProps ,
48
+ { children, tag, forcedPages, targetId, styles , ...props } : NextJsSSRThemeSwitcherProps ,
23
49
defaultTag : "div" | "html" ,
24
50
) {
25
51
const Tag : keyof JSX . IntrinsicElements = tag || defaultTag ;
26
- const state = cookies ( ) . get ( DEFAULT_ID ) ?. value ;
52
+ const key = targetId ? `#${ targetId } ` : DEFAULT_ID ;
53
+ const state = cookies ( ) . get ( key ) ?. value ;
27
54
28
55
const path = headers ( ) . get ( "referer" ) ;
29
56
const forcedPage = forcedPages ?. find ( forcedPage =>
@@ -34,8 +61,8 @@ function sharedServerComponentRenderer(
34
61
: forcedPage ?. props ;
35
62
const themeState = state ? ( parseState ( state ) as ThemeStoreType ) : undefined ;
36
63
const resolvedData = resolveTheme ( themeState , forcedPageProps ) ;
37
- const dataProps = getDataProps ( resolvedData ) ;
38
- if ( targetId ) dataProps . className += " nth-scoped" ;
64
+ const dataProps = getDataProps ( resolvedData , styles ) ;
65
+ if ( targetId ) dataProps . className += styles ?. [ " nth-scoped" ] ?? " nth-scoped" ;
39
66
40
67
return (
41
68
// @ts -expect-error -> svg props and html element props conflict
@@ -45,27 +72,6 @@ function sharedServerComponentRenderer(
45
72
) ;
46
73
}
47
74
48
- function getDataProps ( resolvedData : UpdateProps ) {
49
- const dataProps : DataProps = { className : "" } ;
50
- if ( resolvedData . resolvedColorScheme !== undefined ) {
51
- dataProps [ "data-color-scheme" ] = resolvedData . resolvedColorScheme ;
52
- dataProps . className = resolvedData . resolvedColorScheme ;
53
- }
54
- if ( resolvedData . resolvedTheme !== undefined ) {
55
- dataProps [ "data-theme" ] = resolvedData . resolvedTheme ;
56
- dataProps . className += `theme-${ resolvedData . resolvedTheme } ` ;
57
- }
58
- if ( resolvedData . th ) {
59
- dataProps [ "data-th" ] = resolvedData . th ;
60
- dataProps . className += ` th-${ resolvedData . th } ` ;
61
- }
62
- if ( resolvedData . resolvedColorSchemePref !== undefined ) {
63
- dataProps [ "data-csp" ] = resolvedData . resolvedColorSchemePref ;
64
- dataProps . className += ` csp-${ resolvedData . resolvedColorSchemePref } ` ;
65
- }
66
- return dataProps ;
67
- }
68
-
69
75
/**
70
76
* @example
71
77
* ```tsx
0 commit comments