From ad220fdb95b524b451e11bfcddd1f86e768e007d Mon Sep 17 00:00:00 2001 From: Maxwell Barvian Date: Wed, 13 Nov 2024 05:55:09 -0800 Subject: [PATCH] Add for Vue --- .changeset/thin-hotels-chew.md | 5 +++ packages/vue/src/NumberFlowGroup.vue | 38 +++++++++++++++++++ packages/vue/src/group.ts | 9 +++++ packages/vue/src/index.ts | 2 + packages/vue/src/index.vue | 29 ++++---------- .../examples/_Group/vue/Component.vue | 22 +++++++---- site/src/pages/[...framework]/index.mdx | 14 +++++++ 7 files changed, 90 insertions(+), 29 deletions(-) create mode 100644 .changeset/thin-hotels-chew.md create mode 100644 packages/vue/src/NumberFlowGroup.vue create mode 100644 packages/vue/src/group.ts diff --git a/.changeset/thin-hotels-chew.md b/.changeset/thin-hotels-chew.md new file mode 100644 index 00000000..69974f76 --- /dev/null +++ b/.changeset/thin-hotels-chew.md @@ -0,0 +1,5 @@ +--- +'@number-flow/vue': patch +--- + +Add diff --git a/packages/vue/src/NumberFlowGroup.vue b/packages/vue/src/NumberFlowGroup.vue new file mode 100644 index 00000000..5c5c1580 --- /dev/null +++ b/packages/vue/src/NumberFlowGroup.vue @@ -0,0 +1,38 @@ + + diff --git a/packages/vue/src/group.ts b/packages/vue/src/group.ts new file mode 100644 index 00000000..5159be24 --- /dev/null +++ b/packages/vue/src/group.ts @@ -0,0 +1,9 @@ +import type { NumberFlowLite, partitionParts } from 'number-flow' +import type { InjectionKey, Ref, ComputedRef, MaybeRefOrGetter } from 'vue' + +export type RegisterWithGroup = ( + el: Ref, + parts: ComputedRef> +) => void + +export const key = Symbol() as InjectionKey diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index e3567b96..09d96b5d 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -6,6 +6,8 @@ import { prefersReducedMotion } from 'number-flow' +export { default as NumberFlowGroup } from './NumberFlowGroup.vue' + export type { Value, Format, Trend } from 'number-flow' export { NumberFlowElement } diff --git a/packages/vue/src/index.vue b/packages/vue/src/index.vue index 46fd2620..38fada08 100644 --- a/packages/vue/src/index.vue +++ b/packages/vue/src/index.vue @@ -7,7 +7,8 @@ import { NumberFlowLite, type Props as NumberFlowProps } from 'number-flow' -import { computed, ref } from 'vue' +import { computed, inject, ref, watch } from 'vue' +import { key as groupKey } from './group' type Props = Partial & { locales?: Intl.LocalesArgument @@ -18,7 +19,7 @@ type Props = Partial & { // This is repetitive but I couldn't get it any cleaner using `withDefaults`, // because then you can't destructure, -// and if you don't set defaults Vue will use its own for i.e. booleans. +// and if you don't set defaults Vue will use its own for e.g. booleans. const { locales, format, @@ -41,7 +42,6 @@ defineOptions({ inheritAttrs: false // set them manually to ensure `parts` updates last }) -// Technically the original animationsstart still emits but ah well: const emit = defineEmits<{ (e: 'animationsstart'): void (e: 'animationsfinish'): void @@ -50,32 +50,19 @@ const emit = defineEmits<{ // You're supposed to cache these between uses: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString const formatter = computed(() => new Intl.NumberFormat(locales, format)) - const parts = computed(() => partitionParts(value, formatter.value)) -// Pre effects don't seem to batch to handle grouping like React: -// https://discord.com/channels/325477692906536972/1299071468533055541 -// -// watch( -// [parts, () => isolate], -// ([_, isolate]) => { -// if (!isolate) el.value?.willUpdate() -// }, -// { flush: 'pre' } -// ) -// watch( -// [parts, () => isolate], -// ([_, isolate]) => { -// if (!isolate) el.value?.didUpdate() -// }, -// { flush: 'post' } -// ) +// Handle grouping. Keep as much logic in NumberFlowGroup.vue as possible +// for better tree-shaking: +const register = inject(groupKey, undefined) +register?.(el, parts)