Skip to content

Commit c766ffd

Browse files
committed
handle color mode changes
1 parent dd80617 commit c766ffd

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

src/components/Berry/index.tsx

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,36 @@ export interface BerryProps {
55
mode: BerryMode;
66
};
77

8+
declare global {
9+
interface Window {
10+
Berry: any;
11+
}
12+
}
13+
814
export default function Berry({ mode }: BerryProps) {
15+
useEffect(() => {
16+
function onColorModeChange(newColorMode) {
17+
if (!window.Berry) {
18+
return;
19+
}
20+
21+
window.Berry.update({
22+
colorMode: newColorMode,
23+
})
24+
}
25+
26+
const observer = new MutationObserver((mutations) => {
27+
mutations.forEach((mutation) => {
28+
if (mutation.attributeName === 'data-theme') {
29+
const newColorMode = getCurrentColorMode();
30+
onColorModeChange(newColorMode);
31+
}
32+
});
33+
});
34+
35+
observer.observe(document.documentElement, { attributes: true });
36+
}, []);
37+
938
useEffect(() => {
1039
loadBerry(mode);
1140
}, [mode]);
@@ -35,9 +64,24 @@ function initBerry(mode: BerryMode) {
3564
console.error("Berry not defined");
3665
return;
3766
}
67+
68+
const colorMode = getCurrentColorMode();
69+
const config = {
70+
...(mode === 'inline' ? inlineConfig : popupConfig),
71+
colorMode: colorMode,
72+
};
3873

39-
window.Berry.init(mode === 'inline' ? inlineConfig : popupConfig);
74+
window.Berry.init(config);
4075
};
76+
77+
function getCurrentColorMode(): 'light' | 'dark' {
78+
const theme = document.documentElement.getAttribute('data-theme');
79+
if (theme === 'light' || theme === 'dark') {
80+
return theme;
81+
} else {
82+
return 'light';
83+
}
84+
}
4185

4286

4387
const commonConfig = {
@@ -53,7 +97,8 @@ function initBerry(mode: BerryMode) {
5397
isOpenByDefault: true,
5498
parentElementId: 'inline-berry-chatbot-container',
5599
hideToggle: true,
56-
height: 850,
100+
height: 700,
101+
showResize: false,
57102
}
58103

59104
const popupConfig = {

0 commit comments

Comments
 (0)