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

Percentages are handled in confusing way #81

Open
michalchudziak opened this issue Mar 15, 2018 · 5 comments
Open

Percentages are handled in confusing way #81

michalchudziak opened this issue Mar 15, 2018 · 5 comments
Labels

Comments

@michalchudziak
Copy link

michalchudziak commented Mar 15, 2018

Percent values are supported natively since React Native 0.43. EStyleSheet passes them through to original StyleSheet except cases, when you use calculations with percents, e.g. "100% - 20". Percents are calculated relative to screen width/height on application launch.

The way library handles it right now seems to be confusing. How about splitting percentages based on parent and screen to two (or three) separate units.

My proposal would be to handle it the way it's working on the web, so:

  • X% - uses native RN implementation.
  • Xvw - uses screen's width
  • Xvh - uses screen's height
@michalchudziak michalchudziak changed the title Percentages are handled in confusing way. Percentages are handled in confusing way Mar 15, 2018
@vitalets
Copy link
Owner

Good proposal.
What do you think we should do for expressions with percentage like width: '100% - 20'?
it seems such code should throw error as in new implementation it should be replaced with width: '100vw - 20'.

@ohm924
Copy link

ohm924 commented May 7, 2018

Sorry If my english makes you all confused.
Same as @mike866. I think it is good that we can use vw and vh.
Then I'd decided to edit the code by add tryCalcViewport function in value.js

tryCalcViewport(str) {
    let vpprop = null;
    if(str.endsWith("vw")) {
      vpprop = "width";
    }
    else if(str.endsWith("vh")) {
      vpprop = "height";
    }
    else if(str.endsWith("vp")) {
      vpprop = this.prop;
    }
    if(vpprop != null) {
      let val = str.substr(0, str.length-2)+"%"
      return percent.calc(val, vpprop);
    }
    return null;
}

and insert function into action array at calcString and calcOperandValue

  calcString() {
    let actions = [
      this.tryCalcOperation,
      this.tryCalcViewport, // <<<<<<<<<<<<<<<<<<< insert here
      this.isOperation ? this.tryCalcPercent : null,
      this.tryCalcVar,
      this.tryCalcRem,
    ].filter(Boolean);
    let value = this.tryActions(actions, this.value);
    if (value !== null) {
      this.outValue = value;
    } else {
      this.proxyValue();
    }
  }

and

  calcOperandValue(str) {
    let actions = [
      this.tryCalcVar,
      this.tryCalcViewport, // <<<<<<<<<<<<<<<<<<< insert here
      this.tryCalcPercent,
      this.tryCalcRem,
      this.tryCalcFloat,
    ];
    return this.tryActions(actions, str);
  }

Now I can use vw vh and still use %.

Especially I create vp to force calculate by prop name same as % do.
Not sure is it bug that It's not calculate If use % with no any operand.

Here is my code
value.js.zip

@stale
Copy link

stale bot commented Dec 12, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Dec 12, 2018
@vitalets vitalets added pinned and removed wontfix labels Dec 12, 2018
@vitalets
Copy link
Owner

vitalets commented Mar 5, 2021

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

No branches or pull requests

4 participants