react-native-nitro-input 0.3.0 → 0.3.2

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 (150) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +120 -5
  3. package/android/src/main/cpp/JMaskEngine.cpp +45 -0
  4. package/android/src/main/cpp/JMaskEngine.hpp +9 -0
  5. package/android/src/main/cpp/JReflowEngine.cpp +32 -0
  6. package/android/src/main/cpp/JReflowEngine.hpp +13 -0
  7. package/android/src/main/cpp/JRollingEngine.cpp +42 -2
  8. package/android/src/main/cpp/JRollingEngine.hpp +9 -1
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +24 -0
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +76 -1
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +88 -0
  12. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextMeasure.kt +25 -0
  13. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroTextView.kt +140 -0
  14. package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +16 -0
  15. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +2 -1
  16. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +96 -6
  17. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +388 -136
  18. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroTextView.kt +868 -0
  19. package/android/src/main/java/com/margelo/nitro/nitroinput/ReflowEngine.kt +21 -0
  20. package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +13 -2
  21. package/cpp/MaskEngine.cpp +294 -33
  22. package/cpp/MaskEngine.hpp +68 -0
  23. package/cpp/RollingEngine.cpp +92 -8
  24. package/cpp/RollingEngine.hpp +36 -2
  25. package/ios/HybridNitroInputView.swift +18 -0
  26. package/ios/HybridNitroNumberFormatPlatform.swift +87 -9
  27. package/ios/HybridNitroNumberView.swift +82 -1
  28. package/ios/HybridNitroTextView.swift +134 -0
  29. package/ios/NitroInputView.swift +125 -7
  30. package/ios/NitroNumberView.swift +368 -135
  31. package/ios/NitroTextView.swift +583 -0
  32. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
  33. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
  34. package/lib/commonjs/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
  35. package/lib/commonjs/src/NitroInput.js +15 -2
  36. package/lib/commonjs/src/NitroNumber.js +52 -3
  37. package/lib/commonjs/src/NitroText.js +138 -0
  38. package/lib/commonjs/src/NitroTime.js +65 -0
  39. package/lib/commonjs/src/formatProps.js +76 -16
  40. package/lib/commonjs/src/index.js +6 -1
  41. package/lib/commonjs/src/specs/NitroText.nitro.js +2 -0
  42. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
  43. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
  44. package/lib/module/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
  45. package/lib/module/src/NitroInput.js +15 -2
  46. package/lib/module/src/NitroNumber.js +53 -4
  47. package/lib/module/src/NitroText.js +98 -0
  48. package/lib/module/src/NitroTime.js +29 -0
  49. package/lib/module/src/formatProps.js +76 -17
  50. package/lib/module/src/index.js +2 -0
  51. package/lib/module/src/specs/NitroText.nitro.js +1 -0
  52. package/lib/typescript/src/NitroInput.d.ts +39 -1
  53. package/lib/typescript/src/NitroNumber.d.ts +79 -1
  54. package/lib/typescript/src/NitroText.d.ts +67 -0
  55. package/lib/typescript/src/NitroTime.d.ts +19 -0
  56. package/lib/typescript/src/formatProps.d.ts +30 -4
  57. package/lib/typescript/src/index.d.ts +5 -2
  58. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +43 -0
  59. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +67 -0
  60. package/lib/typescript/src/specs/NitroText.nitro.d.ts +60 -0
  61. package/nitro.json +20 -0
  62. package/nitrogen/generated/android/NitroInput+autolinking.cmake +6 -0
  63. package/nitrogen/generated/android/NitroInputOnLoad.cpp +34 -0
  64. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +102 -0
  65. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +12 -0
  66. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +243 -0
  67. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +40 -0
  68. package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.cpp +58 -0
  69. package/nitrogen/generated/android/c++/JHybridNitroTextMeasureSpec.hpp +64 -0
  70. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.cpp +253 -0
  71. package/nitrogen/generated/android/c++/JHybridNitroTextViewSpec.hpp +104 -0
  72. package/nitrogen/generated/android/c++/JNitroInputCharacterMapping.hpp +61 -0
  73. package/nitrogen/generated/android/c++/JNitroInputMaskAffinity.hpp +64 -0
  74. package/nitrogen/generated/android/c++/JNitroInputTextCase.hpp +61 -0
  75. package/nitrogen/generated/android/c++/JNitroNumberShimmerDirection.hpp +61 -0
  76. package/nitrogen/generated/android/c++/JNitroNumberSignDisplay.hpp +67 -0
  77. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +30 -0
  78. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +100 -0
  79. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.cpp +174 -0
  80. package/nitrogen/generated/android/c++/views/JHybridNitroTextViewStateUpdater.hpp +54 -0
  81. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +36 -0
  82. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +136 -0
  83. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextMeasureSpec.kt +60 -0
  84. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroTextViewSpec.kt +179 -0
  85. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputCharacterMapping.kt +56 -0
  86. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMaskAffinity.kt +25 -0
  87. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextCase.kt +24 -0
  88. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberShimmerDirection.kt +24 -0
  89. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberSignDisplay.kt +26 -0
  90. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewManager.kt +96 -0
  91. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroTextViewStateUpdater.kt +23 -0
  92. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +34 -0
  93. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +146 -11
  94. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +25 -0
  95. package/nitrogen/generated/ios/NitroInputAutolinking.mm +16 -0
  96. package/nitrogen/generated/ios/NitroInputAutolinking.swift +24 -0
  97. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +50 -0
  98. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +146 -0
  99. package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.cpp +11 -0
  100. package/nitrogen/generated/ios/c++/HybridNitroTextMeasureSpecSwift.hpp +90 -0
  101. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.cpp +11 -0
  102. package/nitrogen/generated/ios/c++/HybridNitroTextViewSpecSwift.hpp +233 -0
  103. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +36 -0
  104. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +120 -0
  105. package/nitrogen/generated/ios/c++/views/HybridNitroTextViewComponent.mm +264 -0
  106. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +6 -0
  107. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +78 -0
  108. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +20 -0
  109. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +493 -0
  110. package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec.swift +56 -0
  111. package/nitrogen/generated/ios/swift/HybridNitroTextMeasureSpec_cxx.swift +150 -0
  112. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec.swift +75 -0
  113. package/nitrogen/generated/ios/swift/HybridNitroTextViewSpec_cxx.swift +608 -0
  114. package/nitrogen/generated/ios/swift/NitroInputCharacterMapping.swift +34 -0
  115. package/nitrogen/generated/ios/swift/NitroInputMaskAffinity.swift +48 -0
  116. package/nitrogen/generated/ios/swift/NitroInputTextCase.swift +44 -0
  117. package/nitrogen/generated/ios/swift/NitroNumberShimmerDirection.swift +44 -0
  118. package/nitrogen/generated/ios/swift/NitroNumberSignDisplay.swift +52 -0
  119. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +12 -0
  120. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +21 -0
  121. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +40 -0
  122. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +46 -0
  123. package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.cpp +22 -0
  124. package/nitrogen/generated/shared/c++/HybridNitroTextMeasureSpec.hpp +63 -0
  125. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.cpp +62 -0
  126. package/nitrogen/generated/shared/c++/HybridNitroTextViewSpec.hpp +115 -0
  127. package/nitrogen/generated/shared/c++/NitroInputCharacterMapping.hpp +87 -0
  128. package/nitrogen/generated/shared/c++/NitroInputMaskAffinity.hpp +84 -0
  129. package/nitrogen/generated/shared/c++/NitroInputTextCase.hpp +80 -0
  130. package/nitrogen/generated/shared/c++/NitroNumberShimmerDirection.hpp +80 -0
  131. package/nitrogen/generated/shared/c++/NitroNumberSignDisplay.hpp +88 -0
  132. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +12 -0
  133. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +21 -0
  134. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +40 -0
  135. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +62 -0
  136. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.cpp +74 -0
  137. package/nitrogen/generated/shared/c++/views/HybridNitroTextViewComponent.hpp +149 -0
  138. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +6 -0
  139. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +20 -0
  140. package/nitrogen/generated/shared/json/NitroTextViewConfig.json +30 -0
  141. package/package.json +2 -2
  142. package/src/NitroInput.tsx +73 -0
  143. package/src/NitroNumber.tsx +183 -9
  144. package/src/NitroText.tsx +166 -0
  145. package/src/NitroTime.tsx +68 -0
  146. package/src/formatProps.ts +108 -18
  147. package/src/index.ts +8 -0
  148. package/src/specs/NitroInput.nitro.ts +46 -0
  149. package/src/specs/NitroNumber.nitro.ts +69 -0
  150. package/src/specs/NitroText.nitro.ts +65 -0
@@ -26,10 +26,12 @@ import type {
26
26
  NitroNumberMethods,
27
27
  NitroNumberProps as NativeNitroNumberProps,
28
28
  NitroNumberRevealStyle,
29
+ NitroNumberShimmerDirection,
30
+ NitroNumberSignDisplay,
29
31
  NitroNumberTextAlign,
30
32
  } from './specs/NitroNumber.nitro'
31
33
  import { toNumericWeight, toProcessedColor } from './styleHelpers'
32
- import { formatProps } from './formatProps'
34
+ import { compactParts, formatProps } from './formatProps'
33
35
  import type { NumberFormat } from './NumberFormat'
34
36
 
35
37
  /**
@@ -176,6 +178,19 @@ export interface NitroNumberProps extends Omit<ViewProps, 'children'> {
176
178
  shimmerColor?: ColorValue
177
179
  /** Duration of one sweep in ms (linear, repeating). Default: `950`. */
178
180
  shimmerDuration?: number
181
+ /** The band's slant in degrees: 0 upright, positive leans it like "/", negative the other way. Default: `31`. */
182
+ shimmerAngle?: number
183
+ /** The band's width as a fraction of the number's: `0.5` is a narrow glint. Default: `1`. */
184
+ shimmerWidth?: number
185
+ /**
186
+ * The glyphs' colour outside the band while loading: a light grey makes the
187
+ * figure a skeleton that the band lights up. Default: `color`.
188
+ */
189
+ shimmerBaseColor?: ColorValue
190
+ /** Which way the band sweeps. Default: `'auto'`, the layout direction. */
191
+ shimmerDirection?: NitroNumberShimmerDirection
192
+ /** A pause after each sweep, in ms. Default: `0`. */
193
+ shimmerDelay?: number
179
194
  /** Font size of the digits in points. Default: `32`. */
