@cssxjs/css-to-react-native

中文文档
3.2.0-2发布于 2026/01/25MITTypeScriptCommonJS近 30 天下载 5774

将 CSS 文本转换为本地样式表对象

中文使用文档

css- to- react-inative (英语)

将 CSS 文本转换为 React Native 样式表对象.

试试看

font-size: 18px;
line-height: 24px;
color: red;
{
  fontSize: 18,
  lineHeight: 24,
  color: 'red',
}

将所有类似数字的值转换为数字,将字符串转换为字符串.

自动将间接值转换到它们的回放原生等同物.

text-shadow-offset: 10px 5px;
font-variant: small-caps;
transform: translate(10px, 5px) scale(5);
{
  textShadowOffset: { width: 10, height: 5 },
  fontVariant: ['small-caps'],
  // Fixes backwards transform order
  transform: [
    { translateY: 5 },
    { translateX: 10 },
    { scale: 5 },
  ]
}

还允许用快手值.

font: bold 14px/16px "Helvetica";
margin: 5px 7px 2px;
{
  fontFamily: 'Helvetica',
  fontSize: 14,
  fontWeight: 'bold',
  fontStyle: 'normal',
  fontVariant: [],
  lineHeight: 16,
  marginTop: 5,
  marginRight: 7,
  marginBottom: 2,
  marginLeft: 7,
}

短手只接受React中支持的值,所以 background 只能接受一种颜色, backgroundColor

也有人支持: box-shadow 切换成 shadow- 属性。 请注意,这些只在iOS上工作.

简记

border{Top,Right,Bottom,Left} 手写不支持, 因为 borderStyle 无法适用于个别边界.

API 密码

API主要用于执行者. 然而,主要的API可能对未执行者有用. 主 API 是一组 [property, value] tu.

import transform from 'css-to-react-native';
// or const transform = require('css-to-react-native').default;

transform([
  ['font', 'bold 14px/16px "Helvetica"'],
  ['margin', '5px 7px 2px'],
  ['border-left-width', '5px'],
]); // => { fontFamily: 'Helvetica', ... }

我们不提供一种方法 来得到这些风格的拖拉 在这个图书馆, 所以你需要自己做到这一点。 我期望大多数人会使用 postCSS 或者另一个CSS 解析器。 你应该尽量避免得到这些与 string.split,因为这有很多边缘案例(在评论中出现分号等)

对于执行者,还有少数额外的API可用.

这些是针对特定用途的,大多数人应该只是使用上面的API.

import { getPropertyName, getStylesForProperty } from 'css-to-react-native';

getPropertyName('border-width'); // => 'borderWidth'
getStylesForProperty('borderWidth', '1px 0px 2px 0px'); // => { borderTopWidth: 1, ... }

如果您希望选择退出某些缩写转换, 骆驼中的一系列属性名称 案件可以作为第二个论据转交给 transform。 。 。 .

transform([['border-radius', '50px']], ['borderRadius']);
// { borderRadius: 50 } rather than { borderTopLeft: ... }

这也可以通过提出第三个论点来实现, false 改为 getStylesForProperty。 。 。 .

许可证

获得麻省理工学院许可,版权为2019年克里斯特·卡里,雅各布·帕克和马克西米利安·斯托伊伯.

见 利比里亚 更多信息.

版本与发布记录

@cssxjs/css-to-react-native 共发布 3 个版本,首次发布于 2025/11/05;过去 12 个月发布了 2 个版本。

版本发布日期距今
3.2.0-2latest2026/01/259 个月前
3.2.0-12026/01/199 个月前
3.2.0-02025/11/051 年前

关于 @cssxjs/css-to-react-native 的常见问题

@cssxjs/css-to-react-native 是什么?

将 CSS 文本转换为本地样式表对象

如何安装 @cssxjs/css-to-react-native?

在项目目录运行 npm install @cssxjs/css-to-react-native,或使用 pnpm add / yarn add / bun add。

@cssxjs/css-to-react-native 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cssxjs/css-to-react-native 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cssxjs/css-to-react-native 可以商用吗?

@cssxjs/css-to-react-native 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cssxjs/css-to-react-native 的下载量有多少?

根据 npm 官方下载统计,@cssxjs/css-to-react-native 在 2026/10/11 – 2026/11/09 这 30 天内被下载 5,774 次,日均约 192 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cssxjs/css-to-react-native 还在维护吗?

最新版本 3.2.0-2 发布于 2026/01/25(9 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/11/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。