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

Open
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

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