Detalhes do pacote

style-to-js

remarkablemark13.9mMIT1.1.17

Parses CSS inline style to JavaScript object (camelCased).

style-to-js, css, style, javascript

readme (leia-me)

style-to-js

NPM

NPM version Bundlephobia minified + gzip build codecov NPM downloads

Parses CSS inline style to JavaScript object (camelCased):

StyleToJS(string)

Example

import parse from 'style-to-js';

parse('background-color: #BADA55;');

Output:

{ "backgroundColor": "#BADA55" }

Replit | JSFiddle | Examples

Install

NPM:

npm install style-to-js --save

Yarn:

yarn add style-to-js

CDN:

<script src="https://unpkg.com/style-to-js@latest/umd/style-to-js.min.js"></script>
<script>
  window.StyleToJS(/* string */);
</script>

Usage

Import

Import with ES Modules:

import parse from 'style-to-js';

Require with CommonJS:

const parse = require('style-to-js');

Parse style

Parse single declaration:

parse('line-height: 42');

Output:

{ "lineHeight": "42" }

Notice that the CSS property is camelCased.

Parse multiple declarations:

parse(`
  border-color: #ACE;
  z-index: 1337;
`);

Output:

{
  "borderColor": "#ACE",
  "zIndex": "1337"
}

Vendor prefix

Parse vendor prefix:

parse(`
  -webkit-transition: all 4s ease;
  -moz-transition: all 4s ease;
  -ms-transition: all 4s ease;
  -o-transition: all 4s ease;
  -khtml-transition: all 4s ease;
`);

Output:

{
  "webkitTransition": "all 4s ease",
  "mozTransition": "all 4s ease",
  "msTransition": "all 4s ease",
  "oTransition": "all 4s ease",
  "khtmlTransition": "all 4s ease"
}

Custom property

Parse custom property:

parse('--custom-property: #f00');

Output:

{ "--custom-property": "#f00" }

Unknown declaration

This library does not validate declarations, so unknown declarations can be parsed:

parse('the-answer: 42;');

Output:

{ "theAnswer": "42" }

Invalid declaration

Declarations with missing value are removed:

parse(`
  margin-top: ;
  margin-right: 1em;
`);

Output:

{ "marginRight": "1em" }

Other invalid declarations or arguments:

parse(); // {}
parse(null); // {}
parse(1); // {}
parse(true); // {}
parse('top:'); // {}
parse(':12px'); // {}
parse(':'); // {}
parse(';'); // {}

The following values will throw an error:

parse('top'); // Uncaught Error: property missing ':'
parse('/*'); // Uncaught Error: End of comment missing

Options

reactCompat

When option reactCompat is true, the vendor prefix will be capitalized:

parse(
  `
    -webkit-transition: all 4s ease;
    -moz-transition: all 4s ease;
    -ms-transition: all 4s ease;
    -o-transition: all 4s ease;
    -khtml-transition: all 4s ease;
  `,
  { reactCompat: true },
);

Output:

{
  "WebkitTransition": "all 4s ease",
  "MozTransition": "all 4s ease",
  "msTransition": "all 4s ease",
  "OTransition": "all 4s ease",
  "KhtmlTransition": "all 4s ease"
}

This removes the React warning:

Warning: Unsupported vendor-prefixed style property %s. Did you mean %s?%s", "oTransition", "OTransition"

Testing

Run tests with coverage:

npm test

Run tests in watch mode:

npm run test:watch

Lint files:

npm run lint

Fix lint errors:

npm run lint:fix

Release

Release and publish are automated by Release Please.

Special Thanks

License

MIT

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.1.17 (2025-06-16)

Build System

  • deps: bump style-to-object from 1.0.8 to 1.0.9 (#661) (8a87a94)

1.1.16 (2024-10-06)

Bug Fixes

  • handle underscore for css custom property regex (#503) (c35e55a)

1.1.15 (2024-10-03)

Build System

  • deps: bump style-to-object from 1.0.7 to 1.0.8 (#483) (2b9e265)

1.1.14 (2024-09-09)

Build System

  • deps: bump style-to-object from 1.0.6 to 1.0.7 (#472) (0441b6e)

1.1.13 (2024-08-27)

Continuous Integration

  • github: publish package to npm registry with provenance (7441aa0)

1.1.12 (2024-03-27)

Build System

  • deps: bump style-to-object from 1.0.5 to 1.0.6 (#388) (f6ec541)

1.1.11 (2024-03-19)

Bug Fixes

  • make exports work as documented (448efa2)

1.1.10 (2023-12-01)

Build System

  • deps: bump style-to-object from 1.0.4 to 1.0.5 (#295) (98a8b87)

1.1.9 (2023-11-02)

Build System

  • deps: bump style-to-object from 1.0.3 to 1.0.4 (#261) (a88e221)

1.1.8 (2023-10-20)

Build System

  • build: generate sourceMap and include "src/" in package.json files (#245) (a39c0b9)

1.1.7 (2023-10-20)

Bug Fixes

  • package: bump style-to-object from 1.0.1 to 1.0.3 to fix UMD build (203e976)

1.1.6 (2023-10-17)

Build System

  • deps: bump style-to-object from 1.0.0 to 1.0.1 (#240) (1c9d816)

1.1.5 (2023-10-17)

Build System

  • deps: bump style-to-object from 0.4.2 to 1.0.0 (#236) (1c2e90a)

1.1.4 (2023-08-31)

Build System

  • deps: bump style-to-object from 0.4.1 to 0.4.2 (#177) (d412537)

1.1.3 (2023-01-16)

Build System

  • package: bump style-to-object from 0.4.0 to 0.4.1 (52f0442)

1.1.2 (2022-12-25)

Build System

  • deps: bump style-to-object from 0.3.0 to 0.4.0 (59f1e75)

Miscellaneous Chores

1.1.1 (2022-05-30)

Bug Fixes

  • don't capitalize -ms vendor prefix in reactCompat mode (208926b), closes #2

1.1.0 (2020-11-22)

Features

  • index: pass options from StyleToJS to camelCase (5dca2ef)
  • utilities: add option reactCompat to camelCase (0f60c47)

Bug Fixes

  • utilities: add -khtml- (Konqueror) to vendor prefix (39fba30)

1.0.0 (2020-11-22)

Features

  • index: add StyleToJS that converts CSS style to JS object (59d3869)
  • utilities: add camelCase helper (1e5845f)

Bug Fixes

  • utilities: camelCase CSS vendor prefix correctly (6527322)