180
195
  fontSize?: number
181
196
  /** Font size of `prefix`, e.g. a smaller currency symbol. Defaults to `fontSize`. */
@@ -250,6 +265,69 @@ export interface NitroNumberProps extends Omit<ViewProps, 'children'> {
250
265
  * `'left'` and `'right'` are absolute.
251
266
  */
252
267
  textAlign?: NitroNumberTextAlign
268
+ /**
269
+ * Which values carry a sign, as `Intl.NumberFormat`'s `signDisplay`:
270
+ * `'always'` and `'exceptZero'` put a plus on gains ("+$12.40"), and a plus
271
+ * that turns into a minus swaps like any other glyph. A value that rounds to
272
+ * zero at the shown fraction digits counts as zero. Default: `'auto'`
273
+ * (negatives), or the `format`'s.
274
+ */
275
+ signDisplay?: NitroNumberSignDisplay
276
+ /** The glyph drawn for a plus sign. Default: `'+'`. */
277
+ plusSign?: string
278
+ /** The glyph drawn for a minus sign, e.g. `'−'` (U+2212). Default: `'-'`, or the `format` locale's. */
279
+ minusSign?: string
280
+ /** The prefix's color. Default: `color`. */
281
+ prefixColor?: ColorValue
282
+ /** The suffix's color. Default: `color`. */
283
+ suffixColor?: ColorValue
284
+ /** The fraction digits' and decimal separator's color: dimmer cents. Default: `color`. */
285
+ fractionColor?: ColorValue
286
+ /** The fraction digits' and decimal separator's size: smaller cents. Default: `fontSize`. */
287
+ fractionFontSize?: number
288
+ /**
289
+ * How smaller fraction digits line up with the integer ones: `'baseline'`,
290
+ * or `'top'` for superscript cents ("$12⁹⁹"). Default: `'baseline'`.
291
+ */
292
+ fractionAlign?: NitroNumberAffixAlign
293
+ /**
294
+ * The ten glyphs drawn for 0 to 9, for native digits (Arabic-Indic,
295
+ * Devanagari…). Default: `'0'`…`'9'`, or the `format`'s numbering system.
296
+ */
297
+ digitGlyphs?: string[]
298
+ /**
299
+ * Digit group sizes counted from the decimal point: the first group, then
300
+ * every later one. `[3, 2]` is Indian grouping (12,34,567), `[2]` a clock
301
+ * (12:34:56 with `groupingSeparator=":"`). Default: `[3]`, or the `format`'s.
302
+ */
303
+ groupingSizes?: number[]
304
+ /**
305
+ * The highest digit a position shows before it wraps to 0, keyed by integer
306
+ * position (0 the ones, 1 the tens…), as NumberFlow's `digits`:
307
+ * `{ 1: { max: 5 } }` makes the tens of a clock's seconds wrap after 5, so
308
+ * 59 → 00 turns them one step instead of five back.
309
+ */
310
+ digits?: Record<number, { max: number }>
311
+ /**
312
+ * Rolls turn the wheels below the highest one that changes a full turn too,
313
+ * so 100 → 200 seems to pass through every value between. Rolls only.
314
+ * Default: `false`.
315
+ */
316
+ continuous?: boolean
317
+ /**
318
+ * `false` shows every change at once, without a roll or a reveal (the flash
319
+ * and the pop still play). Default: `true`.
320
+ */
321
+ animated?: boolean
322
+ /** Snap instead of animating while the system's Reduce Motion is on. Default: `true`. */
323
+ respectReduceMotion?: boolean
324
+ /** Called when the figure starts moving from rest: a change, or a reveal. */
325
+ onAnimationStart?: () => void
326
+ /**
327
+ * Called when the figure comes to rest, with the value it shows: once for a
328
+ * run of changes that arrived while it was moving. Snapped changes fire neither.
329
+ */
330
+ onAnimationEnd?: (value: number) => void
253
331
  /** Receives the native Nitro object once the view is mounted. */
254
332
  onNativeRef?: (ref: NitroNumberRef) => void
255
333
  }
@@ -277,6 +355,9 @@ interface Size {
277
355
  height: number
278
356
  }
279
357
 
358
+ const EMPTY: number[] = []
359
+ const EMPTY_STRINGS: string[] = []
360
+
280
361
  /** Each transition's own timing, used when the props leave it unsaid. */
281
362
  const TRANSITION_DEFAULTS: Record<NitroNumberTransition, { duration: number; easing: NitroNumberEasing; stagger: number }> = {
282
363
  roll: { duration: 500, easing: 'easeInOut', stagger: 0 },
@@ -327,6 +408,11 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
327
408
  loading,
328
409
  shimmerColor,
329
410
  shimmerDuration,
411
+ shimmerAngle,
412
+ shimmerWidth,
413
+ shimmerBaseColor,
414
+ shimmerDirection,
415
+ shimmerDelay,
330
416
  fontSize,
331
417
  prefixFontSize,
332
418
  suffixFontSize,
@@ -348,6 +434,22 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
348
434
  fontFamily,
349
435
  color,
350
436
  textAlign,
437
+ signDisplay,
438
+ plusSign,
439
+ minusSign,
440
+ prefixColor,
441
+ suffixColor,
442
+ fractionColor,
443
+ fractionFontSize,
444
+ fractionAlign,
445
+ digitGlyphs,
446
+ groupingSizes,
447
+ digits,
448
+ continuous,
449
+ animated,
450
+ respectReduceMotion,
451
+ onAnimationStart,
452
+ onAnimationEnd,
351
453
  onNativeRef,
352
454
  style,
353
455
  ...viewProps
@@ -363,6 +465,10 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
363
465
  latestOnRevealMilestone.current = onRevealMilestone
364
466
  const latestValue = useRef(value)
365
467
  latestValue.current = value
468
+ const latestOnAnimationStart = useRef(onAnimationStart)
469
+ latestOnAnimationStart.current = onAnimationStart
470
+ const latestOnAnimationEnd = useRef(onAnimationEnd)
471
+ latestOnAnimationEnd.current = onAnimationEnd
366
472
 
367
473
  const [size, setSize] = useState<Size | null>(null)
368
474
 
@@ -406,6 +512,21 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
406
512
  }),
407
513
  []
408
514
  )
515
+ const onAnimationStartCallback = useMemo(
516
+ () =>
517
+ callback(() => {
518
+ latestOnAnimationStart.current?.()
519
+ }),
520
+ []
521
+ )
522
+ // The figure a compact format rolls is not the value (1.2 for 1,234): report the value.
523
+ const onAnimationEndCallback = useMemo(
524
+ () =>
525
+ callback((shown: number) => {
526
+ latestOnAnimationEnd.current?.(compactRef.current ? latestValue.current : shown)
527
+ }),
528
+ []
529
+ )
409
530
  // A new array literal each render must not re-set the native prop. Always an
410
531
  // array (empty = no tiers): removing the prop would reach native as `null`,
411
532
  // which Nitro's array parser rejects ("Value is null, expected an Object").
@@ -424,7 +545,7 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
424
545
  jumpTo: (next) => nativeRef.current?.jumpTo(next),
425
546
  animateTo: (next) => nativeRef.current?.animateTo(next),
426
547
  revealTo: (next) => nativeRef.current?.revealTo(next),
427
- getValue: () => nativeRef.current?.value ?? latestValue.current,
548
+ getValue: () => (compactRef.current ? latestValue.current : nativeRef.current?.value ?? latestValue.current),
428
549
  get native() {
429
550
  return nativeRef.current
430
551
  },
@@ -444,12 +565,45 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
444
565
  () => toProcessedColor(shimmerColor) ?? Infinity,
445
566
  [shimmerColor]
446
567
  )
568
+ const processedShimmerBase = useMemo(() => toProcessedColor(shimmerBaseColor) ?? Infinity, [shimmerBaseColor])
447
569
  const processedFlashUp = useMemo(() => toProcessedColor(flashUpColor) ?? Infinity, [flashUpColor])
448
570
  const processedFlashDown = useMemo(() => toProcessedColor(flashDownColor) ?? Infinity, [flashDownColor])
449
571
  const numericWeight = toNumericWeight(fontWeight) ?? 400
450
572
  const resolvedFontSize = fontSize ?? 32
451
573
  const resolvedAffixAlign = affixAlign ?? 'baseline'
452
574
  const derived = format ? formatProps(format) : undefined
