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

🔨 drop manager pattern for vertical color legend / TAS-799 #4368

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sophiamersmann
Copy link
Member

@sophiamersmann sophiamersmann commented Jan 2, 2025

Drops the 'manager pattern' for the vertical colour legend component.

Current state

  • VerticalColorLegend is a React class component that receives a manager as a prop
    • It is rendered as <VerticalColorLegend manager={this} />
    • The legend is typically also available as a computed property since we often need to know the legend's dimensions (new VerticalColorLegend({ manager: this }))

Changes

This PR splits the vertical colour legend into two pieces, separating state from rendering:

  • VerticalColorLegend is a class with MobX properties that calculates the position of each placed bin and derives its width and height (this instance can be used to get the dimension of a legend)
  • VerticalColorLegendComponent is a function component that accepts a VerticalColorLegend instance (and other props) and renders it at a given position

Drawback

Using the manager pattern allows MobX to be smart about dependencies. For example, suppose the width of a legend depends on a subset of props consumed by the legend. MobX only recomputes the width if any of the props change that are actually used to calculate the width. Passing props explicitly means that all props are considered dependencies for all computed MobX state of the legend. As a result, (i) the legend is recomputed and rerendered more often, and (ii) MobX complains about cyclic dependencies more often (and some of the complaints are false positives).

Rendering more often is probably okay since legend components are typically lightweight. The second effect (cyclic dependencies) is mitigated by splitting the component into state and rendering. We don't see any additional cyclic dependencies in this PR, but we get one in the next where the legends of the map chart component are refactored.

I didn't anticipate this when I started working on this and now I'm honestly a bit on the fence about it. I wouldn't mind if we decided to close this PR without merging.

Copy link
Member Author

sophiamersmann commented Jan 2, 2025

@owidbot
Copy link
Contributor

owidbot commented Jan 2, 2025

Quick links (staging server):

Site Dev Site Preview Admin Wizard Docs

Login: ssh owid@staging-site-refactor-vertical-color-lege

SVG tester:

Number of differences (default views): 0 ✅
Number of differences (all views): 0 ✅

Edited: 2025-01-02 14:24:28 UTC
Execution time: 1.27 seconds

@sophiamersmann sophiamersmann marked this pull request as ready for review January 3, 2025 13:03
@sophiamersmann sophiamersmann force-pushed the refactor-vertical-color-legend branch from ae13f91 to eb5c12d Compare January 3, 2025 14:12
@sophiamersmann sophiamersmann force-pushed the refactor-vertical-color-legend branch from eb5c12d to f061537 Compare January 3, 2025 15:18
@sophiamersmann sophiamersmann changed the title 🔨 drop manager pattern for vertical color legend 🔨 drop manager pattern for vertical color legend / TAS-799 Jan 10, 2025
Copy link

Copy link
Member

@marcelgerber marcelgerber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense!
As you say - this one here is fine since it doesn't introduce any circular dependency warnings. My judgement may be different for the next PR down the line.

Also, of course, feel free to use your own judgement; if you don't like the way this ended up, then feel free to close it also, or reach out to me to talk through it :)

@sophiamersmann
Copy link
Member Author

Thanks for the review!

I want to wait for Daniel's changes to be merged and see how this integrates. We ended up using a similar approach (separating state from render), and if these PRs make Grapher more cohesive after a rebase onto Grapher's new structure, then that gives me more confidence that merging is the right thing to do.

For now, I'll revert this into draft to reflect that I'm waiting for other PRs to be merged.

@sophiamersmann sophiamersmann marked this pull request as draft January 21, 2025 08:01
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.

3 participants