# csscalc

> operate with css units in js

Latest version **0.1.2** (published 2019-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install csscalc
pnpm add csscalc
yarn add csscalc
bun add csscalc
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2019-04-03 |
| First published | 2019-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | morglod |
| Maintainers | morglod |
| Keywords | css, units, math, types, css-in-js |

## Links

- npm: https://www.npmjs.com/package/csscalc
- Repository: https://github.com/Morglod/csscalc
- Homepage: https://github.com/Morglod/csscalc#readme
- Issues: https://github.com/Morglod/csscalc/issues
- npm.io page: https://npm.io/package/csscalc

## 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

- 0.1.2 (latest) — 2019-04-03
- 0.1.1 — 2019-04-03
- 0.1.0 — 2019-04-03

## README

[![NPM Version](https://badge.fury.io/js/csscalc.svg?style=flat)](https://www.npmjs.com/package/csscalc)

# csscalc

JavaScript library to do math with css units.  
Fully typed with typescript.

Currently supported units: `px`, `cm`, `mm`, `Q`, `in`, `pc`, `pt`, `vh`, `vw`, `vmin`, `vmax`, `rem`, `%w`, `%h`.

* Convert between css units
* Calculate css expressions

Based on https://developer.mozilla.org/en-US/docs/Web/CSS/length

## Examples

Calculate what part of screen's width takes specified element:
```js
import { calc } from 'cssunits';

// %w means % of width
calc('100%w / 100vw', element);
```

Calculate area of element:
```js
calc('100%w * 100%h', element);
```

Convert pixels to percent of element's width:
```js
convert(100, 'px', '%w', calcCtx(element));
```

Convert element's width percent to pixels:
```js
convert(50, '%w', 'px', calcCtx(element));

// or

Relative["%w"](50, calcCtx(element));
```

`calc` method converts all known units and then invokes js's `eval` method, so you can use any inline js code in `calc`.  
For example min/max from Math:
```ts
calc('Math.max(100%w, 100%h)', element)
```

## API

`Absolute` map, units -> pixels.  
```ts
Absoulte = {
    "absolute unit name" -> pixels
}
```

`Relative` map, (units, calcContext) -> pixels.  
```ts
Relative = {
    "relative unit name": (units: number, ctx?: CalcContext) => number
}
```

`UnitRegexpStr`, `UnitRegexp`, `UnitRegexpGM` - regular expression to find units.

Convert between units:
```ts
function convert(count: number, fromUnits: Unit, toUnits: Unit, calcCtx?: CalcContext): number;
```

Create calc context:
```ts
function calcCtx(el?: HTMLElement): CalcContext;
```

Calculate expression:
```ts
function calc(expression: string, el: HTMLElement, ctx?: CalcContext): number;
function calc(expression: string, ctx?: CalcContext): number;
```

## Roadmap

* Reactive calculations

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