npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-css-file

v0.0.6

Published

🚄🚄使用css文件书写react-native的样式

Downloads

4

Readme

react-native-css-file

🚄🚄使用css文件书写react-native的样式

按照官方写法

let styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

使用css文件来实现

/**
 * global.css
 */

.container {
    flex: 1;
    justify-content: center;
    align-items: center;
    background-color: #F5FCFF;
}

.welcome {
    font-size: 20;
    text-align: center;
    margin: 10;
}

.instructions{
    text-align: center;
    color: #333;
    margin-bottom: 5;
}
const styles = require('./styles/global').build();

特性

  • 支持类选择器
  • 支持css简写,例如transform: rotate(60deg) scale(1.2)margin: 3px 3px;border: solid 1px #ccc
  • css注释
  • 错误信息跟踪
  • 自定义变量
  • 🚫 派生选择器(考虑实现)

安装

npm install react-native-css-file

使用

首先,在项目的任何地方创建一个css文件。例如在Header组件中创建一个headerStyles.css文件

- Header
  - Header.js
  - headerStyles.css
/* headerStyles.css */

.container {
  flex: 1;
  backgroundColor: #fff;
}

.icon {
  border: solid 1px #fff;
  pading: 3px 5px;
}

package.json中添加一个命令

"css": "rncss ./ --watch",
"css-build": "rncss ./",

然后运行 npm run css,会自动生成一个headerStyles.js文件(注意不要手动更改它)。

然后像以往那样使用css。

// Header.js

const styles = require('./headerStyles').build();

// ...
render() {
  return (
    <View style={styles.container}>
       <View style={styles.Icon} />
    </View>
  );
}

// ...

变量

因为在RN开发过程中经常需要动态的初始化样式,例如通过Dimensions来获取窗口大小然后设置高宽。为了在css中实现这样效果,这里提供了变量支持。

/* header.css*/
.icon {
  height: iconHeight;
  width: iconWidth;
}

.border {
  height: hairlineWidth;
}

在调用build方法时传入你想要设置的值

  require('./headerStyles').build({
    iconHeight: 30,
    iconWidth: 60,
  });

预制的变量有:

  • hairlineWidth StylesSheet.hairlineWidth

示例

 git clone https://github.com/yqz0203/react-native-css-file.git`

 cd ./react-native-css-file/example

 yarn

 npm run css

 react-native run-ios

注意必须使用yarn安装

问题

项目初期不可避免有功能缺失以及BUG,欢迎大家提出issue和共同维护。

License

MIT