575
+ // Compact notation: "1.2K" rolls the figure 1.2 and swaps the suffix.
576
+ const compact = format && derived?.compact ? compactParts(format, value) : undefined
577
+ const compactRef = useRef(false)
578
+ compactRef.current = compact !== undefined
579
+ // Which way the value moved, for a compact figure that fell while the value grew (999 → 1K).
580
+ const previousValue = useRef(value)
581
+ const compactDirection = compact && direction === undefined ? (value >= previousValue.current ? 'up' : 'down') : undefined
582
+ previousValue.current = value
583
+ const processedPrefixColor = useMemo(() => toProcessedColor(prefixColor) ?? Infinity, [prefixColor])
584
+ const processedSuffixColor = useMemo(() => toProcessedColor(suffixColor) ?? Infinity, [suffixColor])
585
+ const processedFractionColor = useMemo(() => toProcessedColor(fractionColor) ?? Infinity, [fractionColor])
586
+ // Arrays reach native only when their contents change.
587
+ const digitMaxKey = digits ? JSON.stringify(digits) : ''
588
+ const digitMax = useMemo(() => {
589
+ if (!digits) return EMPTY
590
+ const out: number[] = []
591
+ for (const [position, spec] of Object.entries(digits)) {
592
+ const index = Number(position)
593
+ if (!Number.isInteger(index) || index < 0 || index >= 20) continue
594
+ while (out.length <= index) out.push(9)
595
+ out[index] = spec.max
596
+ }
597
+ return out
598
+ // eslint-disable-next-line react-hooks/exhaustive-deps
599
+ }, [digitMaxKey])
600
+ const glyphs = digitGlyphs ?? derived?.digitGlyphs ?? EMPTY_STRINGS
601
+ const glyphsKey = glyphs.join('|')
602
+ const stableGlyphs = useMemo(() => glyphs, [glyphsKey]) // eslint-disable-line react-hooks/exhaustive-deps
603
+ const groups = groupingSizes ?? derived?.groupingSizes ?? EMPTY
604
+ const groupsKey = groups.join(',')
605
+ const stableGroups = useMemo(() => groups, [groupsKey]) // eslint-disable-line react-hooks/exhaustive-deps
606
+ const still = animated === false
453
607
 
454
608
  const autoSize = useMemo(
455
609
  () =>
@@ -473,26 +627,26 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
473
627
  {...viewProps}
474
628
  style={[autoSize, style]}
475
629
  hybridRef={hybridRef}
476
- value={value}
477
- fractionDigits={fractionDigits ?? derived?.fractionDigits ?? 0}
630
+ value={compact ? compact.value : value}
631
+ fractionDigits={fractionDigits ?? compact?.fractionDigits ?? derived?.fractionDigits ?? 0}
478
632
  minimumIntegerDigits={minimumIntegerDigits ?? derived?.minimumIntegerDigits ?? 1}
479
633
  groupingSeparator={groupingSeparator ?? derived?.groupingSeparator ?? ''}
480
634
  decimalSeparator={decimalSeparator ?? derived?.decimalSeparator ?? '.'}
481
- prefix={prefix ?? derived?.prefix ?? ''}
482
- suffix={suffix ?? derived?.suffix ?? ''}
635
+ prefix={prefix ?? compact?.prefix ?? derived?.prefix ?? ''}
636
+ suffix={suffix ?? compact?.suffix ?? derived?.suffix ?? ''}
483
637
  transition={transition ?? 'roll'}
484
638
  flashUpColor={processedFlashUp}
485
639
  flashDownColor={processedFlashDown}
486
640
  flashDuration={flashDuration ?? 600}
487
641
  popOnChange={popOnChange ?? 0}
488
- duration={duration ?? TRANSITION_DEFAULTS[transition ?? 'roll'].duration}
642
+ duration={still ? 0 : duration ?? TRANSITION_DEFAULTS[transition ?? 'roll'].duration}
489
643
  easing={easing ?? TRANSITION_DEFAULTS[transition ?? 'roll'].easing}
490
644
  bounce={bounce ?? 0.15}
491
645
  stagger={stagger ?? TRANSITION_DEFAULTS[transition ?? 'roll'].stagger}
492
- rollDirection={direction ?? 'auto'}
646
+ rollDirection={direction ?? compactDirection ?? 'auto'}
493
647
  revealState={reveal === undefined ? 0 : reveal ? 2 : 1}
494
648
  revealStyle={revealStyle ?? 'count'}
495
- revealDuration={revealDuration ?? 2200}
649
+ revealDuration={still ? 0 : revealDuration ?? 2200}
496
650
  revealBounce={revealBounce ?? 0.12}
497
651
  revealGrow={revealGrow ?? 0.2}
498
652
  revealStagger={revealStagger ?? 200}
@@ -500,9 +654,16 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
500
654
  revealMilestoneHold={revealMilestoneHold ?? 0}
501
655
  onRevealEnd={onRevealEndCallback}
502
656
  onRevealMilestone={onRevealMilestoneCallback}
657
+ onAnimationStart={onAnimationStartCallback}
658
+ onAnimationEnd={onAnimationEndCallback}
503
659
  loading={loading ?? false}
504
660
  shimmerColor={processedShimmerColor}
505
661
  shimmerDuration={shimmerDuration ?? 950}
662
+ shimmerAngle={shimmerAngle ?? 31}
663
+ shimmerWidth={shimmerWidth ?? 1}
664
+ shimmerBaseColor={processedShimmerBase}
665
+ shimmerDirection={shimmerDirection ?? 'auto'}
666
+ shimmerDelay={shimmerDelay ?? 0}
506
667
  fontSize={resolvedFontSize}
507
668
  prefixFontSize={prefixFontSize ?? resolvedFontSize}
508
669
  suffixFontSize={suffixFontSize ?? resolvedFontSize}
@@ -515,6 +676,19 @@ export const NitroNumber = forwardRef<NitroNumberHandle, NitroNumberProps>(
515
676
  prefixOffset={prefixOffset ?? 0}
516
677
  suffixOffset={suffixOffset ?? 0}
517
678
  tabularNums={tabularNums ?? true}
679
+ signDisplay={signDisplay ?? derived?.signDisplay ?? 'auto'}
680
+ plusSign={plusSign ?? derived?.plusSign ?? '+'}
681
+ minusSign={minusSign ?? derived?.minusSign ?? '-'}
682
+ digitGlyphs={stableGlyphs}
683
+ groupingSizes={stableGroups}
684
+ digitMax={digitMax}
685
+ continuous={continuous ?? false}
686
+ prefixColor={processedPrefixColor}
687
+ suffixColor={processedSuffixColor}
688
+ fractionColor={processedFractionColor}
689
+ fractionFontSize={fractionFontSize ?? Infinity}
690
+ fractionAlign={fractionAlign ?? 'baseline'}
691
+ respectReduceMotion={respectReduceMotion ?? true}
518
692
  adjustsFontSizeToFit={adjustsFontSizeToFit ?? false}
519
693
  minimumFontScale={minimumFontScale ?? 0.5}
520
694
  allowFontScaling={allowFontScaling ?? false}
@@ -0,0 +1,166 @@
1
+ import React, { useMemo } from 'react'
2
+ import {
3
+ I18nManager,
4
+ PixelRatio,
5
+ StyleSheet,
6
+ type ColorValue,
7
+ type TextStyle,
8
+ type ViewProps,
9
+ } from 'react-native'
10
+ import { getHostComponent, NitroModules } from 'react-native-nitro-modules'
11
+ import NitroTextViewConfig from '../nitrogen/generated/shared/json/NitroTextViewConfig.json'
12
+ import type { NitroTextMeasure, NitroTextMethods, NitroTextProps as NativeNitroTextProps } from './specs/NitroText.nitro'
13
+ import type { NitroInputEasing, NitroInputEffect } from './specs/NitroInput.nitro'
14
+ import type { NitroNumberShimmerDirection, NitroNumberTextAlign } from './specs/NitroNumber.nitro'
15
+ import { toNumericWeight, toProcessedColor } from './styleHelpers'
16
+
17
+ /** The raw Nitro host component behind {@link NitroText}. */
18
+ export const NativeNitroTextView = getHostComponent<NativeNitroTextProps, NitroTextMethods>(
19
+ 'NitroTextView',
20
+ () => NitroTextViewConfig
21
+ )
22
+
23
+ /** What a {@link NitroText} takes: the text, its typography, the morph's timing and the loading shimmer. */
24
+ export interface NitroTextProps extends Omit<ViewProps, 'children'> {
25
+ /** The text: a string or a number. A change morphs to it. */
26
+ children: string | number
27
+ /** Default: `17`. */
28
+ fontSize?: number
29
+ /** Like `Text`'s. Default: `'normal'`. */
30
+ fontWeight?: TextStyle['fontWeight']
31
+ /** Defaults to the system font. */
32
+ fontFamily?: string
33
+ /** Defaults to the platform's label colour. */
34
+ color?: ColorValue
35
+ /** Points after every character. Default: `0`. */
36
+ letterSpacing?: number
37
+ /** Where the line sits in a wider view. Default: `'auto'`, the start edge. */
38
+ textAlign?: NitroNumberTextAlign
39
+ /** Scale with the system text size, like `Text`. Default: `false`. */
40
+ allowFontScaling?: boolean
41
+ /** Cap for `allowFontScaling`; `0` is none. Default: `0`. */
42
+ maxFontSizeMultiplier?: number
43
+ /** Of a morph, in ms; `0` switches at once. Default: `400`. */
44
+ duration?: number
45
+ /** Default: `'expo'`. */
46
+ easing?: NitroInputEasing
47
+ /** Overshoot of the `'spring'` easing. Default: `0.15`. */
48
+ bounce?: number
49
+ /** How characters enter and leave: `'auto'` slides digits and fades letters. Default: `'auto'`. */
50
+ effect?: NitroInputEffect
51
+ /** Snap instead of morphing while Reduce Motion is on. Default: `true`. */
52
+ respectReduceMotion?: boolean
53
+ /** A loading glint through the text, as `NitroNumber`'s `loading`. Default: `false`. */
54
+ loading?: boolean
55
+ shimmerColor?: ColorValue
56
+ /** ms per sweep. Default: `950`. */
57
+ shimmerDuration?: number
58
+ /** Degrees; 0 upright. Default: `31`. */
59
+ shimmerAngle?: number
60
+ /** A fraction of the text's width. Default: `1`. */
61
+ shimmerWidth?: number
62
+ /** The glyphs' colour outside the band: a skeleton. Default: `color`. */
63
+ shimmerBaseColor?: ColorValue
64
+ /** Default: `'auto'`, the layout direction. */
65
+ shimmerDirection?: NitroNumberShimmerDirection
66
+ /** ms of pause after each sweep. Default: `0`. */
67
+ shimmerDelay?: number
68
+ }
69
+
70
+ let measurer: NitroTextMeasure | null = null
71
+ function measure(): NitroTextMeasure {
72
+ measurer ??= NitroModules.createHybridObject<NitroTextMeasure>('NitroTextMeasure')
73
+ return measurer
74
+ }
75
+ const lineHeights = new Map<string, number>()
76
+
77
+ /**
78
+ * A single line of text that morphs to the next, natively, as Torph does on
79
+ * the web: the characters the two share glide to their new places, digits
80
+ * matched by place ("$1,204" → "$1,318" rolls the hundreds and tens), and the
81
+ * rest fade out and in ("Sign in" → "Signing in…"). At rest it is one line
82
+ * drawn in one pass, like a label: no text field, no layer per character.
83
+ * It sizes itself to its text in the render that mounts it (a native
84
+ * measurement, synchronously); a `width` in `style` overrides it.
85
+ *
86
+ * Only the props that were set go to the native view, as
87
+ * react-native-plain-text does: every prop that crosses is converted and
88
+ * stored per view in the commit, and the native side holds the defaults.
89
+ */
90
+ export function NitroText({
91
+ children,
92
+ fontSize,
93
+ fontWeight,
94
+ fontFamily,
95
+ color,
96
+ letterSpacing,
97
+ textAlign,
98
+ allowFontScaling,
99
+ maxFontSizeMultiplier,
100
+ duration,
101
+ easing,
102
+ bounce,
103
+ effect,
104
+ respectReduceMotion,
105
+ loading,
106
+ shimmerColor,
107
+ shimmerDuration,
108
+ shimmerAngle,
109
+ shimmerWidth,
110
+ shimmerBaseColor,
111
+ shimmerDirection,
112
+ shimmerDelay,
113
+ style,
114
+ ...viewProps
115
+ }: NitroTextProps) {
116
+ const text = typeof children === 'string' ? children : String(children)
117
+ const scale = allowFontScaling
118
+ ? maxFontSizeMultiplier && maxFontSizeMultiplier > 0
119
+ ? Math.min(PixelRatio.getFontScale(), maxFontSizeMultiplier)
120
+ : PixelRatio.getFontScale()
121
+ : 1
122
+ const size = (fontSize ?? 17) * scale
123
+ const weight = fontWeight === undefined ? 400 : toNumericWeight(fontWeight) ?? 400
124
+ const family = fontFamily ?? ''
125
+ const spacing = letterSpacing ?? 0
126
+ const width = useMemo(() => measure().measure(text, size, weight, family, spacing), [text, size, weight, family, spacing])
127
+ const fontKey = `${size}|${weight}|${family}`
128
+ let height = lineHeights.get(fontKey)
129
+ if (height === undefined) {
130
+ height = measure().lineHeight(size, weight, family)
131
+ lineHeights.set(fontKey, height)
132
+ }
133
+
134
+ const native: NativeNitroTextProps & Record<string, unknown> = viewProps as never
135
+ native.text = text
136
+ native.style = style == null ? { width: Math.ceil(width), height } : [{ width: Math.ceil(width), height }, style]
137
+ if (size !== 17) native.fontSize = size
138
+ if (weight !== 400) native.fontWeight = weight
139
+ if (family !== '') native.fontFamily = family
140
+ if (spacing !== 0) native.letterSpacing = spacing
141
+ const processedColor = toProcessedColor(color)
142
+ if (processedColor !== undefined) native.color = processedColor
143
+ if (textAlign !== undefined) native.textAlign = textAlign
144
+ if (duration !== undefined) native.duration = duration
145
+ if (easing !== undefined) native.easing = easing
146
+ if (bounce !== undefined) native.bounce = bounce
147
+ if (effect !== undefined) native.effect = effect
148
+ if (respectReduceMotion !== undefined) native.respectReduceMotion = respectReduceMotion
149
+ if (loading !== undefined) native.loading = loading
150
+ const processedShimmerColor = toProcessedColor(shimmerColor)
151
+ if (processedShimmerColor !== undefined) native.shimmerColor = processedShimmerColor
152
+ if (shimmerDuration !== undefined) native.shimmerDuration = shimmerDuration
153
+ if (shimmerAngle !== undefined) native.shimmerAngle = shimmerAngle
154
+ if (shimmerWidth !== undefined) native.shimmerWidth = shimmerWidth
155
+ const processedShimmerBaseColor = toProcessedColor(shimmerBaseColor)
156
+ if (processedShimmerBaseColor !== undefined) native.shimmerBaseColor = processedShimmerBaseColor
157
+ if (shimmerDirection !== undefined) native.shimmerDirection = shimmerDirection
158
+ if (shimmerDelay !== undefined) native.shimmerDelay = shimmerDelay
159
+ // The start edge follows the layout direction, the app's unless the style sets one.
160
+ const direction = style == null ? undefined : (StyleSheet.flatten(style) as { direction?: unknown }).direction
161
+ if (direction === 'rtl' || (direction !== 'ltr' && I18nManager.isRTL)) native.rightToLeft = true
162
+ // The native view is the accessibility element, labelled with its text and
163
+ // announced as text. A label from outside takes over, on this view.
164
+ if (native.accessibilityLabel != null && native.accessible === undefined) native.accessible = true
165
+ return <NativeNitroTextView {...native} />
166
+ }
@@ -0,0 +1,68 @@
1
+ import React, { forwardRef } from 'react'
2
+ import { NitroNumber, type NitroNumberHandle, type NitroNumberProps } from './NitroNumber'
3
+
4
+ /** How a {@link NitroTime} lays the time out. */
5
+ export type NitroTimeFormat = 'm:ss' | 'mm:ss' | 'h:mm:ss' | 'hh:mm:ss'
6
+
7
+ export interface NitroTimeProps
8
+ extends Omit<
9
+ NitroNumberProps,
10
+ | 'value'
11
+ | 'digits'
12
+ | 'groupingSizes'
13
+ | 'groupingSeparator'
14
+ | 'minimumIntegerDigits'
15
+ | 'fractionDigits'
16
+ | 'decimalSeparator'
17
+ | 'format'
18
+ > {
19
+ /** The time in whole seconds (a timer's elapsed time, a countdown's remaining one). Negatives show as 0. */
20
+ seconds: number
21
+ /** Default: `'m:ss'`. Without hours the minutes run past 59 (125:00). */
22
+ timeFormat?: NitroTimeFormat
23
+ /** Between the fields. Default: `':'`. */
24
+ separator?: string
25
+ }
26
+
27
+ const LAYOUT: Record<NitroTimeFormat, { hours: boolean; digits: number }> = {
28
+ 'm:ss': { hours: false, digits: 3 },
29
+ 'mm:ss': { hours: false, digits: 4 },
30
+ 'h:mm:ss': { hours: true, digits: 5 },
31
+ 'hh:mm:ss': { hours: true, digits: 6 },
32
+ }
33
+
34
+ // The tens of seconds, and of minutes when hours are shown, wrap after 5.
35
+ const SECONDS = { 1: { max: 5 } }
36
+ const MINUTES_AND_SECONDS = { 1: { max: 5 }, 3: { max: 5 } }
37
+ // One separator before the seconds: 125:00 is 125 minutes, not 1:25:00.
38
+ const MINUTES_ONLY = [2, 0]
39
+ const PAIRS = [2]
40
+
41
+ /**
42
+ * A clock, a timer or a countdown: `NitroNumber` with the time as its figure
43
+ * (12:34:56 is 123456), grouped in pairs, and the tens of seconds and minutes
44
+ * wrapping after 5, so 0:59 → 1:00 turns them one step, as a clock's wheels
45
+ * do, instead of rolling back through 4, 3, 2, 1.
46
+ */
47
+ export const NitroTime = forwardRef<NitroNumberHandle, NitroTimeProps>(function NitroTime(
48
+ { seconds, timeFormat = 'm:ss', separator = ':', ...props },
49
+ ref
50
+ ) {
51
+ const layout = LAYOUT[timeFormat]
52
+ const total = Math.max(0, Math.floor(Number.isFinite(seconds) ? seconds : 0))
53
+ const s = total % 60
54
+ const value = layout.hours
55
+ ? Math.floor(total / 3600) * 10000 + (Math.floor(total / 60) % 60) * 100 + s
56
+ : Math.floor(total / 60) * 100 + s
57
+ return (
58
+ <NitroNumber
59
+ ref={ref}
60
+ value={value}
61
+ minimumIntegerDigits={layout.digits}
62
+ groupingSeparator={separator}
63
+ groupingSizes={layout.hours ? PAIRS : MINUTES_ONLY}
64
+ digits={layout.hours ? MINUTES_AND_SECONDS : SECONDS}
65
+ {...props}
66
+ />
67
+ )
68
+ })