Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Turn valid CSS into React Native Stylesheet objects.

License

NotificationsYou must be signed in to change notification settings

kristerkari/css-to-react-native-transform

Repository files navigation

NPM versionBuild StatusCoverage StatusDownloads per monthcontributions welcome

A lightweight wrapper on top ofcss-to-react-nativeto 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

importtransformfrom"css-to-react-native-transform";// or const transform = require("css-to-react-native-transform").default;transform(`  .foo {    color: #f00;  }`);

↓ ↓ ↓ ↓ ↓ ↓

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

ignoreRule option

transform(`  .foo {    color: red;  }  .bar {    font-size: 12px;  }`,{ignoreRule:(selector)=>{if(selector===".foo"){returntrue;}},},);

↓ ↓ ↓ ↓ ↓ ↓

{  bar:{    fontSize:12;}}

CSS Modules :export block

Parsing theCSS Modules (ICSS) :export is supported. The:export is often used to share variables from CSS or from a preprocessor like Sass/Less/Stylus to #"auto" data-snippet-clipboard-copy-content="transform(` .foo { color: #f00; } :export { myProp: #fff; }`);">

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)

WhenCSS 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

  • Forrem unit the root elementfont-size is currently set to 16 pixels. Asetting needs to be implemented to allow the user to define the root elementfont-size.
  • There is also support for thebox-shadow shorthand, and this converts intoshadow- 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

About

Turn valid CSS into React Native Stylesheet objects.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp