All Projects → jkomyno → react-native-user-inactivity

jkomyno / react-native-user-inactivity

Licence: MIT License
Simple component that alerts when the user is inactive (i.e. when the App surface hasn't been touched for X ms)

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to react-native-user-inactivity

react-native-component-splitter
VS Code Extension allows splitting React Native Component into Sub-Component
Stars: ✭ 33 (-77.7%)
Mutual labels:  react-native-component
react-native-ios-modal
A react-native component for displaying a modal on iOS by natively wrapping a react-native view inside a UIViewController and presenting it.
Stars: ✭ 94 (-36.49%)
Mutual labels:  react-native-component
react-native-swipe-action-list
A list view that supports swipe actions for React Native (Android & iOS).
Stars: ✭ 18 (-87.84%)
Mutual labels:  react-native-component
react-native-vector-image
iOS/Android native vector assets generated from SVG
Stars: ✭ 224 (+51.35%)
Mutual labels:  react-native-component
react-native-select-pro
React Native dropdown (select) component developed by Mobile Reality
Stars: ✭ 79 (-46.62%)
Mutual labels:  react-native-component
react-native-uiw
A UI component library based on React Native (Android & iOS).
Stars: ✭ 28 (-81.08%)
Mutual labels:  react-native-component
React Native Responsive Dimensions
Resposive fontSize, height and width for react-native components, that automatically adjusts itself based on screen-size of the device.
Stars: ✭ 243 (+64.19%)
Mutual labels:  react-native-component
react-native-touchable-safe
A single easy-to-use `<Touchable>` component, which harnesses the power of all React Native's `Touchable*` components.
Stars: ✭ 15 (-89.86%)
Mutual labels:  react-native-component
react-native-chat-ui
Actively maintained, community-driven chat UI implementation with an optional Firebase BaaS.
Stars: ✭ 168 (+13.51%)
Mutual labels:  react-native-component
react-native-card-list
A React Native component which displays a list of image cards that zoom to fullscreen
Stars: ✭ 19 (-87.16%)
Mutual labels:  react-native-component
react-native-multi-selectbox
Platform independent (Android / iOS) Selectbox | Picker | Multi-select | Multi-picker. The idea is to bring out the common user interface & user experience on both platforms.
Stars: ✭ 169 (+14.19%)
Mutual labels:  react-native-component
react-native-image-blur-shadow
A React Native Image component with Blur Drop Shadows,100% JavaScript, 0 dependency component. Supports Android, iOS and Web. A light weight <Image/> component for your react native project.
Stars: ✭ 80 (-45.95%)
Mutual labels:  react-native-component
react-native-radio-buttons-group
Simple, best and easy to use radio buttons for react native apps.
Stars: ✭ 145 (-2.03%)
Mutual labels:  react-native-component
react-native-android-notification-listener
React Native Android Notification Listener - Listen for status bar notifications from all applications
Stars: ✭ 87 (-41.22%)
Mutual labels:  react-native-component
react-native-masonry-brick-list
Staggered Or Masonary List View For React Native Written in pure js
Stars: ✭ 24 (-83.78%)
Mutual labels:  react-native-component
react-native-touchable
React-Native button helper library
Stars: ✭ 46 (-68.92%)
Mutual labels:  react-native-component
react-native-carousel-component
React Native Carousel Component for IOS & Android
Stars: ✭ 61 (-58.78%)
Mutual labels:  react-native-component
iconic-input
Beautiful Input components for React Native... <IconicTextbox/> and much more!
Stars: ✭ 22 (-85.14%)
Mutual labels:  react-native-component
react-native-bubble-tabbar
🧼 Bubble Tab Bar Component for React Native which supports React Navigation V5 and TypeScript
Stars: ✭ 43 (-70.95%)
Mutual labels:  react-native-component
react-native-window-guard
SafeAreaView alternative for React Native which provides relevant window insets for both iOS and Android.
Stars: ✭ 30 (-79.73%)
Mutual labels:  react-native-component

react-native-user-inactivity

Version Documentation Maintenance License: MIT

Functional React Native component that notifies when the user stops interacting with the mobile screen for a given amount of time.

As of version 1.1.0, react-native-user-inactivity resets the timer also when the keyboard appears or disappears. If you want to avoid this behaviour, you can set the skipKeyboard property to true.

As of version 1.0.0, react-native-user-inactivity has been rebuilt as a functional component that uses the new React Hook API. Thanks to usetimeout-react-hook, react-native-user-inactivity supports timers different than the standard one (setTimeout). This has solved some of the most recurrent issues, such as #12, #16, #17.

Install

npm install react-native-user-inactivity

If you are running a version of react < 17 you'll need to include the --legacy-peer-deps flag.

npm install react-native-user-inactivity --legacy-peer-deps

🔑 Key features

  • 🥇 supports generic timers (you're no longer constrained to setTimeout)
  • ⚠️ optional reset capability of the timer
  • super elastic behaviour thanks to the Hooks API
  • 💪 written in TypeScript
  • ✔️ the core logic of this component is delegated to usetimeout-react-hook, which has 100% code coverage

How to use

This package primarily exposes a single functional component, UserInactivity. The signature of the UserInactivity React props is the following:

interface UserInactivityProps<T = unknown> {
  /**
   * Number of milliseconds after which the view is considered inactive.
   * If it changed, the timer restarts and the view is considered active until
   * the new timer expires.
   * It defaults to 1000.
   */
  timeForInactivity?: number;

  /**
   * If it's explicitly set to `true` after the component has already been initialized,
   * the timer restarts and the view is considered active until the new timer expires.
   * It defaults to true.
   */
  isActive?: boolean;

  /**
   * Generic usetimeout-react-hook's TimeoutHandler implementation.
   * It defaults to the standard setTimeout/clearTimeout implementation.
   * See https://github.com/jkomyno/usetimeout-react-hook/#-how-to-use.
   */
  timeoutHandler?: TimeoutHandler<T>;

  /**
   * Children components to embed inside UserInactivity's View.
   * If any children component is pressed, `onAction` is called after
   * `timeForInactivity` milliseconds.
   */
  children: React.ReactNode;

  /**
   * If set to true, the timer is not reset when the keyboard appears
   * or disappears.
   */
  skipKeyboard?: boolean;

  /**
   * Optional custom style for UserInactivity's View.
   * It defaults to { flex: 1 }.
   */
  style?: StyleProp<ViewStyle>;

  /**
   * Callback triggered anytime UserInactivity's View isn't touched for more than
   * `timeForInactivity` seconds.
   * It's `active` argument is true if and only if the View wasn't touched for more
   * than `timeForInactivity` milliseconds.
   */
  onAction: (active: boolean) => void;
}

When a native timer is needed (in order to avoid issues such as #12, #16, #17) an implementation of usetimeout-react-hook's TimeoutHandler should be passed to the timeoutHandler prop. A default one (BackgroundTimer) is optionally provided: in order to use it you must:

  • manually run: npm i -S react-native-background-timer
  • manually link the native library: react-native link react-native-background-timer

In case of doubts, please refer to the official react-native-background-timer repository.

The default BackgroundTimer can be used like this:

import UserInactivity from 'react-native-user-inactivity';
import BackgroundTimer from 'react-native-user-inactivity/lib/BackgroundTimer';

export default () => {
  return (
    <UserInactivity
      timeForInactivity={2000}
      timeoutHandler={BackgroundTimer}
      onAction={isActive => { console.log(isActive); }}
      style={{ flex: 1, paddingTop: '10%' }}
    >
  );
}

Warning: it seems that react-native-background-timer doesn't work properly with Android 10+ (#41). I'm currently unable to reproduce the problem, but help from the open-source community on this matter is certainly appreciated.

Typings

Since the component itself is written in TypeScript, your editor's intellisense system should automagically detect the typings file (even if you're using plain JS!), thus providing a better developer experience. In fact, autocomplete capabilities and warning should come for free as you're typing the props to pass to the UserInactivity component.

💪 Practical Example

import React, { useState } from 'react';
import { View, Text, TextInput, Button } from 'react-native';
import UserInactivity from 'react-native-user-inactivity';

export default () => {
  const [active, setActive] = useState(true);
  const [timer, setTimer] = useState(2000);

  return (
    <View style={{ flex: 1 }}>
      <UserInactivity
        isActive={active}
        timeForInactivity={timer}
        onAction={isActive => { setActive(isActive); }}
        style={{ flex: 1, paddingTop: '10%' }}
      >
        <Button id="btn-1" title="1 Press this to simulate activity" />
        <Button id="btn-2" title="2 Press this to simulate activity" />
        <Text id="text-1" style={{ textAlign: 'center' }}>Type below to simulate activity</Text>
        <TextInput
          id="text-input-1"
          style={{height: 40, borderColor: 'gray', borderWidth: 1}}
          onChange={() => { setActive(true); }}
          textContentType="creditCardNumber"
          value={timer.toString(10)}
          onChangeText={text => setTimer(Number.parseInt(text || 0, 10))}
        />
      </UserInactivity>
      <View style={{ flex: 3, backgroundColor: '#fcfcaa', }}>
        <Text style={{ textAlign: 'center' }}>{active ? 'ACTIVE' : 'NOT ACTIVE'}</Text>
        <Button title="Manually set to Active" onPress={() => { setActive(true); }} />
      </View>
    </View>
  );
}

Also, please checkout the example on Snack/Expo.


🚀 Build package

This package is built using TypeScript, so the source needs to be converted in JavaScript before being usable by the users. This can be achieved using TypeScript directly:

npm run build

👤 Author

Alberto Schiabel

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. The code is short, throughly commented and well tested, so you should feel quite comfortable working on it. If you have any doubt or suggestion, please open an issue.

⚠️ Issues

Chances are the problem you have bumped into have already been discussed and solved in the past. Please take a look at the issues (both the closed ones and the comments to the open ones) before opening a new issue. Unfortunately, at the moment I'm not able to offer fast support, because I am a student worker and React Native is no longer part of my main tech stack.

🦄 Show your support

Give a ⭐️ if this project helped or inspired you! In the future, I might consider offering premium support to Github Sponsors.

📝 License

Built with ❤️ by Alberto Schiabel.
This project is MIT licensed.

Related packages

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].