@dargmuesli/nuxt-vio 20.0.0-beta.6 → 20.0.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,12 @@
1
1
  <template>
2
2
  <NuxtTime
3
- v-bind="{ datetime: props.datetime, ...delegatedProps }"
3
+ v-bind="forwardedProps"
4
4
  :locale="props.options.locale || defaultLocale"
5
5
  :time-zone="props.options.timeZone || defaultTimeZone"
6
6
  />
7
7
  </template>
8
8
 
9
9
  <script setup lang="ts">
10
- import { reactiveOmit } from '@vueuse/core'
11
-
12
10
  import type { NuxtTimeProps } from 'nuxt/app'
13
11
 
14
12
  const { locale: defaultLocale } = useI18n()
@@ -27,5 +25,9 @@ const props = withDefaults(
27
25
  },
28
26
  )
29
27
 
30
- const delegatedProps = reactiveOmit(props.options, 'locale', 'timeZone')
28
+ const forwardedProps = computed(() => {
29
+ const { locale, timeZone, ...delegated } = props.options
30
+
31
+ return { datetime: props.datetime, ...delegated }
32
+ })
31
33
  </script>
@@ -0,0 +1,23 @@
1
+ export const useTimeZone = () =>
2
+ useNuxtApp().ssrContext?.event.context.$timeZone ||
3
+ useCookie(TIMEZONE_COOKIE_NAME, {
4
+ httpOnly: false,
5
+ sameSite: 'strict',
6
+ secure: true,
7
+ }).value ||
8
+ (import.meta.client
9
+ ? Intl.DateTimeFormat().resolvedOptions().timeZone
10
+ : undefined)
11
+
12
+ export const useNow = () => useState(STATE_KEY_NOW, () => new Date())
13
+
14
+ export const useFromNow = () => {
15
+ const { locale } = useI18n()
16
+ const now = useNow()
17
+
18
+ const formatter = new Intl.RelativeTimeFormat(locale.value, {
19
+ numeric: 'auto',
20
+ })
21
+
22
+ return (to: Date) => getFromTo(now.value, to, formatter)
23
+ }
package/package.json CHANGED
@@ -114,5 +114,5 @@
114
114
  "start:static": "serve playground/.output/public --ssl-cert ./.config/certificates/ssl.crt --ssl-key ./.config/certificates/ssl.key"
115
115
  },
116
116
  "type": "module",
117
- "version": "20.0.0-beta.6"
117
+ "version": "20.0.0-beta.8"
118
118
  }
@@ -156,6 +156,7 @@ export const POLYFILLS = [
156
156
  ]
157
157
  export const REGEX_UUID =
158
158
  /^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/
159
+ export const STATE_KEY_NOW = 'dateTimeNow'
159
160
  export const TESTING_COOKIE_NAME = 'vio_is-testing'
160
161
  export const TIMEZONE_COOKIE_NAME = [COOKIE_PREFIX, 'tz'].join(COOKIE_SEPARATOR)
161
162
  export const TIMEZONE_HEADER_KEY = `X-${VIO_SITE_NAME}-Timezone`
@@ -116,3 +116,28 @@ export const getEmailDateTimeFormatter = (
116
116
  ...dateTimeFormatOptions,
117
117
  timeZone: 'UTC',
118
118
  })
119
+
120
+ export const getFromTo = (
121
+ from: Date,
122
+ to: Date,
123
+ formatter: Intl.RelativeTimeFormat,
124
+ ) => {
125
+ const difference = (to.getTime() - from.getTime()) / 1000
126
+ const units = [
127
+ { unit: 'year' as const, seconds: 365 * 24 * 3600 },
128
+ { unit: 'month' as const, seconds: 30 * 24 * 3600 },
129
+ { unit: 'week' as const, seconds: 7 * 24 * 3600 },
130
+ { unit: 'day' as const, seconds: 24 * 3600 },
131
+ { unit: 'hour' as const, seconds: 3600 },
132
+ { unit: 'minute' as const, seconds: 60 },
133
+ { unit: 'second' as const, seconds: 1 },
134
+ ]
135
+
136
+ for (const { unit, seconds } of units) {
137
+ const delta = difference / seconds
138
+
139
+ if (Math.abs(delta) >= 1 || unit === 'second') {
140
+ return formatter.format(Math.round(delta), unit)
141
+ }
142
+ }
143
+ }
@@ -1,10 +0,0 @@
1
- export const useTimeZone = () =>
2
- useNuxtApp().ssrContext?.event.context.$timeZone ||
3
- useCookie(TIMEZONE_COOKIE_NAME, {
4
- httpOnly: false,
5
- sameSite: 'strict',
6
- secure: true,
7
- }).value ||
8
- (import.meta.client
9
- ? Intl.DateTimeFormat().resolvedOptions().timeZone
10
- : undefined)
File without changes
File without changes
File without changes