@octanejs/visx 0.1.43 → 0.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,6 +4,13 @@ The complete current Airbnb Visx 4.x visualization toolkit for Octane. The
4
4
  package keeps the upstream aggregate API and exposes every public feature package
5
5
  as a subpath:
6
6
 
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @octanejs/visx
11
+ pnpm add @octanejs/visx
12
+ ```
13
+
7
14
  ```tsrx
8
15
  import { AxisBottom, AxisLeft } from '@octanejs/visx/axis';
9
16
  import { scaleBand, scaleLinear } from '@octanejs/visx/scale';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/visx",
3
- "version": "0.1.43",
3
+ "version": "0.1.45",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -123,10 +123,10 @@
123
123
  "mitt": "^2.1.0",
124
124
  "reduce-css-calc": "^1.3.0",
125
125
  "svg-path-properties": "2.0.2",
126
- "@octanejs/floating-ui": "0.1.48"
126
+ "@octanejs/floating-ui": "0.1.50"
127
127
  },
128
128
  "peerDependencies": {
129
- "octane": "0.1.49"
129
+ "octane": "0.1.51"
130
130
  },
131
131
  "devDependencies": {
132
132
  "@juggle/resize-observer": "^3.4.0",
@@ -149,7 +149,7 @@
149
149
  "react-dom": "^19.2.7",
150
150
  "typescript": "^5.9.3",
151
151
  "vitest": "^4.1.10",
152
- "octane": "0.1.49"
152
+ "octane": "0.1.51"
153
153
  },
154
154
  "scripts": {
155
155
  "compile:check": "node scripts/check-tsrx-compile.mjs",
@@ -0,0 +1,21 @@
1
+ const INDEX_THRESHOLD = 64;
2
+
3
+ export type CategoricalIndex<Domain extends readonly string[]> = (key: Domain[number]) => number;
4
+
5
+ export default function createCategoricalIndex<Domain extends readonly string[]>(
6
+ domain: Domain,
7
+ ): CategoricalIndex<Domain> {
8
+ if (domain.length < INDEX_THRESHOLD) {
9
+ return (key) => domain.indexOf(key);
10
+ }
11
+
12
+ const indexes = new Map<string, number>();
13
+ for (let index = 0; index < domain.length; index++) {
14
+ const key = domain[index];
15
+ if (indexes.get(key) === undefined) {
16
+ indexes.set(key, index);
17
+ }
18
+ }
19
+
20
+ return (key) => indexes.get(key) ?? -1;
21
+ }
@@ -1,3 +1,6 @@
1
+ import { useStructuralMemo } from '@octanejs/visx/kernel';
2
+ import { useMemo } from 'octane';
3
+ import createCategoricalIndex from './categoricalIndex';
1
4
  import { getCategoricalColor } from './categoricalColor';
2
5
  import useTheme from './useTheme.tsrx';
3
6
  import warn from './warn';
@@ -7,17 +10,22 @@ export type CategoricalColorAccessor<Domain extends readonly string[]> =
7
10
 
8
11
  export default function useCategoricalScale<Domain extends readonly string[]>(domain: Domain): CategoricalColorAccessor<Domain> {
9
12
  const theme = useTheme();
13
+ const stableDomain = useStructuralMemo(domain, 1);
14
+ const colors = theme.colors.categorical;
10
15
 
11
- return (key) => {
12
- const index = domain.indexOf(key);
16
+ return useMemo(() => {
17
+ const indexOf = createCategoricalIndex(stableDomain);
18
+ return (key) => {
19
+ const index = indexOf(key);
13
20
 
14
- if (index < 0) {
15
- warn(
16
- `[@octanejs/visx/theme] useCategoricalScale received "${key}" outside its domain; using index 0.`,
17
- );
18
- return getCategoricalColor(theme.colors.categorical, 0);
19
- }
21
+ if (index < 0) {
22
+ warn(
23
+ `[@octanejs/visx/theme] useCategoricalScale received "${key}" outside its domain; using index 0.`,
24
+ );
25
+ return getCategoricalColor(colors, 0);
26
+ }
20
27
 
21
- return getCategoricalColor(theme.colors.categorical, index);
22
- };
28
+ return getCategoricalColor(colors, index);
29
+ };
30
+ }, [colors, stableDomain]);
23
31
  }
@@ -1,5 +1,6 @@
1
1
  import { useStructuralMemo } from '@octanejs/visx/kernel';
2
2
  import { useMemo } from 'octane';
3
+ import createCategoricalIndex from './categoricalIndex';
3
4
  import { getCategoricalColor } from './categoricalColor';
4
5
  import useTheme from './useTheme.tsrx';
5
6
  import warn from './warn';
@@ -21,9 +22,10 @@ export default function useColorScale<Domain extends readonly string[]>(
21
22
  const fallbackColors = theme.colors.categorical;
22
23
  const colors = stableRange ?? fallbackColors;
23
24
 
24
- return useMemo(
25
- () => (key) => {
26
- const index = stableDomain.indexOf(key);
25
+ return useMemo(() => {
26
+ const indexOf = createCategoricalIndex(stableDomain);
27
+ return (key) => {
28
+ const index = indexOf(key);
27
29
 
28
30
  if (index < 0) {
29
31
  warn(
@@ -33,7 +35,6 @@ export default function useColorScale<Domain extends readonly string[]>(
33
35
  }
34
36
 
35
37
  return colors[index % colors.length] ?? getCategoricalColor(fallbackColors, index);
36
- },
37
- [colors, fallbackColors, stableDomain],
38
- );
38
+ };
39
+ }, [colors, fallbackColors, stableDomain]);
39
40
  }