All Projects → kszitt → react-native-sass-to-stylesheet

kszitt / react-native-sass-to-stylesheet

Licence: other
css和sass文件自动转换成react-native样式文件

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to react-native-sass-to-stylesheet

fabric-samples-nodocker
🌱 Deploy fabric-samples without docker. Currently support v1.4.x & 2.0.x .
Stars: ✭ 35 (-33.96%)
Mutual labels:  native
haxeui-hxwidgets
The hxWidgets backend of the HaxeUI framework -
Stars: ✭ 20 (-62.26%)
Mutual labels:  native
react-native-smart-code
Support React & ReactNative.In react-native,it's create base64 String,which is qrcode or barcode ,and without webview.In react,we use jsbarcode.
Stars: ✭ 14 (-73.58%)
Mutual labels:  native
kafka-consumer-lag-monitoring
Client tool that exports the consumer lag of Kafka consumer groups to Prometheus or your terminal
Stars: ✭ 45 (-15.09%)
Mutual labels:  native
scala-native-starter
A starter for scala-native.
Stars: ✭ 27 (-49.06%)
Mutual labels:  native
anitomy-js
Native Node.js wrapper for Anitomy
Stars: ✭ 21 (-60.38%)
Mutual labels:  native
node-webrtc
🔌 WebRTC bindings for Node, written according to the W3C specification.
Stars: ✭ 23 (-56.6%)
Mutual labels:  native
ti.map
Use native Apple Maps & Google Maps in iOS and Android with Axway Titanium
Stars: ✭ 49 (-7.55%)
Mutual labels:  native
microui-odin
A tiny immediate-mode UI library for The Odin Programming Language
Stars: ✭ 24 (-54.72%)
Mutual labels:  native
react-native-audio-polyfill
Audio polyfill for desktop and native.
Stars: ✭ 13 (-75.47%)
Mutual labels:  native
Bank-Account-Simulation
A Bank Account Simulation with JavaFX and SQLite back-end. Material UX|UI.
Stars: ✭ 19 (-64.15%)
Mutual labels:  stylesheets
abifestival-app
Cross-platform festival-app built with the Appcelerator Titanium framework
Stars: ✭ 16 (-69.81%)
Mutual labels:  native
xUnwind
🔥 xUnwind is a collection of Android native stack unwinding solutions.
Stars: ✭ 127 (+139.62%)
Mutual labels:  native
pygments-high-contrast-stylesheets
WCAG AA passing Pygments stylesheets
Stars: ✭ 42 (-20.75%)
Mutual labels:  stylesheets
SilentCryptoMiner
A Silent (Hidden) Free Crypto Miner Builder - Supports ETH, ETC, XMR and many more.
Stars: ✭ 547 (+932.08%)
Mutual labels:  native
react-native-screen-keyboard
On-screen keyboard with customisable keys and tactile / UI feedback 📱
Stars: ✭ 22 (-58.49%)
Mutual labels:  native
auth0-android-sample
Auth0 Integration Samples for Android Applications
Stars: ✭ 61 (+15.09%)
Mutual labels:  native
Expo-Super-Mario-World
Native Super Mario World in Expo
Stars: ✭ 24 (-54.72%)
Mutual labels:  native
IL2CPP Resolver
A run-time API resolver for IL2CPP Unity.
Stars: ✭ 114 (+115.09%)
Mutual labels:  native
SquirrelJME
SquirrelJME is a Java ME 8 Virtual Machine for embedded and Internet of Things devices. It has the ultimate goal of being 99.9% compatible with the Java ME standard.
Stars: ✭ 148 (+179.25%)
Mutual labels:  native

语言

English

描述

css文件自动转换成react-native样式文件。
1、支持变量
2、支持媒体查询
3、支持嵌套
4、支持transform
5、适配各种手机
6、支持群组选择器
7、忽略文件
8、scss文件支持@import

安装

npm install react-native-sass-to-stylesheet --save-dev

使用

初始化

新建toStyles.js,并添加以下内容

const ToStyles = require("react-native-sass-to-stylesheet");

ToStyles.init(path[, options]);

.init(path[, options])

  • path{string} 要监听的文件夹路径,必须
  • options{object}
    • space{number} css文件缩进值,默认2
    • postfix{string} 转换生成的js文件后缀,默认Style.js。例如:home.scss转换生成homeStyle.js
    • initTransform{boolean} 启动服务后,是否自动转换所有的css文件,默认false
    • adaptation{boolean} 适配各种手机,默认true。如果单个样式不需要适配,请添加 !important标志
    • templatePath{string} 自动转换文件模板路径,默认./template.js
    • ignored{array} 忽略文件"xxx.scss",忽略文件夹"home"或者"component/home",默认[]
启动
node toStyles.js
SCSS文件

.init()path目录下,创建、修改css或者scss文件,保存。会在当前目录下生成js文件。例如:home.scss如下

.header {
  font: 12px/24px;
  .logo {
    position: absolute;
    .img {
      width: 100px;
      height: 100px;
    }
  }
}
转换后,生成的homeStyle.js
import {StyleSheet, PixelRatio, Dimensions} from 'react-native';
const pixelRatio = PixelRatio.get();
let {width, height} =  Dimensions.get('window');

function getAdaptation(num){    // 可以在"options.templatePath"模板中自定义该函数
  let unitWidth = width / 1080; // 1080 => UI设计图的宽度
  return parseFloat((num*unitWidth).toFixed(2));
}

let styles = {
  header: {
    fontSize: getAdaptation(12),
    lineHeight: getAdaptation(24)
  },
  header_logo: {
    position: "absolute"
  },
  header_logo_img: {
    width: getAdaptation(100),
    height: getAdaptation(100)
  }
};

const styleSheet = StyleSheet.create(styles);
export default styleSheet;
在react native中使用
import Style from "homeStyle.js";
...
render(){
    return (
       <View style={Style.header}>
           <View style={Style.header_logo}>
               <Image source={...} style={Style.header_logo_img}/>
           </View>
       </View>
    );
}

示例

font
.main {
  font: italic bold 12px/24px "Arial";
  font-variant: small-caps, lining-nums;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    fontVariant: [
      "small-caps",
      "lining-nums"
    ],
    fontSize: getAdaptation(12),
    lineHeight: getAdaptation(24),
    fontStyle: "italic",
    fontWeight: "bold",
    fontFamily: "Arial"
  }
};
margin padding
.main {
  margin: 0 10px;
  padding: 1px 2px 3px;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    marginTop: 0,
    marginBottom: 0,
    marginRight: getAdaptation(10),
    marginLeft: getAdaptation(10),
    paddingTop: getAdaptation(1),
    paddingBottom: getAdaptation(3),
    paddingRight: getAdaptation(2),
    paddingLeft: getAdaptation(2),
  }
};
border
.main {
  border: 1px solid #333;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    borderWidth: getAdaptation(1),
    borderColor: "#333",
    borderStyle: "solid"
  }
};
text-decoration
.main {
  text-decoration: underline solid red;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    textDecorationLine: "underline",
    textDecorationColor: "red",
    textDecorationStyle: "solid"
  }
};
text-shadow
.main {
  text-shadow: 5px 5px 10px red;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    textShadowOffset: {
      width: getAdaptation(5),
      height: getAdaptation(5)
    },
    textShadowRadio: getAdaptation(10),
    textShadowColor: "red"
  }
};
shadow
.main {
  box-shadow: 10px 10px 5px .5 #888888;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    shadowOffset: {
      width: getAdaptation(10),
      height: getAdaptation(10)
    },
    shadowRadio: getAdaptation(5),
    shadowOpacity: .5,
    shadowColor: "#888888"
  }
};
transform
.main {
  transform: translate(10px, 20px) rotateY(-10.3deg) scaleX(.5) skew(60deg);
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    transform: [
      {
        translateX: getAdaptation(10),
        translateY: getAdaptation(20)
      },
      {
        rotateY: "-10.3deg"
      },
      {
        scaleX: .5
      },
      {
        skewX: "60deg"
      }
    ]
  }
};
变量
$size: 12px !global; // 别的页面也可以使用
$color: red;
.header {
  font: $size/24px;
  .left {
    color: $color;
  }
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  header: {
    fontSize: getAdaptation(12),
    lineHeight: getAdaptation(24)
  },
  header_left: {
    color: "red"
  }
};
@import
@import "../header.scss"; // 必须写后缀名
.header {
  color: red;
}
// header.scss
.header {
  width: 100px;
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  header: {
    width: getAdaptation(100),
    color: "red"
  }
};
群组选择器
.main {
  display: flex;
  .left, .right {
    position: absolute;
    left: 0;
  }
  .left {
    left: 10px;
  }
}

↓ ↓ ↓ ↓ ↓ ↓

let styles = {
  main: {
    display: "flex"
  },
  main_left: {
    position: "absolute",
    left: getAdaptation(10)
  },
  main_right: {
    position: "absolute",
    left: 0
  }
};
媒体查询
.main {
  width: 500px;
}
@media only screen and (min-width: 500px) and (max-width: 1000px) {
  .main {
    width: 100%;
    height: 1000px;
  }
}

↓ ↓ ↓ ↓ ↓ ↓

import {StyleSheet, PixelRatio, Dimensions} from 'react-native';
const pixelRatio = PixelRatio.get();
let {width, height} =  Dimensions.get('window');

function getAdaptation(num){    // 可以在"options.templatePath"模板中自定义该函数
  let unitWidth = width / 1080; // 1080 => UI设计图的宽度
  return parseFloat((num*unitWidth).toFixed(2));
}

let styles = {
  main: {
    width: getAdaptation(500)
  },
  main_top: {
    fontSize: getAdaptation(12)
  }
};


let media = {
  "width>=500&&width<=1000": {
    main: {
      width: "100%",
      height: getAdaptation(1000)
    }
  }
};

// 媒体查询
(function addMedia(){   // 可以在"options.templatePath"模板中自定义该函数
  for(let k in media){
    if(eval(k)){
      for(let j in media[k]){
        styles[j] = Object.assign(styles[j] || {}, media[k][j]);
      }
    }
  }
}());
const styleSheet = StyleSheet.create(styles);
export default styleSheet;

自动生成模板

默认的自动生成模板
import {StyleSheet, PixelRatio, Dimensions} from 'react-native';
const pixelRatio = PixelRatio.get();
let {width, height} =  Dimensions.get('window');

function getAdaptation(num){
  let unitWidth = width / 1080; // 1080 => UI设计图的宽度
  return parseFloat((num*unitWidth).toFixed(2));
}

let styles = {};
let media = {};

const styleSheet = StyleSheet.create(styles);

export default styleSheet;
使用自定义模板

修改init(path[, options])options.templatePath模板路径,写入你的模板。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].