@cognitap/radiant 0.0.60 → 0.0.62

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.
Files changed (57) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/library/Breadcrumb.d.ts.map +1 -1
  3. package/dist/library/Breadcrumb.js +8 -5
  4. package/dist/library/Breadcrumb.js.map +1 -1
  5. package/dist/library/Pagination.d.ts +2 -2
  6. package/dist/library/Pagination.d.ts.map +1 -1
  7. package/dist/library/Pagination.js +9 -8
  8. package/dist/library/Pagination.js.map +1 -1
  9. package/dist/library/Resizable.d.ts +56 -0
  10. package/dist/library/Resizable.d.ts.map +1 -0
  11. package/dist/library/Resizable.js +345 -0
  12. package/dist/library/Resizable.js.map +1 -0
  13. package/dist/library/Separator.d.ts +6 -1
  14. package/dist/library/Separator.d.ts.map +1 -1
  15. package/dist/library/Separator.js +11 -3
  16. package/dist/library/Separator.js.map +1 -1
  17. package/dist/library/Table.d.ts +2 -7
  18. package/dist/library/Table.d.ts.map +1 -1
  19. package/dist/library/Table.js +3 -4
  20. package/dist/library/Table.js.map +1 -1
  21. package/dist/library/cn.d.ts.map +1 -1
  22. package/dist/library/cn.js +4 -0
  23. package/dist/library/cn.js.map +1 -1
  24. package/dist/library/index.d.ts +1 -0
  25. package/dist/library/index.d.ts.map +1 -1
  26. package/dist/library/index.js +1 -0
  27. package/dist/library/index.js.map +1 -1
  28. package/dist/library/internal/decimal.d.ts +3 -0
  29. package/dist/library/internal/decimal.d.ts.map +1 -0
  30. package/dist/library/internal/decimal.js +15 -0
  31. package/dist/library/internal/decimal.js.map +1 -0
  32. package/dist/library/internal/numberField.d.ts +1 -1
  33. package/dist/library/internal/numberField.d.ts.map +1 -1
  34. package/dist/library/internal/numberField.js +3 -13
  35. package/dist/library/internal/numberField.js.map +1 -1
  36. package/dist/library/internal/resizable.d.ts +15 -0
  37. package/dist/library/internal/resizable.d.ts.map +1 -0
  38. package/dist/library/internal/resizable.js +86 -0
  39. package/dist/library/internal/resizable.js.map +1 -0
  40. package/dist/library/styles.css +1 -1
  41. package/dist/library/themes/components.d.ts +10 -0
  42. package/dist/library/themes/components.d.ts.map +1 -1
  43. package/dist/library/themes/components.js +10 -0
  44. package/dist/library/themes/components.js.map +1 -1
  45. package/docs/architecture.md +4 -5
  46. package/package.json +1 -1
  47. package/src/Breadcrumb.tsx +8 -6
  48. package/src/Pagination.tsx +6 -10
  49. package/src/Resizable.tsx +581 -0
  50. package/src/Separator.tsx +22 -5
  51. package/src/Table.tsx +3 -10
  52. package/src/cn.ts +4 -0
  53. package/src/index.ts +13 -0
  54. package/src/internal/decimal.ts +17 -0
  55. package/src/internal/numberField.ts +4 -16
  56. package/src/internal/resizable.ts +115 -0
  57. package/src/themes/components.ts +10 -0
package/src/index.ts CHANGED
@@ -672,3 +672,16 @@ export {
672
672
  } from './CodeBlock.js'
673
673
 
674
674
  export { NumberField, type NumberFieldProps, type NumberFieldFormatOptions } from './NumberField.js'
