Skip to content

Commit 8c39437

Browse files
authored
Merge pull request #1 from invoate/develop
Adds support for the .bind modifier
2 parents 85ff114 + 5c4fcd6 commit 8c39437

File tree

8 files changed

+105
-24
lines changed

8 files changed

+105
-24
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.env.json
3+
.idea

README.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ This is a simple plugin for [AlpineJS](https://alpinejs.dev) that wraps the popu
44

55
## Installation
66

7-
This package requires AlpineJS to already be installed, you can install this package via npm:
7+
You can install this package via NPM:
88

9-
```bash
9+
```shell
1010
npm install @invoate/alpine-daysjs
1111
```
1212

13+
or by CDN:
14+
15+
```html
16+
<!-- Plugin -->
17+
<script src="https://unpkg.com/@invoate/alpine-dayjs/dist/alpine-dayjs.min.js" defer></script>
18+
<!-- AlpineJS -->
19+
<script src="https://unpkg.com/alpinejs/dist/cdn.min.js" defer></script>
20+
```
21+
1322
### Setup
1423

1524
First you must register the plugin with Alpine.
@@ -18,22 +27,10 @@ First you must register the plugin with Alpine.
1827
import Alpine from "alpinejs"
1928
import alpineDayJS from "alpine-dayjs"
2029

21-
Alpine.plugin(alpineDayJS({
22-
// options
23-
}))
24-
25-
//
30+
Alpine.plugin(alpineDayJS({}))
2631

2732
Alpine.start()
2833
```
29-
30-
The package comes with sensible defaults however you may wish to configure them.
31-
32-
| Argument | Description | Default |
33-
|---------------|----------------------------------------------------------|------------|
34-
| dayjs | Pass your own instance of DayJS | na |
35-
| defaultFormat | The default format dates should be formatted as, default | DD/MM/YYYY |
36-
3734
### Usage
3835

3936
In your views, you can now use the x-dayjs directive.
@@ -47,6 +44,38 @@ In your views, you can now use the x-dayjs directive.
4744

4845
<!-- Displays the current date using the format YYYY-MM-DD -->
4946
<time x-data x-dayjs x-dayjs-format="YYYY-MM-DD"></time>
47+
48+
<!-- Use a AlpineJS property with the .bind modifier -->
49+
<div x-data="{ date: '2000-01-01' }">
50+
<time x-dayjs.bind="date"></time>
51+
</div>
52+
```
53+
54+
### Options
55+
56+
The package comes with sensible defaults however you may wish to configure them.
57+
58+
| Argument | Description | Default |
59+
|---------------|----------------------------------------------------------|------------|
60+
| dayjs | Pass your own instance of DayJS | na |
61+
| defaultFormat | The default format dates should be formatted as, default | DD/MM/YYYY |
62+
63+
```js
64+
import dayjs from "dayjs"
65+
import localeEs from "dayjs/locale/es"
66+
import localizedFormat from "dayjs/plugin/localizedFormat"
67+
import Alpine from "alpinejs"
68+
import alpineDayJS from "alpine-dayjs"
69+
70+
dayjs.extend(localizedFormat)
71+
dayjs.locale(localeEs)
72+
73+
Alpine.plugin(alpineDayJS({
74+
dayjs: dayjs // A custom DayJS instance with the LocalizedFormat plugin and Spanish Locale
75+
defaultFormat: 'LLL' // LocalizedFormat and Spanish Locale = D [de] MMMM [de] YYYY H:mm
76+
}))
77+
78+
Alpine.start()
5079
```
5180

5281
## Credits

dist/alpine-dayjs.cjs.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,25 @@ var options = {
250250
dayjs: import_dayjs.default,
251251
defaultFormat: "DD/MM/YYYY"
252252
};
253-
var dayjsDirective = (el, { expression }) => {
254-
const date = expression ? options.dayjs(expression) : options.dayjs();
253+
var updateElement = function(el, date) {
255254
const format = el.getAttribute("x-dayjs-format") || options.defaultFormat;
256255
const formattedDate = date.format(format);
257256
el.setAttribute("datetime", date.toISOString());
258257
el.textContent = formattedDate;
259258
};
259+
var dayjsDirective = (el, { modifiers, expression }, { evaluateLater, effect }) => {
260+
const isBound = modifiers.includes("bind");
261+
if (isBound) {
262+
let getUpdatedValue = evaluateLater(expression);
263+
effect(() => {
264+
getUpdatedValue((value) => {
265+
updateElement(el, options.dayjs(value));
266+
});
267+
});
268+
} else {
269+
updateElement(el, options.dayjs(expression));
270+
}
271+
};
260272
var dayjsPlugin = function(supplied = {}) {
261273
options = { ...options, ...supplied };
262274
return function(Alpine) {

dist/alpine-dayjs.esm.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,25 @@ var options = {
238238
dayjs: import_dayjs.default,
239239
defaultFormat: "DD/MM/YYYY"
240240
};
241-
var dayjsDirective = (el, { expression }) => {
242-
const date = expression ? options.dayjs(expression) : options.dayjs();
241+
var updateElement = function(el, date) {
243242
const format = el.getAttribute("x-dayjs-format") || options.defaultFormat;
244243
const formattedDate = date.format(format);
245244
el.setAttribute("datetime", date.toISOString());
246245
el.textContent = formattedDate;
247246
};
247+
var dayjsDirective = (el, { modifiers, expression }, { evaluateLater, effect }) => {
248+
const isBound = modifiers.includes("bind");
249+
if (isBound) {
250+
let getUpdatedValue = evaluateLater(expression);
251+
effect(() => {
252+
getUpdatedValue((value) => {
253+
updateElement(el, options.dayjs(value));
254+
});
255+
});
256+
} else {
257+
updateElement(el, options.dayjs(expression));
258+
}
259+
};
248260
var dayjsPlugin = function(supplied = {}) {
249261
options = { ...options, ...supplied };
250262
return function(Alpine) {

dist/alpine-dayjs.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)