Skip to content

Commit e0cd606

Browse files
authored
Merge pull request #5 from marcreichel/magic-function
✨ Add magic function
2 parents 4128597 + 76fecf5 commit e0cd606

7 files changed

+72
-7
lines changed

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ window.Alpine.start();
5555

5656
## 🪄 Usage
5757

58+
### Directive
59+
5860
To convert a Date to the human-readable distance from now, add the `x-data` and `x-timeago` directives to an element and
5961
pass the date (as a `Date` or a string in ISO format) to the `x-timeago` directive. The directive will update the output
6062
every 30 seconds.
@@ -66,22 +68,52 @@ every 30 seconds.
6668
Under the hood the directive is using [`formatDistanceToNow`](https://date-fns.org/v2.28.0/docs/formatDistanceToNow)
6769
from `date-fns`.
6870

69-
### No suffix
71+
#### No suffix
7072

7173
If you do not want the "[diff] ago" suffix or "in [diff]" prefix, you can use the `x-timeago.pure` modifier.
7274

7375
```html
7476
<span x-data="{ date: new Date() }" x-timeago.pure="date"></span>
7577
```
7678

77-
### Include seconds
79+
#### Include seconds
7880

7981
Distances less than a minute are more detailed.
8082

8183
```html
8284
<span x-data="{ date: new Date() }" x-timeago.seconds="date"></span>
8385
```
8486

87+
### Magic function
88+
89+
As of version 1.3.0 of this package a `$timeago` magic function is included which will return the human-readable
90+
distance from now.
91+
92+
```html
93+
<span x-data="{ date: new Date() }" x-text="$timeago(date)"></span>
94+
```
95+
96+
> **Note**: Using the magic function the distance does not get updated automatically. You have to update it yourself if
97+
> you want to.
98+
99+
#### No suffix
100+
101+
If you do not want the "[diff] ago" suffix or "in [diff]" prefix, you can provide `true` as the second parameter to the
102+
function.
103+
104+
```html
105+
<span x-data="{ date: new Date() }" x-text="$timeago(date, true)"></span>
106+
```
107+
108+
#### Include seconds
109+
110+
If you want distances less than a minute to be more detailed, you can provide `true` as the third parameter to the
111+
function.
112+
113+
```html
114+
<span x-data="{ date: new Date() }" x-text="$timeago(date, null, true)"></span>
115+
```
116+
85117
### Other locales
86118

87119
If you are using the `npm` installation method for this package or the ESM distribution, you can use the

dist/alpine-timeago.js

+16-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/alpine-timeago.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/alpine-timeago.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/alpine-timeago.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515
<div x-data="{ date: new Date() }" x-timeago.pure="date"></div>
1616
<div x-data="{ date: new Date() }" x-timeago.seconds="date"></div>
1717
<div x-data="{ date: new Date() }" x-timeago.pure.seconds="date"></div>
18+
<div x-data="{ date: new Date() }" x-text="$timeago(date)"></div>
19+
<div x-data="{ date: new Date() }" x-text="$timeago(date, true)"></div>
20+
<div x-data="{ date: new Date() }" x-text="$timeago(date, null, true)"></div>
21+
<div x-data="{ date: new Date() }" x-text="$timeago(date, true, true)"></div>
1822
</body>
1923
</html>

src/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { differenceInSeconds, formatDistanceToNow, parseISO } from 'date-fns';
1+
import { formatDistanceToNow, parseISO } from 'date-fns';
22

33
let locale = null;
44

@@ -45,6 +45,20 @@ function TimeAgo(Alpine) {
4545

4646
cleanup(() => clearInterval(interval));
4747
});
48+
49+
Alpine.magic('timeago', el => (expression, pure, seconds) => {
50+
if (pure == null) {
51+
pure = false;
52+
}
53+
if (seconds == null) {
54+
seconds = false;
55+
}
56+
return formatDistanceToNow(expression, {
57+
addSuffix: !pure,
58+
includeSeconds: seconds,
59+
locale,
60+
})
61+
});
4862
}
4963

5064
TimeAgo.configure = (config) => {

0 commit comments

Comments
 (0)