【翻译】用 Reanimated CSS 动画为 TextInput 添加发光效果

用 Reanimated CSS 动画为 TextInput 添加发光效果

细微的 UI 反馈会让应用更有生命力。给文本输入框增加质感最简单的方法之一,就是在用户开始输入时触发一个发光效果。

借助 Reanimated 的 CSS 动画支持,你可以用很少几行代码做到这一点:不需要 useSharedValue,不需要 useAnimatedStyle,只用 keyframes 和 boxShadow。

The Approach

思路很直接:

  1. 定义两个关键帧动画:一个让 boxShadow 渐显,一个让它渐隐。
ts 复制代码
const glowIn: CSSAnimationKeyframes = {
  from: { boxShadow: `0 0 0 0 ${GLOW_COLOR}00` },
  to: { boxShadow: `0 0 32px 4px ${GLOW_COLOR}80` },
};
 
const glowOut: CSSAnimationKeyframes = {
  from: { boxShadow: `0 0 32px 4px ${GLOW_COLOR}59` },
  to: { boxShadow: `0 0 0 0 ${GLOW_COLOR}00` },
};
  1. 根据输入框是否有文本,在两者之间切换。
ts 复制代码
const [text, setText] = useState("");
const hasText = text.length > 0;
 
return (
  <Animated.View
    style={{
      animationName: hasText ? glowIn : glowOut,
    }}
  />
);
  1. 用 Animated.View 包住 TextInput,再通过样式属性以声明式方式应用动画。
ts 复制代码
import { useState } from "react";
import { StyleSheet, TextInput, View } from "react-native";
import Animated, { type CSSAnimationKeyframes } from "react-native-reanimated";
 
const GLOW_COLOR = "#6C63FF";
 
const glowIn: CSSAnimationKeyframes = {
  from: { boxShadow: `0 0 0 0 ${GLOW_COLOR}00` },
  to: { boxShadow: `0 0 32px 4px ${GLOW_COLOR}80` },
};
 
const glowOut: CSSAnimationKeyframes = {
  from: { boxShadow: `0 0 32px 4px ${GLOW_COLOR}59` },
  to: { boxShadow: `0 0 0 0 ${GLOW_COLOR}00` },
};
 
export function GlowInput() {
  const [text, setText] = useState("");
  const hasText = text.length > 0;
 
  return (
    <View style={styles.container}>
      <Animated.View
        style={[
          styles.glow,
          {
            animationName: hasText ? glowIn : glowOut,
            animationDuration: hasText ? "400ms" : "500ms",
            animationFillMode: "forwards",
            animationTimingFunction: "ease-out",
          },
        ]}
      >
        <TextInput
          autoFocus
          value={text}
          onChangeText={setText}
          placeholder="Type something..."
          placeholderTextColor="#999"
          cursorColor={GLOW_COLOR}
          style={styles.input}
        />
      </Animated.View>
    </View>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    paddingHorizontal: 24,
  },
  glow: {
    borderRadius: 999,
  },
  input: {
    color: "#fff",
    fontSize: 18,
    fontWeight: "500",
    paddingHorizontal: 20,
    paddingVertical: 16,
    backgroundColor: "#1c1c1e",
    borderRadius: 999,
    borderWidth: 1,
    borderColor: "#333",
  },
});

Tips

  • 替换颜色 :修改 GLOW_COLOR 来匹配你应用的强调色。错误态可以用红色,成功态可以用绿色,按你的场景来定。
  • 调节强度 :32px 的模糊半径和 4px 的扩散值决定了发光有多"戏剧化"。如果你想更克制一些,就把它们调小。
  • 改为在 focus 时触发 :如果你希望用户点按输入框就发光,而不是输入后才发光,把 hasText 判断改成 onFocus / onBlur 状态即可。

Want to go deeper?

React Native 课程里有一节 Animations and Gestures ,内容包括 Reanimated 辅助函数(withTiming、withSpring 及相关 API)和 React Native Gesture Handler 的手势类型,并配有一个小型示例应用可供参考。

课程:Animations and Gestures

完整课程目录可见 React Native course。

如果你希望持续获取实用技巧、实验内容与更新,也可以订阅我的 newsletter。

如果你想和我合作,可以通过 beto@codewithbeto.dev 联系我。

相关推荐
JavaGuide1 分钟前
NVIDIA 又开源了!这次给 AI Agent 加上权限管控
前端·后端
excel11 分钟前
prisma 如何处理数据库竞态
前端·数据库·后端
莪_幻尘1 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm
风骏时光牛马2 小时前
AI模型综合能力评测:性能、指令遵循与多场景实测对比
前端
Frag0ut2 小时前
Chrome与Chromium内核浏览器在Windows 11上的新特性全景解析
前端·chrome·windows·web安全·chromium·gemini ai·playready drm
hiahiahia1232 小时前
实现完整 Tool Dispatcher
开发语言·前端
IT_陈寒2 小时前
Java线程池这破玩意,差点让我周末加班排查到凌晨
前端·人工智能·后端
Mr_Mao3 小时前
告别选型困难!集 VueUse、ahooks、Mantine 于一身:286 个 Hook 的 ReaUse 来了
前端·javascript·react.js
泡海椒4 小时前
趋势分析报表:jquick-pdf折线图PDF生成实战
java·大数据·运维·服务器·前端·pdf
默_笙4 小时前
🚙 一条腿走路会摔:ES 关键词检索、混合召回与 ReRank 裁判
前端·javascript