# css-vars-from-json

> Generates css custom properties (variables) from json or javascript object.

Latest version **1.1.0** (published 2021-09-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install css-vars-from-json
pnpm add css-vars-from-json
yarn add css-vars-from-json
bun add css-vars-from-json
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2021-09-23 |
| First published | 2021-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Timo Bechtel |
| Maintainers | timobechtel |
| Keywords | css, css properties, json theme, properties, generate, json, styles |

## Links

- npm: https://www.npmjs.com/package/css-vars-from-json
- Repository: https://github.com/TimoBechtel/css-vars-from-json
- Homepage: https://github.com/TimoBechtel/css-vars-from-json#readme
- Issues: https://github.com/TimoBechtel/css-vars-from-json/issues
- npm.io page: https://npm.io/package/css-vars-from-json

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2021-09-23
- 1.0.0 — 2021-05-15

## README

<h1 align="center">css-vars-from-json</h1>
<h3 align="center">Generates css custom properties (variables) from json or javascript object.</h3>
<p align="center">
  <a href="https://github.com/TimoBechtel/css-vars-from-json/blob/main/LICENSE" target="_blank">
    <img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
  </a>
</p>
<p align="center">
  ·
  <a href="https://github.com/TimoBechtel/css-vars-from-json">Homepage</a>
  ·
  <a href="https://github.com/TimoBechtel/css-vars-from-json/issues">Report Bug / Request Feature</a>
  ·
</p>

## Table of Contents

- [About](#About)
- [Installation](#Install)
- [Usage](#Usage)
- [Test](#run-tests)
- [Contact](#contact)
- [Contributing](#Contributing)
- [License](#license)

## About

This small utility generates a style string with css custom properties (variables) from an javascript object or json string.

It is useful, if you have a styled theme defined as json object and want to use it in your css.

For example,

```json
{
	"font": {
		"family": {
			"primary": "Inter"
		}
	}
}
```

can be used as,

```css
p {
	font-family: var(--font-family-primary);
}
```

## Install

### NPM:

```sh
yarn add css-vars-from-json
```

## Usage

```js
import cssVars from 'css-vars-from-json';

const theme = cssVars({
	colors: {
		primary: 'red',
		secondary: {
			__default: 'blue',
			// base value, will be rendered as --colors-secondary: blue
			light: 'lightblue',
		},
	},
	shadows: {
		main: 'box-shadow: 1px 1px black',
	},
	font: {
		family: {
			primary: 'Arial',
		},
	},
});

document.body.setAttribute('style', theme);

console.log(theme);
// => "--colors-primary: red;--colors-secondary: blue;--colors-secondary-light: lightblue;--shadows-main: box-shadow: 1px 1px black;--font-family-primary: Arial;"
```

### Svelte example

ThemeProvider.svelte

```html
<script>
	import { theme } from './my-design-system';
	import generateCSSVariables from 'css-vars-from-json';

	const style = generateCSSVariables(theme);
</script>

<div {style}>
	<slot />
</div>
```

App.svelte

```html
<script>
	import ThemeProvider from './ThemeProvider.svelte';
</script>

<ThemeProvider>
	<h1>Hi</h1>
</ThemeProvider>

<style>
	h1 {
		color: var(--color-primary);
	}
</style>
```

## Run tests

```sh
yarn run test
```

## 🤝 Contributing

Contributions, issues and feature requests are welcome!<br />

1. Check [issues](https://github.com/TimoBechtel/css-vars-from-json/issues)
1. Fork the Project
1. Create your Feature Branch (`git checkout -b feat/AmazingFeature`)
1. Test your changes `yarn run test`
1. Commit your Changes (`git commit -m 'feat: add amazingFeature'`)
1. Push to the Branch (`git push origin feat/AmazingFeature`)
1. Open a Pull Request

### Commit messages

This project uses semantic-release for automated release versions. So commits in this project follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0-beta.2/) guidelines. I recommend using [commitizen](https://github.com/commitizen/cz-cli) for automated commit messages.

## Show your support

Give a ⭐️ if this project helped you!

## 📝 License

Distributed under the [MIT](https://github.com/TimoBechtel/css-vars-from-json/blob/main/LICENSE) License.

---

_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_

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