Coder Social home page Coder Social logo

chuanyu0201 / react-native-animated-stopwatch-timer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rgommezz/react-native-animated-stopwatch-timer

1.0 0.0 0.0 2.79 MB

Stopwatch & Timer component that smoothly animate the digits change, with all layout animations running on the UI thread ⚡

License: MIT License

JavaScript 16.49% TypeScript 83.51%

react-native-animated-stopwatch-timer's Introduction

react-native-animated-stopwatch-timer

npm npm bundle size platforms: ios, android, web license MIT

A React Native Stopwatch/Timer component that empowers reanimated worklets to smoothly animate the digit change. Cross-platform, performant, with all layout animations executed on the UI thread at 60FPS. Compatible with Expo.

Features

  • 🔥 Performant: all digit animations are executed on the UI thread
  • ⚙️ Highly configurable: easily control its behaviour via props, like animation parameters and styles
  • ⌚ Dual mode: use it as a stopwatch or timer
  • 📱 Expo compatible: no need to eject to enjoy this component
  • 🛠️ Type safe: fully written in TS
  • 💻 Snack example: a snack link is provided so you can try it out in your browser

Preview

stopwatch.mp4

Try it out

🧑‍💻 Run the snack example app to see it in action. The source code for the example is under the /example folder.

Installation

npm install react-native-animated-stopwatch-timer

You also need to install react-native-reanimated 2.5.x or higher.

npm install react-native-reanimated

If you are installing reanimated on a bare React Native app, you should also follow these additional installation instructions.

Modes

You can use this component in two different modes:

  • Stopwatch: The timer starts counting up from 0 (default).
  • Timer: The timer starts counting down from a given time. Use the initialTimeInMs prop to activate this mode.

Usage

import { useRef } from 'react';
import StopwatchTimer, {
  StopwatchTimerMethods,
} from 'react-native-animated-stopwatch-timer';

const App = () => {
  const stopwatchTimerRef = useRef<StopwatchTimerMethods>(null);

  // Methods to control the stopwatch
  function play() {
    stopwatchTimerRef.current?.play();
  }

  function pause() {
    stopwatchTimerRef.current?.pause();
  }

  function reset() {
    stopwatchTimerRef.current?.reset();
  }

  return <StopwatchTimer ref={stopwatchTimerRef} />;
};

Props

Name Required Type Description
animationDuration no number The enter/exit animation duration in milliseconds of a digit. Defaults to 80
animationDelay no number The enter/exit animation delay in milliseconds of a digit. Defaults to 0
animationDistance no number The enter/exit animation vertical distance in dp of a digit. Defaults to 120
containerStyle no StyleProp<ViewStyle> The style of the stopwatch/timer View container
digitStyle no StyleProp<TextStyle> Extra style applied to each digit, excluding separators (: and ,). This property is useful if the fontFamily has different widths per digit to avoid an unpleasant fluctuation of the total component width as it runs. Check the example app where this is used on iOS's default San Francisco font, which presents this issue.
initialTimeInMs no number If you want to use it as a timer, set this value
leadingZeros no 1 or 2 The number of zeros for the minutes. Defaults to 1
enterAnimationType no 'slide-in-up' or 'slide-in-down' Whether the new digit should enter from the top or the bottom
separatorStyle no StyleProp<TextStyle> Extra style applied only to separators. In this case, the colon (:) and the comma (,)
onFinish no () => void Callback executed when the timer reaches 0 (only when working in timer mode and initialTimeInMs prop is provided)
textCharStyle no StyleProp<TextStyle> The style applied to each individual character of the stopwatch/timer
trailingZeros no 0, 1 or 2 If 0, the component will only display seconds and minutes. If 1, the component will display seconds, minutes, and a hundredth of ms. If 2, the component will display seconds, minutes, and tens of ms. Defaults to 1

Methods

play: () => void

Starts the stopwatch/timer or resumes it if paused. It has no effect if it's already running.

stopwatchTimerRef.current?.play();

pause: () => number

Pauses the stopwatch/timer. It has no effect if it is either paused or reset. The method returns a snapshot of the time elapsed in ms.

stopwatchTimerRef.current?.pause();

reset: () => void

Resets the stopwatch/timer.

stopwatchTimerRef.current?.reset();

getSnapshot: () => number

Returns the current time elapsed in ms.

stopwatchTimerRef.current?.getSnapshot();

stopwatchTimerRef refers to the ref passed to the StopwatchTimer component.

Contributing

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

License

MIT


Made with create-react-native-library

react-native-animated-stopwatch-timer's People

Contributors

rgommezz avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.