An open API service indexing awesome lists of open source software.

https://github.com/kristerkari/babel-plugin-react-native-stylename-to-style

Transform styleName property to style property in react-native.
https://github.com/kristerkari/babel-plugin-react-native-stylename-to-style

babel babel-plugin react-native style stylename

Last synced: 3 months ago
JSON representation

Transform styleName property to style property in react-native.

Awesome Lists containing this project

README

        

# babel-plugin-react-native-stylename-to-style

[![NPM version](http://img.shields.io/npm/v/babel-plugin-react-native-stylename-to-style.svg)](https://www.npmjs.org/package/babel-plugin-react-native-stylename-to-style)
[![Build Status](https://travis-ci.org/kristerkari/babel-plugin-react-native-stylename-to-style.svg?branch=master)](https://travis-ci.org/kristerkari/babel-plugin-react-native-stylename-to-style)
[![Build status](https://ci.appveyor.com/api/projects/status/1040mtj6qyu9vkg8/branch/master?svg=true)](https://ci.appveyor.com/project/kristerkari/babel-plugin-react-native-stylename-to-style/branch/master)
[![Coverage Status](https://coveralls.io/repos/github/kristerkari/babel-plugin-react-native-stylename-to-style/badge.svg?branch=master)](https://coveralls.io/github/kristerkari/babel-plugin-react-native-stylename-to-style?branch=master)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github)

Transform JSX `styleName` property to `style` property in react-native. The `styleName` attribute and syntax are based on [babel-plugin-react-css-modules](https://github.com/gajus/babel-plugin-react-css-modules#conventions).

## Usage

### Step 1: Install

```sh
yarn add --dev babel-plugin-react-native-stylename-to-style
```

or

```sh
npm install --save-dev babel-plugin-react-native-stylename-to-style
```

### Step 2: Configure `.babelrc`

You must give one or more file extensions inside an array in the plugin options.

```
{
"presets": [
"react-native"
],
"plugins": [
["react-native-stylename-to-style", {
"extensions": ["css"]
}]
]
}
```

## Syntax

## Anonymous reference

Anonymous reference can be used when there is only one stylesheet import.

### Single class

```jsx
import "./Button.css";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import Button from "./Button.css";

Foo
;
```

### Multiple classes

```jsx
import "./Button.css";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import Button from "./Button.css";

Foo
;
```

### Expression

```jsx
import "./Button.css";
const name = "wrapper";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import Button from "./Button.css";
const name = "wrapper";

Foo
;
```

### Expression with ternary

```jsx
import "./Button.css";

const condition = true;
const name = "wrapper";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import Button from "./Button.css";

const condition = true;
const name = "wrapper";

Foo
;
```

### with `styleName` and `style`:

```jsx
import "./Button.css";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import Button from "./Button.css";

Foo
;
```

## Named reference

Named reference is used to refer to a specific stylesheet import.

### Single class

```jsx
import foo from "./Button.css";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import foo from "./Button.css";

Foo
;
```

### Multiple classes

```jsx
import foo from "./Button.css";
import bar from "./Grid.css";

Foo
;
```

↓ ↓ ↓ ↓ ↓ ↓

```jsx
import foo from "./Button.css";
import bar from "./Grid.css";

Foo
;
```