app-motion.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import React, { useEffect, useMemo, useState } from "react";
  2. import { palette } from "./app-model.js";
  3. const h = React.createElement as (
  4. type: React.ElementType | string,
  5. props?: Record<string, unknown> | null,
  6. ...children: React.ReactNode[]
  7. ) => React.ReactElement;
  8. const SHIMMER_FRAME_INTERVAL_MS = 80;
  9. export const SHIMMER_SWEEP_PERIOD_MS = 1_800;
  10. export const SPINNER_FRAME_INTERVAL_MS = 80;
  11. export const SPINNER_WIDTH = 1;
  12. const SPINNER_FRAMES = [
  13. "⠋",
  14. "⠙",
  15. "⠹",
  16. "⠸",
  17. "⠼",
  18. "⠴",
  19. "⠦",
  20. "⠧",
  21. "⠇",
  22. "⠏",
  23. ] as const;
  24. const SHIMMER_PADDING_COLUMNS = 10;
  25. const SHIMMER_BAND_HALF_WIDTH = 5;
  26. const SHIMMER_HIGHLIGHT_STRENGTH = 0.9;
  27. const SHIMMER_HIGHLIGHT_THRESHOLD = 0.35;
  28. const HEX_COLOR_PATTERN = /^#([0-9a-f]{6})$/i;
  29. const HEX_RADIX = 16;
  30. const RGB_MAX = 255;
  31. type ShimmerTextSegment = {
  32. color: string;
  33. highlighted: boolean;
  34. text: string;
  35. };
  36. type RgbColor = {
  37. b: number;
  38. g: number;
  39. r: number;
  40. };
  41. export function ShimmerText({
  42. animated,
  43. baseColor = palette.accent,
  44. frameMs,
  45. highlightColor = palette.text,
  46. text,
  47. }: {
  48. animated: boolean;
  49. baseColor?: string;
  50. frameMs?: number;
  51. highlightColor?: string;
  52. text: string;
  53. }): React.ReactElement {
  54. const localFrameMs = useShimmerFrame(animated && frameMs === undefined);
  55. const effectiveFrameMs = frameMs ?? localFrameMs;
  56. const segments = useMemo(
  57. () =>
  58. animated
  59. ? shimmerTextSegments(text, effectiveFrameMs, { baseColor, highlightColor })
  60. : [{ color: baseColor, highlighted: false, text }],
  61. [animated, baseColor, effectiveFrameMs, highlightColor, text],
  62. );
  63. if (!animated) {
  64. return h("text", { style: { fg: baseColor } }, text);
  65. }
  66. return h(
  67. "box",
  68. {
  69. style: {
  70. flexDirection: "row",
  71. },
  72. },
  73. ...segments.map((segment, index) =>
  74. h(
  75. "text",
  76. {
  77. key: `shimmer-${index}`,
  78. style: { fg: segment.color },
  79. },
  80. segment.text,
  81. ),
  82. ),
  83. );
  84. }
  85. export function shimmerTextSegments(
  86. text: string,
  87. frameMs: number,
  88. options: {
  89. baseColor?: string;
  90. highlightColor?: string;
  91. } = {},
  92. ): ShimmerTextSegment[] {
  93. const chars = Array.from(text);
  94. if (chars.length === 0) return [];
  95. const baseColor = options.baseColor ?? palette.accent;
  96. const highlightColor = options.highlightColor ?? palette.text;
  97. const period = chars.length + SHIMMER_PADDING_COLUMNS * 2;
  98. const sweepPosition = Math.floor(
  99. (positiveModulo(frameMs, SHIMMER_SWEEP_PERIOD_MS) / SHIMMER_SWEEP_PERIOD_MS) * period,
  100. );
  101. return chars.map((char, index) => {
  102. const position = index + SHIMMER_PADDING_COLUMNS;
  103. const distance = Math.abs(position - sweepPosition);
  104. const intensity = shimmerIntensity(distance);
  105. return {
  106. color: colorForIntensity(baseColor, highlightColor, intensity),
  107. highlighted: intensity >= SHIMMER_HIGHLIGHT_THRESHOLD,
  108. text: char,
  109. };
  110. });
  111. }
  112. export function useShimmerFrame(animated: boolean): number {
  113. const [frameMs, setFrameMs] = useState(() => Date.now());
  114. useEffect(() => {
  115. if (!animated) return undefined;
  116. const timer = setInterval(() => {
  117. setFrameMs(Date.now());
  118. }, SHIMMER_FRAME_INTERVAL_MS);
  119. return () => clearInterval(timer);
  120. }, [animated]);
  121. return frameMs;
  122. }
  123. export function useSpinnerFrame(animated: boolean): string {
  124. const [frameMs, setFrameMs] = useState(() => Date.now());
  125. useEffect(() => {
  126. if (!animated) return undefined;
  127. const timer = setInterval(() => {
  128. setFrameMs(Date.now());
  129. }, SPINNER_FRAME_INTERVAL_MS);
  130. return () => clearInterval(timer);
  131. }, [animated]);
  132. return spinnerFrame(frameMs);
  133. }
  134. export function spinnerFrame(frameMs: number): string {
  135. const periodMs = SPINNER_FRAME_INTERVAL_MS * SPINNER_FRAMES.length;
  136. const frameIndex = Math.floor(positiveModulo(frameMs, periodMs) / SPINNER_FRAME_INTERVAL_MS);
  137. return SPINNER_FRAMES[frameIndex] ?? SPINNER_FRAMES[0];
  138. }
  139. function shimmerIntensity(distance: number): number {
  140. if (distance > SHIMMER_BAND_HALF_WIDTH) return 0;
  141. const x = Math.PI * (distance / SHIMMER_BAND_HALF_WIDTH);
  142. return 0.5 * (1 + Math.cos(x));
  143. }
  144. function colorForIntensity(baseColor: string, highlightColor: string, intensity: number): string {
  145. if (intensity <= 0) return baseColor;
  146. return blendHexColor(baseColor, highlightColor, intensity * SHIMMER_HIGHLIGHT_STRENGTH);
  147. }
  148. function blendHexColor(baseColor: string, highlightColor: string, amount: number): string {
  149. const base = parseHexColor(baseColor);
  150. const highlight = parseHexColor(highlightColor);
  151. if (!base || !highlight) {
  152. return amount >= SHIMMER_HIGHLIGHT_THRESHOLD ? highlightColor : baseColor;
  153. }
  154. return rgbToHex({
  155. r: blendChannel(base.r, highlight.r, amount),
  156. g: blendChannel(base.g, highlight.g, amount),
  157. b: blendChannel(base.b, highlight.b, amount),
  158. });
  159. }
  160. function parseHexColor(value: string): RgbColor | undefined {
  161. const match = HEX_COLOR_PATTERN.exec(value);
  162. if (!match) return undefined;
  163. const hex = match[1] ?? "";
  164. return {
  165. r: Number.parseInt(hex.slice(0, 2), HEX_RADIX),
  166. g: Number.parseInt(hex.slice(2, 4), HEX_RADIX),
  167. b: Number.parseInt(hex.slice(4, 6), HEX_RADIX),
  168. };
  169. }
  170. function rgbToHex(color: RgbColor): string {
  171. return `#${hexChannel(color.r)}${hexChannel(color.g)}${hexChannel(color.b)}`;
  172. }
  173. function hexChannel(value: number): string {
  174. return Math.round(clamp(value, 0, RGB_MAX)).toString(HEX_RADIX).padStart(2, "0");
  175. }
  176. function blendChannel(base: number, highlight: number, amount: number): number {
  177. const clampedAmount = clamp(amount, 0, 1);
  178. return base + (highlight - base) * clampedAmount;
  179. }
  180. function clamp(value: number, min: number, max: number): number {
  181. return Math.min(max, Math.max(min, value));
  182. }
  183. function positiveModulo(value: number, modulo: number): number {
  184. return ((value % modulo) + modulo) % modulo;
  185. }