Skip to content

Instantly share code, notes, and snippets.

@monsat
Last active November 21, 2023 10:52
Show Gist options
  • Save monsat/e9e0589fd3cc3c749e38e09392b921f5 to your computer and use it in GitHub Desktop.
Save monsat/e9e0589fd3cc3c749e38e09392b921f5 to your computer and use it in GitHub Desktop.
The TimeLimit component is a versatile Vue.js component designed to display content only within a specified time range. This component is particularly useful in scenarios where you need to show or hide elements based on time constraints, such as limited-time offers, event announcements, or scheduled content.
<script setup lang="ts">
const props = defineProps<{
startAt?: Date | number
endAt?: Date | number
now?: Date | number
}>()
const isBetween = computed(() => {
const now = props.now ?? Date.now()
const startAt = props.startAt ?? now
const endAt = props.endAt ?? now
return now >= startAt && now <= endAt
})
</script>
<template>
<slot v-if="isBetween"></slot>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment