Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS sign() and abs() support? #1449

Closed
2 of 3 tasks
u2re-dev opened this issue Aug 5, 2024 · 4 comments
Closed
2 of 3 tasks

CSS sign() and abs() support? #1449

u2re-dev opened this issue Aug 5, 2024 · 4 comments

Comments

@u2re-dev
Copy link

u2re-dev commented Aug 5, 2024

What would you want to propose?

I want to ask support for CSS sign() and abs().

Suggested solution

@support (opacity: abs(-1)) {
  // legal sign()
}

@support not (opacity: abs(-1)) {
  // workaround math
  // such as max(value, calc(0 - value))
}

@support (opacity: sign(0.5)) {
  // legal sign()
}

@support not (opacity: sign(0.5)) {
  // workaround math
  // such as clamp(value / max(abs(value), 0.0001), -1, 1)
}

Additional context

About spec and chrome support:

Validations

  • Follow our Code of Conduct
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Would you like to open a PR for this feature?

  • I'm willing to open a PR
@u2re-dev u2re-dev added the feature request New feature or request label Aug 5, 2024
@romainmenke
Copy link
Member

romainmenke commented Aug 5, 2024

Hi @unit-404,

We already support abs() and sign() in css-calc, the underlying package behind all math related fallbacks.

So it would be relatively straightforward to create a plugin for this.
You can see this package in action here: https://github.com/csstools/postcss-plugins/blob/main/plugins/postcss-trigonometric-functions/src/index.ts

If you have time, please consider contributing : https://github.com/csstools/postcss-plugins/blob/main/CONTRIBUTING.md#creating-a-new-plugin-here

The name for this plugin should be sign-functions to mirror: https://cssdb.org/#sign-functions

@romainmenke
Copy link
Member

This has shipped in postcss-preset-env v10.1: https://github.com/csstools/postcss-plugins/blob/main/plugin-packs/postcss-preset-env/CHANGELOG.md#1010

Thank you again for requesting this

@u2re-dev
Copy link
Author

u2re-dev commented Nov 11, 2024

Solutions for fallback with custom properties (I didn't reinterpret to CSS)...
In general, you can leave the pre-calculation solution with math if the values allow it.

/* solution for dynamic custom properties */
/* I didn't include `@supports` feature here. */

// added
$cps: 0.00001;

// any type (abs only)
@function abs($a) {
    @return string.unquote("max(#{$a}, calc(-1 * #{$a}))");
}

// if number type
@function sign($a) {
    @return string.unquote("round(to-zero, clamp(-1, calc(#{$a} / max(#{abs($a)}, #{$cps})), 1), 1)");
}

// if length such as px
@function sign-unit($a, $unit: "px") {
    @return string.unquote("round(to-zero, clamp(-1#{$unit}, calc(#{$a} / max(tan(tan2(#{abs($a)}, 1#{$unit})), #{$cps})), 1#{$unit}), 1#{$unit})");
}

@romainmenke
Copy link
Member

romainmenke commented Nov 11, 2024

As before, PR's are welcome

If you have time, please consider contributing : https://github.com/csstools/postcss-plugins/blob/main/CONTRIBUTING.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants