0.1.10 • Published 9 months ago

@herberthtk/react-native-viewport v0.1.10

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

react-native-viewport

Build StatusMIT LicenseSupports AndroidSupports iOSnpm versionnpm

A fully customizable React Native wrapper to check whether a component is in the view port to track impressions, It is purely written in TypeScript

Demos

1. Track if the view container is 100% visible on the screen

100% view

2. Set a custom minimum value to Track and trigger visible if the view container is visible on the screen (For this case, the minimum value 70% to trigger isVisibility)

70% view

Why choose this package

There many react-native viewport packages on npm but some of them are no longer actively maintained by their authors, doesn't support Typescript, and most importantly none of them support custom threshold for minimum percentage of the view to be considered visible.

Since I failed to get a package that fulfils the video requirements of my client's application, I decided to write it myself and we are happy with the results

Installation

    npm i @herberthtk/react-native-viewport

Or

    yarn add @herberthtk/react-native-viewport

Usage

import InViewport from '@herberthtk/react-native-viewport';

 const Section = () => {
  const [focused, setFocused] = useState(false);

  const handleChange = useCallback((isVisible: boolean) => {
    setFocused(isVisible);
  }, []);

  const backgroundColor = focused ? 'red' : '#ccc';

  return (
    <InViewport
      threshold={0.7} // This component will be considered visible if it is 70% visible (Default is 1 that means 100%)  
      delay={100} // This component will check if it is visible every 200ms (default is 1000ms or 1 second) )
      onChange={handleChange}
      style={[styles.section, { backgroundColor }]}
    >
      <Text style={focused ? { color: '#fff' } : { color: '#000' }}>
        {focused ? 'Focused' : 'Not focused'}
      </Text>
    </InViewport>
  );
};

const App = (): React.JSX.Element => {
  return (
    <SafeAreaView style={styles.container}>
      <ScrollView>
        {[...new Array(5)].map((_, k) => (
          <Section key={k} />
        ))}
      </ScrollView>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  section: {
    width: Dimensions.get('screen').width,
    height: Dimensions.get('screen').height,
    justifyContent: 'center',
    alignItems: 'center',
    borderBottomColor: 'blue',
    borderBottomWidth: 4,
  },
});

export default App;

Component props

PropertytypeRequiredDescriptionDefault value
disabledbooleanfalseIf enabled the component will not track visibility statusfalse
delaynumberfalseHow frequent to check visibility status. Not setting lower less than a second will increase frequency of updates leading performance issues especially in old architecture bridge mode1000ms
thresholdnumberfalseA fraction (0 to 1) that is greater than 0 but less or equal to 1, 1 means 100% 0.7 means 70% and so on, The value indicate the minimum percentage of the container to be considered visible1
onChangeFunctiontrueThis is the core and exposed method that listens and updates visibility statusundefined

This component also accepts all properties of the View component like style Check full list of properties here

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with Herbert kavuma

0.1.10

9 months ago

0.1.9

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago