/* eslint-disable react/no-array-index-key */
import React, { useMemo } from "react"
import { AnimatePresence, motion } from "motion/react"
import { Block, useStyletron } from "@mezo-org/mezo-clay"
type SlotNumberProps = {
className?: string
children: number
formatFunction?: (value: number) => string
}
type SlotNumberDigitProps = {
className?: string
children: number
}
function SlotNumberDigit(props: SlotNumberDigitProps) {
const { children: value, ...restProps } = props
const [css] = useStyletron()
const digits = [...Array(10).keys()]
return (
{value}
{digits.map((digit) => (
{digit}
))}
)
}
/**
* SlotNumber component displays a number with animated digits.
*
* @typedef {object} SlotNumberProps
* @property {number} children - The number to display.
* @property {function} [formatFunction] - Optional function to format the number.
* @property {BlockProps} [restProps] - Additional properties for the Block component.
* @see https://pyszkowski.dev/writings/slot-number
* @returns {JSX.Element} The rendered SlotNumber component.
*/
function SlotNumber(props: SlotNumberProps) {
const { children: value, formatFunction, ...restProps } = props
const [css] = useStyletron()
const characters = useMemo(() => {
const formattedValue = formatFunction
? formatFunction(value)
: value.toString()
return formattedValue
.split("")
.map((character) => (/^[0-9]$/.test(character) ? +character : character))
.reverse()
}, [formatFunction, value])
return (
{characters.map((character, index) =>
typeof character === "number" ? (
{character}
) : (
{character}
),
)}
)
}
export default SlotNumber