Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/react/src/components/number-flow/examples/with-timer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NumberFlow } from '@ark-ui/react/number-flow'
import { Timer, useTimer } from '@ark-ui/react/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/number-flow.module.css'
import timerStyles from 'styles/timer.module.css'

const formatOptions = { minimumIntegerDigits: 2 }

export const WithTimer = () => {
const timer = useTimer({ countdown: true, startMs: 5 * 60 * 1000 })

return (
<Timer.RootProvider value={timer} className={styles.Card}>
<span className={styles.Label}>Offer ends in</span>
<Timer.Area className={`${timerStyles.Area} ${styles.Display}`}>
<NumberFlow.Root value={timer.time.minutes} formatOptions={formatOptions} trend={-1} className={styles.Root}>
<NumberFlow.Segments />
</NumberFlow.Root>
<Timer.Separator className={timerStyles.Separator}>:</Timer.Separator>
<NumberFlow.Root value={timer.time.seconds} formatOptions={formatOptions} trend={-1} className={styles.Root}>
<NumberFlow.Segments />
</NumberFlow.Root>
</Timer.Area>
<Timer.Control className={styles.Actions}>
<Timer.ActionTrigger className={button.Root} data-variant="solid" action="start">
<PlayIcon /> Start
</Timer.ActionTrigger>
<Timer.ActionTrigger className={button.Root} action="pause">
<PauseIcon /> Pause
</Timer.ActionTrigger>
<Timer.ActionTrigger className={button.Root} action="resume">
<PlayIcon /> Resume
</Timer.ActionTrigger>
<Timer.ActionTrigger className={button.Root} action="reset">
<RotateCcwIcon /> Reset
</Timer.ActionTrigger>
</Timer.Control>
</Timer.RootProvider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { Pricing } from './examples/pricing.tsx'
export { RootProvider } from './examples/root-provider.tsx'
export { Timing } from './examples/timing.tsx'
export { Trend } from './examples/trend.tsx'
export { WithTimer } from './examples/with-timer.tsx'
41 changes: 41 additions & 0 deletions packages/solid/src/components/number-flow/examples/with-timer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NumberFlow } from '@ark-ui/solid/number-flow'
import { Timer, useTimer } from '@ark-ui/solid/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-solid'
import button from 'styles/button.module.css'
import styles from 'styles/number-flow.module.css'
import timerStyles from 'styles/timer.module.css'

const formatOptions = { minimumIntegerDigits: 2 }

export const WithTimer = () => {
const timer = useTimer({ countdown: true, startMs: 5 * 60 * 1000 })

return (
<Timer.RootProvider value={timer} class={styles.Card}>
<span class={styles.Label}>Offer ends in</span>
<Timer.Area class={`${timerStyles.Area} ${styles.Display}`}>
<NumberFlow.Root value={timer().time.minutes} formatOptions={formatOptions} trend={-1} class={styles.Root}>
<NumberFlow.Segments />
</NumberFlow.Root>
<Timer.Separator class={timerStyles.Separator}>:</Timer.Separator>
<NumberFlow.Root value={timer().time.seconds} formatOptions={formatOptions} trend={-1} class={styles.Root}>
<NumberFlow.Segments />
</NumberFlow.Root>
</Timer.Area>
<Timer.Control class={styles.Actions}>
<Timer.ActionTrigger class={button.Root} data-variant="solid" action="start">
<PlayIcon /> Start
</Timer.ActionTrigger>
<Timer.ActionTrigger class={button.Root} action="pause">
<PauseIcon /> Pause
</Timer.ActionTrigger>
<Timer.ActionTrigger class={button.Root} action="resume">
<PlayIcon /> Resume
</Timer.ActionTrigger>
<Timer.ActionTrigger class={button.Root} action="reset">
<RotateCcwIcon /> Reset
</Timer.ActionTrigger>
</Timer.Control>
</Timer.RootProvider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { Pricing } from './examples/pricing.tsx'
export { RootProvider } from './examples/root-provider.tsx'
export { Timing } from './examples/timing.tsx'
export { Trend } from './examples/trend.tsx'
export { WithTimer } from './examples/with-timer.tsx'
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<script lang="ts">
import { NumberFlow } from '@ark-ui/svelte/number-flow'
import { Timer, useTimer } from '@ark-ui/svelte/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-svelte'
import button from 'styles/button.module.css'
import styles from 'styles/number-flow.module.css'
import timerStyles from 'styles/timer.module.css'

const formatOptions = { minimumIntegerDigits: 2 }
const id = $props.id()
const timer = useTimer({ id, countdown: true, startMs: 5 * 60 * 1000 })
</script>

<Timer.RootProvider value={timer} class={styles.Card}>
<span class={styles.Label}>Offer ends in</span>
<Timer.Area class={`${timerStyles.Area} ${styles.Display}`}>
<NumberFlow.Root value={timer().time.minutes} {formatOptions} trend={-1} class={styles.Root}>
<NumberFlow.Segments />
</NumberFlow.Root>
<Timer.Separator class={timerStyles.Separator}>:</Timer.Separator>
<NumberFlow.Root value={timer().time.seconds} {formatOptions} trend={-1} class={styles.Root}>
<NumberFlow.Segments />
</NumberFlow.Root>
</Timer.Area>
<Timer.Control class={styles.Actions}>
<Timer.ActionTrigger class={button.Root} data-variant="solid" action="start">
<PlayIcon /> Start
</Timer.ActionTrigger>
<Timer.ActionTrigger class={button.Root} action="pause">
<PauseIcon /> Pause
</Timer.ActionTrigger>
<Timer.ActionTrigger class={button.Root} action="resume">
<PlayIcon /> Resume
</Timer.ActionTrigger>
<Timer.ActionTrigger class={button.Root} action="reset">
<RotateCcwIcon /> Reset
</Timer.ActionTrigger>
</Timer.Control>
</Timer.RootProvider>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import PricingExample from './examples/pricing.svelte'
import RootProviderExample from './examples/root-provider.svelte'
import TimingExample from './examples/timing.svelte'
import TrendExample from './examples/trend.svelte'
import WithTimerExample from './examples/with-timer.svelte'

const meta = {
title: 'Components / Number Flow',
Expand Down Expand Up @@ -82,3 +83,9 @@ export const Trend = {
Component: TrendExample,
}),
}

export const WithTimer = {
render: () => ({
Component: WithTimerExample,
}),
}
54 changes: 54 additions & 0 deletions packages/vue/src/components/number-flow/examples/with-timer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<script setup lang="ts">
import { NumberFlow } from '@ark-ui/vue/number-flow'
import { Timer, useTimer } from '@ark-ui/vue/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-vue-next'
import button from 'styles/button.module.css'
import styles from 'styles/number-flow.module.css'
import timerStyles from 'styles/timer.module.css'

const formatOptions = { minimumIntegerDigits: 2 }
const timer = useTimer({ countdown: true, startMs: 5 * 60 * 1000 })
</script>

<template>
<Timer.RootProvider :value="timer" :class="styles.Card">
<span :class="styles.Label">Offer ends in</span>
<Timer.Area :class="[timerStyles.Area, styles.Display]">
<NumberFlow.Root
:model-value="timer.time.minutes"
:format-options="formatOptions"
:trend="-1"
:class="styles.Root"
>
<NumberFlow.Segments />
</NumberFlow.Root>
<Timer.Separator :class="timerStyles.Separator">:</Timer.Separator>
<NumberFlow.Root
:model-value="timer.time.seconds"
:format-options="formatOptions"
:trend="-1"
:class="styles.Root"
>
<NumberFlow.Segments />
</NumberFlow.Root>
</Timer.Area>
<Timer.Control :class="styles.Actions">
<Timer.ActionTrigger :class="button.Root" data-variant="solid" action="start">
<PlayIcon />
Start
</Timer.ActionTrigger>
<Timer.ActionTrigger :class="button.Root" action="pause">
<PauseIcon />
Pause
</Timer.ActionTrigger>
<Timer.ActionTrigger :class="button.Root" action="resume">
<PlayIcon />
Resume
</Timer.ActionTrigger>
<Timer.ActionTrigger :class="button.Root" action="reset">
<RotateCcwIcon />
Reset
</Timer.ActionTrigger>
</Timer.Control>
</Timer.RootProvider>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import PricingExample from './examples/pricing.vue'
import RootProviderExample from './examples/root-provider.vue'
import TimingExample from './examples/timing.vue'
import TrendExample from './examples/trend.vue'
import WithTimerExample from './examples/with-timer.vue'

const meta: Meta = {
title: 'Components / Number Flow',
Expand Down Expand Up @@ -94,3 +95,10 @@ export const Trend = {
template: '<Component />',
}),
}

export const WithTimer = {
render: () => ({
components: { Component: WithTimerExample },
template: '<Component />',
}),
}
56 changes: 56 additions & 0 deletions packages/web/examples/number-flow/with-timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { createElement, Pause, Play, RotateCcw } from 'lucide'

import '@ark-ui/web/number-flow'
import '@ark-ui/web/timer'

import button from 'styles/button.module.css'
import styles from 'styles/number-flow.module.css'
import timerStyles from 'styles/timer.module.css'

const icon = (node: typeof Play) => createElement(node, { 'aria-hidden': 'true' }).outerHTML

const flow = (unit: string) => /* html */ `
<ark-number-flow class="${styles.Root}" data-unit="${unit}" trend="-1">
<ark-number-flow-segments></ark-number-flow-segments>
</ark-number-flow>`

export function mount(root: HTMLElement) {
root.innerHTML = /* html */ `
<ark-timer class="${styles.Card}" countdown start-ms="${5 * 60 * 1000}">
<span class="${styles.Label}">Offer ends in</span>
<ark-timer-area class="${timerStyles.Area} ${styles.Display}">
${flow('minutes')}
<ark-timer-separator class="${timerStyles.Separator}">:</ark-timer-separator>
${flow('seconds')}
</ark-timer-area>
<ark-timer-control class="${styles.Actions}">
<ark-timer-action-trigger action="start">
<button class="${button.Root}" data-variant="solid">${icon(Play)} Start</button>
</ark-timer-action-trigger>
<ark-timer-action-trigger action="pause">
<button class="${button.Root}">${icon(Pause)} Pause</button>
</ark-timer-action-trigger>
<ark-timer-action-trigger action="resume">
<button class="${button.Root}">${icon(Play)} Resume</button>
</ark-timer-action-trigger>
<ark-timer-action-trigger action="reset">
<button class="${button.Root}">${icon(RotateCcw)} Reset</button>
</ark-timer-action-trigger>
</ark-timer-control>
</ark-timer>`

const timer = root.querySelector('ark-timer')!
const flows = root.querySelectorAll('ark-number-flow')

for (const el of flows) el.formatOptions = { minimumIntegerDigits: 2 }

const sync = () => {
const time = timer.api?.time
if (!time) return
for (const el of flows) el.value = time[el.dataset.unit as 'minutes' | 'seconds']
}

timer.addEventListener('tick', sync)
root.querySelector('ark-timer-control')!.addEventListener('click', () => queueMicrotask(sync))
timer.ready.then(sync)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { mount as LiveExample } from '../../../examples/number-flow/live.js'
import { mount as LocaleExample } from '../../../examples/number-flow/locale.js'
import { mount as PrefixSuffixExample } from '../../../examples/number-flow/prefix-suffix.js'
import { mount as TimingExample } from '../../../examples/number-flow/timing.js'
import { mount as WithTimerExample } from '../../../examples/number-flow/with-timer.js'

const meta: Meta = { title: 'Components / Number Flow' }
export default meta
Expand All @@ -24,3 +25,5 @@ export const Locale: StoryObj = { name: 'Locale', render: renderExample(LocaleEx
export const PrefixSuffix: StoryObj = { name: 'Prefix Suffix', render: renderExample(PrefixSuffixExample) }

export const Timing: StoryObj = { name: 'Timing', render: renderExample(TimingExample) }

export const WithTimer: StoryObj = { name: 'With Timer', render: renderExample(WithTimerExample) }
11 changes: 11 additions & 0 deletions packages/web/src/components/number-flow/tests/number-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event'
import { mount as Basic } from '../../../../examples/number-flow/basic.js'
import { mount as Formatting } from '../../../../examples/number-flow/formatting.js'
import { mount as PrefixSuffix } from '../../../../examples/number-flow/prefix-suffix.js'
import { mount as WithTimer } from '../../../../examples/number-flow/with-timer.js'
import { mount } from '../../../test-utils.js'

const user = userEvent.setup()
Expand Down Expand Up @@ -48,3 +49,13 @@ test('number flow formats with formatOptions', async () => {
expect(texts()[1]).toBe('+12.4%')
expect(texts()[2]).toBe('1.3M')
})

test('number flow follows a countdown timer and resets with it', async () => {
mount(WithTimer)
const [minutes, seconds] = document.querySelectorAll('ark-number-flow')
await waitFor(() => expect([minutes.value, seconds.value]).toEqual([5, 0]))
await user.click(screen.getByRole('button', { name: 'Start' }))
await waitFor(() => expect(minutes.value).toBe(4), { timeout: 3000 })
await user.click(screen.getByRole('button', { name: 'Reset' }))
await waitFor(() => expect([minutes.value, seconds.value]).toEqual([5, 0]))
})
8 changes: 8 additions & 0 deletions website/src/content/pages/utilities/number-flow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ when the number gains or loses a digit.

<Example id="composition" />

### With Timer

Pair `NumberFlow` with the [Timer](/docs/components/timer) to roll a countdown. Read the time from `useTimer`, render
one `NumberFlow.Root` per unit with `minimumIntegerDigits: 2`, and set `trend={-1}` so every digit rolls down as the
time runs out.

<Example id="with-timer" />

### Live Announcements

Set `live` to announce changes to screen readers. The value is announced through `HiddenValueText` once the roll
Expand Down
Loading