UNPKG

@cometchat/chat-uikit-react-native

Version:

Ready-to-use Chat UI Components for React Native

76 lines 3.48 kB
import { useMemo, useState, useEffect, } from "react"; import { useColorScheme, Platform } from "react-native"; import { deepMerge } from "../shared/helper/helperFunctions"; import { Brightness, CometChatThemeHelper } from "./CometChatThemeHelper"; import { CompThemeContext, ThemeContext, } from "./context"; import { darkThemeMaker, lightThemeMaker } from "./default/default"; import { useThemeInternal } from "./hook"; function useDebounce(value, delay) { const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => { const handler = setTimeout(() => { setDebouncedValue(value); }, delay); return () => { clearTimeout(handler); }; }, [value, delay]); return debouncedValue; } export const CometChatThemeProvider = ({ children, theme = {}, }) => { const rawScheme = useColorScheme(); // For iOS, debounce the scheme value (300ms delay in this example). // For Android, use the raw value. const scheme = Platform.OS === "ios" ? useDebounce(rawScheme, 300) : rawScheme; const parentProviderTheme = useThemeInternal(); const { mode = parentProviderTheme.mode, dark = parentProviderTheme.dark, light = parentProviderTheme.light, } = theme; const lightTheme = useMemo(() => { if (light) { const updatedColors = light.color ? CometChatThemeHelper.updateColors(light.color, Brightness.LIGHT) : {}; const updatedSpacing = light.spacing ? CometChatThemeHelper.updateSpacing(light.spacing) : {}; light.color = updatedColors; light.spacing = updatedSpacing; const mergedTheme = deepMerge(parentProviderTheme.light, light); const defaultLightThemeWithOverridesApplied = lightThemeMaker(mergedTheme.spacing, mergedTheme.color, mergedTheme.typography); return deepMerge(defaultLightThemeWithOverridesApplied, light); } return parentProviderTheme.light; }, [light, parentProviderTheme.light]); const darkTheme = useMemo(() => { if (dark) { const updatedColors = dark.color ? CometChatThemeHelper.updateColors(dark.color, Brightness.DARK) : {}; const updatedSpacing = dark.spacing ? CometChatThemeHelper.updateSpacing(dark.spacing) : {}; dark.color = updatedColors; dark.spacing = updatedSpacing; const mergedTheme = deepMerge(parentProviderTheme.dark, dark); const defaultDarkThemeWithOverridesApplied = darkThemeMaker(mergedTheme.spacing, mergedTheme.color, mergedTheme.typography); return deepMerge(defaultDarkThemeWithOverridesApplied, dark); } return parentProviderTheme.dark; }, [dark, parentProviderTheme.dark]); const themeValue = { light: lightTheme, dark: darkTheme, // Use the (conditionally) debounced scheme when determining the mode mode: mode === "auto" ? (typeof scheme === "string" ? scheme : "light") : mode, }; return (<ThemeContext.Provider value={themeValue}> {children} </ThemeContext.Provider>); }; export const CometChatCompThemeProvider = ({ children, theme = {}, }) => { return (<CompThemeContext.Provider value={theme}> {children} </CompThemeContext.Provider>); }; //# sourceMappingURL=provider.js.map