Solid Primitives Range

# @solid-primitives/range [![size](https://img.shields.io/badge/size-1.53_kB-blue?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/range) [![version](https://img.shields.io/npm/v/@solid-primitives/range?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/range) [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-1.json)](https://github.com/solidjs-community/solid-primitives#contribution-process) [![tested with vitest](https://img.shields.io/badge/tested_with-vitest-6E9F18?style=for-the-badge&logo=vitest)](https://vitest.dev) Control Flow Primitives for displaying a number range or given number of elements. - [`createNumericRange`](#createnumericrange) - Reactively generates a number array for a given range. Convenient companion to the built-in ``. - [`repeat`](#repeat) - Primitive for mapping a number of elements. Underlying helper for the [``](#repeat-1) control flow. - [``](#repeat-1) - Control Flow Component for displaying a number of elements. ⚠️ Deprecated — use Solid 2.0's built-in ``. - [`mapRange`](#maprange) - Primitive for mapping a number range of given start, end, and step values. Underlying helper for the [``](#range) control flow. - [``](#range) - Control Flow Component for displaying a number range of elements. - [`indexRange`](#indexrange) - Primitive for mapping a number range while keeping previous elements of the same index. Underlying helper for the [``](#indexrange-1) control flow. - [``](#indexrange-1) - Control Flow Component for displaying a number range of elements, where elements receive a number value as signal. ## Installation ```bash npm install @solid-primitives/range # or yarn add @solid-primitives/range # or pnpm add @solid-primitives/range ``` ## `createNumericRange` ```tsx repl import { render } from "@solidjs/web"; import { createSignal, For } from "solid-js"; import { createNumericRange } from "@solid-primitives/range"; function Demo() { const [to, setTo] = createSignal(5); const nums = createNumericRange(0, to, 2); return ( <> setTo(+e.currentTarget.value)} />

createNumericRange(0, {to()}, 2):

