如何使用 @shopify/restyle 在 React Native 中构建类型强制的 UI 组件

自从我在博客上写一篇技术文章以来已经有一段时间了,这是一篇关于使用 @shopify/restyle 和 expo 在 react native 中构建类型强制 ui 组件的新文章。

@shopify/restyle 是一个强大的 react native 样式库,可为您的 ui 组件带来类型安全性和一致性。与传统的样式方法不同,restyle 允许您创建集中式主题配置,在整个应用程序中强制执行设计系统原则。

入门

项目设置

使用 expo 设置您的 react native 项目

npx create-expo-app@latest

登录后复制转到您的项目目录并使用 expo 安装 @shopify/restyle 包

cd /path/to/projectnpx expo install @shopify/restyle

登录后复制

创建你的主题

创建 theme.tsx 文件来定义您的设计系统:

touch theme.tsx

登录后复制复制并粘贴默认主题配置

import {createtheme} from '@shopify/restyle';const palette = {  purplelight: '#8c6ff7',  purpleprimary: '#5a31f4',  purpledark: '#3f22ab',  greenlight: '#56dcba',  greenprimary: '#0ecd9d',  greendark: '#0a906e',  black: '#0b0b0b',  white: '#f0f2f3',};const theme = createtheme({  colors: {    mainbackground: palette.white,    cardprimarybackground: palette.purpleprimary,  },  spacing: {    s: 8,    m: 16,    l: 24,    xl: 40,  },  textvariants: {    header: {      fontweight: 'bold',      fontsize: 34,    },    body: {      fontsize: 16,      lineheight: 24,    },    defaults: {      // we can define a default text variant here.    },  },});export type theme = typeof theme;export default theme;

登录后复制

实施主题提供者

更新您的 app/_layout.tsx:

import { darktheme, defaulttheme } from "@react-navigation/native";import { usefonts } from "expo-font";import { stack } from "expo-router";import * as splashscreen from "expo-splash-screen";import { statusbar } from "expo-status-bar";import { useeffect } from "react";import "react-native-reanimated";import { themeprovider } from "@shopify/restyle";import theme from "@/theme";// prevent the splash screen from auto-hiding before asset loading is complete.splashscreen.preventautohideasync();export default function rootlayout() {  const [loaded] = usefonts({    spacemono: require("../assets/fonts/spacemono-regular.ttf"),  });  useeffect(() => {    if (loaded) {      splashscreen.hideasync();    }  }, [loaded]);  if (!loaded) {    return null;  }  return (                                            );}

登录后复制

创建可重用组件

文本组件

touch components/text.tsx

登录后复制

// in components/text.tsximport {createtext} from '@shopify/restyle';import {theme} from '../theme';export const text = createtext();

登录后复制

让我们在主屏幕上使用它

import { text } from "@/components/text";import { safeareaview } from "react-native-safe-area-context";export default function homescreen() {  return (                  this is the home screen. built using @shopify/restyle.            );}

登录后复制

正如您在上面的代码中看到的,我们将边距作为“m”而不是数字传递。我们从 theme.tsxfile 中获取值。

// ./theme.tsxconst theme = createtheme({  spacing: {    s: 8,    m: 16, // margin="m"    l: 24,    xl: 40,  },  textvariants: {    header: { // our text header variant      fontweight: 'bold',      fontsize: 34,    },    body: {      fontsize: 16,      lineheight: 24,    },  },    // ...rest of code  },});

登录后复制

这就是我们的主页视图的外观

如何使用 @shopify/restyle 在 React Native 中构建类型强制的 UI 组件 );};type props = spacingprops & variantprops & backgroundcolorprops & react.componentprops;const cardskeleton = createrestylecomponent([ spacing, createvariant({ themekey: “cardvariants” }),]);export const skeletonloader = () => { return ( );};登录后复制

瞧,我们使用 @shopify/restyle 使用

制作了一个骨架加载卡

如何使用 @shopify/restyle 在 React Native 中构建类型强制的 UI 组件

支持深色模式

让我们从在 theme.tsx 文件中添加深色主题配置开始

// theme.tsxexport const darktheme: theme = {  ...theme,  colors: {    ...theme.colors,    mainbackground: palette.white,    cardprimarybackground: palette.purpledark,    greenprimary: palette.purplelight,  },  textvariants: {    ...theme.textvariants,    defaults: {      ...theme.textvariants.header,      color: palette.purpledark,    },  },

登录后复制

通过将深色主题配置添加到我们的layout.tsx 文件中来将其添加到我们的应用布局中

 // app/_layout.tsximport { usefonts } from "expo-font";import { stack } from "expo-router";import * as splashscreen from "expo-splash-screen";import { statusbar } from "expo-status-bar";import { useeffect } from "react";import "react-native-reanimated";import { themeprovider } from "@shopify/restyle";import theme, { darktheme } from "@/theme";import { usecolorscheme } from "react-native";// prevent the splash screen from auto-hiding before asset loading is complete.splashscreen.preventautohideasync();export default function rootlayout() {  const [loaded] = usefonts({    spacemono: require("../assets/fonts/spacemono-regular.ttf"),  });  const colorschema = usecolorscheme();  useeffect(() => {    if (loaded) {      splashscreen.hideasync();    }  }, [loaded]);  if (!loaded) {    return null;  }  return (                                            );}

登录后复制使用react-native中的usecolorscheme钩子获取颜色模式

  // app/_layout.tsx import { usecolorscheme } from "react-native";  //... rest of the code  const colorschema = usecolorscheme();

登录后复制基于颜色模式,使用默认的浅色主题或在深色模式下使用 theme.tsx 文件中定义的 darktheme 配置

 // app/_layout.tsx import theme, { darkTheme } from "@/theme"; //... rest of the code                                          

登录后复制

这是深色和浅色模式。

如何使用 @shopify/restyle 在 React Native 中构建类型强制的 UI 组件

如何使用 @shopify/restyle 在 React Native 中构建类型强制的 UI 组件

瞧,我们成功地使用 @shopify/restyle 包创建了类型强制的 ui 组件

谢谢你:)

以上就是如何使用 @shopify/restyle 在 React Native 中构建类型强制的 UI 组件的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2648213.html

(0)
上一篇 2025年3月7日 08:08:22
下一篇 2025年2月26日 15:12:31

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 掌握依赖倒置原则:使用 DI 实现干净代码的最佳实践

    如果您熟悉面向对象编程,或者刚刚开始探索它,您可能遇到过缩写词solid。 solid 代表了一组旨在帮助开发人员编写干净、可维护和可扩展代码的原则。在这篇文章中,我们将重点关注 solid 中的“d”,它代表依赖倒置原则。 但在深入了解细…

    2025年3月7日 编程技术
    200
  • OST 掌握 JavaScript 的重要 JS 概念

    JavaScript 是一种多功能且功能强大的语言,对于现代 Web 开发至关重要。要精通 JavaScript,理解其一些核心概念至关重要。这些概念不仅有助于编写高效且可维护的代码,还使开发人员能够构建复杂且动态的 Web 应用程序。在本…

    2025年3月7日
    200
  • 掌握 JavaScript:利用高阶流释放函数响应式编程的力量

    javascript 中使用高阶流的函数响应式编程 (frp) 是处理代码中复杂的、基于时间的交互的强大方法。这是一种将我们的程序视为一系列数据流,而不是一系列命令式命令的方式。 让我们首先了解什么是流。在 frp 中,流是随时间变化的值序…

    2025年3月7日
    200
  • Javascript 中的符号及其示例

    symbol 是一个内置对象,其构造函数返回一个 symbol 基元 — 也称为 symbol 值 或只是一个 symbol — 保证是唯一的。符号通常用于向对象添加唯一的属性键,这些属性键不会与任何其他代码可能添加到该对象的键发生冲突,并…

    2025年3月7日
    200
  • Logging System with Proxy and Fetch

    代理对象:fetchlogger 包装了 fetch 函数。它使用 apply trap 来拦截对 fetch 的调用。 请求日志记录:记录请求的 url 和选项。响应处理:记录响应状态、状态文本和 url。克隆响应以确保正文可以被多次读取…

    2025年3月7日
    200
  • 如何将交互式图表和图形添加到 Tailwind CSS 管理模板

    管理仪表板模板对于有效管理和可视化数据至关重要。 tailwind css 以其实用性优先的方法而闻名,它简化了设计令人惊叹的管理仪表板的过程。向这些仪表板添加交互式图表和图形可以将原始数据转换为富有洞察力的可视化效果,从而增强整体用户体验…

    2025年3月7日 编程技术
    200
  • typescript断言类型

    TypeScript 中的断言类型明确了表达式或变量的类型,帮助编译器理解特定的类型。使用断言类型有两种方法:非空断言操作符 (!) 用于确保变量不会为 null 或 undefined,类型断言语法 () 将所需类型包围在表达式周围。断言…

    2025年3月7日
    200
  • typescript高级技巧

    高级 TypeScript 技巧包括:联合类型、元组、枚举、接口继承、泛型函数和类、模块命名空间和导入、类和方法装饰器、async/await 和 Promise.all,以及类型断言、类型保护和编译时检查。这些技巧可显著提高代码质量、可维…

    2025年3月7日
    200
  • js如何加密

    JavaScript 提供多种加密方法:1. 字符串加密(AES);2. 哈希算法(SHA256);3. 对称加密(TripleDES);4. 非对称加密(rsa);5. 其他方法(base64、Web Crypto API)。 如何使用 …

    2025年3月7日
    200
  • 如何封装js

    封装 JavaScript 代码可提高代码可维护性、可扩展性和可测试性。步骤如下:创建模块。定义私有变量和函数。导出公共接口。导入模块。 如何封装 JavaScript 代码 封装是将代码组织成可重用的模块的过程。这有助于提高代码的可维护性…

    2025年3月7日
    200

发表回复

登录后才能评论