├── .changeset ├── README.md └── config.json ├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ ├── bug-report.yml │ └── feature-request.yml └── workflows │ ├── main.yml │ └── publish.yml ├── .gitignore ├── .prettierignore ├── .prettierrc ├── .vscode └── extensions.json ├── CHANGELOG.md ├── LICENSE ├── README.md ├── _docs ├── _OLD_CHANGELOG.md └── demo.gif ├── bun.lockb ├── dev ├── components │ └── demos │ │ ├── base-demo.tsx │ │ ├── continuous.tsx │ │ └── trend.tsx ├── fonts │ ├── .DS_Store │ ├── geist.css │ └── geist.ttf ├── hooks │ └── use-cycle.ts ├── icons │ ├── github.tsx │ ├── shuffle.tsx │ └── solidjs.tsx ├── markdown.tsx ├── markdown │ ├── npm-install.md │ └── usage.md ├── pages │ ├── +Layout.tsx │ ├── +Page.tsx │ ├── +config.ts │ ├── _error │ │ └── +Page.tsx │ └── test │ │ ├── +Page.tsx │ │ └── +config.ts ├── styles.css ├── tsconfig.json └── vite.config.ts ├── env.d.ts ├── eslint.config.mjs ├── package.json ├── postcss.config.js ├── src └── index.tsx ├── tailwind.config.js ├── test ├── index.test.tsx └── server.test.tsx ├── tsconfig.json ├── tsup.config.ts └── vitest.config.ts /.changeset/README.md: -------------------------------------------------------------------------------- 1 | # Changesets 2 | 3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works 4 | with multi-package repos, or single-package repos to help you version and publish your code. You can 5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets) 6 | 7 | We have a quick list of common questions to get you started engaging with this project in 8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) 9 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@3.0.4/schema.json", 3 | "changelog": ["@changesets/changelog-github", { "repo": "blankeos/solid-number-flow" }], 4 | "commit": false, 5 | "fixed": [], 6 | "linked": [], 7 | "access": "public", 8 | "baseBranch": "main", 9 | "updateInternalDependencies": "patch", 10 | "ignore": [] 11 | } 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug-report.yml: -------------------------------------------------------------------------------- 1 | name: "🐛 Bug report" 2 | description: Create a report to help us improve 3 | body: 4 | - type: markdown 5 | attributes: 6 | value: | 7 | Thank you for reporting an issue :pray:. 8 | 9 | The more information you fill in, the better the community can help you. 10 | - type: textarea 11 | id: description 12 | attributes: 13 | label: Describe the bug 14 | description: Provide a clear and concise description of the challenge you are running into. 15 | validations: 16 | required: true 17 | - type: input 18 | id: link 19 | attributes: 20 | label: Minimal Reproduction Link 21 | description: | 22 | Please provide a link to a minimal reproduction of the bug you are running into. 23 | It makes the process of verifying and fixing the bug much easier. 24 | Note: 25 | - Your bug will may get fixed much faster if we can run your code and it doesn't have dependencies other than the solid-js and solid-primitives. 26 | - To create a shareable code example you can use [Stackblitz](https://stackblitz.com/) (https://solid.new). Please no localhost URLs. 27 | - Please read these tips for providing a minimal example: https://stackoverflow.com/help/mcve. 28 | placeholder: | 29 | e.g. https://stackblitz.com/edit/...... OR Github Repo 30 | validations: 31 | required: true 32 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature-request.yml: -------------------------------------------------------------------------------- 1 | name: "Feature Request" 2 | description: For feature/enhancement requests. Please search for existing issues first. 3 | body: 4 | - type: markdown 5 | attributes: 6 | value: | 7 | Thank you for bringing your ideas here :pray:. 8 | 9 | The more information you fill in, the better the community can understand your idea. 10 | - type: textarea 11 | id: problem 12 | attributes: 13 | label: Describe The Problem To Be Solved 14 | description: Provide a clear and concise description of the challenge you are running into. 15 | validations: 16 | required: true 17 | - type: textarea 18 | id: solution 19 | attributes: 20 | label: Suggest A Solution 21 | description: | 22 | A concise description of your preferred solution. Things to address include: 23 | - Details of the technical implementation 24 | - Tradeoffs made in design decisions 25 | - Caveats and considerations for the future 26 | validations: 27 | required: true 28 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # I got this from Matt Pockock 2 | # https://www.youtube.com/watch?v=eh89VE3Mk5g 3 | name: CI 4 | 5 | on: 6 | push: 7 | branches: 8 | - '**' 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | # ... 15 | - uses: actions/checkout@v4 16 | - uses: oven-sh/setup-bun@v2 17 | 18 | - run: bun install --frozen-lockfile 19 | - run: bun run ci 20 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # I got this from Matt Pockock 2 | # https://www.youtube.com/watch?v=eh89VE3Mk5g 3 | name: Publish 4 | on: 5 | push: 6 | branches: 7 | - 'main' 8 | 9 | # Publish workflows don't happen at the same time. 10 | concurrency: ${{ github.workflow }}-${{ github.ref }} 11 | 12 | jobs: 13 | publish: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: oven-sh/setup-bun@v2 18 | with: 19 | bun-version: latest 20 | - run: bun install --frozen-lockfile 21 | - run: bun run build 22 | 23 | - name: Create Release Pull Request or Publish 24 | id: changesets 25 | uses: changesets/action@v1 26 | with: 27 | publish: bun run publish-ci 28 | env: 29 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 30 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | gitignore 4 | 5 | # tsup 6 | tsup.config.bundled_*.{m,c,}s 7 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | _docs/ch-template.hbs -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "all", 3 | "tabWidth": 2, 4 | "printWidth": 100, 5 | "plugins": ["prettier-plugin-tailwindcss"], 6 | "semi": true, 7 | "singleQuote": true, 8 | "useTabs": false, 9 | "bracketSpacing": true 10 | } 11 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint"] 3 | } 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # solid-number-flow 2 | 3 | ## 0.5.3 4 | 5 | ### Minor Changes 6 | 7 | - [#5](https://github.com/Blankeos/solid-number-flow/pull/5) [`040cd79`](https://github.com/Blankeos/solid-number-flow/commit/040cd792917b7041baba213e66603b820bb3992d) Thanks [@Blankeos](https://github.com/Blankeos)! - feat: Upgraded compatibility with v0.5.3 number-flow. 8 | 9 | ### Patch Changes 10 | 11 | - [#5](https://github.com/Blankeos/solid-number-flow/pull/5) [`040cd79`](https://github.com/Blankeos/solid-number-flow/commit/040cd792917b7041baba213e66603b820bb3992d) Thanks [@Blankeos](https://github.com/Blankeos)! - chore: Improved linting. 12 | 13 | ## 0.3.3 14 | 15 | ### Patch Changes 16 | 17 | - chore: Add max as contributor. 18 | 19 | - feat: Added support for non-parts props. Fixes #3 [`#3`](https://github.com/Blankeos/solid-number-flow/issues/3) 20 | 21 | - docs: Added examples for trend and continuous. 22 | 23 | - fix: Non parts props work + examples in docs. [`#4`](https://github.com/Blankeos/solid-number-flow/pull/4) Thanks @blankeos! 24 | 25 | ## 0.3.2 26 | 27 | ### Patch Changes 28 | 29 | - chore: Updated deps, npm keywords, better landing page. 30 | 31 | ## 0.3.1 32 | 33 | ### Patch Changes 34 | 35 | - fix: formatting and locales [`#2`](https://github.com/Blankeos/solid-number-flow/pull/2) Thanks @blankeos! 36 | 37 | - fix: don't define more than once [`#1`](https://github.com/Blankeos/solid-number-flow/pull/1) Thanks @brenelz! 38 | 39 | - chore: more docs on site and readme. 40 | 41 | ## 0.3.0 42 | 43 | ### Patch Changes 44 | 45 | - fix: Bug fixes on ssr. 46 | 47 | - chore: Better dev environment, docs, landing, and tests. 48 | 49 | - feat: Finished port + landing page. 50 | 51 | - chore: Codebase formatting. 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 {{me}} 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | solid-number-flow 3 |

