-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shane Osbourne
committed
Dec 4, 2024
1 parent
0548bdf
commit fe3a5fd
Showing
59 changed files
with
2,360 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
special-pages/pages/new-tab/app/components/BackgroundProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { createContext, Fragment, h } from 'preact'; | ||
import styles from './BackgroundReceiver.module.css'; | ||
import { values } from '../customizer/values.js'; | ||
import { computed, signal } from '@preact/signals'; | ||
import { useContext } from 'preact/hooks'; | ||
import { CustomizerContext } from '../customizer/CustomizerProvider.js'; | ||
|
||
/** | ||
* @import { BackgroundVariant } from "../../types/new-tab" | ||
*/ | ||
|
||
const BackgroundContext = createContext({ | ||
/** @type {import("@preact/signals").Signal<BackgroundVariant>} */ | ||
current: signal({ kind: 'default' }), | ||
}); | ||
|
||
/** | ||
* @param {object} props | ||
* @param {import("preact").ComponentChild} props.children | ||
*/ | ||
export function BackgroundProvider({ children }) { | ||
const { data } = useContext(CustomizerContext); | ||
const bg = computed(() => data.value.background); | ||
return <BackgroundContext.Provider value={{ current: bg }}>{children}</BackgroundContext.Provider>; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
export function BackgroundConsumer() { | ||
const { current } = useContext(BackgroundContext); | ||
const background = current.value; | ||
if (background === null) { | ||
return <div class={styles.root}></div>; | ||
} | ||
switch (background.kind) { | ||
case 'hex': { | ||
return ( | ||
<div | ||
class={styles.root} | ||
style={{ | ||
backgroundColor: background.value, | ||
}} | ||
></div> | ||
); | ||
} | ||
case 'color': { | ||
const color = values.colors[background.value]; | ||
return ( | ||
<div | ||
class={styles.root} | ||
style={{ | ||
backgroundColor: color.hex, | ||
}} | ||
></div> | ||
); | ||
} | ||
case 'gradient': { | ||
const gradient = values.gradients[background.value]; | ||
return ( | ||
<Fragment key="gradient"> | ||
<div | ||
class={styles.root} | ||
style={{ | ||
backgroundImage: `url(${gradient.path})`, | ||
backgroundSize: 'cover', | ||
backgroundRepeat: 'no-repeat', | ||
}} | ||
/> | ||
<div | ||
class={styles.root} | ||
style={{ | ||
backgroundImage: `url(gradients/grain.png)`, | ||
backgroundRepeat: 'repeat', | ||
opacity: 0.5, | ||
mixBlendMode: 'soft-light', | ||
}} | ||
></div> | ||
</Fragment> | ||
); | ||
} | ||
case 'userImage': { | ||
const img = background.value; | ||
return ( | ||
<div | ||
class={styles.root} | ||
style={{ | ||
backgroundImage: `url(${img.src})`, | ||
backgroundSize: 'cover', | ||
backgroundRepeat: 'no-repeat', | ||
backgroundPosition: 'center center', | ||
}} | ||
></div> | ||
); | ||
} | ||
default: { | ||
throw new Error('Unreachable!'); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
special-pages/pages/new-tab/app/components/BackgroundReceiver.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.root { | ||
position: fixed; | ||
z-index: 0; | ||
inset: 0; | ||
width: 100vw; | ||
height: 100vh; | ||
transition: all .3s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
special-pages/pages/new-tab/app/customizer/CustomizerProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { createContext, h } from 'preact'; | ||
import { useCallback } from 'preact/hooks'; | ||
import { effect, signal, useSignal } from '@preact/signals'; | ||
|
||
/** | ||
* @typedef {import('../../types/new-tab.js').CustomizerData} CustomizerData | ||
* @typedef {import('../service.hooks.js').State<CustomizerData, undefined>} State | ||
* @typedef {import('../service.hooks.js').Events<CustomizerData, undefined>} Events | ||
*/ | ||
|
||
/** | ||
* These are the values exposed to consumers. | ||
*/ | ||
export const CustomizerContext = createContext({ | ||
/** @type {import("@preact/signals").Signal<CustomizerData>} */ | ||
data: signal({ | ||
background: { kind: 'default' }, | ||
userImages: [], | ||
theme: 'system', | ||
}), | ||
/** @type {(bg: CustomizerData['background']) => void} */ | ||
select: (bg) => {}, | ||
upload: () => {}, | ||
/** | ||
* @type {(theme: import('../../types/new-tab').CustomizerData['theme']) => void} | ||
*/ | ||
setTheme: (theme) => {}, | ||
/** | ||
* @type {(id: string) => void} | ||
*/ | ||
deleteImage: (id) => {}, | ||
}); | ||
|
||
/** | ||
* A data provider that will use `RMFService` to fetch data, subscribe | ||
* to updates and modify state. | ||
* | ||
* @param {Object} props | ||
* @param {import("./customizer.service.js").CustomizerService} props.service | ||
* @param {CustomizerData} props.initialData | ||
* @param {import("preact").ComponentChild} props.children | ||
*/ | ||
export function CustomizerProvider({ service, initialData, children }) { | ||
// const [state, dispatch] = useReducer(withLog('RMFProvider', reducer), initial) | ||
const data = useSignal(initialData); | ||
|
||
effect(() => { | ||
const unsub = service.onBackground((evt) => { | ||
data.value = { ...data.value, background: evt.data }; | ||
}); | ||
const unsub1 = service.onTheme((evt) => { | ||
data.value = { ...data.value, theme: evt.data }; | ||
}); | ||
const unsub2 = service.onImages((evt) => { | ||
data.value = { ...data.value, userImages: evt.data }; | ||
}); | ||
|
||
return () => { | ||
unsub(); | ||
unsub1(); | ||
unsub2(); | ||
}; | ||
}); | ||
|
||
/** @type {(bg: CustomizerData['background']) => void} */ | ||
const select = useCallback( | ||
(bg) => { | ||
service.setBackground(bg); | ||
}, | ||
[service], | ||
); | ||
|
||
const upload = useCallback(() => { | ||
service.upload(); | ||
}, [service]); | ||
|
||
const setTheme = useCallback( | ||
(theme) => { | ||
service.setTheme(theme); | ||
}, | ||
[service], | ||
); | ||
|
||
const deleteImage = useCallback( | ||
(id) => { | ||
service.deleteImage(id); | ||
}, | ||
[service], | ||
); | ||
|
||
return <CustomizerContext.Provider value={{ data, select, upload, setTheme, deleteImage }}>{children}</CustomizerContext.Provider>; | ||
} |
Oops, something went wrong.