@actbase/css-to-react-native-transform 中文文档教程

发布于 3 年前 浏览 13 项目主页 更新于 2 年前

css-to-react-native-transform

NPM 版本构建状态构建状态覆盖状态每月下载量contributions welcomeGreenkeeper 徽章

一个轻量级的包装器 css-to-react-native 允许将有效的 CSS 转换为 React Native Stylesheet 对象。

为简单起见,它仅转换类选择器(例如 .myClass {})和分组类选择器(例如 .myClass, .myOtherClass {})。 如果需要,将来可以将更复杂的选择器的解析添加为功能标志后面的新功能(例如 transform(css, { parseAllSelectors: true }))。

示例:

.myClass {
  font-size: 18px;
  line-height: 24px;
  color: red;
}

.other {
  padding: 1rem;
}

转换为:

{
  myClass: {
    fontSize: 18,
    lineHeight: 24,
    color: "red"
  },
  other: {
    paddingBottom: 16,
    paddingLeft: 16,
    paddingRight: 16,
    paddingTop: 16
  }
}

API

Transform CSS

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

transform(`
  .foo {
    color: #f00;
  }
`);

↓ ↓ ↓ ↓ ↓ ↓

{
  foo: {
    color: "#f00";
  }
}

CSS Modules :export block

支持解析 CSS 模块 (ICSS) :export:export 通常用于从 CSS 或从 Sass/Less/Stylus 等预处理器共享变量到 Javascript:

transform(`
  .foo {
    color: #f00;
  }

  :export {
    myProp: #fff;
  }
`);

↓ ↓ ↓ ↓ ↓ ↓

{
  foo: {
    color: "#f00";
  },
  myProp: "#fff";
}

CSS Media Queries (experimental)

CSS 媒体查询的 API 和解析语法可能未来的变化

transform(
  `
  .container {
    background-color: #f00;
  }

  @media (orientation: landscape) {
    .container {
      background-color: #00f;
    }
  }
`,
  { parseMediaQueries: true },
);

↓ ↓ ↓ ↓ ↓ ↓

{
  __mediaQueries: {
    "@media (orientation: landscape)": [{
      expressions: [
        {
          feature: "orientation",
          modifier: undefined,
          value: "landscape",
        },
      ],
      inverse: false,
      type: "all",
    }],
  },
  container: {
    backgroundColor: "#f00",
  },
  "@media (orientation: landscape)": {
    container: {
      backgroundColor: "#00f",
    },
  },
}

你也可以指定一个平台作为媒体查询类型(“android”,“dom”,“ios”,“macos”,“web”,“windows”) :

transform(
  `
  .container {
    background-color: #f00;
  }

  @media android and (orientation: landscape) {
    .container {
      background-color: #00f;
    }
  }
`,
  { parseMediaQueries: true },
);

CSS Viewport Units (experimental)

当使用 CSS Viewport Units 时,一个特殊的 __viewportUnits 特征标志被添加到结果中. 这样做是为了让将视口单位转换为像素的实现知道样式对象内部有视口单位,并且如果样式对象不包含任何视口单位,则可以避免做额外的工作。

transform(`.foo { font-size: 1vh; }`);

↓ ↓ ↓ ↓ ↓ ↓

{
   __viewportUnits: true,
  foo: {
    fontSize: "1vh";
  }
}

Limitations

  • For rem unit the root element font-size is currently set to 16 pixels. A setting needs to be implemented to allow the user to define the root element font-size.
  • There is also support for the box-shadow shorthand, and this converts into shadow- properties. Note that these only work on iOS.

Dependencies

该库具有以下包作为依赖项:

  • css - CSS parser / stringifier
  • css-mediaquery - Parses and determines if a given CSS Media Query matches a set of values.
  • css-to-react-native - Convert CSS text to a React Native stylesheet object

css-to-react-native-transform

NPM versionBuild StatusBuild statusCoverage StatusDownloads per monthcontributions welcomeGreenkeeper badge

A lightweight wrapper on top of css-to-react-native to allow valid CSS to be turned into React Native Stylesheet objects.

To keep things simple it only transforms class selectors (e.g. .myClass {}) and grouped class selectors (e.g. .myClass, .myOtherClass {}). Parsing of more complex selectors can be added as a new feature behind a feature flag (e.g. transform(css, { parseAllSelectors: true })) in the future if needed.

Example:

.myClass {
  font-size: 18px;
  line-height: 24px;
  color: red;
}

.other {
  padding: 1rem;
}

is transformed to:

{
  myClass: {
    fontSize: 18,
    lineHeight: 24,
    color: "red"
  },
  other: {
    paddingBottom: 16,
    paddingLeft: 16,
    paddingRight: 16,
    paddingTop: 16
  }
}

API

Transform CSS

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

transform(`
  .foo {
    color: #f00;
  }
`);

↓ ↓ ↓ ↓ ↓ ↓

{
  foo: {
    color: "#f00";
  }
}

CSS Modules :export block

Parsing the CSS Modules (ICSS) :export is supported. The :export is often used to share variables from CSS or from a preprocessor like Sass/Less/Stylus to Javascript:

transform(`
  .foo {
    color: #f00;
  }

  :export {
    myProp: #fff;
  }
`);

↓ ↓ ↓ ↓ ↓ ↓

{
  foo: {
    color: "#f00";
  },
  myProp: "#fff";
}

CSS Media Queries (experimental)

The API and parsed syntax for CSS Media Queries might change in the future

transform(
  `
  .container {
    background-color: #f00;
  }

  @media (orientation: landscape) {
    .container {
      background-color: #00f;
    }
  }
`,
  { parseMediaQueries: true },
);

↓ ↓ ↓ ↓ ↓ ↓

{
  __mediaQueries: {
    "@media (orientation: landscape)": [{
      expressions: [
        {
          feature: "orientation",
          modifier: undefined,
          value: "landscape",
        },
      ],
      inverse: false,
      type: "all",
    }],
  },
  container: {
    backgroundColor: "#f00",
  },
  "@media (orientation: landscape)": {
    container: {
      backgroundColor: "#00f",
    },
  },
}

You can also speficy a platform as the media query type ("android", "dom", "ios", "macos", "web", "windows"):

transform(
  `
  .container {
    background-color: #f00;
  }

  @media android and (orientation: landscape) {
    .container {
      background-color: #00f;
    }
  }
`,
  { parseMediaQueries: true },
);

CSS Viewport Units (experimental)

When CSS Viewport Units are used, a special __viewportUnits feature flag is added to the result. This is done so that the implementation that transforms viewport units to pixels knows that the style object has viewport units inside it, and can avoid doing extra work if the style object does not contain any viewport units.

transform(`.foo { font-size: 1vh; }`);

↓ ↓ ↓ ↓ ↓ ↓

{
   __viewportUnits: true,
  foo: {
    fontSize: "1vh";
  }
}

Limitations

  • For rem unit the root element font-size is currently set to 16 pixels. A setting needs to be implemented to allow the user to define the root element font-size.
  • There is also support for the box-shadow shorthand, and this converts into shadow- properties. Note that these only work on iOS.

Dependencies

This library has the following packages as dependencies:

  • css - CSS parser / stringifier
  • css-mediaquery - Parses and determines if a given CSS Media Query matches a set of values.
  • css-to-react-native - Convert CSS text to a React Native stylesheet object
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文