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.
$ 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)
In your view:
<p>
Stimulus 1.0.0 was released
<time data-controller="timeago" data-timeago-datetime="2018-01-30 09:00"></time>.
</p>
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.
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.
$ yarn install
$ yarn dev
Prettier and ESLint are responsible to lint and format this component:
$ yarn lint
$ yarn format
Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.
This project is released under the MIT license.