3.2.0 • Published 2 years ago

css-to-react-native v3.2.0

Weekly downloads
2,495,773
License
MIT
Repository
github
Last release
2 years ago

css-to-react-native

Converts CSS text to a React Native stylesheet object.

Try it here

font-size: 18px;
line-height: 24px;
color: red;
{
  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.

text-shadow-offset: 10px 5px;
font-variant: small-caps;
transform: translate(10px, 5px) scale(5);
{
  textShadowOffset: { width: 10, height: 5 },
  fontVariant: ['small-caps'],
  // Fixes backwards transform order
  transform: [
    { translateY: 5 },
    { translateX: 10 },
    { scale: 5 },
  ]
}

Also allows shorthand values.

font: bold 14px/16px "Helvetica";
margin: 5px 7px 2px;
{
  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.

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.

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.

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 for more information.

styled-components@tw-rn/processoreasy-select-rnmikedemarais-styled-components@towbe/rncssstyled-box@infinitebrahmanuniverse/nolb-css-treact-native-markdown-displayer@everything-registry/sub-chunk-1420@triframe/coredummy-styled-componentstailwindcss-rntailwindcss-react-nativetailwind-rftailwind-rntailwind-rn-iotailwind-rn-ltstailwind-rn-webtailwind-react-native-classnamestest-iki-mini-appwith-transitionrn-shikispeedy-styled-components@dothq/styled-components@district/react-native-markdown-display@dscottpi/react-native-markdown@cloudimage-strapi/content-plugin@cruzach/styled-components@cosmicmedia/react-native-markdown-display@design-blocks/block@design-blocks/core@bencryrus/react-native-markdown-displayjinghuan-react-nativeiphong-styled-componentskafirchain-tetrisreact-native-livelo-componentsreact-native-storybok-atomic-templatestyled-components-matmalkowskistyled-components-mikedemarais-forkstyled-components-native-esmstyled-components-v2styled-components-aretecodestyled-components-v5.1-react18styled-components-webstyled-components-with-middlewaresstyled-cssstyled-native-componentsspeed-styled-componentsstnenopmoc-iustilerueb-componentstransform-css-to-js@ankitfxz/params-ui@asvetliakov/styled-components@atoto/native@areslabs/babel-plugin-import-css@astly/parsers@agriffis/styled-components@baifendian/adhere-ui-formdesign@baolq/styled-components@carlossless/styled-components@clentfort/styled-components@chibikookie/styled-components@chancey/styled-components@0x706b/styled-components128981semzub@actbase/css-to-react-native-transform@code-atlas/react-native-markdown-display@codingdud/coolicons@ddx0510/react-native-markdown-display@dash-ui/react-native@danieldunderfelt/react-native-markdown-displayyup-react-native-markdown-displayzubtesttodayunonative@e-mine/react-native-html-text@hnordt/react-native-postcss-transformer@flowchase/react-native-markdown-display@gcencic/styled-components@foxize/params-ui@itayn-fireberry-org/itayn-test@matmello/styled-components@marionebl/styled-components@martinklepsch/satori@napred/native@neo-component/react@natalya_myunster/react-native-html-text@native-html/css-processor@emotion/primitives-core@eeacms/volto-columns-tabs-block@eeacms/volto-grid-block@elemental-design/styled-components@dynasty/styled-components@migueloller/styled-components@intellihr/styled-components@interunit/crossplatform@jeongshin/react-native-code-highlighter@jeongshin/react-native-markdown-display@jamsch/nativewind@jonasmerlin/react-native-markdown-display
3.2.0

2 years ago

3.1.0

2 years ago

3.0.0

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago