Skip to content

Commit a7d1bcf

Browse files
committed
chore: turn TIme component to runes
1 parent e12dbc2 commit a7d1bcf

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

example/webpack/components/Time.svelte

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
<script>
2-
import { onDestroy } from 'svelte';
2+
let { class: className, ...props} = $props();
33
4-
let className;
5-
export { className as class };
4+
let date = $state(new Date());
5+
let time = $derived(`${date.getHours()}:${twoDigits(date.getMinutes())}:${twoDigits(date.getSeconds())}`);
66
7-
let date = new Date();
8-
const active = true;
9-
$: time = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
7+
function twoDigits(str) {
8+
return str.toLocaleString('en', { minimumIntegerDigits: 2 });
9+
}
1010
11-
const interval = setInterval(() => date = new Date(), 1000);
11+
$effect(() => {
12+
const interval = setInterval(() => {
13+
date = new Date();
14+
}, 1000);
1215
13-
onDestroy(() => {
14-
clearInterval(interval);
15-
})
16+
return () => {
17+
clearInterval(interval);
18+
};
19+
});
1620
</script>
1721

22+
<div
23+
class="time bolder light { true ? 'lighter red' : ''} {className}"
24+
class:bold={true}
25+
{...props}
26+
>{time}</div>
27+
1828
<style module="mixed">
1929
:local(div) {
2030
text-align: right;
@@ -51,17 +61,11 @@
5161
5262
:local(div) *.bold { font-size: 12px;}
5363
54-
.btn {
64+
.time {
5565
float:right;
5666
animation: opacity 4s infinite alternate;
5767
}
5868
59-
@media screen {
60-
.btn {
61-
color: red;
62-
}
63-
}
64-
6569
@keyframes opacity {
6670
0% {
6771
opacity: 0;
@@ -70,8 +74,12 @@
7074
opacity: 1;
7175
}
7276
}
73-
</style>
74-
<div
75-
class="btn bolder light { true ? 'lighter red' : ''}"
76-
class:bold={true}
77-
>{time}</div>
77+
@keyframes opacity2 {
78+
from {
79+
opacity: 0;
80+
}
81+
to {
82+
opacity: 1;
83+
}
84+
}
85+
</style>

0 commit comments

Comments
 (0)