Skip to content

Commit

Permalink
refactor: Meteors logic ( use Array.from() instead of new Array)
Browse files Browse the repository at this point in the history
  • Loading branch information
selemondev committed Oct 8, 2024
1 parent d59df38 commit 0c7948a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 8 additions & 5 deletions docs/src/components/spark-ui/meteors/Meteors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ const props = withDefaults(defineProps<MeteorsProps>(), {
const meteorStyles = ref()
const getRandomPosition = (max: number) => `${Math.floor(Math.random() * max)}px`
const getRandomDelay = () => `${(Math.random() * 1 + 0.2).toFixed(2)}s`
const getRandomDuration = () => `${Math.floor(Math.random() * 8 + 2)}s`
watch(() => props.number, (val) => {
const styles = [...Array.of(val)].map(() => ({
const styles = Array.from({ length: val }, () => ({
top: -5,
left: `${Math.floor(Math.random() * window.innerWidth)}px`,
animationDelay: `${Math.random() * 1 + 0.2}s`,
animationDuration: `${Math.floor(Math.random() * 8 + 2)}s`,
left: getRandomPosition(window.innerWidth),
animationDelay: getRandomDelay(),
animationDuration: getRandomDuration(),
}))
meteorStyles.value = styles
}, {
deep: true,
immediate: true,
})
</script>
Expand Down
14 changes: 9 additions & 5 deletions docs/src/example/meteors/Meteors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ const props = withDefaults(defineProps<MeteorsProps>(), {
const meteorStyles = ref()
const getRandomPosition = (max: number) => `${Math.floor(Math.random() * max)}px`
const getRandomDelay = () => `${(Math.random() * 1 + 0.2).toFixed(2)}s`
const getRandomDuration = () => `${Math.floor(Math.random() * 8 + 2)}s`
watch(() => props.number, (val) => {
const styles = [...Array.of(val)].map(() => ({
const styles = Array.from({ length: val }, () => ({
top: -5,
left: `${Math.floor(Math.random() * window.innerWidth)}px`,
animationDelay: `${Math.random() * 1 + 0.2}s`,
animationDuration: `${Math.floor(Math.random() * 8 + 2)}s`,
left: getRandomPosition(window.innerWidth),
animationDelay: getRandomDelay(),
animationDuration: getRandomDuration(),
}))
meteorStyles.value = styles
}, {
deep: true,
immediate: true,
})
</script>
Expand Down Expand Up @@ -50,6 +53,7 @@ watch(() => props.number, (val) => {
70% {
opacity: 1;
}
100% {
transform: rotate(215deg) translateX(-500px);
opacity: 0;
Expand Down

0 comments on commit 0c7948a

Please sign in to comment.