react-native-nitro-input 0.4.0 → 0.4.3

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 (43) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +2 -1
  3. package/android/src/main/java/com/margelo/nitro/nitroinput/DigitSlots.kt +143 -0
  4. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +6 -0
  5. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +24 -1
  6. package/android/src/main/java/com/margelo/nitro/nitroinput/InputFonts.kt +50 -5
  7. package/android/src/main/java/com/margelo/nitro/nitroinput/InputSizing.kt +7 -1
  8. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +117 -6
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +290 -137
  10. package/ios/HybridNitroInputView.swift +4 -0
  11. package/ios/InputSizing.swift +98 -4
  12. package/ios/NitroInputView.swift +79 -12
  13. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  14. package/lib/commonjs/src/NitroInput.js +3 -1
  15. package/lib/commonjs/src/NitroNumber.js +94 -29
  16. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  17. package/lib/module/src/NitroInput.js +3 -1
  18. package/lib/module/src/NitroNumber.js +94 -29
  19. package/lib/typescript/src/NitroInput.d.ts +11 -0
  20. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +4 -0
  21. package/lib/typescript/src/specs/NitroInputMeasure.nitro.d.ts +2 -0
  22. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +18 -0
  23. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +4 -0
  24. package/nitrogen/generated/android/c++/JNitroInputSizing.hpp +9 -1
  25. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +10 -0
  26. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +12 -0
  27. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSizing.kt +12 -2
  28. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +12 -0
  29. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +12 -0
  30. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +2 -0
  31. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +22 -0
  32. package/nitrogen/generated/ios/swift/NitroInputSizing.swift +12 -2
  33. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +4 -0
  34. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +4 -0
  35. package/nitrogen/generated/shared/c++/NitroInputSizing.hpp +9 -1
  36. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +4 -0
  37. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +6 -0
  38. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +2 -0
  39. package/package.json +1 -1
  40. package/src/NitroInput.tsx +14 -0
  41. package/src/NitroNumber.tsx +121 -54
  42. package/src/specs/NitroInput.nitro.ts +4 -0
  43. package/src/specs/NitroInputMeasure.nitro.ts +2 -0
@@ -445,6 +445,17 @@ export interface NitroInputProps extends Omit<ViewProps, 'children' | 'onFocus'
445
445
  * Default: `0`.
446
446
  */
447
447
  letterSpacing?: number
448
+ /**
449
+ * Unset (the default): the font's tabular figures, where it has them (the
450
+ * system fonts do), at no cost. `true`: every digit as wide as the widest
451
+ * even in a font without them (Open Runde), laid out that wide and centred,
452
+ * so typing over a `keepPlaceholder` mask never moves what is still to come
453
+ * and an amount keeps its width as its digits change; that layout costs a
454
+ * little on each change. `false`: the font's proportional figures. A
455
+ * style's `fontVariant: ['proportional-nums']` is `false` too, as on a
456
+ * `TextInput`; the prop wins.
457
+ */
458
+ tabularNums?: boolean
448
459
  /** Points between the prefix and the text, in place of the letter spacing there. Defaults to the letter spacing. */
449
460
  prefixSpacing?: number
450
461
  /** Points between the text and the suffix, in place of the letter spacing there. Defaults to the letter spacing. */
@@ -842,6 +853,7 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
842
853
  prefixAlign,
843
854
  suffixAlign,
844
855
  letterSpacing: letterSpacingProp,
856
+ tabularNums,
845
857
  prefixSpacing,
846
858
  suffixSpacing,
847
859
  prefixOffset,
