Skip to content

Warn when using overlapping styles (e.g. border and borderBottom) #6348

Closed

Description

@gaearon

This "good first bug" is taken by @YongPilMoon. Don't work on it unless that's you!

This has been discussed a few times before but I don’t think there was any conclusion, and PRs intending to solve the issue were not merged for various reasons.

I would like to close those PRs as outdated, and reboot the discussion about this.
Performance considered, the conclusion from @sebmarkbage and @syranide seems to be:

We should consider not allowing conflicting style rules at all.

— #2013 (comment)

IMHO, all things considered it's better to just disallow overlapping and warn in dev.

— #4661 (comment)

Radium by @ianobermiller came to the same conclusion in FormidableLabs/radium#95 but there was some backlash afterwards. React Native seems to allow style expansions but only for a few attributes (e.g. margin and padding, but not border).

I’m closing old pull requests about this, and creating this issue to track implementation of the behavior we seem to agree upon: we should warn in __DEV__ when border and borderBottom are used at the same time. We can discuss more specifics (should either be ignored? should we allow a few whitelisted properties but warn for others?) in this issue.

As a migration strategy, we can suggest people to use something like https://github.com/ActionIQ/style-builder if they really need those shortcuts. It’s also something we’ll need to decide upon when implementing integrated styling.


Related issues:

Related PRs:

Activity

  1. mattborn commented on Mar 28, 2016

    @mattborn

    @gaearon Thank you for resurfacing this! Crazy that it has been a year since I opened the Radium issue.

    I like the idea of warning about shorthand properties like border, but not borderBottom. There shouldn’t be any surprises when merging style objects using the longer property names.

    Question. Is the intent of this issue to address only the analysis of individual style objects or is there also a desire to analyze and warn about possible collisions of properties that are conditionally applied?

  2. gaearon commented on Mar 28, 2016

    @gaearon
    ContributorAuthor

    Question. Is the intent of this issue to address only the analysis of individual style objects or is there also a desire to analyze and warn about possible collisions of properties that are conditionally applied?

    Not sure I understand the question, can you elaborate?

  3. mattborn commented on Mar 29, 2016

    @mattborn

    Sure. If it is invalid to have both border and borderColor on the same element, then it makes sense to warn about this. In my experience, this combination is easy to avoid, but it is more difficult to anticipate and debug when styles are being applied dynamically (conditional logic based on props or state, media queries, Radium, etc.) or passed down as props and merged with lower order style objects. If a warning for the shorthand border attribute occurs when there are no other attributes that might affect the border involved, then the warning seems more like a dogmatic rule than a helpful debugging tool since shorthand border is still technically valid. However, if there is an attribute in a separate style object—like changing borderColor on hover for example—that would affect the border involved and will get applied at runtime to the node that already has shorthand border on it, I’d like to be warned as early as possible as this is where things tend to break for me.

  4. gaearon commented on Mar 29, 2016

    @gaearon
    ContributorAuthor

    Ah, good point. I’d probably warn only if we know the styles overlap but I’m not sure how easy it is to tell. I haven’t thought about it much and I don’t have a good solution.

  5. jimfb commented on Mar 29, 2016

    @jimfb
    Contributor

    @gaearon An alternate solution that I like better is to use csstext as per #5397, which IIRC cleanly solves a superset of these issues.

  6. aweary commented on Mar 29, 2016

    @aweary
    Contributor

    What about situations where a third-party component defines styles and allows overrides? Say a component defines its own border style and I want to just override the borderBottom style?

  7. rtsao commented on Sep 13, 2016

    @rtsao

    I've started working on dead-simple helper functions to convert shorthand properties to longform (among other things): https://github.com/rtsao/lostyle/tree/master/src (docs)

  8. sophiebits commented on May 24, 2018

    @sophiebits
    Contributor

    This is super unpredictable and confusing and I just ran into this.

    I'd suggest we at least warn on updates when we know it's problematic? That is, if we go from {background: a, backgroundSize: b} to {background: c, backgroundSize: b}, warn that backgroundSize is being overridden (but if backgroundSize changes at the same time, don't warn). We can do this for all property shorthands and know whether it's problematic from the property enumeration order.

  9. sophiebits commented on May 24, 2018

    @sophiebits
    Contributor

    Marking as "good first issue". Let's add a DEV-only warning for this per my comment above. I think a good place to begin investigating would be here:

    https://github.com/facebook/react/blob/v16.4.0/packages/react-dom/src/client/ReactDOMFiberComponent.js#L619

    If you'd like to take this on, please comment here so that we don't have two people working on this.

  10. supertinou commented on May 24, 2018

    @supertinou

    @sophiebits, I am happy to work on this one :)

  11. 11 remaining items

  12. sophiebits commented on Oct 9, 2018

    @sophiebits
    Contributor

    All right @YongPilMoon, it's yours.

  13. sophiebits commented on Oct 9, 2018

    @sophiebits
    Contributor

    Adding a note here: removing a property can also trigger this bug. https://stackoverflow.com/q/52636618/49485

  14. added 2 commits that reference this issue on Nov 9, 2018
    84bcf8c
    399ac96
  15. added a commit that references this issue on Nov 9, 2018
    8ae867e
  16. added a commit that references this issue on Jan 23, 2019
    e07d8b1
  17. added a commit that references this issue on Jan 31, 2019
    57c87af
  18. gaearon commented on Feb 26, 2020

    @gaearon
    ContributorAuthor
  19. added a commit that references this issue on Jun 25, 2026
    4ccb413
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions