feat(map-control): add style prop - #1052
Open
samithahansaka wants to merge 1 commit into
Open
Conversation
Allows inline styles to be applied to the control container, so a control can break out of the default layout, for example spanning the full width of the map with `inset: 0px 0px auto 0px`. The styles are compared by value, since callers typically pass an inline object literal that would otherwise be a new reference on every render. Properties dropped between renders are removed from the element rather than left behind. Refs visgl#379
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
styleprop toMapControl, applied to the same container element that already receivesclassName.Closes #379. The
classNamehalf of that issue shipped in #967, so this is the remaining part.Motivation
The original request was to let a control break out of the default layout, for example
{inset: '0px 0px auto 0px'}to make it span the full width of the map. Without this, the container the control renders into cannot be styled at all from React, because it is created imperatively and handed to the Maps JavaScript API.Implementation
It follows the pattern already used by
InfoWindowandPopover:setValueForStylesfromsrc/libraries/set-value-for-styles.ts, with aprevStyleRefholding the previously applied stylesuseLayoutEffect, so the control is not painted unstyled for a frameclassNamemoved into the same effect, matchinginfo-window.tsxUsing the shared helper means the prop behaves exactly like React's
styleeverywhere else in the library: numbers get an implicitpxsuffix, CSS custom properties work, and falsy values unset a property. It also only touches the properties actually passed in, so inline styles the Maps API writes on the container are left alone.Tests
Five new tests covering application, updates, removal of properties dropped between renders,
pxsuffixing, custom properties, and that unrelated inline styles survive an update.npm testandnpm run buildpass.Two things worth discussing
Interaction with control layout. You raised this in the issue: controls registered at the same
positionare laid out by the API, so styles that change size or placement will affect neighbouring controls. I documented that caveat rather than trying to prevent it, since preventing it would mean deciding which properties are allowed. Happy to restrict the prop if you would rather.Two eslint-disable comments.
react-hooks/immutabilityreports the container mutation twice, once for the indirect write throughsetValueForStylesand once for the directclassNameassignment, so both needed suppressing. If there is a preferred way to structure this so the rule is satisfied without the second comment, I am glad to change it.