{n => {n} } ); } render(() => , document.getElementById("app")!); ``` Reactively generates an array of numbers for the given range. Mirrors the `range()` API from Python — one argument gives `[0, to)`, two or three give `[from, to)` with an optional step. All arguments accept either a plain number or a reactive accessor. Pairs naturally with Solid 2.0's built-in ``. ```ts // static createNumericRange(5) // [0, 1, 2, 3, 4] createNumericRange(2, 7) // [2, 3, 4, 5, 6] createNumericRange(0, 10, 2) // [0, 2, 4, 6, 8] createNumericRange(5, 0) // [5, 4, 3, 2, 1] (descending) // reactive const [to, setTo] = createSignal(5); const nums = createNumericRange(to); // updates when `to` changes // compose with — pass the signal, not the called value const nums = createNumericRange(count); {n =>
{n}
}
``` #### Definition ```ts function createNumericRange(to: MaybeAccessor): Accessor; function createNumericRange( start: MaybeAccessor, to: MaybeAccessor, step?: MaybeAccessor, ): Accessor; ``` ## `repeat` Reactively maps a number range of specified length with a callback function - underlying helper for the `` control flow. ```tsx repl import { render } from "@solidjs/web"; import { createMemo, createSignal } from "solid-js"; import { repeat } from "@solid-primitives/range"; function Demo() { const [count, setCount] = createSignal(3); const items = createMemo( repeat(count, i => (
  • item {i} (created at {new Date().toLocaleTimeString()})
  • )), ); return ( <>
      {items()}
    ); } render(() => , document.getElementById("app")!); ``` ```ts const [length, setLength] = createSignal(10); const mapped = repeat(length, index => { const [value, setValue] = createSignal(index); createEffect( () => value(), v => { /* react to value */ }, ); return value; }); ``` #### Definition ```ts function repeat( times: Accessor, mapFn: (i: number) => T, options?: { fallback?: Accessor; }, ): Accessor; ``` ## `` ```tsx repl import { render } from "@solidjs/web"; import { createSignal } from "solid-js"; import { Repeat } from "@solid-primitives/range"; function Demo() { const [times, setTimes] = createSignal(3); return ( <> no items

    }> {i =>
    ★ {i}
    }
    ); } render(() => , document.getElementById("app")!); ``` > **Deprecated** — Solid 2.0 ships a built-in `` in `@solidjs/web`. Migrate to that: > > ```tsx > import { Repeat } from "@solidjs/web"; > > // basic — children receive a plain index number > {(i) =>
    {i}
    }
    > > // with offset (replaces a start > 0 use-case) > {(i) =>
    {i}
    }
    > > // with fallback — wrap in since built-in has no fallback prop > 0} fallback={

    no items

    }> > {(i) =>
    {i}
    }
    >
    > ``` > > **Note:** The built-in `` does not diff — it re-renders all children when `count` changes. If incremental creation/disposal of children is important for your use case, keep using this primitive. Control Flow component for displaying a specified number of elements. The `times` prop is reactive – changing it will only create new elements for added numbers. ```tsx
    // with a render prop: {n =>
    {n}
    }
    // with fallback: no items...

    }>
    ``` #### Definition ```ts function Repeat(props: { times: number; fallback?: T; children: ((index: number) => T) | T; }): Accessor; ``` ## `mapRange` ```tsx repl import { render } from "@solidjs/web"; import { createMemo, createSignal } from "solid-js"; import { mapRange } from "@solid-primitives/range"; function Demo() { const [to, setTo] = createSignal(2); const items = createMemo( mapRange( () => 0, to, () => 0.5, n =>
  • {n}
  • , ), ); return ( <>

    mapRange(0, {to()}, 0.5)

      {items()}
    ); } render(() => , document.getElementById("app")!); ``` Reactively maps a number range of specified `start`, `to` and `step`, with a callback function - underlying helper for the [``](#range) control flow. All `start`, `to` and `step` arguments are accessors, and changing them will cause the mapped array to be recalculated, mapping new items for numbers added to the range. `step` will become negative _(the range will be descending)_ if `to` is smaller than `start`. Range stops at `to`, it is not included in the range. ```ts const [to, setTo] = createSignal(5); const mapped = mapRange( () => 0, to, () => 0.5, number => { const [value, setValue] = createSignal(number); createEffect( () => value(), v => { /* react to value */ }, ); return value; }, ); mapped(); // => [0, 0.5, 1, 1.5, 2...] setTo(3); // changes the output array, mapping only added numbers ``` #### Definition ```ts function mapRange( start: Accessor, to: Accessor, step: Accessor, mapFn: (n: number) => T, options?: { fallback?: Accessor; }, ): Accessor; ``` ## `` Creates a list of elements by mapping a number range of specified `start`, `to`, and `step`. All `start`, `to` and `step` props are reactive, and changing them will cause the elements array to be recalculated, creating new elements for numbers added to the range. - `start` defaults to 0. - `to` defaults to 1. Range stops at `to`, it is not included in the range. - `step` will become negative _(the range will be descending)_ if `to` is smaller than `start`. ```tsx
    // with a render prop: {n =>
    {n}
    }
    // with fallback: no items...

    }>
    ``` Array spread shortcut: ```tsx const [start, setStart] = createSignal(0); const [to, setTo] = createSignal(10); const [step, setStep] = createSignal(2); ``` #### Definition `RangeProps` is an interface of `start`, `to` and `step` props, OR `0`, `1` and `2` indexes of a spread array. ```ts function Range( props: RangeProps & { fallback?: T; children: ((number: number) => T) | T; }, ): Accessor; ``` ## `indexRange` ```tsx repl import { render } from "@solidjs/web"; import { createMemo, createSignal } from "solid-js"; import { indexRange } from "@solid-primitives/range"; function Demo() { const [start, setStart] = createSignal(0); const items = createMemo( indexRange( start, () => start() + 5, () => 1, n =>
  • {n()}
  • , ), ); return ( <>
      {items()}
    ); } render(() => , document.getElementById("app")!); ``` Primitive for mapping a number range of specified `start`, `to` and `step`, while keeping previous elements of the same index. Underlying helper for the [``](#indexrange-1) control flow. All `start`, `to` and `step` arguments are accessors, and changing them will cause the mapped array to be recalculated, mapping new items appended at the end of the range. `step` will become negative _(the range will be descending)_ if `to` is smaller than `start`. Range stops at `to`, it is not included in the range. ```ts const [to, setTo] = createSignal(5); const mapped = indexRange( () => 0, to, () => 0.5, number => { const [value, setValue] = createSignal(number()); createEffect( () => number(), n => handleNewNumber(n), ); return value; }, ); mapped(); // => [0, 0.5, 1, 1.5, 2...] setTo(3); // changes the output array, mapping only added indexes ``` #### Definition ```ts function indexRange( start: Accessor, to: Accessor, step: Accessor, mapFn: (n: Accessor) => T, options?: { fallback?: Accessor; }, ): Accessor; ``` ## `` ```tsx repl import { render } from "@solidjs/web"; import { createSignal } from "solid-js"; import { IndexRange } from "@solid-primitives/range"; function Demo() { const [to, setTo] = createSignal(3); return ( <> setTo(Math.min(+e.currentTarget.value, 20))} /> empty range

    }> {n =>
    row {n()}
    }
    ); } render(() => , document.getElementById("app")!); ``` Control Flow Component for displaying a number range of elements, where elements receive a number value as signal, by mapping a number range of specified `start`, `to`, and `step`. All `start`, `to` and `step` props are reactive, and changing them will cause the elements array to be recalculated, creating new elements for numbers added to the range. - `start` defaults to 0. - `to` defaults to 1. Range stops at `to`, it is not included in the range. - `step` will become negative _(the range will be descending)_ if `to` is smaller than `start`. ```tsx
    // with a render prop: {n =>
    {n()}
    }
    // with fallback: no items...

    }>
    ``` Array spread shortcut: ```tsx const [start, setStart] = createSignal(0); const [to, setTo] = createSignal(10); const [step, setStep] = createSignal(2); ``` #### Definition `RangeProps` is an interface of `start`, `to` and `step` props, OR `0`, `1` and `2` indexes of a spread array. ```ts function IndexRange( props: RangeProps & { fallback?: T; children: ((number: Accessor) => T) | T; }, ): Accessor; ``` ## Possible improvements ###### (PRs Welcome) - [ ] Currently the `mapRange` is handling decremanting ranges by swapping `start` and `to` with each other, and then cloning and reversing the mapped array. Doing this during the range mapping could possibly be more performant. - [ ] For Ranges, because of how numbers are calculated, fractions might sometimes loose precision. E.g. a range from `1.64` to `2` by `0.2` step would generate numbers: `[1.64, 1.8399999999999999]` instead of `[1.64, 1.84]`. - [ ] Both `mapRange` and `indexRange` are missing index arguments in the mapping function. ## Changelog See [CHANGELOG.md](./CHANGELOG.md)