@@ -1537,6 +1549,8 @@ export const NitroInput = forwardRef<NitroInputHandle, NitroInputProps>(
1537
1549
  textWeight: numericWeight,
1538
1550
  textFamily: fontFamily ?? '',
1539
1551
  textLetterSpacing: letterSpacing ?? 0,
1552
+ tabularNums: tabularNums ?? !flat?.fontVariant?.includes('proportional-nums'),
1553
+ slotDigits: tabularNums === true,
1540
1554
  prefixSpacing: prefixSpacing ?? Infinity,
1541
1555
  suffixSpacing: suffixSpacing ?? Infinity,
1542
1556
  allowFontScaling: allowFontScaling ?? false,
@@ -376,6 +376,100 @@ interface Size {
376
376
  const EMPTY: number[] = []
377
377
  const EMPTY_STRINGS: string[] = []
378
378
 
379
+ /** What the native callbacks reach: one NitroNumber's refs and state setter. */
380
+ interface NumberSelf {
381
+ nativeRef: React.MutableRefObject<NitroNumberRef | null>
382
+ latestOnNativeRef: React.MutableRefObject<((native: NitroNumberRef) => void) | undefined>
383
+ setSize: React.Dispatch<React.SetStateAction<Size | null>>
384
+ latestOnRevealEnd: React.MutableRefObject<(() => void) | undefined>
385
+ latestOnRevealMilestone: React.MutableRefObject<((index: number, milestone: number) => void) | undefined>
386
+ latestOnAnimationStart: React.MutableRefObject<(() => void) | undefined>
387
+ latestOnAnimationEnd: React.MutableRefObject<((value: number) => void) | undefined>
388
+ compactRef: React.MutableRefObject<boolean>
389
+ latestValue: React.MutableRefObject<ReturnType<typeof withMinorDigits>>
390
+ }
391
+
392
+ type Deref<T extends object> = () => T | undefined
393
+
394
+ /**
395
+ * A weak reference where the engine has them (Hermes, JSC, V8), else a strong
396
+ * one. Two functions on purpose: an engine keeps one scope per function for
397
+ * everything its closures capture, so a `() => target` fallback written beside
398
+ * the weak closure would put `target` in the weak closure's scope too.
399
+ */
400
+ function weakly<T extends object>(target: T): Deref<T> {
401
+ return typeof WeakRef === 'function' ? weakDeref(new WeakRef(target)) : strongDeref(target)
402
+ }
403
+
404
+ function weakDeref<T extends object>(ref: WeakRef<T>): Deref<T> {
405
+ return () => ref.deref()
406
+ }
407
+
408
+ function strongDeref<T extends object>(target: T): Deref<T> {
409
+ return () => target
410
+ }
411
+
412
+ /**
413
+ * A Nitro callback that calls `handler(self, ...args)` for as long as the
414
+ * component behind `self` is alive, holding `self` only weakly.
415
+ *
416
+ * Why. Nitro keeps every callback prop of a view as a JS function held from
417
+ * C++ (in the Fabric props of its shadow node), which Hermes treats as a GC
418
+ * root. A closure written inside the component shares the render's scope,
419
+ * so it reaches the fiber (through `setSize`) and the hybrid view's handle
420
+ * (through `nativeRef`), and the fiber holds the shadow node that holds the
421
+ * props that hold the closure: a loop through native code that Hermes can
422
+ * never collect. Every unmounted NitroNumber stayed, its fiber subtree, its
423
+ * hybrid view and Nitro's C++ part included: over 4,000 after a minute of
424
+ * a scrolling list, and the app's memory climbed with them. Built here, in
425
+ * module scope, the closure holds only the weak reference and `handler`
426
+ * (itself module-level); the fiber is what keeps `self` alive.
427
+ */
428
+ function relay<A extends unknown[]>(self: NumberSelf, handler: (self: NumberSelf, ...args: A) => void) {
429
+ return relayTo(weakly(self), handler)
430
+ }
431
+
432
+ /**
433
+ * The closure itself, made where `self` is not in scope at all: Hermes (in
434
+ * debuggable builds, at least) keeps every variable of a function that makes
435
+ * a closure in that closure's scope, used or not, and a `self` there would
436
+ * hold the component strongly again.
437
+ */
438
+ function relayTo<A extends unknown[]>(live: Deref<NumberSelf>, handler: (self: NumberSelf, ...args: A) => void) {
439
+ return callback((...args: A) => {
440
+ const target = live()
441
+ if (target !== undefined) handler(target, ...args)
442
+ })
443
+ }
444
+
445
+ function attachNative(self: NumberSelf, instance: NitroNumberRef) {
446
+ self.nativeRef.current = instance
447
+ self.latestOnNativeRef.current?.(instance)
448
+ }
449
+
450
+ function applySize(self: NumberSelf, width: number, height: number) {
451
+ self.setSize((previous) =>
452
+ previous !== null && previous.width === width && previous.height === height ? previous : { width, height }
453
+ )
454
+ }
455
+
456
+ function fireRevealEnd(self: NumberSelf) {
457
+ self.latestOnRevealEnd.current?.()
458
+ }
459
+
460
+ function fireRevealMilestone(self: NumberSelf, index: number, milestone: number) {
461
+ self.latestOnRevealMilestone.current?.(index, milestone)
462
+ }
463
+
464
+ function fireAnimationStart(self: NumberSelf) {
465
+ self.latestOnAnimationStart.current?.()
466
+ }
467
+
468
+ /** The figure a compact format rolls is not the value (1.2 for 1,234): report the value. */
469
+ function fireAnimationEnd(self: NumberSelf, shown: number) {
470
+ self.latestOnAnimationEnd.current?.(self.compactRef.current ? toNumber(self.latestValue.current) : shown)
471
+ }
472
+
379
473
  /** Each transition's own timing, used when the props leave it unsaid. */
380
474
  const TRANSITION_DEFAULTS: Record<NitroNumberTransition, { duration: number; easing: NitroNumberEasing; stagger: number }> = {
381
475
  roll: { duration: 500, easing: 'easeInOut', stagger: 0 },
@@ -476,6 +570,8 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
476
570
  ref
477
571
  ) {
478
572
  const nativeRef = useRef<NitroNumberRef | null>(null)
573
+ // Whether a compact format is rolling (its figure is not the value); set below.
574
+ const compactRef = useRef(false)
479
575
  const latestOnNativeRef = useRef(onNativeRef)
480
576
  latestOnNativeRef.current = onNativeRef
481
577
  const latestOnRevealEnd = useRef(onRevealEnd)
@@ -493,61 +589,33 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
493
589
 
494
590
  const [size, setSize] = useState<Size | null>(null)
495
591
 
496
- // Nitro callbacks must be wrapped with `callback()` and the wrapper object
497
- // has to be referentially stable, otherwise every render re-sets the prop.
498
- const hybridRef = useMemo(
499
- () =>
500
- callback((instance: NitroNumberRef) => {
501
- nativeRef.current = instance
502
- latestOnNativeRef.current?.(instance)
503
- }),
504
- []
505
- )
506
- const onSizeChange = useMemo(
507
- () =>
508
- callback((width: number, height: number) => {
509
- setSize((previous) =>
510
- previous !== null &&
511
- previous.width === width &&
512
- previous.height === height
513
- ? previous
514
- : { width, height }
515
- )
516
- }),
517
- []
518
- )
519
-
520
- // Stable like the others; the latest handler is read through a ref so a
521
- // caller passing an inline arrow doesn't re-set the native prop each render.
522
- const onRevealEndCallback = useMemo(
523
- () =>
524
- callback(() => {
525
- latestOnRevealEnd.current?.()
526
- }),
527
- []
528
- )
529
- const onRevealMilestoneCallback = useMemo(
530
- () =>
531
- callback((index: number, milestone: number) => {
532
- latestOnRevealMilestone.current?.(index, milestone)
533
- }),
534
- []
535
- )
536
- const onAnimationStartCallback = useMemo(
537
- () =>
538
- callback(() => {
539
- latestOnAnimationStart.current?.()
540
- }),
541
- []
542
- )
543
- // The figure a compact format rolls is not the value (1.2 for 1,234): report the value.
544
- const onAnimationEndCallback = useMemo(
545
- () =>
546
- callback((shown: number) => {
547
- latestOnAnimationEnd.current?.(compactRef.current ? toNumber(latestValue.current) : shown)
548
- }),
592
+ // The native callbacks: wrapped with `callback()` and referentially stable
593
+ // (a new wrapper re-sets the prop every render), and built in module scope
594
+ // around a weak reference to this component's state (see `relay`).
595
+ const self = useMemo<NumberSelf>(
596
+ () => ({
597
+ nativeRef,
598
+ latestOnNativeRef,
599
+ setSize,
600
+ latestOnRevealEnd,
601
+ latestOnRevealMilestone,
602
+ latestOnAnimationStart,
603
+ latestOnAnimationEnd,
604
+ compactRef,
605
+ latestValue,
606
+ }),
607
+ // Every member is a ref or a state setter, stable for the component's life.
608
+ // eslint-disable-next-line react-hooks/exhaustive-deps
549
609
  []
550
610
  )
611
+ const hybridRef = useMemo(() => relay(self, attachNative), [self])
612
+ const onSizeChange = useMemo(() => relay(self, applySize), [self])
613
+ // The latest handlers are read through refs, so a caller passing an inline
614
+ // arrow doesn't re-set the native prop each render.
615
+ const onRevealEndCallback = useMemo(() => relay(self, fireRevealEnd), [self])
616
+ const onRevealMilestoneCallback = useMemo(() => relay(self, fireRevealMilestone), [self])
617
+ const onAnimationStartCallback = useMemo(() => relay(self, fireAnimationStart), [self])
618
+ const onAnimationEndCallback = useMemo(() => relay(self, fireAnimationEnd), [self])
551
619
  // A new array literal each render must not re-set the native prop. Always an
552
620
  // array (empty = no tiers): removing the prop would reach native as `null`,
553
621
  // which Nitro's array parser rejects ("Value is null, expected an Object").
@@ -599,7 +667,6 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
599
667
  // rolls the figure 1.2.
600
668
  const figure = useMemo(() => (format ? figureOf(format, input) : undefined), [format, input])
601
669
  const compact = derived?.compact ? figure : undefined
602
- const compactRef = useRef(false)
603
670
  compactRef.current = compact !== undefined
604
671
  // Which way the value moved, for a compact figure that fell while the value grew (999 → 1K).
605
672
  const numericValue = toNumber(input)
@@ -268,6 +268,10 @@ export interface NitroInputProps extends HybridViewProps {
268
268
  suffixAlign: NitroInputAffixAlign
269
269
  /** Points added after every glyph, like `Text`'s `letterSpacing`; an affix gets it in proportion to its size. */
270
270
  textLetterSpacing: number
271
+ /** The font's tabular figures (`tnum`) rather than its proportional ones. */
272
+ tabularNums: boolean
273
+ /** Every digit laid out as wide as the widest, centred: tabular figures for a font without them. */
274
+ slotDigits: boolean
271
275
  /** Points between the prefix and the text, in place of the letter spacing there. Not finite: the letter spacing. */
272
276
  prefixSpacing: number
273
277
  /** Points between the text and the suffix, in place of the letter spacing there. Not finite: the letter spacing. */
@@ -47,6 +47,8 @@ export interface NitroInputSizing {
47
47
  textWeight: number
48
48
  textFamily: string
49
49
  textLetterSpacing: number
50
+ tabularNums: boolean
51
+ slotDigits: boolean
50
52
  prefixSpacing: number
51
53
  suffixSpacing: number
52
54
  allowFontScaling: boolean