# css-to-react-native

> Convert CSS text to a React Native stylesheet object

Latest version **3.2.0** (published 2023-02-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install css-to-react-native
pnpm add css-to-react-native
yarn add css-to-react-native
bun add css-to-react-native
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2023-02-14 |
| First published | 2016-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 87.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1177 |
| Author | Jacob Parker |
| Maintainers | kristerkari, jacobp100, mxstbr |
| Keywords | styled-components, React, ReactNative, styles, CSS |

## Links

- npm: https://www.npmjs.com/package/css-to-react-native
- Repository: https://github.com/styled-components/css-to-react-native
- Homepage: https://github.com/styled-components/css-to-react-native#readme
- Issues: https://github.com/styled-components/css-to-react-native/issues
- npm.io page: https://npm.io/package/css-to-react-native

## Dependencies (3)

- [camelize](https://npm.io/package/camelize.md) ^1.0.0
- [css-color-keywords](https://npm.io/package/css-color-keywords.md) ^1.0.0
- [postcss-value-parser](https://npm.io/package/postcss-value-parser.md) ^4.0.2

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 3.2.0 (latest) — 2023-02-14
- 3.1.0 — 2023-01-12
- 3.0.0 — 2019-10-10
- 2.3.2 — 2019-08-25
- 2.3.1 — 2019-05-07
- 2.3.0 — 2019-02-07
- 2.2.2 — 2018-09-27
- 2.2.1 — 2018-06-23
- 2.2.0 — 2018-05-09
- 2.1.2 — 2018-02-18
- 2.1.1 — 2018-02-04
- 2.1.0 — 2018-02-03
- 2.0.4 — 2017-05-22
- 2.0.3 — 2017-04-02
- 2.0.1 — 2017-01-25
- … 8 more at https://npm.io/package/css-to-react-native/versions

## README

# css-to-react-native

Converts CSS text to a React Native stylesheet object.

[Try it here](https://csstox.surge.sh)

```css
font-size: 18px;
line-height: 24px;
color: red;
```

```js
{
  fontSize: 18,
  lineHeight: 24,
  color: 'red',
}
```

Converts all number-like values to numbers, and string-like to strings.

Automatically converts indirect values to their React Native equivalents.

```css
text-shadow-offset: 10px 5px;
font-variant: small-caps;
transform: translate(10px, 5px) scale(5);
```

```js
{
  textShadowOffset: { width: 10, height: 5 },
  fontVariant: ['small-caps'],
  // Fixes backwards transform order
  transform: [
    { translateY: 5 },
    { translateX: 10 },
    { scale: 5 },
  ]
}
```

Also allows shorthand values.

```css
font: bold 14px/16px "Helvetica";
margin: 5px 7px 2px;
```

```js
{
  fontFamily: 'Helvetica',
  fontSize: 14,
  fontWeight: 'bold',
  fontStyle: 'normal',
  fontVariant: [],
  lineHeight: 16,
  marginTop: 5,
  marginRight: 7,
  marginBottom: 2,
  marginLeft: 7,
}
```

Shorthands will only accept values that are supported in React, so `background` will only accept a colour, `backgroundColor`

There is also support for the `box-shadow` shorthand, and this converts into `shadow-` properties. Note that these only work on iOS.

#### Shorthand Notes

`border{Top,Right,Bottom,Left}` shorthands are not supported, because `borderStyle` cannot be applied to individual border sides.

# API

The API is mostly for implementors. However, the main API may be useful for non-implementors. The main API is an array of `[property, value]` tuples.

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

transform([
  ['font', 'bold 14px/16px "Helvetica"'],
  ['margin', '5px 7px 2px'],
  ['border-left-width', '5px'],
]); // => { fontFamily: 'Helvetica', ... }
```

We don't provide a way to get these style tuples in this library, so you'll need to do that yourself. I expect most people will use postCSS or another CSS parser. You should try avoid getting these with `string.split`, as that has a lot of edge cases (colons and semi-colons appearing in comments etc.)

For implementors, there is also a few extra APIs available.

These are for specific use-cases, and most people should just be using the API above.

```js
import { getPropertyName, getStylesForProperty } from 'css-to-react-native';

getPropertyName('border-width'); // => 'borderWidth'
getStylesForProperty('borderWidth', '1px 0px 2px 0px'); // => { borderTopWidth: 1, ... }
```

Should you wish to opt-out of transforming certain shorthands, an array of property names in camelCase can be passed as a second argument to `transform`.

```js
transform([['border-radius', '50px']], ['borderRadius']);
// { borderRadius: 50 } rather than { borderTopLeft: ... }
```

This can also be done by passing a third argument, `false` to `getStylesForProperty`.

## License

Licensed under the MIT License, Copyright © 2019 Krister Kari, Jacob Parker, and Maximilian Stoiber.

See [LICENSE.md](./LICENSE.md) for more information.

---
_Source: https://npm.io/package/css-to-react-native · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
