Skip to content

updateStyle(): Make it possible to update element styles for the same style object #3023

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

Closed
wants to merge 1 commit into from

Conversation

kfule
Copy link
Contributor

@kfule kfule commented May 14, 2025

This PR addresses the case in #2915 where styles are not updated by the same style object.

Description

In this PR, if the old and new style objects are the same, clear the style and then re-set the properties. This is the same process as when updating from a null or string style to an object style.

Motivation and Context

This same style object case is taken into account in v1 and works fine (flems v1.1.7.)

if (old === style) element.style.cssText = "", old = null

In v2, however, the style updates were skipped completely for the same style object (flems v2.3.0.) It appears to have been omitted in the v2 changes (8134c51). This case has no note in the documentation and no warning like the same attrs.

The case of reusing objects may not be preferable. However, I think this case works well and is better to fix than warnings or documentation.
In the performance context, skipping style updates would also be possible with string styles or class, which would be better for performance.

How Has This Been Tested?

npm run test with additional test
I also confirmed that the bundle file works fine with the above flems.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My change requires a documentation update, and I've opened a pull request to update it already:
  • I have read https://mithril.js.org/contributing.html.

@kfule kfule requested a review from a team as a code owner May 14, 2025 11:17
@dead-claudia
Copy link
Member

It's okay to propose this for v3 in the discussion forums, but the design intent is for style objects and attribute objects to share the same diffing and patching behavior. Anything different is going to be very surprising to everyone.

@dead-claudia
Copy link
Member

If there isn't a warning for reusing style attributes, there should be, and I'd be happy to accept a PR for that.

@dead-claudia
Copy link
Member

Oh, I forgot to mention: the current behavior is to ignore same-attrs objects, and the plan is to throw in v3 if the old and new attrs objects are equal, or if the old and new style objects are equal.

@kfule
Copy link
Contributor Author

kfule commented May 15, 2025

@dead-claudia
Thanks for your comment! I understand your thoughts😀

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

Successfully merging this pull request may close these issues.

2 participants