react-native-paper icon indicating copy to clipboard operation
react-native-paper copied to clipboard

Documentation about the use of react-native-safe-area-context

Open jcubic opened this issue 1 year ago • 5 comments

Ask your Question

It seems that in documentation there are no information what to do after installing react-native-safe-area-context. I would expect that you need to import it in your code and use it.

Getting started guide only show that you need to install but no mention where to use it.

jcubic avatar May 27 '24 19:05 jcubic

good question!

Alanmc021 avatar May 27 '24 20:05 Alanmc021

This is the code I used:

import { View, StyleSheet } from 'react-native';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
import {
  PaperProvider,
  MD3DarkTheme,
  useTheme,
  Text
} from 'react-native-paper';

function App() {
  const theme = useTheme();
  const backgroundColor = theme.colors.background
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={[{ backgroundColor }, styles.containers]}> 
        <Text>React Native Paper</Text>
      </View>
    </SafeAreaView>
  );
}

export default function Main() {
  const { theme } = useMaterial3Theme();
  return (
    <SafeAreaProvider>
      <PaperProvider theme={{ ...MD3DarkTheme, colors: theme.dark }}>
        <App />
      </PaperProvider>
    </SafeAreaProvider>
  );
}

const styles = StyleSheet.create({
  containers: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

I can create a PR with updated docs. Not sure how much should I leave out for getting started guide.

jcubic avatar May 27 '24 21:05 jcubic

Hey @jcubic Can you check what happens if you just install the library and do nothing else? As far as I know, there is no need to do anything (unless you want some custom configuration), because SafeAreaProvider is initialized in PaperProvider Here is the link to the PaperProvider implementation https://github.com/callstack/react-native-paper/blob/main/src/core/PaperProvider.tsx

seb-zabielski avatar May 28 '24 11:05 seb-zabielski

Oh, ok. So this should be written in docs. If Provider is not needed, then you only need to show how the App component should look like:

import { View, StyleSheet } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { Text } from 'react-native-paper';

function App() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={styles.container}> 
        <Text>React Native Paper</Text>
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  }
});

This should show how to get started and show familiar React Native elements.

jcubic avatar May 28 '24 17:05 jcubic

Yes, I agree with you, this might help new users

seb-zabielski avatar May 29 '24 07:05 seb-zabielski

Forgive me if I ask a stupid question. Why is there a need to install react-native-safe-area-context manually, even though it is already dependency of react-native-paper and it is exactly a part of PaperProvider that we can't change it?

PaperProvider.tsx

const PaperProvider = (props: Props) => {
  // ...
  return (
    <SafeAreaProviderCompat>
      <PortalHost>
         {/* ... */}
      </PortalHost>
    </SafeAreaProviderCompat>
);

SafeAreaProviderCompat.tsx

import {
  initialWindowMetrics,
  SafeAreaInsetsContext,
  SafeAreaProvider,
} from 'react-native-safe-area-context';

export default function SafeAreaProviderCompat({ children, style }: Props) {
  return (
    <SafeAreaInsetsContext.Consumer>
        {/* ... */}
          <SafeAreaProvider initialMetrics={initialMetrics} style={style}>
            {children}
          </SafeAreaProvider>
        {/* ... */}
    </SafeAreaInsetsContext.Consumer>
  );
}
```

Wurushu avatar Nov 25 '24 10:11 Wurushu