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

Is there a way to control the order of nested spans being created by styleToHTML #163

Open
maierson opened this issue Dec 10, 2019 · 3 comments

Comments

@maierson
Copy link

No description provided.

@maierson
Copy link
Author

Hello,

Thank you for the good work on this library.

I am implementing background-color AND margin-top on inline styles. This creates 2 nested spans from the following styleToHTML function:

function styleToHTML(style) {
  if(style.startsWith("backgroundcolor-") { // the background color key
     return (
        <span style={{ 
            backgroundColor: style.replace("backgroundcolor-", "") , // the color value
            display: "inline-block", // force sizing
            overflow: "hidden" // keep content from spilling out (consistent line height with editor)
         }} />
      );
  }

  if(style.startsWith("margintop-") {
     return (
        <span
          style={{
            marginTop: `${style.replace("margintop-", "")}px`,
            display: "inline-block"
          }}
        />
    );
  }
}

The order of the created spans differs depending on when the user applies the styles so it can be either:
(the margin is only added to the bottom line)

#1 (good: add margin BEFORE background):

<span style="margin-top:10px">
  <span style="background-color:red">
     text
  </span>
</span>

Screen Shot 2019-12-10 at 3 41 59 PM

#2 (bad: add margin AFTER background):

<span style="background-color:red">
  <span style="margin-top:10px">
     text
  </span>
</span>

Screen Shot 2019-12-10 at 3 42 41 PM

The problem is that in option #2 the inner span margin will increase the height of the outer span so the red background height will be increased by the size of the margin. I want to consistently obtain option #1 regardless of the order in which the user assigns the color or the margin values. In other words always render the margin span on the outside.

Is this possible?

I've tried programmatically removing the background styles before setting the margin and then adding them back in after setting the margin but I get inconsistent results.

Thank you.

@jtabone16
Copy link

@maierson Ever figure this out? Running into the same issue

@maierson
Copy link
Author

maierson commented Jul 8, 2020

@jtabone16 To be honest I can't remember. We have a pretty huge codebase and this was a while ago. I know that I'll have to revisit this though in the next month or so. If I find a solution I'll post back. Wish I could be more helpful :/

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

No branches or pull requests

2 participants