Skip to content

Releases: arkarlov/react-style-stringify

v1.1.0

24 Oct 19:33
Compare
Choose a tag to compare

Cleaner, Leaner CSS

v1.1.0 (2024-10-24)

✨ Stripped spaces between CSS rules.

Whitespace between CSS rules has been removed for more compact output.

stringifyCSSProperties({
  flex: 1,
  padding: 20,
  backgroundColor: "teal",
});

Before

// Output: "flex:1; padding:20px; background-color:teal;"

After

// Output: "flex:1;padding:20px;background-color:teal;"

🧹 Removed empty CSS rules from the output in stringifyStyleMap.

Previously, passing 'selector': {} resulted in selector{}, but now such rules are excluded from the result.

stringifyStyleMap({
      header: { color: "teal" },
      main: {},
      footer: { color: "teal" },
    });

Before

// Output: "header{color:teal;} main{} footer{color:teal;}"

After

// Output: "header{color:teal;}footer{color:teal;}"

✂️ Trimmed unnecessary spaces in CSS selectors.

Extra spaces around combinators (e.g., >, +, ~) are now removed.

stringifyStyleMap({
  "#root ul.my-list > li": {
    padding: 10,
  },
});

Before

// Output: "#root ul.my-list > li{padding:10px;}"

After

// Output: "#root ul.my-list>li{padding:10px;}"

🤖 What's Changed

Full Changelog: v1.0.0...v1.1.0

v1.0.0

24 Sep 22:12
Compare
Choose a tag to compare

Initial Release

v1.0.0 (2024-09-24)

🎉 Highlights

This is the first release of the react-style-stringify package.
It provides utility functions to convert CSS objects into string representations, making it easier to work with styles in React projects and beyond.

✨ Key Features

Converts a CSSProperties object into a CSS string.
Converts a Record<string, CSSProperties> map into a CSS string.
Automatically adds units (e.g., px) to numeric CSS properties when necessary.
Allows injecting !important for all properties.
Supports both CommonJS and ESModules.

📦 Installation

npm install react-style-stringify

or

yarn add react-style-stringify

☝️ Note: You need to install @types/react if you're using TypeScript without React.

🚀 Usage

Import utils:

import {
  stringifyCSSProperties,
  stringifyStyleMap,
} from "react-style-stringify";

Convert a single CSSProperties object:

const cssString = stringifyCSSProperties({
  flex: 1,
  padding: 20,
  backgroundColor: "teal",
});
// Output: "flex:1; padding:20px; background-color:teal;"

Convert a Record<string, CSSProperties> object:

const cssMapString = stringifyStyleMap({
  p: {
    margin: 0,
    color: "teal",
  },
  "#root ul.my-list > li": {
    padding: 10,
  },
});
// Output: "p{margin:0; color:teal;} #root ul.my-list > li{padding:10px;}"

Inject !important into CSS string:

const cssString = stringifyCSSProperties(
  {
    flex: 1,
    padding: 20,
    backgroundColor: "teal",
  },
  true
);
// Output: "flex:1!important; padding:20px!important; background-color:teal!important;"
const cssMapString = stringifyStyleMap(
  {
    p: {
      margin: 0,
      color: "teal",
    },
    "#root ul.my-list > li": {
      padding: 10,
    },
  },
  true
);
// Output: "p{margin:0!important; color:teal!important;} #root ul.my-list > li{padding:10px!important;}"

🛠️ Supported Environments

React / TypeScript (with @types/react for CSSProperties type support)
Vanilla JavaScript


💡Feel free to contribute by submitting Pull Requests and Issues.