ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Ignite 的 Icon 与 PressableIcon 组件:内置图标注册表、Props 详解与自定义图标实战

Ignite 的 Icon 与 PressableIcon 组件:内置图标注册表、Props 详解与自定义图标实战 Ignite 的 Icon 与 PressableIcon 组件内置图标注册表、Props 详解与自定义图标实战【免费下载链接】igniteInfinite Reds battle-tested React Native project boilerplate, along with a CLI, component/model generators, and more! 9 years of continuous development and counting.项目地址: https://gitcode.com/GitHub_Trending/ig/ignite本篇技术指南围绕 Infinite Red 开源项目 Ignite 的 React Native 样板代码boilerplate中预置的Icon与PressableIcon组件展开讲解它们如何基于预定义图标图片 注册表完成图标渲染逐一说明icon、color、size、style、containerStyle、onPress等 Props 的语义与用法并结合组件源码与 Showroom 演示代码给出在Header、ListItem、Button、导航栏等真实场景中的组合用法以及新增自定义图标的完整操作流程。读完本文你将能够直接在本项目的任意界面中渲染、着色、缩放和包装图标并能将自有的 PNG 素材接入 Ignite 的图标体系。一、组件定位一个注册表驱动的图片型图标方案Ignite 的图标方案不是使用字体图标icon font而是图片型图标所有图标都是项目assets/icons/目录下的 PNG 图片通过iconRegistry这个名称 → require 资源的映射注册表统一管理。在 Icon.tsx 中这一体系由两个对外组件和一个注册表对象构成Icon将图标图片包裹在 React Native 的View中渲染适合纯展示场景PressableIcon将同一张图片包裹在TouchableOpacity中渲染并透传onPress适合可点击的图标按钮iconRegistry以字符串名为键、以require(...)图片资源为值的注册表对象同时导出了类型IconTypes keyof typeof iconRegistry保证传入的icon名称有 TypeScript 类型约束Icon.tsx。Icon icondebug /PressableIcon iconladybug onPress{() Alert.alert(Hello)} /两个组件共享同一套BaseIconPropsicon、color、size、style、containerStyle差异仅在于外层容器与继承的 React Native 组件类型PressableIconProps OmitTouchableOpacityProps, style BaseIconPropsIconProps OmitViewProps, style BaseIconPropsIcon.tsx。因此除了下文列出的自有 Props 外你可以把TouchableOpacity用于PressableIcon或View用于Icon支持的任何 Props 透传进去它们会被转发到对应的外层容器组件。二、内置图标注册表可用的 19 个图标名称icon是必填的 Props值为注册表中的字符串名。以下名称来自 Icon.tsx 中iconRegistry的实际定义其中标注// demo remove-current-line的条目属于演示demo专用图标执行 Ignite CLI 的remove-demo命令后会被自动移除图标名称对应图片资源说明backassets/icons/back.png返回箭头bellassets/icons/bell.png铃铛/通知caretLeftassets/icons/caretLeft.png左箭头caretRightassets/icons/caretRight.png右箭头checkassets/icons/check.png对勾clapassets/icons/demo/clap.png鼓掌democommunityassets/icons/demo/community.png社区democomponentsassets/icons/demo/components.png组件demodebugassets/icons/demo/debug.png调试demogithubassets/icons/demo/github.pngGitHub 标志demoheartassets/icons/demo/heart.png爱心demohiddenassets/icons/hidden.png隐藏ladybugassets/icons/ladybug.png瓢虫演示中最常用lockassets/icons/lock.png锁menuassets/icons/menu.png菜单moreassets/icons/more.png更多pinassets/icons/demo/pin.png图钉demopodcastassets/icons/demo/podcast.png播客demosettingsassets/icons/settings.png设置slackassets/icons/demo/slack.pngSlack 标志demoviewassets/icons/view.png眼睛/查看xassets/icons/x.png关闭Icon iconbell /由于IconTypes直接由keyof typeof iconRegistry推导如果你传入一个不存在的名称TypeScript 会在编译期报错这是该方案相比字符串硬编码更安全的一点。Showroom 的 DemoIcon.tsx 会遍历Object.keys(iconRegistry)并把全部图标平铺展示每个size{35}、着色theme.colors.tint、下方标注图标名运行时运行 demo 即可直观核对所有可用图标。2.1 每个图标的基础图片所有内置图标均以1x、2x、3x三种分辨率存放在boilerplate/assets/icons/目录如ladybug.png24x24、ladybug2x.png48x48、ladybug3x.png72x72由 React Native 的图片加载机制按屏幕像素密度自动选取。未显式传入size时图标按图片原始分辨率渲染。三、Props 全解从基础渲染到样式覆写3.1color一键改色的 tintColorcolor是可选字符串用于设置图标图片的tintColorReact Native 图片样式属性实现对单色 PNG 的重新着色。Icon iconx color#7C7C7C /从源码看未传color时组件会自动取当前主题的文字色作为默认着色const { theme } useAppTheme() const $imageStyle: StylePropImageStyle [ $imageStyleBase, { tintColor: color ?? theme.colors.text }, ... ]这一默认行为在 Icon.tsxPressableIcon与 Icon.tsxIcon中完全一致——组件通过useAppTheme()读取主题浅色/深色模式下无需额外传参即可获得正确的前景色。实战中常见的做法是结合主题调色板palette传色例如 DemoIcon.tsx 用同一只瓢虫展示了 5 种调色板配色accent500、primary500、secondary500、neutral700、angry500。这些颜色均定义于 colors.ts 的palette常量。导航场景则更常见的是按选中态切换颜色见 DemoNavigator.tsxIcon iconpodcast color{focused ? colors.tint : colors.tintInactive} size{30} /3.2size统一缩放图标尺寸size是可选数字同时设定图标图片的宽高Icon iconx size{24} /源码中的实现是条件式覆写size ! undefined { width: size, height: size }Icon.tsx。即不传size时保持图片原始分辨率传入后强制等比例缩放到指定边长。DemoIcon.tsx 用ladybug展示了不传size与size{35}、size{50}、size{75}四种形态的对比。3.3style覆写图标图片自身样式style是可选对象类型为StylePropImageStyle用于覆写图标图片的样式。默认基础样式仅设置resizeMode: containIcon.tsx确保图标在容器内等比完整显示、不被裁切。Icon iconladybug style{{ width: 20, height: 20 }} /需要注意style直接作用于Image$imageStyleBase、tintColor、size计算值会与$imageStyleOverride合并而width/height的覆写优先级高于size的计算值。ImageStyle支持tintColor、resizeMode、transform等属性Header.tsx 就利用style在 RTL 布局下对返回箭头做了 180° 旋转style{isRTL ? { transform: [{ rotate: 180deg }] } : {}}3.4containerStyle覆写外层容器样式containerStyle是可选对象类型为StylePropViewStyle设置的是外层容器TouchableOpacity或View的样式。这在需要给图标加背景、padding、边框或整体布局对齐时非常有用Icon iconbug containerStyle{{ backgroundColor: red }} /源码中容器样式作为整个style传给外层组件TouchableOpacity {...pressableProps} style{$containerStyleOverride}Icon.tsx。需要注意containerStyle会完全替换而非合并默认容器样式因此 Header 内部将其与主题样式合并后再传入containerStyle{themed([$actionIconContainer, { backgroundColor }])}。3.5onPress让图标可点击onPress仅适用于PressableIcon会被转发到内层TouchableOpacity的onPress上PressableIcon iconladybug onPress{() Alert.alert(Hello)} /四、真实场景Icon 与其它组件的组合用法4.1 Header 的左右操作按钮Header.tsx 中HeaderAction在未提供文字操作时直接渲染PressableIcon并将icon、color、onPress、containerStyle全部透传形成典型的返回/更多图标按钮PressableIcon size{24} icon{icon} color{iconColor} onPress{onPress} containerStyle{themed([$actionIconContainer, { backgroundColor }])} style{isRTL ? { transform: [{ rotate: 180deg }] } : {}} /4.2 ListItem 的左右图标ListItem.tsx 的ListItemAction使用Icon渲染列表项左侧/右侧图标并借助containerStyle传入图标容器边距与行高对齐Icon size{24} icon{icon} color{iconColor} containerStyle{themed([ $iconContainerStyles, side left $iconContainerLeft, side right $iconContainerRight, { height: size }, ])} /4.3 Button 的 Accessory 图标Button.tsx 通过LeftAccessory/RightAccessory接收渲染函数demo 中在函数内返回Icon并借用 props 传入的containerStyle完成定位实现图标 文字按钮Button presetreversed RightAccessory{(props) ( Icon containerStyle{props.style} style{$iconStyle} iconladybug / )} {translate(demoButton:useCase.passingContent.multiLine)} /Button4.4 TextField 的辅助图标TextField.tsx 的文档注释同样示范了将Icon用作输入框右/左辅助图标并根据可编辑状态切换颜色RightAccessory{(props) ( Icon iconladybug containerStyle{props.style} color{props.editable ? colors.textDim : colors.text} / )}五、自定义图标三步接入新的图标资源内置图标不够用时的扩展流程在 Icon.md 文档 中已有说明结合源码可以归纳为三步第一步放置图片资源。将图标 PNG 放到assets/icons/目录demo 图标放assets/icons/demo/-- icon/ -- icons/ -- index.ts -- my-custom-icon.png第二步注册到iconRegistry。在 Icon.tsx 的iconRegistry对象中新增一条记录export const iconRegistry { // ... custom: require(./myCustomIcon.png), }注册的同时IconTypes类型会自动包含custom无需手动维护类型。项目使用assets/icons这样的路径别名在tsconfig.json/babel.config.js中配置因此注册时也可以写require(assets/icons/myCustomIcon.png)。第三步通过iconprop 使用。Icon iconcustom /PressableIcon iconcustom onPress{() Alert.alert(Im a custom PressableIcon!)} /此后该图标即可在Header、ListItem、Button、TextField、导航栏等所有接受IconTypes的场景中直接引用并同样支持color、size、style、containerStyle的全部能力。// demo remove-current-line这类标记仅用于演示图标普通自定义图标不加该注释即可被remove-demo流程保留。六、小结Ignite 的Icon/PressableIcon组件以图片资源 iconRegistry注册表 类型推导为核心icon从注册表取图color经tintColor一键着色默认取主题文字色size等比缩放style/containerStyle分别覆写图片与容器样式PressableIcon额外提供TouchableOpacity的点击能力。配套的 DemoIcon.tsx 是完整的可运行示例遍历全部图标、size 渐变、palette 配色、样式覆写四组用例任何新图标只需放图 注册即可全局复用。该方案不引入字体文件与外部图标库依赖图标即素材风格完全可控适合作为团队统一图标资产的落地方式。【免费下载链接】igniteInfinite Reds battle-tested React Native project boilerplate, along with a CLI, component/model generators, and more! 9 years of continuous development and counting.项目地址: https://gitcode.com/GitHub_Trending/ig/ignite创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表