Skip to content

A Stimulus controller that returns the distance between the given date and now in words.

License

Notifications You must be signed in to change notification settings

marcoroth/stimulus-timeago

 
 

Repository files navigation

Stimulus Timeago

Netlify Status

Getting started

A Stimulus controller that returns the distance between the given date and now in words.

This controller is using date-fns/formatDistanceToNow behind the scene.

Installation

$ yarn add stimulus-timeago

And use it in your JS file:

import { Application } from "stimulus"
import Timeago from "stimulus-timeago"

const application = Application.start()
application.register("timeago", Timeago)

Usage

In your view:

<p>
  Stimulus 1.0.0 was released
  <time data-controller="timeago" data-timeago-datetime="2018-01-30 09:00"></time>.
</p>

Configuration

Attribute Default Description Optional
data-timeago-datetime undefined String that can be parsed by Date.parse().
data-timeago-refresh-interval undefined Interval in milliseconds to reload the distance.
data-timeago-include-seconds false Distances less than a minute are more detailed
data-timeago-add-suffix false Result specifies if now is earlier or later than the passed date

includeSeconds and addSuffix are the options of the date-fns/formatDistanceToNow method.

Extending Controller

You can use inheritance to extend the functionality of any Stimulus components.

import Timeago from "stimulus-timeago"

export default class extends Timeago {
  connect() {
    super.connect()
    console.log("Do what you want here.")
  }
}

These controllers will automatically have access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.

Development

Project setup

$ yarn install
$ yarn dev

Linter

Prettier and ESLint are responsible to lint and format this component:

$ yarn lint
$ yarn format

Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

License

This project is released under the MIT license.

About

A Stimulus controller that returns the distance between the given date and now in words.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 75.0%
  • JavaScript 25.0%