necolas / react-native-web

Cross-platform React UI packages
https://necolas.github.io/react-native-web
MIT License
21.46k stars 1.77k forks source link

Text component inline style overrides global style [color] #2634

Closed JinYuSha0 closed 5 months ago

JinYuSha0 commented 5 months ago

Is there an existing issue for this?

Describe the issue

image image

Expected behavior

Why text component's color defaults to black instead of inheriting This is incompatible with browser specifications https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/exports/Text/index.js#L189

Steps to reproduce

Add global styles and create text components

Test case

https://codesandbox.io/p/sandbox/fervent-lichterman-jv4n25

Additional comments

In fact, the performance of react-native on Android follows AppTheme, and iOS has Appearance, so it is more reasonable to set it as inheritance.

JinYuSha0 commented 5 months ago

I submitted this PR [#2635]

necolas commented 5 months ago

This is how Text works in react native

JinYuSha0 commented 5 months ago

Well, if anyone encounters this problem, say you want to modify the font color globally in react-native-web, or switch themes, you can try this solution:

import {Platform, Text, View} from 'react-native';
import React from 'react';
import RootStack from './src/navigation/rootStack';

function App(): React.JSX.Element {
  const content = (
    <AppProvider>
      <RootStack />
    </AppProvider>
  );
  if (Platform.OS === 'web') {
    // Why wrapped by Text component?
    // Create a TextAncestorContext, make the color of the child Text component inherited
    // Related issues: https://github.com/necolas/react-native-web/issues/2634
    return (
      <Text style={{flex: 1, display: 'flex', color: 'your target color'}}>
         {content}
      </Text>
    );
  }
  return content;
}

export default App;