question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Currently this SASS

$a: 1px
$b: 2%
div
  top: $a + $b + 3em

produces an error.

But with calc in mind code above actually makes sense. It could produce

div {
  top: calc(1px + 2% + 3em);
}

Is it possible to implement this feature in SASS?

I imagine it as if calc was a literal for first-class value representing linear combination of units. So I would expect it to have properties of linear combinations, e.g.:

  • you can linearly combine them with each other and other simple values
    • multiplication by number produces another calc
    • sum of calcs and other lengths is a calc
    • but trivial calcs like calc(1px) is just plain old 1px, so if in result of some computation you get trivial calc it should be reduced to plain value.
  • product of calcs and other lengths should probably be an error? At least at the point of css emitting.
  • optionally one can be able to access components of calc, and iterate through them. Something like extract-component(calc(1px + 1%), px) => 1 and extract-units(calc(1px + 1%)) => (px, %)

Also with all above implemented one doesn’t even need special literal for calc. Simple 1px + 1% does the trick. But it still should be there for backward compatibility, I think.

some examples:

sass expression desired css output
10px + 10% calc(10px + 10%)
10px + calc(1px + 2em) calc(11px + 2em)
calc(10px + 10%) * 2 calc(20px + 20%)
calc(10px + 10%) + calc(1px + 1em) calc(11px + 10% + 1em)
calc(10px + 10% + 1em) - 1em calc(10px + 10%)
calc(10px + 10%) - 10px 10%

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
lunelsoncommented, Nov 24, 2017

FWIW I wrote a small library that does this. It implements add subtract divide and multiply functions—my solution to ‘being explicit’ about using calc—each of which can take either numeric or calc-based arguments, and which will output a new calc expression, unless the arguments are mathematically compatible, in which case it falls back to normal Sass math.

That’s basically my suggestion of how the API could work BTW, if this was integrated in to core Sass.

https://github.com/lunelson/sass-calc

0reactions
nex3commented, Dec 31, 2020

I’m merging this into #818, since this will cover that use-case and that issue is older and has more 👍s.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mortgage Loan Calculator with PMI
Use this calculator to generate an estimated amortization schedule for your current mortgage. Quickly see how much interest you could pay and your...
Read more >
First-Class Mail & Postage - USPS
TIP: Use the USPS ® Price Calculator tool to calculate your domestic postage costs. You'll need to know your mailpiece's weight, shape, and...
Read more >
First Class Package Price Calculator – collectpostmarks.com
Save yourself the headache of using the USPS online price calculator for your First Class packages. Simply enter the origin, destination, and weight...
Read more >
Request for Comments: First-Class Calc - Sass
We're looking to change that with a new proposal we call “First-Class Calc”. This proposal changes calc() (and other supported mathematical ...
Read more >
USPS Postage Rates. Online Calculator - VIPparcel
Calculate USPS postage cost. ... US Postal Service postage rates for International & Domestic shipping with online calculator. ... First-Class Package.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found