675
+
676
+ export {
677
+ Resizable,
678
+ ResizablePanel,
679
+ ResizableHandle,
680
+ useResizable,
681
+ type ResizableProps,
682
+ type ResizablePanelProps,
683
+ type ResizableHandleProps,
684
+ type ResizableState,
685
+ type ResizablePanelState,
686
+ type ResizableItem
687
+ } from './Resizable.js'
@@ -0,0 +1,17 @@
1
+ /** Add finite JavaScript numbers through decimal coefficients before conversion. */
2
+ export function addDecimal(left: number, right: number) {
3
+ const parts = (value: number) => {
4
+ const [mantissa, exponent = '0'] = value.toString().split('e')
5
+ return {
6
+ coefficient: BigInt(mantissa!.replace('.', '')),
7
+ exponent: Number(exponent) - (mantissa!.split('.')[1]?.length ?? 0)
8
+ }
9
+ }
10
+ const a = parts(left),
11
+ b = parts(right),
12
+ exponent = Math.min(a.exponent, b.exponent)
13
+ const coefficient =
14
+ a.coefficient * 10n ** BigInt(a.exponent - exponent) +
15
+ b.coefficient * 10n ** BigInt(b.exponent - exponent)
16
+ return Number(coefficient.toString() + 'e' + exponent)
17
+ }
@@ -1,3 +1,5 @@
1
+ import { addDecimal } from './decimal.js'
2
+
1
3
  /** A bounded editable decimal adapter; Intl supplies display and locale symbols. */