4 | 5 | # solid-number-flow 6 | 7 |
8 | Demo 9 |
10 | 11 |
12 | Bun 13 | 14 | NPM Downloads 15 | NPM License 16 | NPM Bundle Size 17 |
18 | 19 | A SolidJS component to transition, format, and localize numbers. Forked from [@barvian/number-flow](https://github.com/barvian/number-flow). 20 | 21 | ## Quick start 22 | 23 | Install it: 24 | 25 | ```bash 26 | npm i solid-number-flow 27 | # or 28 | yarn add solid-number-flow 29 | # or 30 | pnpm add solid-number-flow 31 | # or 32 | bun add solid-number-flow 33 | ``` 34 | 35 | Use it: 36 | 37 | ```tsx 38 | import solid-number-flow from 'solid-number-flow' 39 | 40 | export default function Page() { 41 | const [value, setValue] = createSignal(398.43); 42 | 43 | return ( 44 | <> 45 | 46 | 47 | 48 | ) 49 | } 50 | 51 | ``` 52 | -------------------------------------------------------------------------------- /_docs/_OLD_CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### Changelog 2 | 3 | All notable changes to this project will be documented in this file. Dates are displayed in UTC. 4 | 5 | Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). 6 | 7 | #### [0.3.3](https://github.com/Blankeos/solid-number-flow/compare/0.3.2...0.3.3) 8 | 9 | - feat: Non parts props work + examples in docs. [`#4`](https://github.com/Blankeos/solid-number-flow/pull/4) Thanks! 10 | - feat: Added support for non-parts props. Fixes #3 [`#3`](https://github.com/Blankeos/solid-number-flow/issues/3) 11 | - feat: Added examples for trend and continuous. [`0ea1c9c`](https://github.com/Blankeos/solid-number-flow/commit/0ea1c9c167385b4834a6333a790792d74a2de27c) 12 | - chore: release 0.0.3 [`588cbe2`](https://github.com/Blankeos/solid-number-flow/commit/588cbe21ac5d9ac2c1b3dcf8eb0f2c00d3fdf255) 13 | - chore: Added max as contributor. [`f4b121f`](https://github.com/Blankeos/solid-number-flow/commit/f4b121f881d14b3f2c89fdafe30945e1d3d23a55) 14 | 15 | #### [0.3.2](https://github.com/Blankeos/solid-number-flow/compare/0.3.1...0.3.2) 16 | 17 | > 20 October 2024 18 | 19 | - chore: Updated deps, npm keywords, better landing page. [`d0c310b`](https://github.com/Blankeos/solid-number-flow/commit/d0c310ba7e9fa453e4bcdab38d87aabaebd5c3e0) 20 | - chore: release 0.3.2 [`77fd36c`](https://github.com/Blankeos/solid-number-flow/commit/77fd36c98fa270adef5eb6700baafb647e3eaf13) 21 | 22 | #### [0.3.1](https://github.com/Blankeos/solid-number-flow/compare/0.3.0...0.3.1) 23 | 24 | > 17 October 2024 25 | 26 | - fix: formatting and locales. [`#2`](https://github.com/Blankeos/solid-number-flow/pull/2) Thanks! 27 | - fix: don't define more than once [`#1`](https://github.com/Blankeos/solid-number-flow/pull/1) Thanks! 28 | - chore: more docs on site and readme. [`6350841`](https://github.com/Blankeos/solid-number-flow/commit/635084107e60669f3c33cb0e343b81200ebbb783) 29 | - chore: release 0.3.1 [`f6ee64a`](https://github.com/Blankeos/solid-number-flow/commit/f6ee64a36de678766a34a7939de7404d7dd51b98) 30 | - Define outside component [`9388853`](https://github.com/Blankeos/solid-number-flow/commit/9388853615ebfc58bd7e8f74cd1e80aa6fc35217) 31 | 32 | #### [0.3.0](https://github.com/Blankeos/solid-number-flow/compare/0.0.3...0.3.0) 33 | 34 | > 16 October 2024 35 | 36 | #### 0.0.3 37 | 38 | > 22 October 2024 39 | 40 | - feat: Non parts props work + examples in docs. [`#4`](https://github.com/Blankeos/solid-number-flow/pull/4) Thanks! 41 | - fix: formatting and locales. [`#2`](https://github.com/Blankeos/solid-number-flow/pull/2) Thanks! 42 | - fix: don't define more than once [`#1`](https://github.com/Blankeos/solid-number-flow/pull/1) Thanks! 43 | - feat: Added support for non-parts props. Fixes #3 [`#3`](https://github.com/Blankeos/solid-number-flow/issues/3) 44 | - feat: Finished port + landing page. [`7852ff7`](https://github.com/Blankeos/solid-number-flow/commit/7852ff7db4062558d557e47985bbac8c12de70e7) 45 | - feat: Added examples for trend and continuous. [`0ea1c9c`](https://github.com/Blankeos/solid-number-flow/commit/0ea1c9c167385b4834a6333a790792d74a2de27c) 46 | - chore: Better dev environment, docs, landing, and tests. [`a5b09b0`](https://github.com/Blankeos/solid-number-flow/commit/a5b09b030d79128c5ab82128b88bd0e76587047f) 47 | -------------------------------------------------------------------------------- /_docs/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Blankeos/solid-number-flow/22e3f1ecdc2e27b5f03b9ed261369abd84e67856/_docs/demo.gif -------------------------------------------------------------------------------- /bun.lockb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Blankeos/solid-number-flow/22e3f1ecdc2e27b5f03b9ed261369abd84e67856/bun.lockb -------------------------------------------------------------------------------- /dev/components/demos/base-demo.tsx: -------------------------------------------------------------------------------- 1 | import { clsx } from 'clsx'; 2 | import { 3 | children, 4 | ComponentProps, 5 | createSignal, 6 | FlowProps, 7 | JSX, 8 | mergeProps, 9 | Show, 10 | splitProps, 11 | } from 'solid-js'; 12 | 13 | // Tabs 14 | import { Tabs } from '@kobalte/core/tabs'; 15 | 16 | // TODO: Dropdown (use @kobalte) 17 | // Dropdown 18 | // import { 19 | // Menu, 20 | // MenuButton, 21 | // MenuItem, 22 | // MenuItems, 23 | // Switch, 24 | // Field, 25 | // Label, 26 | // type SwitchProps, 27 | // type MenuButtonProps, 28 | // type MenuItemProps, 29 | // type MenuItemsProps, 30 | // type MenuProps 31 | // } from '@headlessui/react' 32 | 33 | type TabValue = 'preview' | 'code'; 34 | 35 | // Switch 36 | import { Switch } from '@kobalte/core/switch'; 37 | 38 | // =========================================================================== 39 | // UI 40 | // =========================================================================== 41 | 42 | export type DemoProps = { 43 | ref?: HTMLDivElement; 44 | children: JSX.Element; 45 | class?: string; 46 | defaultValue?: TabValue; 47 | code?: JSX.Element; 48 | minHeight?: string; 49 | title?: JSX.Element; 50 | }; 51 | 52 | type Props = DemoProps & { onClick?: () => void }; 53 | 54 | function Demo(props: FlowProps) { 55 | const _props = mergeProps( 56 | { 57 | defaultValue: 'preview', 58 | minHeight: 'min-h-[20rem]', 59 | children: undefined, 60 | renderTitle: false, 61 | }, 62 | props, 63 | ); 64 | 65 | const [knowsToClick, setKnowsToClick] = createSignal(false); 66 | const [active, setActive] = createSignal(_props.defaultValue); 67 | 68 | function handleClick() { 69 | if (!_props.onClick) return; 70 | 71 | setKnowsToClick(true); 72 | _props?.onClick?.(); 73 | } 74 | 75 | const handleMouseDown: JSX.EventHandler = (event) => { 76 | if (!_props.onClick) return; 77 | 78 | // Prevent selection of text: 79 | // https://stackoverflow.com/a/43321596 80 | if (event.detail > 1) { 81 | event.preventDefault(); 82 | } 83 | }; 84 | 85 | /** Prevent doublle-render when using it in https://github.com/solidjs/solid/issues/2345#issuecomment-2427189199 */ 86 | const renderedTitle = children(() => _props.title); 87 | 88 | return ( 89 | setActive(val as TabValue)} 94 | // onValueChange={(val) => setActive(val as TabValue)} 95 | > 96 | 97 | {/* */} 98 | 99 | 106 | 107 | {/* // Motion.div */} 108 |
114 | 115 | Preview 116 | 117 | 124 | 125 | {/* // Motion.div */} 126 |
132 | 133 | Code 134 | 135 | 136 | {/* */} 137 | 138 | 145 | 146 |
{renderedTitle()}
147 |
148 | 149 |
154 | {_props.children} 155 | {_props?.onClick && ( 156 | 162 | Click anywhere to change numbers 163 | 164 | )} 165 |
166 |
167 | 168 | {_props?.code}« 169 | 170 | 171 | ); 172 | } 173 | 174 | export default Demo; 175 | 176 | export function DemoTitle(props: JSX.IntrinsicElements['span'] & { children: string }) { 177 | const [_props, other] = splitProps(props, ['class', 'children']); 178 | 179 | return ( 180 | 181 | {props.children} 182 | 183 | ); 184 | } 185 | 186 | // export function DemoMenu(props: MenuProps) { 187 | // return 188 | // } 189 | 190 | // export function DemoMenuButton({ 191 | // children, 192 | // class, 193 | // ...props 194 | // }: MenuButtonProps & { children: JSX.Element }) { 195 | // return ( 196 | // 203 | // {children} 204 | // 208 | // 209 | // ) 210 | // } 211 | 212 | // export function DemoMenuItems({ class, ...props }: MenuItemsProps) { 213 | // return ( 214 | // 221 | // ) 222 | // } 223 | 224 | // export function DemoMenuItem({ 225 | // class, 226 | // children, 227 | // ...props 228 | // }: MenuItemProps<'button'> & { children: JSX.Element }) { 229 | // return ( 230 | // 239 | // {children} 240 | // {props.disabled && } 241 | // 242 | // ) 243 | // } 244 | 245 | export function DemoSwitch(props: ComponentProps) { 246 | const [_props, other] = splitProps(props, ['class', 'children']); 247 | 248 | return ( 249 | 250 | 256 | 257 | 258 | {props.children as any} 259 | 260 | ); 261 | } 262 | -------------------------------------------------------------------------------- /dev/components/demos/continuous.tsx: -------------------------------------------------------------------------------- 1 | import Demo, { DemoSwitch, type DemoProps } from 'dev/components/demos/base-demo'; 2 | 3 | import { useCycle } from 'dev/hooks/use-cycle'; 4 | import { continuous } from 'number-flow'; 5 | import { createSignal } from 'solid-js'; 6 | import NumberFlow from 'src'; 7 | 8 | const NUMBERS = [120, 140]; 9 | 10 | export default function ContinuousDemo(props: Omit) { 11 | const [value, cycleValue] = useCycle(NUMBERS); 12 | const [isContinuous, setContinuous] = createSignal(false); 13 | 14 | return ( 15 | <> 16 | 20 | continuous 21 | 22 | } 23 | onClick={cycleValue} 24 | > 25 |
26 | 32 |
33 |
34 | 35 | ); 36 | } 37 | -------------------------------------------------------------------------------- /dev/components/demos/trend.tsx: -------------------------------------------------------------------------------- 1 | import Demo, { type DemoProps } from 'dev/components/demos/base-demo'; 2 | 3 | import { useCycle } from 'dev/hooks/use-cycle'; 4 | import { Trend } from 'number-flow'; 5 | import NumberFlow from 'src'; 6 | 7 | const NUMBERS = [19, 20]; 8 | 9 | export default function TrendDemo(props: Omit) { 10 | const [value, cycleValue] = useCycle(NUMBERS); 11 | const [trend, cycleTrend] = useCycle([true, false, 'increasing', 'decreasing'] as Trend[]); 12 | 13 | return ( 14 | <> 15 | 19 | trend: {JSON.stringify(trend())} 20 | 21 | } 22 | onClick={cycleValue} 23 | > 24 |
25 | 31 |
32 |
33 | 34 | ); 35 | } 36 | -------------------------------------------------------------------------------- /dev/fonts/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Blankeos/solid-number-flow/22e3f1ecdc2e27b5f03b9ed261369abd84e67856/dev/fonts/.DS_Store -------------------------------------------------------------------------------- /dev/fonts/geist.css: -------------------------------------------------------------------------------- 1 | @font-face { 2 | font-family: 'Geist'; 3 | src: url('./geist.ttf') format('truetype'); 4 | font-weight: 100 900; /* Define the range of weights if it's a variable font */ 5 | font-style: normal; 6 | } 7 | -------------------------------------------------------------------------------- /dev/fonts/geist.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Blankeos/solid-number-flow/22e3f1ecdc2e27b5f03b9ed261369abd84e67856/dev/fonts/geist.ttf -------------------------------------------------------------------------------- /dev/hooks/use-cycle.ts: -------------------------------------------------------------------------------- 1 | import { createMemo, createSignal } from 'solid-js'; 2 | 3 | /** 4 | * A hook that toggles between two or multiple values (by implementing a common state pattern). 5 | * 6 | * Forked from https://github.com/Blankeos/bagon-hooks/blob/main/src/use-toggle/use-toggle.ts 7 | */ 8 | export function useCycle(options: readonly T[] = [false, true] as any) { 9 | const [_options, _setOptions] = createSignal(options); 10 | 11 | function toggle() { 12 | const value = _options()[0]!; 13 | const index = Math.abs(_options()!.indexOf(value)); 14 | 15 | _setOptions( 16 | _options()! 17 | .slice(index + 1) 18 | .concat(value), 19 | ); 20 | } 21 | 22 | const currentOption = createMemo(() => _options()[0]!); 23 | 24 | return [currentOption, toggle] as const; 25 | } 26 | -------------------------------------------------------------------------------- /dev/icons/github.tsx: -------------------------------------------------------------------------------- 1 | import { JSX, VoidProps } from 'solid-js'; 2 | 3 | export function IconGithub(props: VoidProps>) { 4 | return ( 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 19 | 24 | 25 | 26 | 31 | 37 | 44 | 45 | 46 | 47 | 54 | 55 | 56 | ); 57 | } 58 | -------------------------------------------------------------------------------- /dev/icons/shuffle.tsx: -------------------------------------------------------------------------------- 1 | import { JSX, VoidProps } from 'solid-js'; 2 | 3 | export function IconShuffle(props: VoidProps>) { 4 | return ( 5 | 6 | 12 | 13 | ); 14 | } 15 | -------------------------------------------------------------------------------- /dev/icons/solidjs.tsx: -------------------------------------------------------------------------------- 1 | import { JSX, VoidProps } from 'solid-js'; 2 | 3 | export function IconSolidJS(props: VoidProps>) { 4 | return ( 5 | 6 | 10 | 11 | 12 | 13 | 14 | 15 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 31 | 39 | 40 | 41 | 42 | 43 | 44 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | ); 59 | } 60 | -------------------------------------------------------------------------------- /dev/markdown.tsx: -------------------------------------------------------------------------------- 1 | import * as shikiji from 'shikiji'; 2 | import { createEffect, createResource, createSignal, FlowProps, JSX, Show } from 'solid-js'; 3 | import { Dynamic } from 'solid-js/web'; 4 | import { MDXProvider } from 'solid-marked'; 5 | 6 | export function Markdown(props: FlowProps) { 7 | const [highlighter] = createResource(async () => 8 | shikiji.getHighlighter({ 9 | langs: ['tsx', 'jsx', 'md', 'mdx', 'markdown', 'bash', 'js', 'ts'], 10 | themes: ['vitesse-dark'], 11 | }), 12 | ); 13 | 14 | return ( 15 | 20 | 21 | {props.children} 22 | 23 | 24 | ); 25 | }, 26 | Paragraph(props): JSX.Element { 27 | return

{props.children}

; 28 | }, 29 | Root(props): JSX.Element { 30 | return
{props.children}
; 31 | }, 32 | Blockquote(props): JSX.Element { 33 | return
{props.children}
; 34 | }, 35 | Image(props): JSX.Element { 36 | return {props.alt; 37 | }, 38 | Code(props): JSX.Element { 39 | const [ref, setRef] = createSignal(); 40 | createEffect(() => { 41 | const current = ref(); 42 | const instance = highlighter(); 43 | const content = props.children; 44 | if (current && instance && content) { 45 | current.innerHTML = instance.codeToHtml(content, { 46 | lang: (props.lang ?? undefined) as shikiji.BuiltinLanguage, 47 | theme: 'vitesse-dark', 48 | }); 49 | } 50 | }); 51 | return ( 52 |
53 | {/* Render the code without syntax highlights but less opaque. */} 54 |
{props.children}
55 |
56 | ); 57 | }, 58 | InlineCode(props): JSX.Element { 59 | return {props.children}; 60 | }, 61 | List(props): JSX.Element { 62 | return ( 63 | 64 | {props.children} 65 | 66 | ); 67 | }, 68 | ListItem(props): JSX.Element { 69 | return ( 70 |
  • 71 | 72 | 73 | {props.children} 74 | 75 |
  • 76 | ); 77 | }, 78 | Link(props): JSX.Element { 79 | return ( 80 | 81 | {props.children} 82 | 83 | ); 84 | }, 85 | }} 86 | > 87 | {props.children} 88 |
    89 | ); 90 | } 91 | -------------------------------------------------------------------------------- /dev/markdown/npm-install.md: -------------------------------------------------------------------------------- 1 | ```sh 2 | npm install solid-number-flow 3 | ``` 4 | -------------------------------------------------------------------------------- /dev/markdown/usage.md: -------------------------------------------------------------------------------- 1 | ```ts 2 | // Basic usage 3 | import NumberFlow from 'solid-number-flow' 4 | 5 | 10 | ``` 11 | -------------------------------------------------------------------------------- /dev/pages/+Layout.tsx: -------------------------------------------------------------------------------- 1 | import { FlowProps } from 'solid-js'; 2 | import '../styles.css'; 3 | 4 | export default function Layout(props: FlowProps) { 5 | return <>{props.children}; 6 | } 7 | -------------------------------------------------------------------------------- /dev/pages/+Page.tsx: -------------------------------------------------------------------------------- 1 | import ContinuousDemo from 'dev/components/demos/continuous'; 2 | import TrendDemo from 'dev/components/demos/trend'; 3 | import { useCycle } from 'dev/hooks/use-cycle'; 4 | import { Format } from 'number-flow'; 5 | import NumberFlow from 'src'; 6 | import { IconGithub } from '../icons/github'; 7 | import { IconShuffle } from '../icons/shuffle'; 8 | import { IconSolidJS } from '../icons/solidjs'; 9 | import { Markdown } from '../markdown'; 10 | 11 | import pkgJSON from 'src/../package.json'; 12 | 13 | // @ts-ignore idk what type I need to override. 14 | import Usage from '../markdown/usage.md'; 15 | 16 | // @ts-ignore idk what type I need to override. 17 | import NPMInstall from '../markdown/npm-install.md'; 18 | 19 | const NUMBERS = [321, -3243.6, 42, 398.43, -3243.5, 1435237.2, 12348.43, -3243.6, 54323.2]; 20 | const LOCALES = ['fr-FR', 'en-US', 'fr-FR', 'en-US', 'en-US', 'zh-CN', 'en-US', 'en-US', 'fr-FR']; 21 | const FORMATS = [ 22 | { 23 | // style: "unit", 24 | // unit: "meter", 25 | // notation: "compact", 26 | // signDisplay: "never", 27 | }, 28 | { 29 | style: 'currency', 30 | currency: 'USD', 31 | currencySign: 'accounting', 32 | signDisplay: 'always', 33 | }, 34 | {}, 35 | { 36 | style: 'percent', 37 | signDisplay: 'always', 38 | }, 39 | {}, 40 | { 41 | style: 'unit', 42 | unit: 'meter', 43 | notation: 'compact', 44 | minimumFractionDigits: 2, 45 | maximumFractionDigits: 2, 46 | signDisplay: 'never', 47 | }, 48 | { 49 | style: 'currency', 50 | currency: 'USD', 51 | }, 52 | {}, 53 | { 54 | // style: "percent", 55 | signDisplay: 'always', 56 | }, 57 | ] as Format[]; 58 | 59 | export default function HomePage() { 60 | const [value, cycleValue] = useCycle(NUMBERS); 61 | const [locale, cycleLocale] = useCycle(LOCALES); 62 | const [format, cycleFormat] = useCycle(FORMATS); 63 | 64 | function cycle() { 65 | cycleValue(); 66 | cycleLocale(); 67 | cycleFormat(); 68 | } 69 | return ( 70 |
    71 |
    72 | 73 | NumberFlow{' '} 74 | v{pkgJSON.version} 75 | 76 | 83 | 84 |
    85 | 92 | 93 | 98 | 99 | 100 |
    101 | 102 |

    103 | A Solid component to transition, localize, and format numbers. 104 |
    105 | Dependency-free. Accessible. Customizable. 106 |

    107 | 108 |

    109 | Ported from{' '} 110 | 111 | barvian/number-flow 112 | 113 |

    114 |
    115 | 116 |
    117 |
    118 | } /> 119 |
    120 | 121 |
    122 | } /> 123 |
    124 |
    125 | 126 |
    127 | 128 |
    129 | 130 | 131 |
    132 | 133 |
    134 |
    135 | ); 136 | } 137 | -------------------------------------------------------------------------------- /dev/pages/+config.ts: -------------------------------------------------------------------------------- 1 | import config from 'vike-solid/config'; 2 | import type { Config } from 'vike/types'; 3 | 4 | // Default config (can be overridden by pages) 5 | export default { 6 | extends: [config], 7 | ssr: true, 8 | } satisfies Config; 9 | -------------------------------------------------------------------------------- /dev/pages/_error/+Page.tsx: -------------------------------------------------------------------------------- 1 | import { Show } from 'solid-js'; 2 | import { usePageContext } from 'vike-solid/usePageContext'; 3 | 4 | export default function Page() { 5 | const { is404 } = usePageContext(); 6 | return ( 7 | 11 |

    500 Internal Server Error

    12 |

    Something went wrong.

    13 | 14 | } 15 | > 16 |

    404 Page Not Found

    17 |

    This page could not be found.

    18 |
    19 | ); 20 | } 21 | -------------------------------------------------------------------------------- /dev/pages/test/+Page.tsx: -------------------------------------------------------------------------------- 1 | import { continuous } from 'number-flow'; 2 | import { createSignal, onMount } from 'solid-js'; 3 | // import { NumberFlow } from 'src/NumberFlow'; 4 | import NumberFlow from 'src'; 5 | 6 | export default function Page() { 7 | const [toggle, setToggle] = createSignal(false); 8 | const [value1, setValue1] = createSignal(123); 9 | const [value2, setValue2] = createSignal(0); 10 | const [value3, setValue3] = createSignal(123); 11 | const [value4, setValue4] = createSignal(0); 12 | const [value5, setValue5] = createSignal(0); 13 | 14 | function triggerChange(useAlternateValues: boolean = false) { 15 | const defaultValues = { 16 | value1: 500, 17 | value2: 1.42, 18 | value3: 500, 19 | value4: 1_500_540, 20 | value5: 88, 21 | }; 22 | 23 | const alternateValues = { 24 | value1: 100, 25 | value2: 1203, 26 | value3: 7298, 27 | value4: 12.1, 28 | value5: 50, 29 | }; 30 | 31 | const values = useAlternateValues ? alternateValues : defaultValues; 32 | 33 | setTimeout(() => { 34 | setValue1(values.value1); 35 | }, 500); 36 | setTimeout(() => { 37 | setValue2(values.value2); 38 | }, 800); 39 | setTimeout(() => { 40 | setValue3(values.value3); 41 | }, 1000); 42 | setTimeout(() => { 43 | setValue4(values.value4); 44 | }, 1500); 45 | setTimeout(() => { 46 | setValue5(values.value5); 47 | }, 1500); 48 | } 49 | 50 | onMount(() => { 51 | triggerChange(); 52 | }); 53 | 54 | return ( 55 |
    56 | 57 | 64 | 65 | 74 | 79 | 80 | 88 | 89 | Back to Home 90 |
    91 | ); 92 | } 93 | -------------------------------------------------------------------------------- /dev/pages/test/+config.ts: -------------------------------------------------------------------------------- 1 | import type { Config } from 'vike/types'; 2 | 3 | // Default config (can be overridden by pages) 4 | export default { 5 | ssr: false, 6 | // ssr: true, 7 | } satisfies Config; 8 | -------------------------------------------------------------------------------- /dev/styles.css: -------------------------------------------------------------------------------- 1 | @import url('./fonts/geist.css'); 2 | 3 | @tailwind base; 4 | @tailwind components; 5 | @tailwind utilities; 6 | 7 | body { 8 | margin: 0; 9 | font-family: 10 | 'Geist', 11 | -apple-system, 12 | BlinkMacSystemFont, 13 | 'Segoe UI', 14 | 'Roboto', 15 | 'Oxygen', 16 | 'Ubuntu', 17 | 'Cantarell', 18 | 'Fira Sans', 19 | 'Droid Sans', 20 | 'Helvetica Neue', 21 | sans-serif; 22 | -webkit-font-smoothing: antialiased; 23 | -moz-osx-font-smoothing: grayscale; 24 | } 25 | 26 | code { 27 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; 28 | } 29 | -------------------------------------------------------------------------------- /dev/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "types": ["vite/client", "solid-marked/env"] 5 | }, 6 | "exclude": ["node_modules", "dist"] 7 | } 8 | -------------------------------------------------------------------------------- /dev/vite.config.ts: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import solidMarkedPlugin from 'unplugin-solid-marked'; 3 | import { defineConfig } from 'vite'; 4 | 5 | // Vike 6 | import vikeSolid from 'vike-solid/vite'; 7 | import vike from 'vike/plugin'; 8 | 9 | export default defineConfig({ 10 | resolve: { 11 | alias: { 12 | src: path.resolve(__dirname, '../src'), 13 | dev: path.resolve(__dirname), 14 | }, 15 | }, 16 | plugins: [ 17 | solidMarkedPlugin.vite({}), 18 | { 19 | name: 'Reaplace env variables', 20 | transform(code, id) { 21 | if (id.includes('node_modules')) { 22 | return code; 23 | } 24 | return code 25 | .replace(/process\.env\.SSR/g, 'false') 26 | .replace(/process\.env\.DEV/g, 'true') 27 | .replace(/process\.env\.PROD/g, 'false') 28 | .replace(/process\.env\.NODE_ENV/g, '"development"') 29 | .replace(/import\.meta\.env\.SSR/g, 'false') 30 | .replace(/import\.meta\.env\.DEV/g, 'true') 31 | .replace(/import\.meta\.env\.PROD/g, 'false') 32 | .replace(/import\.meta\.env\.NODE_ENV/g, '"development"'); 33 | }, 34 | }, 35 | vike({ 36 | prerender: true, 37 | }), 38 | vikeSolid(), 39 | ], 40 | server: { 41 | port: 3000, 42 | }, 43 | build: { 44 | target: 'esnext', 45 | }, 46 | }); 47 | -------------------------------------------------------------------------------- /env.d.ts: -------------------------------------------------------------------------------- 1 | declare global { 2 | interface ImportMeta { 3 | env: { 4 | NODE_ENV: 'production' | 'development' 5 | PROD: boolean 6 | DEV: boolean 7 | } 8 | } 9 | namespace NodeJS { 10 | interface ProcessEnv { 11 | NODE_ENV: 'production' | 'development' 12 | PROD: boolean 13 | DEV: boolean 14 | } 15 | } 16 | } 17 | 18 | export {} 19 | -------------------------------------------------------------------------------- /eslint.config.mjs: -------------------------------------------------------------------------------- 1 | import typescriptEslint from '@typescript-eslint/eslint-plugin'; 2 | import tsParser from '@typescript-eslint/parser'; 3 | import solid from 'eslint-plugin-solid/configs/recommended'; 4 | import globals from 'globals'; 5 | 6 | export default [ 7 | { 8 | ignores: ['dist/**/*', '**/*.js', '**/*.cjs', '**/*.mjs'], 9 | }, 10 | { 11 | files: ['**/*.{ts,tsx}'], 12 | ...solid, 13 | }, 14 | { 15 | plugins: { 16 | '@typescript-eslint': typescriptEslint, 17 | }, 18 | languageOptions: { 19 | parser: tsParser, 20 | ecmaVersion: 'latest', 21 | sourceType: 'module', 22 | globals: { 23 | ...globals.node, 24 | }, 25 | }, 26 | 27 | rules: { 28 | '@typescript-eslint/no-unused-vars': [ 29 | 1, 30 | { 31 | argsIgnorePattern: '^_', 32 | varsIgnorePattern: '^_', 33 | }, 34 | ], 35 | 'solid/no-unknown-namespaces': [ 36 | 'off', 37 | { 38 | // an array of additional namespace names to allow 39 | allowedNamespaces: [], // Array 40 | }, 41 | ], 42 | }, 43 | }, 44 | ]; 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "solid-number-flow", 3 | "version": "0.5.3", 4 | "author": "Carlo Taleon", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/Blankeos/solid-number-flow.git" 8 | }, 9 | "main": "./dist/index.js", 10 | "module": "./dist/index.js", 11 | "devDependencies": { 12 | "@changesets/changelog-github": "^0.5.0", 13 | "@changesets/cli": "^2.27.10", 14 | "@kobalte/core": "^0.13.7", 15 | "@tailwindcss/typography": "^0.5.15", 16 | "@types/node": "^20.12.12", 17 | "@typescript-eslint/eslint-plugin": "^8.19.1", 18 | "@typescript-eslint/parser": "^8.19.1", 19 | "auto-changelog": "^2.5.0", 20 | "autoprefixer": "^10.4.20", 21 | "clsx": "^2.1.1", 22 | "concurrently": "^8.2.2", 23 | "esbuild": "^0.21.3", 24 | "esbuild-plugin-solid": "^0.6.0", 25 | "eslint": "^9.18.0", 26 | "eslint-plugin-eslint-comments": "^3.2.0", 27 | "eslint-plugin-no-only-tests": "^3.3.0", 28 | "eslint-plugin-solid": "^0.14.5", 29 | "jsdom": "^24.0.0", 30 | "postcss": "^8.4.47", 31 | "prettier": "3.3.3", 32 | "prettier-plugin-tailwindcss": "^0.6.8", 33 | "shiki": "^1.22.0", 34 | "shikiji": "^0.10.2", 35 | "solid-js": "^1.9.4", 36 | "solid-marked": "^0.6.3", 37 | "tailwindcss": "^3.4.14", 38 | "tsup": "^8.3.0", 39 | "tsup-preset-solid": "^2.2.0", 40 | "typescript": "^5.6.3", 41 | "unplugin-solid-marked": "^0.6.3", 42 | "vike": "^0.4.199", 43 | "vike-solid": "^0.7.6", 44 | "vite": "^5.4.9", 45 | "vite-plugin-solid": "^2.10.2", 46 | "vitest": "^1.6.0" 47 | }, 48 | "peerDependencies": { 49 | "solid-js": "^1.9.4" 50 | }, 51 | "exports": { 52 | "solid": { 53 | "development": "./dist/dev.jsx", 54 | "import": "./dist/index.jsx" 55 | }, 56 | "development": { 57 | "import": { 58 | "types": "./dist/index.d.ts", 59 | "default": "./dist/dev.js" 60 | } 61 | }, 62 | "import": { 63 | "types": "./dist/index.d.ts", 64 | "default": "./dist/index.js" 65 | } 66 | }, 67 | "browser": {}, 68 | "bugs": { 69 | "url": "https://github.com/Blankeos/solid-number-flow/issues" 70 | }, 71 | "contributors": [ 72 | { 73 | "name": "Maxwell Barvian", 74 | "email": "max@barvian.me", 75 | "url": "https://barvian.me" 76 | } 77 | ], 78 | "description": "A SolidJS component to transition, format, and localize numbers. Forked from @barvian/number-flow.", 79 | "engines": { 80 | "node": ">=18" 81 | }, 82 | "files": [ 83 | "dist" 84 | ], 85 | "homepage": "https://solid-number-flow.pages.dev", 86 | "keywords": [ 87 | "solid", 88 | "solid-js", 89 | "number-flow", 90 | "slot machine", 91 | "accessible", 92 | "odometer", 93 | "animation", 94 | "number-format", 95 | "number-animation", 96 | "animated-number" 97 | ], 98 | "license": "MIT", 99 | "private": false, 100 | "scripts": { 101 | "dev": "vite serve dev", 102 | "build": "tsup", 103 | "build:site": "vite build dev", 104 | "preview:site": "vite preview dev", 105 | "test": "concurrently bun:test:*", 106 | "test:client": "vitest", 107 | "test:ssr": "bun run test:client --mode ssr", 108 | "prepublishOnly": "bun run build", 109 | "format": "prettier --ignore-path .gitignore -w \"src/**/*.{js,ts,json,css,tsx,jsx}\" \"dev/**/*.{js,ts,json,css,tsx,jsx}\"", 110 | "lint": "concurrently bun:lint:*", 111 | "lint:code": "eslint .", 112 | "lint:types": "tsc --noEmit", 113 | "update-deps": "bunx npm-check-updates --format group --interactive", 114 | "ci": "bun run lint && bun run build", 115 | "publish-ci": "bun run lint && bun run build && changeset publish" 116 | }, 117 | "sideEffects": false, 118 | "type": "module", 119 | "types": "./dist/index.d.ts", 120 | "typesVersions": {}, 121 | "dependencies": { 122 | "number-flow": "^0.5.3" 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | type Data, 3 | define, 4 | type Format, 5 | formatToData, 6 | NumberFlowLite, 7 | type Props, 8 | renderInnerHTML, 9 | type Value, 10 | } from 'number-flow'; 11 | import { 12 | Accessor, 13 | createContext, 14 | createEffect, 15 | createMemo, 16 | createSignal, 17 | FlowProps, 18 | onCleanup, 19 | onMount, 20 | splitProps, 21 | useContext, 22 | VoidProps, 23 | } from 'solid-js'; 24 | import { JSX } from 'solid-js/jsx-runtime'; 25 | import { Dynamic } from 'solid-js/web'; 26 | export type { Format, Trend, Value } from 'number-flow'; 27 | 28 | // Can't wait to not have to do this in React 19: 29 | const OBSERVED_ATTRIBUTES = ['data', 'digits'] as const; 30 | type ObservedAttribute = (typeof OBSERVED_ATTRIBUTES)[number]; 31 | export class NumberFlowElement extends NumberFlowLite { 32 | static observedAttributes = OBSERVED_ATTRIBUTES; 33 | attributeChangedCallback(_attr: ObservedAttribute, _oldValue: string, _newValue: string) { 34 | // this[attr] = JSON.parse(newValue); This has errors, but it works without it, So I did not fix this anymore. 35 | } 36 | } 37 | 38 | define('number-flow', NumberFlowElement); 39 | 40 | type BaseProps = JSX.HTMLAttributes & 41 | Partial & { 42 | isolate?: boolean; 43 | willChange?: boolean; 44 | onAnimationsStart?: (e: CustomEvent) => void; 45 | onAnimationsFinish?: (e: CustomEvent) => void; 46 | }; 47 | 48 | type NumberFlowImplProps = BaseProps & { 49 | innerRef: NumberFlowElement | undefined; 50 | group: Accessor; 51 | data: Accessor; 52 | }; 53 | 54 | // You're supposed to cache these between uses: 55 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString 56 | // Serialize to strings b/c React: 57 | const formatters: Record = {}; 58 | 59 | // =========================================================================== 60 | // IMPLEMENTATION (Equivalent to the React Class Component) 61 | // =========================================================================== 62 | /** Used for `prevProps` because accessing signals always gives "latest" values, we don't want that. */ 63 | type NumberFlowImplProps_NoSignals = Omit & { 64 | group: GroupContext | undefined; 65 | data: Data | undefined; 66 | }; 67 | 68 | function NumberFlowImpl(props: VoidProps) { 69 | let el: NumberFlowElement | undefined; 70 | 71 | const updateProperties = (prevProps?: NumberFlowImplProps_NoSignals) => { 72 | if (!el) return; 73 | 74 | // // el.manual = !props.isolate; (Not sure why but this breaks the animations, so isolate might not work right now. I personally think it has a very niche usecase though). 75 | if (props.transformTiming) 76 | el.transformTiming ?? NumberFlowElement.defaultProps['transformTiming']; 77 | if (props.spinTiming) el.spinTiming ?? NumberFlowElement.defaultProps['spinTiming']; 78 | if (props.opacityTiming) el.opacityTiming ?? NumberFlowElement.defaultProps['opacityTiming']; 79 | if (props.animated != null) el.animated = props.animated; 80 | if (props.respectMotionPreference != null) 81 | el.respectMotionPreference = props.respectMotionPreference; 82 | if (props.trend != null) el.trend = props.trend; 83 | if (props.plugins != null) el.plugins = props.plugins; 84 | 85 | // eslint-disable-next-line solid/reactivity 86 | if (prevProps?.onAnimationsStart) 87 | // eslint-disable-next-line solid/reactivity 88 | el.removeEventListener('onanimationsstart', prevProps.onAnimationsStart as EventListener); 89 | if (props.onAnimationsStart) 90 | el.addEventListener('animationsstart', props.onAnimationsStart as EventListener); 91 | 92 | // eslint-disable-next-line solid/reactivity 93 | if (prevProps?.onAnimationsFinish) 94 | // eslint-disable-next-line solid/reactivity 95 | el.removeEventListener('onanimationsfinish', prevProps.onAnimationsFinish as EventListener); 96 | if (props.onAnimationsFinish) 97 | el.addEventListener('onanimationsfinish', props.onAnimationsFinish as EventListener); 98 | }; 99 | 100 | // Equivalent of componentDidMount 101 | onMount(() => { 102 | updateProperties(); 103 | if (el) { 104 | el.digits = props.digits; 105 | el.data = props.data(); 106 | } 107 | }); 108 | 109 | // Equivalent of getSnapshotBeforeUpdate 110 | // @ts-ignore 111 | createEffect((prevProps?: NumberFlowImplProps_NoSignals) => { 112 | updateProperties(prevProps); 113 | 114 | // eslint-disable-next-line solid/reactivity 115 | if (prevProps?.data !== props.data()) { 116 | if (props.group()) { 117 | props.group()!.willUpdate(); 118 | props.group()!.didUpdate(); 119 | return; 120 | } 121 | if (!props.isolate) { 122 | el?.willUpdate(); 123 | el?.didUpdate(); 124 | return; 125 | } 126 | } 127 | 128 | return { 129 | ...props, 130 | group: props.group(), 131 | data: props.data(), 132 | }; 133 | }); 134 | 135 | /** 136 | * It's exactly like a signal setter, but we're setting two things: 137 | * - innerRef (from props) 138 | * - this ref 139 | */ 140 | const handleRef = (elRef: NumberFlowElement) => { 141 | // eslint-disable-next-line solid/reactivity 142 | props.innerRef = elRef; 143 | el = elRef; 144 | }; 145 | 146 | const [_used, others] = splitProps(props, [ 147 | // Remove the 'used' 148 | 'class', 149 | 'aria-label', 150 | 'role', 151 | 'digits', 152 | 'data', 153 | 'innerHTML', 154 | // Also remove the ones used in `updateProperties` 155 | 'transformTiming', 156 | 'spinTiming', 157 | 'opacityTiming', 158 | 'animated', 159 | 'respectMotionPreference', 160 | 'trend', 161 | 'plugins', 162 | ]); 163 | 164 | return ( 165 | 180 | ); 181 | } 182 | 183 | // =========================================================================== 184 | // ROOT 185 | // =========================================================================== 186 | export type NumberFlowProps = BaseProps & { 187 | value: Value; 188 | locales?: Intl.LocalesArgument; 189 | format?: Format; 190 | prefix?: string; 191 | suffix?: string; 192 | }; 193 | 194 | export default function NumberFlow(props: VoidProps) { 195 | const [_, others] = splitProps(props, ['value', 'locales', 'format', 'prefix', 'suffix']); 196 | 197 | let innerRef: NumberFlowElement | undefined; 198 | const group = useNumberFlowGroupContext(); 199 | 200 | const localesString = createMemo(() => (props.locales ? JSON.stringify(props.locales) : '')); 201 | const formatString = createMemo(() => (props.format ? JSON.stringify(props.format) : '')); 202 | const data = createMemo(() => { 203 | const formatter = (formatters[`${localesString()}:${formatString()}`] ??= new Intl.NumberFormat( 204 | props.locales, 205 | props.format, 206 | )); 207 | 208 | return formatToData(props.value, formatter, props.prefix, props.suffix); 209 | }); 210 | 211 | return ; 212 | } 213 | 214 | // =========================================================================== 215 | // NumberFlowGroup 216 | // =========================================================================== 217 | 218 | type GroupContext = { 219 | useRegister: (ref: NumberFlowElement) => void; 220 | willUpdate: () => void; 221 | didUpdate: () => void; 222 | }; 223 | 224 | const NumberFlowGroupContext = createContext>(() => undefined); 225 | 226 | const useNumberFlowGroupContext = () => useContext(NumberFlowGroupContext); 227 | 228 | export function NumberFlowGroup(props: FlowProps) { 229 | let flows = new Set(); 230 | let updating = false; 231 | let pending = new WeakMap(); 232 | 233 | const value = createMemo(() => ({ 234 | useRegister(ref) { 235 | onMount(() => { 236 | flows.add(ref); 237 | onCleanup(() => { 238 | flows.delete(ref); 239 | }); 240 | }); 241 | }, 242 | willUpdate() { 243 | if (updating) return; 244 | updating = true; 245 | flows.forEach((ref) => { 246 | const f = ref; 247 | if (!f || !f.created) return; 248 | f.willUpdate(); 249 | pending.set(f, true); 250 | }); 251 | }, 252 | didUpdate() { 253 | flows.forEach((ref) => { 254 | const f = ref; 255 | if (!f || !pending.get(f)) return; 256 | f.didUpdate(); 257 | pending.delete(f); 258 | }); 259 | updating = false; 260 | }, 261 | })); 262 | 263 | return ( 264 | 265 | {props.children} 266 | 267 | ); 268 | } 269 | 270 | // =========================================================================== 271 | // src/index.tsx 272 | // =========================================================================== 273 | 274 | import { 275 | canAnimate as _canAnimate, 276 | prefersReducedMotion as _prefersReducedMotion, 277 | } from 'number-flow'; 278 | 279 | function usePrefersReducedMotion() { 280 | const [prefersReducedMotion, set] = createSignal(false); 281 | 282 | onMount(() => { 283 | set(_prefersReducedMotion?.matches ?? false); 284 | 285 | const onChange = ({ matches }: MediaQueryListEvent) => { 286 | set(matches); 287 | }; 288 | _prefersReducedMotion?.addEventListener('change', onChange); 289 | 290 | onCleanup(() => { 291 | _prefersReducedMotion?.removeEventListener('change', onChange); 292 | }); 293 | }); 294 | 295 | return prefersReducedMotion; 296 | } 297 | 298 | /** Untested, but based on the implementation in https://github.com/barvian/number-flow/blob/main/packages/svelte/src/lib/index.ts. */ 299 | export function useCanAnimate( 300 | props: { respectMotionPreference: boolean } = { respectMotionPreference: true }, 301 | ) { 302 | const [canAnimate, setCanAnimate] = createSignal(_canAnimate); 303 | 304 | onMount(() => { 305 | setCanAnimate(_canAnimate); 306 | }); 307 | 308 | const prefersReducedMotion = usePrefersReducedMotion(); 309 | 310 | const canAnimateWithPreference = createMemo(() => { 311 | canAnimate() && !prefersReducedMotion(); 312 | }); 313 | 314 | const finalCanAnimate = createMemo(() => { 315 | return props.respectMotionPreference ? canAnimateWithPreference() : canAnimate(); 316 | }); 317 | 318 | return finalCanAnimate; 319 | } 320 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | export default { 3 | content: ['./dev/**/*.{html,js,ts,jsx,tsx}'], 4 | theme: { 5 | extend: { 6 | fontFamily: { 7 | geist: ['Geist', 'sans-serif'], 8 | }, 9 | }, 10 | }, 11 | plugins: [require('@tailwindcss/typography')], 12 | } 13 | -------------------------------------------------------------------------------- /test/index.test.tsx: -------------------------------------------------------------------------------- 1 | import { isServer } from 'solid-js/web'; 2 | import { describe, expect, it } from 'vitest'; 3 | 4 | describe('environment', () => { 5 | it('runs on client', () => { 6 | expect(typeof window).toBe('object'); 7 | expect(isServer).toBe(false); 8 | }); 9 | }); 10 | -------------------------------------------------------------------------------- /test/server.test.tsx: -------------------------------------------------------------------------------- 1 | import { describe, expect, it } from 'vitest'; 2 | import { renderToString } from 'solid-js/web'; 3 | import NumberFlow from 'src'; 4 | 5 | describe('Hello', () => { 6 | it('renders a hello component', () => { 7 | const string = renderToString(() => ); 8 | expect(string).toContain('123'); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true, 4 | "target": "ESNext", 5 | "module": "ESNext", 6 | "lib": ["DOM", "DOM.Iterable", "ESNext"], 7 | "moduleResolution": "node", 8 | "resolveJsonModule": true, 9 | "esModuleInterop": true, 10 | "noEmit": true, 11 | "isolatedModules": true, 12 | "skipLibCheck": true, 13 | "allowSyntheticDefaultImports": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "noUncheckedIndexedAccess": true, 16 | "jsx": "preserve", 17 | "jsxImportSource": "solid-js", 18 | "types": [], 19 | "baseUrl": "." 20 | }, 21 | "exclude": ["node_modules", "dist", "./dev"] 22 | } 23 | -------------------------------------------------------------------------------- /tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup' 2 | import * as preset from 'tsup-preset-solid' 3 | 4 | const preset_options: preset.PresetOptions = { 5 | // array or single object 6 | entries: [ 7 | // default entry (index) 8 | { 9 | // entries with '.tsx' extension will have `solid` export condition generated 10 | entry: 'src/index.tsx', 11 | // will generate a separate development entry 12 | dev_entry: true, 13 | }, 14 | ], 15 | // Set to `true` to remove all `console.*` calls and `debugger` statements in prod builds 16 | drop_console: true, 17 | // Set to `true` to generate a CommonJS build alongside ESM 18 | // cjs: true, 19 | } 20 | 21 | const CI = 22 | process.env['CI'] === 'true' || 23 | process.env['GITHUB_ACTIONS'] === 'true' || 24 | process.env['CI'] === '"1"' || 25 | process.env['GITHUB_ACTIONS'] === '"1"' 26 | 27 | export default defineConfig(config => { 28 | const watching = !!config.watch 29 | 30 | const parsed_options = preset.parsePresetOptions(preset_options, watching) 31 | 32 | if (!watching && !CI) { 33 | const package_fields = preset.generatePackageExports(parsed_options) 34 | 35 | console.log(`package.json: \n\n${JSON.stringify(package_fields, null, 2)}\n\n`) 36 | 37 | // will update ./package.json with the correct export fields 38 | preset.writePackageJson(package_fields) 39 | } 40 | 41 | return preset.generateTsupOptions(parsed_options) 42 | }) 43 | -------------------------------------------------------------------------------- /vitest.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vitest/config' 2 | import solidPlugin from 'vite-plugin-solid' 3 | 4 | export default defineConfig(({ mode }) => { 5 | // to test in server environment, run with "--mode ssr" or "--mode test:ssr" flag 6 | // loads only server.test.ts file 7 | const testSSR = mode === 'test:ssr' || mode === 'ssr' 8 | 9 | return { 10 | plugins: [ 11 | solidPlugin({ 12 | // https://github.com/solidjs/solid-refresh/issues/29 13 | hot: false, 14 | // For testing SSR we need to do a SSR JSX transform 15 | solid: { generate: testSSR ? 'ssr' : 'dom' }, 16 | }), 17 | ], 18 | test: { 19 | watch: false, 20 | isolate: !testSSR, 21 | env: { 22 | NODE_ENV: testSSR ? 'production' : 'development', 23 | DEV: testSSR ? '' : '1', 24 | SSR: testSSR ? '1' : '', 25 | PROD: testSSR ? '1' : '', 26 | }, 27 | environment: testSSR ? 'node' : 'jsdom', 28 | transformMode: { web: [/\.[jt]sx$/] }, 29 | ...(testSSR 30 | ? { 31 | include: ['test/server.test.{ts,tsx}'], 32 | } 33 | : { 34 | include: ['test/*.test.{ts,tsx}'], 35 | exclude: ['test/server.test.{ts,tsx}'], 36 | }), 37 | }, 38 | resolve: { 39 | conditions: testSSR ? ['node'] : ['browser', 'development'], 40 | }, 41 | } 42 | }) 43 | --------------------------------------------------------------------------------