Home > Software design >  How to change the colour of a ScrollView's scrollbar when the user has set dark mode on in Reac
How to change the colour of a ScrollView's scrollbar when the user has set dark mode on in Reac

Time:01-05

Long story short, if I use a <ScrollView> element while my decide doesn't have dark mode on, the scrollbar is dark grey-ish, however, if the user has dark mode on, it's lighter colour and blends with my app's background.

I was wondering if there's a simple way to change the colour of a ScrollView's scrollbar when dark mode in on in Android React Native?

CodePudding user response:

you could use the react-native-scrollbar library to set the scrollbar color in your app: Try

import { ScrollView } from 'react-native-scrollbar';

<ScrollView
scrollbarColor="#FF0000"
// other props here
>
{/* content goes here */}
</ScrollView>

CodePudding user response:

Here's an example of how you can use the StyleSheet module to apply custom styles to your scrollbar. You can then use the Platform module to detect whether the device is in dark mode or light mode and apply the appropriate styles:

    import { StyleSheet, Platform } from 'react-native';

const styles = StyleSheet.create({
  scrollView: {
    // Other styles for the ScrollView
  },
  lightScrollBar: {
    // Styles for the scrollbar when the device is in light mode
    color: '#FF0000', // Red scrollbar for light mode
  },
  darkScrollBar: {
    // Styles for the scrollbar when the device is in dark mode
    color: '#00FF00', // Green scrollbar for dark mode
  },
});

const ScrollView = ({ children }) => {
  const isDarkMode = Platform.select({
    ios: () => Appearance.getColorScheme() === 'dark',
    android: () => useColorScheme() === 'dark',
  })();

  return (
    <ScrollView
      style={[styles.scrollView, isDarkMode ? styles.darkScrollBar : styles.lightScrollBar]}
    >
      {children}
    </ScrollView>
  );
};
  • Related