2
4
  export type NumberFieldFormatOptions = {
3
5
  style?: 'decimal' | 'percent' | 'currency'
@@ -172,22 +174,8 @@ export function requireFiniteNumber(name: string, value: unknown, nullable = fal
172
174
  }
173
175
  export const clampNumber = (value: number, min?: number, max?: number) =>
174
176
  Math.min(max ?? Infinity, Math.max(min ?? -Infinity, value))
175
- /** Add decimal coefficients before converting back to the public JavaScript number. */
177
+ /** Add a decimal step, respecting the public finite-number range. */
176
178
  export function addNumberStep(value: number, amount: number, min?: number, max?: number) {
177
- const parts = (number: number) => {
178
- const [mantissa, exponent = '0'] = number.toString().split('e')
179
- const fraction = mantissa!.split('.')[1]?.length ?? 0
180
- return {
181
- coefficient: BigInt(mantissa!.replace('.', '')),
182
- exponent: Number(exponent) - fraction
183
- }
184
- }
185
- const a = parts(value),
186
- b = parts(amount),
187
- exponent = Math.min(a.exponent, b.exponent)
188
- const coefficient =
189
- a.coefficient * 10n ** BigInt(a.exponent - exponent) +
190
- b.coefficient * 10n ** BigInt(b.exponent - exponent)
191
- const next = clampNumber(Number(coefficient.toString() + 'e' + exponent), min, max)
179
+ const next = clampNumber(addDecimal(value, amount), min, max)
192
180
  return Number.isFinite(next) ? next : value
193
181
  }
@@ -0,0 +1,115 @@
1
+ import { addDecimal } from './decimal.js'
2
+ export type ResizableItem = { value: string; min?: number; max?: number; collapsible?: boolean }
3
+ export type ResizableRange = readonly [number, number]
4
+ const finite = (value: unknown): value is number =>
5
+ typeof value === 'number' && Number.isFinite(value)
6
+ export const sameResizableValue = (a: number[], b: number[]) =>
7
+ a.length === b.length && a.every((value, index) => value === b[index])
8
+ export function checkResizableItems(items: readonly ResizableItem[]) {
9
+ if (!Array.isArray(items) || items.length < 2)
10
+ throw new TypeError('Resizable requires at least two items.')
11
+ const seen = new Set<string>()
12
+ for (const item of items) {
13
+ if (!item || typeof item.value !== 'string' || !item.value.trim() || seen.has(item.value))
14
+ throw new TypeError('Resizable panel keys must be unique nonempty strings.')
15
+ seen.add(item.value)
16
+ const min = item.min ?? 0,
17
+ max = item.max ?? 100
18
+ if (!finite(min) || !finite(max) || min < 0 || max > 100 || min > max)
19
+ throw new TypeError('Resizable bounds must be percentages with 0 <= min <= max <= 100.')
20
+ }
21
+ }
22
+ export function checkResizableValue(
23
+ items: readonly ResizableItem[],
24
+ value: unknown
25
+ ): asserts value is number[] {
26
+ if (
27
+ !Array.isArray(value) ||
28
+ value.length !== items.length ||
29
+ !value.every(
30
+ (size, index) =>
31
+ finite(size) &&
32
+ ((size === 0 && items[index]!.collapsible) ||
33
+ (size >= (items[index]!.min ?? 0) && size <= (items[index]!.max ?? 100)))
34
+ )
35
+ )
36
+ throw new TypeError('Resizable needs one finite size per panel within its bounds.')
37
+ if (Math.abs(value.reduce(addDecimal, 0) - 100) > 1e-7)
38
+ throw new TypeError('Resizable sizes must total 100.')
39
+ }
40
+ export function resizableIndex(items: readonly ResizableItem[], value: string) {
41
+ const index = items.findIndex(item => item.value === value)
42
+ if (index < 0) throw new TypeError('Unknown Resizable panel: ' + value)
43
+ return index
44
+ }
45
+ export const resizableNeighbor = (items: readonly ResizableItem[], index: number) =>
46
+ index === items.length - 1 ? index - 1 : index + 1
47
+ const ranges = (item: ResizableItem): ResizableRange[] =>
48
+ item.collapsible
49
+ ? [
50
+ [0, 0],
51
+ [item.min ?? 0, item.max ?? 100]
52
+ ]
53
+ : [[item.min ?? 0, item.max ?? 100]]
54
+ /** Recover exact constraint endpoints after a few floating-point operations. */
55
+ function snapBoundary(value: number, endpoints: readonly number[], scale: number) {
56
+ const closest = endpoints.reduce(
57
+ (best, point) => (Math.abs(value - point) < Math.abs(value - best) ? point : best),
58
+ endpoints[0]!
59
+ )
60
+ return Math.abs(value - closest) <= Number.EPSILON * 8 * Math.max(1, Math.abs(scale))
61
+ ? closest
62
+ : value
63
+ }
64
+ export function resizableRanges(
65
+ items: readonly ResizableItem[],
66
+ value: number[],
67
+ index: number
68
+ ): ResizableRange[] {
69
+ const neighbor = resizableNeighbor(items, index),
70
+ sum = addDecimal(value[index]!, value[neighbor]!)
71
+ return ranges(items[index]!)
72
+ .flatMap(a =>
73
+ ranges(items[neighbor]!).flatMap(b => {
74
+ const min = snapBoundary(Math.max(a[0], addDecimal(sum, -b[1])), a, sum),
75
+ max = snapBoundary(Math.min(a[1], addDecimal(sum, -b[0])), a, sum)
76
+ return min <= max ? [[min, max] as const] : []
77
+ })
78
+ )
79
+ .sort((a, b) => a[0] - b[0])
80
+ }
81
+ export function resizePanel(
82
+ items: readonly ResizableItem[],
83
+ value: number[],
84
+ index: number,
85
+ size: number,
86
+ directional = true
87
+ ) {
88
+ if (!finite(size)) throw new TypeError('Resizable sizes must be finite.')
89
+ const intervals = resizableRanges(items, value, index)
90
+ if (!intervals.length) return value
91
+ const current = value[index]!
92
+ let next = intervals[0]![0],
93
+ distance = Infinity
94
+ for (const [min, max] of intervals) {
95
+ const candidate = Math.max(min, Math.min(max, size)),
96
+ difference = Math.abs(candidate - size)
97
+ if (difference < distance) {
98
+ next = candidate
99
+ distance = difference
100
+ }
101
+ }
102
+ if (directional && next === current && size !== current) {
103
+ if (size > current) next = intervals.find(range => range[0] > current)?.[0] ?? next
104
+ else next = [...intervals].reverse().find(range => range[1] < current)?.[1] ?? next
105
+ }
106
+ if (next === current) return value
107
+ const neighbor = resizableNeighbor(items, index),
108
+ result = [...value]
109
+ result[index] = next
110
+ const sum = addDecimal(value[index]!, value[neighbor]!),
111
+ peer = items[neighbor]!
112
+ result[neighbor] = snapBoundary(addDecimal(sum, -next), ranges(peer).flat(), sum)
113
+ checkResizableValue(items, result)
114
+ return result
115
+ }
@@ -63,6 +63,16 @@ export const componentRegistry = {
63
63
  focusWidth: 'css',
64
64
  focusOffset: 'css'
65
65
  },
66
+ resizable: {
67
+ handleSize: 'css',
68
+ handleColor: 'css',
69
+ handleBackground: 'css',
70
+ handleHoverBackground: 'css',
71
+ handleActiveBackground: 'css',
72
+ focusColor: 'css',
73
+ focusWidth: 'css',
74
+ focusOffset: 'css'
75
+ },
66
76
  numberField: { gap: 'css' },
67
77
  passwordInput: { gap: 'css' },
68
78
  meter: {