Socket
Socket
Sign inDemoInstall

@gravitylabs/css-calc-transform

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @gravitylabs/css-calc-transform

Transform CSS properties with calc() values to pixels based on window and element dimensions.


Version published
Maintainers
1
Install size
273 kB
Created

Changelog

Source

v1.0.0

  • Added: support for em unit.
  • Added: support for min, max, and clamp functions.
  • Added: better handling of calc containing invalid values or unsupported units.
  • Breaking: using parent.font.size instead of font.size when calculating fontSize with percentages.

Readme

Source

CSS calc() to pixels transform

NPM version Build Status Size contributions welcome

Tiny Javascript library to transform CSS properties with CSS calc() function values to pixels based on window and element dimensions.

Install

yarn add --save css-calc-transform

or

npm install --save css-calc-transform

Usage

Pixels

import { transform } from "css-calc-transform";

transform({
  prop: "width",
  value: "calc(10px + (100px / 3.5))"
});

↓ ↓ ↓ ↓ ↓ ↓

38.57142857142857

Percentages

import { transform } from "css-calc-transform";

const parentElementDimensions = {
  width: 480,
  height: 100
};

transform({
  prop: "width",
  value: "calc(100% - 10px)",
  parent: parentElementDimensions
});

↓ ↓ ↓ ↓ ↓ ↓

470

Viewport units

import { transform } from "css-calc-transform";

const windowDimensions = {
  width: 480,
  height: 640
};

transform({
  prop: "height",
  value: "calc(50vh + 10px)",
  win: windowDimensions
});

↓ ↓ ↓ ↓ ↓ ↓

330

rem unit

import { transform } from "css-calc-transform";

transform({
  prop: "fontSize",
  value: "calc(2rem + 1px)",
});

↓ ↓ ↓ ↓ ↓ ↓

33

em unit

When em units are used on font-size, the size is relative to the font-size of the parent.

When used on other properties, it’s relative to the font-size of the element itself.

https://www.digitalocean.com/community/tutorials/css-rem-vs-em-units

import { transform } from "css-calc-transform";

transform({
  prop: "fontSize",
  value: "calc(2em + 1px)",
  parent: {
    font: {
      size: 16
    }
  }
});

↓ ↓ ↓ ↓ ↓ ↓

33
import { transform } from "css-calc-transform";

transform({
  prop: "height",
  value: "calc(10px + 2em)",
  font: {
    size: 16
  }
});

↓ ↓ ↓ ↓ ↓ ↓

42

min(), max(), clamp()

import { transform } from "css-calc-transform";

transform({
  prop: "height",
  value: "calc(min(2px, 3px) + clamp(100px, 150px, 200px) + max(1px, 2px))",
});

↓ ↓ ↓ ↓ ↓ ↓

154

More examples

For more examples, please have a look at the tests.

Dependencies

  • None

Keywords

FAQs

Last updated on 07 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc