Giter VIP home page Giter VIP logo

react-flip-clock-countdown's Introduction

react-flip-clock-countdown

A 3D animated countdown component for React.

NPM JavaScript Style Guide

react flip clock countdown demo

Install

npm install --save @leenguyen/react-flip-clock-countdown

Or

yarn add @leenguyen/react-flip-clock-countdown

Props

The FlipClockCountdown has all properties of div and additional props below

Name Type Required Default Description
to Date|string|number yes Date or timestamp in the future.
containerProps object no undefined Props apply to the flip clock container. This prop is deprecated, you should apply directly to the FlipClockCountdown component.
onComplete func no Callback when countdown ends
Signature:
function() => void
onTick func no Callback on every interval tick
Signature:
function({ timeDelta, completed }) => void
renderMap Array<boolean> no [true, true, true, true] Each element represents the render state of each section (day, hour, minute, second). If true section will be rendered, false otherwise.
labels Array<string> no ['Days', 'Hours', 'Minutes', 'Seconds'] Custom array of labels used to represent information for each section (day, hour, minute, second).
showLabels boolean no true Set it to false if you don't want to show the labels.
showSeparators boolean no true Set it to false if you don't want to show the separators (colon) between time unit.
labelStyle React.CSSProperties no undefined The styles apply to labels font-size, color, width, height, etc.
digitBlockStyle React.CSSProperties no undefined The styles apply to digit blocks like font-size, color, width, height, etc.
separatorStyle object no undefined The styles apply to separator (colon), includes size and color.
dividerStyle object no undefined The style will be applied to divider, includes color and height.
duration number no 0.7 Duration (in second) when flip card. Valid value in range (0, 1).
hideOnComplete boolean no true By befault, the countdown will be hidden when it completed (or show children if provided). This will keep the timer in place and stuck at zeros when the countdown is completed.

to

The to prop can be a Date object, string, or timestamp in the future. This date is compared with the current date.

Valid values can be (and more):

  • '2022-02-08T14:27:32.635Z' // Date time string format
  • 1644330452635 // Timestamp in milliseconds
  • new Date(1644330452635) // Date object

className

Class names applied to flip clock container element. Use it to custom flip-clock's styles. See example

containerProps

All props of div

children

This component also considers the child that may live within the <FlipClockCountdown></FlipClockCountdown> element, which, in case it's available, replaces the countdown's component state once it's complete. See example.

onComplete

Callback when countdown ends.

function() => void

See example.

onTick

Callback on every interval tick.

function({ timeDelta, completed }) => void

  • timeDelta: { total: number, days: number, hours: number, minutes: number, seconds: number} - the remaining time in formatted.
  • completed: boolean - countdown's state.

Usage

Basic usage

import React, { Component } from 'react';

import FlipClockCountdown from '@leenguyen/react-flip-clock-countdown';
import '@leenguyen/react-flip-clock-countdown/dist/index.css';

class Example extends Component {
  render() {
    return <FlipClockCountdown to={new Date().getTime() + 24 * 3600 * 1000 + 5000} />;
  }
}

Render a React Component when countdown is complete

In case you want to change the output of the component, or want to signal that the countdown's work is done, you can do this by either using the onComplete callback or by specifying a React child within <FlipClockCountdown></FlipClockCountdown>, which will only be shown once the countdown is complete.

import React, { Component } from 'react';

import FlipClockCountdown from '@leenguyen/react-flip-clock-countdown';
import '@leenguyen/react-flip-clock-countdown/dist/index.css';

class Completed extends Component {
  render() {
    return <span>The countdown is complete</span>
  }
}

class RenderByUsingReactChild extends Component {
  render() {
    return (
      <FlipClockCountdown to={new Date().getTime() + 24 * 3600 * 1000 + 5000}>
        <Completed />
      </FlipClockCountdown>;
    )
  }
}

class RenderByUsingCallback extends Component {
  constructor(props) {
    super(props);

    this.endTime = new Date().getTime() + 24 * 3600 * 1000 + 5000;
    this.state = {
      isCompleted: false
    }

    this.handleComplete = this.handleComplete.bind(this);
  }

  handleComplete() {
    this.setState({ isCompleted: true });
  }

  render() {
    return (
      <React.Fragment>
        {isCompleted && <Completed />}
        <FlipClockCountdown onComplete={this.handleComplete} to={this.endTime} />
      </React.Fragment>
    )
  }
}

Render a custom countdown

Custom styles

class Example extends Component {
  render() {
    return (
      <FlipClockCountdown
        to={new Date().getTime() + 24 * 3600 * 1000 + 5000}
        labels={['DAYS', 'HOURS', 'MINUTES', 'SECONDS']}
        labelStyle={{ fontSize: 10, fontWeight: 500, textTransform: 'uppercase' }}
        digitBlockStyle={{ width: 40, height: 60, fontSize: 30 }}
        dividerStyle={{ color: 'white', height: 1 }}
        separatorStyle={{ color: 'red', size: '6px' }}
        duration={0.5}
      >
        Finished
      </FlipClockCountdown>
    );
  }
}

Custom styles via css

import 'styles.css';

class Example extends Component {
  render() {
    return <FlipClockCountdown to={new Date().getTime() + 24 * 3600 * 1000 + 5000} className='flip-clock' />;
  }
}
/* styles.css */

.flip-clock {
  --fcc-flip-duration: 0.5s; /* transition duration when flip card */
  --fcc-digit-block-width: 40px; /* width of digit card */
  --fcc-digit-block-height: 60px; /* height of digit card, highly recommend in even number */
  --fcc-digit-font-size: 30px; /* font size of digit */
  --fcc-digit-color: white; /* color of digit */
  --fcc-label-font-size: 10px; /* font size of label */
  --fcc-label-color: #ffffff; /* color of label */
  --fcc-background: black; /* background of digit card */
  --fcc-divider-color: white; /* color of divider */
  --fcc-divider-height: 1px; /* height of divider */
  --fcc-separator-size: 6px; /* size of colon */
  --fcc-separator-color: red; /* color of colon */
}

Custom section to be rendered

In case you don't want to display the date, use renderMap to custom render state of each section

class Example extends Component {
  render() {
    return (
      <FlipClockCountdown to={new Date().getTime() + 24 * 3600 * 1000 + 5000} renderMap={[false, true, true, true]}>
        Finished
      </FlipClockCountdown>
    );
  }
}

Contributing

The package is made up of 2 main folders:

  • /src contains the FlipClockCountdown
  • /examples contains the create-react-app and create-next-app based demo website

To setup and run a local copy:

  1. Clone this repo with https://github.com/sLeeNguyen/react-flip-clock-countdown
  2. Run npm install in the root folder
  3. Run npm install in the examples/react-app folder
  4. In separate terminal windows, run npm start in the root and examples/react-app folders.

When you're done working on your changes, feel free to send PRs with the details and include a screenshot if you've changed anything visually.

License

MIT © leenguyen

react-flip-clock-countdown's People

Contributors

davidbates avatar liquidg3 avatar sleenguyen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-flip-clock-countdown's Issues

Allows countdown for a period of time

In the previous versions (1.4.0 and earlier), the clock has only one option is countdown to a certain time in the future and we cannot trigger the countdown clock manually.

This feature will add a new option that allows:

  • countdown for a period of time
  • trigger the countdown manually

defaultProps warning using React 18

I noticed that we have a warning in the console. If defaultProps is being deprecated it may affect your library.

Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead. at s (webpack-internal:///(sc_client)/./node_modules/@leenguyen/react-flip-clock-countdown/dist/index.js:1:1734) at Countdown at Lazy at div at div at div at div at section at main at InnerLayoutRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:226:11) at RedirectErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:72:9) at RedirectBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:80:11) at NotFoundBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/not-found-boundary.js:59:11) at LoadingBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:330:11) at ErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/error-boundary.js:100:11) at InnerScrollAndFocusHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:139:9) at ScrollAndFocusHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:213:11) at RenderFromTemplateContext (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/render-from-template-context.js:15:44) at Lazy at OuterLayoutRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:339:11) at Lazy at InnerLayoutRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:226:11) at RedirectErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:72:9) at RedirectBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:80:11) at NotFoundBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/not-found-boundary.js:59:11) at LoadingBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:330:11) at ErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/error-boundary.js:100:11) at InnerScrollAndFocusHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:139:9) at ScrollAndFocusHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:213:11) at RenderFromTemplateContext (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/render-from-template-context.js:15:44) at Lazy at OuterLayoutRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:339:11) at Lazy at div at body at html at InnerLayoutRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:226:11) at RedirectErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:72:9) at RedirectBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:80:11) at NotFoundBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/not-found-boundary.js:59:11) at LoadingBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:330:11) at ErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/error-boundary.js:100:11) at InnerScrollAndFocusHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:139:9) at ScrollAndFocusHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:213:11) at RenderFromTemplateContext (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/render-from-template-context.js:15:44) at Lazy at OuterLayoutRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/layout-router.js:339:11) at Lazy at RedirectErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:72:9) at RedirectBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/redirect-boundary.js:80:11) at NotFoundBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/not-found-boundary.js:59:11) at ReactDevOverlay (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/react-dev-overlay/internal/ReactDevOverlay.js:66:9) at HotReload (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/react-dev-overlay/hot-reloader-client.js:277:11) at Router (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/app-router.js:88:11) at ErrorBoundaryHandler (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/error-boundary.js:77:9) at ErrorBoundary (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/error-boundary.js:100:11) at AppRouter (webpack-internal:///(sc_client)/./node_modules/next/dist/client/components/app-router.js:387:13)

className in typescript

Seems className property its not defined, thus giving an error when used on the function.

Type '{ className: string; to: Date; digitBlockStyle: { fontSize: number; fontFamily: string; }; showLabels: false; showSeparators: true; }' is not assignable to type 'IntrinsicAttributes & Pick<FlipClockCountdownProps, "to" | "containerProps" | "labelStyle" | "digitBlockStyle" | "separatorStyle" | "dividerStyle" | "duration"> & InexactPartial<...> & InexactPartial<...>'.
Property 'className' does not exist on type 'IntrinsicAttributes & Pick<FlipClockCountdownProps, "to" | "containerProps" | "labelStyle" | "digitBlockStyle" | "separatorStyle" | "dividerStyle" | "duration"> & InexactPartial<...> & InexactPartial<...>'.

Adding the prop to FlipClockCountdown would fix the issue

Feature: Allow modify border radius of digitBlock

Hi! Thanks for this amazing component. I wonder if you could add some new CSS vars to customize the digit blocks border radius (top and bottom).
I have many webs, which different styles and I want to customize in some of them to be squared, not rounded.

Regarding to #10, you can make all UI elements full customizable so it could be integrated with specific themes in any website. I think it could be cool and give much power to the component.

What do you think @sLeeNguyen ?

Default value for clock

Hi, I want to set a default value for clock before its start running, maybe 00:00:00. Can this version support it.
Thanks!

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.