Skip to content

Commit 1baf323

Browse files
committed
✨ ajout des customElements
1 parent 6368265 commit 1baf323

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

resources/js/elements/Confetti.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import confetti from 'canvas-confetti'
2+
import { windowHeight } from '@helpers/window.js'
3+
4+
export class Confetti extends HTMLElement {
5+
connectedCallback () {
6+
const rect = this.getBoundingClientRect()
7+
const y = (rect.top + rect.height / 2) / windowHeight()
8+
confetti({
9+
particleCount: 100,
10+
zIndex: 3000,
11+
spread: 90,
12+
disableForReducedMotion: true,
13+
origin: { y }
14+
})
15+
}
16+
}

resources/js/elements/TimeAgo.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const terms = [
2+
{
3+
time: 45,
4+
divide: 60,
5+
text: "moins d'une minute"
6+
},
7+
{
8+
time: 90,
9+
divide: 60,
10+
text: 'environ une minute'
11+
},
12+
{
13+
time: 45 * 60,
14+
divide: 60,
15+
text: '%d minutes'
16+
},
17+
{
18+
time: 90 * 60,
19+
divide: 60 * 60,
20+
text: 'environ une heure'
21+
},
22+
{
23+
time: 24 * 60 * 60,
24+
divide: 60 * 60,
25+
text: '%d heures'
26+
},
27+
{
28+
time: 42 * 60 * 60,
29+
divide: 24 * 60 * 60,
30+
text: 'environ un jour'
31+
},
32+
{
33+
time: 30 * 24 * 60 * 60,
34+
divide: 24 * 60 * 60,
35+
text: '%d jours'
36+
},
37+
{
38+
time: 45 * 24 * 60 * 60,
39+
divide: 24 * 60 * 60 * 30,
40+
text: 'environ un mois'
41+
},
42+
{
43+
time: 365 * 24 * 60 * 60,
44+
divide: 24 * 60 * 60 * 30,
45+
text: '%d mois'
46+
},
47+
{
48+
time: 365 * 1.5 * 24 * 60 * 60,
49+
divide: 24 * 60 * 60 * 365,
50+
text: 'environ un an'
51+
},
52+
{
53+
time: Infinity,
54+
divide: 24 * 60 * 60 * 365,
55+
text: '%d ans'
56+
}
57+
]
58+
59+
/**
60+
* Custom element permettant d'afficher une date de manière relative
61+
*
62+
* @property {number} timer
63+
*/
64+
export class TimeAgo extends HTMLElement {
65+
connectedCallback () {
66+
const timestamp = parseInt(this.getAttribute('time'), 10) * 1000
67+
const date = new Date(timestamp)
68+
this.updateText(date)
69+
}
70+
71+
disconnectedCallback () {
72+
window.clearTimeout(this.timer)
73+
}
74+
75+
updateText (date) {
76+
const seconds = (new Date().getTime() - date.getTime()) / 1000
77+
let term = null
78+
const prefix = this.getAttribute('prefix')
79+
for (term of terms) {
80+
if (Math.abs(seconds) < term.time) {
81+
break
82+
}
83+
}
84+
if (seconds >= 0) {
85+
this.innerHTML = `${prefix || 'il y a'} ${term.text.replace('%d', Math.round(seconds / term.divide))}`
86+
} else {
87+
this.innerHTML = `${prefix || 'dans'} ${term.text.replace('%d', Math.round(Math.abs(seconds) / term.divide))}`
88+
}
89+
let nextTick = Math.abs(seconds) % term.divide
90+
if (nextTick === 0) {
91+
nextTick = term.divide
92+
}
93+
if (nextTick > 2147482) {
94+
return
95+
}
96+
this.timer = window.setTimeout(() => {
97+
window.requestAnimationFrame(() => {
98+
this.updateText(date)
99+
})
100+
}, 1000 * nextTick)
101+
}
102+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const DAY = 1000 * 60 * 60 * 24
2+
const HOUR = 1000 * 60 * 60
3+
const MINUTE = 1000 * 60
4+
5+
export class TimeCountdown extends HTMLElement {
6+
connectedCallback () {
7+
const timestamp = parseInt(this.getAttribute('time'), 10) * 1000
8+
const date = new Date(timestamp)
9+
this.updateText(date)
10+
}
11+
12+
disconnectedCallback () {
13+
window.clearTimeout(this.timer)
14+
}
15+
16+
updateText (date) {
17+
const now = new Date().getTime()
18+
const distance = date - now
19+
const days = Math.floor(distance / DAY)
20+
const hours = Math.floor((distance % DAY) / HOUR)
21+
const minutes = Math.floor((distance % HOUR) / MINUTE)
22+
const seconds = Math.floor((distance % MINUTE) / 1000)
23+
if (distance < 0) {
24+
this.innerText = ''
25+
return ''
26+
}
27+
let timeInterval = 1000
28+
if (days > 0) {
29+
this.innerText = `${days}j ${hours}h`
30+
timeInterval = HOUR
31+
} else if (hours > 0) {
32+
this.innerText = `${hours}h ${minutes}m`
33+
timeInterval = MINUTE
34+
} else {
35+
this.innerText = `${minutes}m ${seconds}s`
36+
}
37+
if (distance > 0) {
38+
this.timer = window.setTimeout(() => {
39+
if (window.requestAnimationFrame) {
40+
window.requestAnimationFrame(() => this.updateText(date))
41+
} else {
42+
this.updateText(date)
43+
}
44+
}, timeInterval)
45+
}
46+
}
47+
}

resources/js/elements/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import preactCustomElement from '@helpers/preact.js'
2+
import { Comments } from '@components/Comments.jsx'
3+
import { Confetti } from './Confetti.js'
4+
import { TimeAgo } from './TimeAgo.js'
5+
import { TimeCountdown } from './TimeCountdown.js'
6+
7+
customElements.define('con-fetti', Confetti)
8+
customElements.define('time-ago', TimeAgo)
9+
customElements.define('time-countdown', TimeCountdown)
10+
preactCustomElement('comments-area', Comments, ['target'])

0 commit comments

Comments
 (0)