Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
fix: color mode warning fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj authored and Suraj committed Jun 2, 2023
1 parent 9e4d60d commit b109acc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ComponentMeta } from '@storybook/react-native';
import { MultipleProvider } from './MultipleProvider';
const MyMultipleProviderMeta: ComponentMeta<typeof MultipleProvider> = {
title: 'api/stories/MultipleProvider',
component: MultipleProvider,
};

export { MultipleProvider } from './MultipleProvider';
//
export default MyMultipleProviderMeta;
49 changes: 49 additions & 0 deletions example/storybook/src/api/MultipleProvder/MultipleProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { memo } from 'react';
import { View, Pressable, Text } from 'react-native';
import { styled } from '@gluestack-style/react';
import { Wrapper } from '../../components/Wrapper';
import { get, set } from '@gluestack-style/react';

const StyleView = memo(
styled(
View,
{
w: 100,
h: 100,
bg: '$red500',

_dark: {
bg: '$info600',
},
},
{}
)
);

export function MultipleProvider() {
const [currentColorMode, setCurrentColorMode] = React.useState(get());

return (
<Wrapper colorMode={currentColorMode}>
<Wrapper>
<Pressable
style={{
backgroundColor: 'gray',
padding: 12,
marginBottom: 12,
}}
onPress={() => {
// set(get() === 'dark' ? 'light' : 'dark');
setCurrentColorMode(currentColorMode === 'dark' ? 'light' : 'dark');
}}
>
<Text style={{ color: 'white' }}>
Toggle {currentColorMode === 'dark' ? 'light' : 'dark'}
</Text>
</Pressable>
<StyleView />
</Wrapper>
</Wrapper>
);
}
export default MultipleProvider;
19 changes: 5 additions & 14 deletions packages/react/src/StyledProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,20 @@ import { platformSpecificSpaceUnits } from './utils';
import { createGlobalStylesWeb } from './createGlobalStylesWeb';

type Config = any;
let colorModeSet = false;

export const defaultConfig: { config: Config; colorMode: COLORMODES } = {
config: {},
colorMode: 'light',
};

// interface ConfigContextData {
// config: Config;
// setConfig: (config: Config) => void;
// }

const defaultContextData: Config = defaultConfig;

const StyledContext = React.createContext<Config>(defaultContextData);

// type IContext = {
// config: Config;
// colorMode?: COLORMODES;
// };

const setCurrentColorModeForWeb = (currentColorMode: string) => {
if (Platform.OS === 'web' && currentColorMode) {
set(currentColorMode === 'dark' ? 'dark' : 'light');
colorModeSet = true;
}
};
export const StyledProvider: React.FC<{
Expand Down Expand Up @@ -58,7 +49,7 @@ export const StyledProvider: React.FC<{
if (Platform.OS === 'web') {
document.documentElement.classList.add(`gs`);
}
setCurrentColorModeForWeb(currentColorMode);
// setCurrentColorModeForWeb(currentColorMode);

onChange((currentColor: string) => {
// only for web
Expand All @@ -78,8 +69,8 @@ export const StyledProvider: React.FC<{
setCurrentColorModeForWeb(currentColorMode);
}, [currentColorMode]);

// Set colormode server side
if (typeof window === 'undefined') {
// Set colormode for the first time
if (!colorModeSet) {
setCurrentColorModeForWeb(currentColorMode);
}

Expand Down

0 comments on commit b109acc

Please sign in to comment.