From c4aad17c66578a5c69d29d35947f0a8680496e15 Mon Sep 17 00:00:00 2001 From: Akash Gautam Date: Mon, 10 Feb 2025 18:42:48 +0530 Subject: [PATCH 1/5] fix: provider update --- .../ui/gluestack-ui-provider/index.tsx | 32 +++++++------------ .../ui/gluestack-ui-provider/index.web.tsx | 3 +- .../ui/gluestack-ui-provider/types.ts | 1 + .../ui/gluestack-ui-provider/index.tsx | 32 +++++++------------ .../ui/gluestack-ui-provider/index.web.tsx | 9 +++++- .../ui/gluestack-ui-provider/types.ts | 1 + 6 files changed, 34 insertions(+), 44 deletions(-) create mode 100644 apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/types.ts create mode 100644 apps/templates/next-app-router-with-nativewind/components/ui/gluestack-ui-provider/types.ts diff --git a/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx b/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx index 4855e434..d1ab4871 100644 --- a/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx +++ b/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx @@ -1,40 +1,30 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { config } from './config'; -import { ColorSchemeName, useColorScheme, View, ViewProps } from 'react-native'; +import { View, ViewProps } from 'react-native'; import { OverlayProvider } from '@gluestack-ui/overlay'; import { ToastProvider } from '@gluestack-ui/toast'; -import { colorScheme as colorSchemeNW } from 'nativewind'; - -type ModeType = 'light' | 'dark' | 'system'; - -const getColorSchemeName = ( - colorScheme: ColorSchemeName, - mode: ModeType -): 'light' | 'dark' => { - if (mode === 'system') { - return colorScheme ?? 'light'; - } - return mode; -}; +import { useColorScheme } from 'nativewind'; +import { ModeType } from './types'; export function GluestackUIProvider({ mode = 'light', ...props }: { - mode?: 'light' | 'dark' | 'system'; + mode?: ModeType; children?: React.ReactNode; style?: ViewProps['style']; }) { - const colorScheme = useColorScheme(); - - const colorSchemeName = getColorSchemeName(colorScheme, mode); + const { colorScheme, setColorScheme } = useColorScheme(); - colorSchemeNW.set(mode); + useEffect(() => { + setColorScheme(mode); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [mode]); return ( { @@ -21,7 +22,7 @@ export function GluestackUIProvider({ mode = 'light', ...props }: { - mode?: 'light' | 'dark' | 'system'; + mode?: ModeType; children?: React.ReactNode; }) { let cssVariablesWithMode = ``; diff --git a/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/types.ts b/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/types.ts new file mode 100644 index 00000000..08daa2ba --- /dev/null +++ b/apps/templates/expo-router-with-nativewind/components/ui/gluestack-ui-provider/types.ts @@ -0,0 +1 @@ +export type ModeType = 'light' | 'dark' | 'system'; diff --git a/apps/templates/next-app-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx b/apps/templates/next-app-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx index 4855e434..d1ab4871 100644 --- a/apps/templates/next-app-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx +++ b/apps/templates/next-app-router-with-nativewind/components/ui/gluestack-ui-provider/index.tsx @@ -1,40 +1,30 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { config } from './config'; -import { ColorSchemeName, useColorScheme, View, ViewProps } from 'react-native'; +import { View, ViewProps } from 'react-native'; import { OverlayProvider } from '@gluestack-ui/overlay'; import { ToastProvider } from '@gluestack-ui/toast'; -import { colorScheme as colorSchemeNW } from 'nativewind'; - -type ModeType = 'light' | 'dark' | 'system'; - -const getColorSchemeName = ( - colorScheme: ColorSchemeName, - mode: ModeType -): 'light' | 'dark' => { - if (mode === 'system') { - return colorScheme ?? 'light'; - } - return mode; -}; +import { useColorScheme } from 'nativewind'; +import { ModeType } from './types'; export function GluestackUIProvider({ mode = 'light', ...props }: { - mode?: 'light' | 'dark' | 'system'; + mode?: ModeType; children?: React.ReactNode; style?: ViewProps['style']; }) { - const colorScheme = useColorScheme(); - - const colorSchemeName = getColorSchemeName(colorScheme, mode); + const { colorScheme, setColorScheme } = useColorScheme(); - colorSchemeNW.set(mode); + useEffect(() => { + setColorScheme(mode); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [mode]); return ( { @@ -21,7 +22,7 @@ export function GluestackUIProvider({ mode = 'light', ...props }: { - mode?: 'light' | 'dark' | 'system'; + mode?: ModeType; children?: React.ReactNode; }) { let cssVariablesWithMode = ``; @@ -80,6 +81,12 @@ export function GluestackUIProvider({ return ( <> +