1- import { h } from 'preact' ;
1+ import { Fragment , h } from 'preact' ;
22import styles from './BackgroundReceiver.module.css' ;
3+ import { values } from '../customizer/values.js' ;
34import { useContext } from 'preact/hooks' ;
45import { CustomizerContext } from '../customizer/CustomizerProvider.js' ;
6+ import { detectThemeFromHex } from '../customizer/utils.js' ;
57
68/**
79 * @import { BackgroundVariant, BrowserTheme } from "../../types/new-tab"
@@ -18,12 +20,22 @@ export function inferSchemeFrom(background, browserTheme, system) {
1820 switch ( background . kind ) {
1921 case 'default' :
2022 return { bg : browser , browser } ;
21- case 'gradient' :
23+ case 'color' : {
24+ const color = values . colors [ background . value ] ;
25+ return { bg : color . colorScheme , browser } ;
26+ }
27+
28+ case 'gradient' : {
29+ const gradient = values . gradients [ background . value ] ;
30+ return { bg : gradient . colorScheme , browser } ;
31+ }
32+
2233 case 'userImage' :
34+ return { bg : background . value . colorScheme , browser } ;
35+
2336 case 'hex' :
24- console . log ( 'not supported yet!' ) ;
37+ return { bg : detectThemeFromHex ( background . value ) , browser } ;
2538 }
26- return { bg : browser , browser } ;
2739}
2840
2941/**
@@ -40,23 +52,90 @@ export function themeFromBrowser(browserTheme, system) {
4052
4153/**
4254 * @param {object } props
55+ * @param {import("@preact/signals").Signal<'light' | 'dark'> } props.bg
4356 * @param {import("@preact/signals").Signal<'light' | 'dark'> } props.browser
4457 */
45- export function BackgroundConsumer ( { browser } ) {
58+ export function BackgroundConsumer ( { bg , browser } ) {
4659 const { data } = useContext ( CustomizerContext ) ;
4760 const background = data . value . background ;
4861
4962 switch ( background . kind ) {
5063 case 'default' : {
5164 return < div className = { styles . root } data-testid = "BackgroundConsumer" data-background-kind = "default" data-theme = { browser } /> ;
5265 }
53- case 'hex' :
54- case 'color' :
55- case 'gradient' :
56- case 'userImage' :
66+ case 'hex' : {
67+ return (
68+ < div
69+ class = { styles . root }
70+ data-animate = "true"
71+ data-testid = "BackgroundConsumer"
72+ style = { {
73+ backgroundColor : background . value ,
74+ } }
75+ > </ div >
76+ ) ;
77+ }
78+ case 'color' : {
79+ const color = values . colors [ background . value ] ;
80+ return (
81+ < div
82+ class = { styles . root }
83+ data-animate = "true"
84+ data-background-color = { color . hex }
85+ data-testid = "BackgroundConsumer"
86+ style = { {
87+ backgroundColor : color . hex ,
88+ } }
89+ > </ div >
90+ ) ;
91+ }
92+ case 'gradient' : {
93+ const gradient = values . gradients [ background . value ] ;
94+ return (
95+ < Fragment key = "gradient" >
96+ < div
97+ class = { styles . root }
98+ data-animate = "false"
99+ data-testid = "BackgroundConsumer"
100+ style = { {
101+ backgroundColor : gradient . fallback ,
102+ backgroundImage : `url(${ gradient . path } )` ,
103+ backgroundSize : 'cover' ,
104+ backgroundRepeat : 'no-repeat' ,
105+ } }
106+ />
107+ < div
108+ class = { styles . root }
109+ data-animate = "false"
110+ style = { {
111+ backgroundImage : `url(gradients/grain.png)` ,
112+ backgroundRepeat : 'repeat' ,
113+ opacity : 0.5 ,
114+ mixBlendMode : 'soft-light' ,
115+ } }
116+ > </ div >
117+ </ Fragment >
118+ ) ;
119+ }
120+ case 'userImage' : {
121+ const img = background . value ;
122+ return (
123+ < div
124+ class = { styles . root }
125+ data-animate = "true"
126+ data-testid = "BackgroundConsumer"
127+ style = { {
128+ backgroundImage : `url(${ img . src } )` ,
129+ backgroundSize : 'cover' ,
130+ backgroundRepeat : 'no-repeat' ,
131+ backgroundPosition : 'center center' ,
132+ } }
133+ > </ div >
134+ ) ;
135+ }
57136 default : {
58- console . warn ( 'not supported yet !' ) ;
59- return < div className = { styles . root } data-testid = "BackgroundConsumer" data-background-kind = "default" data-theme = { browser } / >;
137+ console . warn ( 'Unreachable !' ) ;
138+ return < div className = { styles . root } > </ div > ;
60139 }
61140 }
62141}
0 commit comments