Repository navigation
Fix unreadable MapLibre popup text in dark notebook themes #1412
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,9 +110,49 @@ def _panel_header_chevron(color: str) -> List[Dict[str, Any]]: | |
| ] | ||
|
|
||
|
|
||
| MAP_CSS_CLASS = "leafmap-maplibre" | ||
|
|
||
|
|
||
| def _map_widget_css() -> Any: | ||
| """Builds the map widget CSS with a readable popup text color. | ||
|
|
||
| MapLibre popups have a white background but no text color, so in a dark | ||
| notebook theme the popup text inherits the light theme color and is | ||
| unreadable. The upstream widget CSS is kept and a dark text color is | ||
| appended for popups. The rule is scoped to leafmap maps because anywidget | ||
| loads the CSS for the whole page. | ||
|
|
||
| Returns: | ||
| The CSS for the map widget, or the upstream CSS unchanged if it cannot | ||
| be read. | ||
| """ | ||
| # anywidget may wrap the upstream CSS file in a FileContents object, whose | ||
| # str() is the file contents. | ||
| original = getattr(MapWidget, "_css", None) | ||
| try: | ||
| if isinstance(original, os.PathLike) or ( | ||
| isinstance(original, str) and os.path.isfile(original) | ||
| ): | ||
| with open(original, encoding="utf-8") as f: | ||
| css = f.read() | ||
| else: | ||
| css = str(original) if original is not None else "" | ||
| except Exception: | ||
| css = "" | ||
| if not css.strip(): | ||
| # Never replace the upstream map CSS with only the popup rule. | ||
| return original | ||
| return ( | ||
| css + f"\n.{MAP_CSS_CLASS} .maplibregl-popup-content {{" | ||
| f" color: {SIDEBAR_PANEL_TEXT_COLOR}; }}\n" | ||
| ) | ||
|
|
||
|
|
||
| class Map(MapWidget): | ||
| """The Map class inherits from the MapWidget class of the maplibre.ipywidget module.""" | ||
|
|
||
| _css = _map_widget_css() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concern (moderate confidence): The fix also only applies if |
||
|
|
||
| def __init__( | ||
| self, | ||
| center: Tuple[float, float] = (0, 20), | ||
|
|
@@ -254,6 +294,7 @@ def __init__( | |
| ) | ||
|
|
||
| super().__init__(map_options, height=height) | ||
| self.add_class(MAP_CSS_CLASS) | ||
| if use_message_queue is None: | ||
| use_message_queue = os.environ.get( | ||
| "USE_MESSAGE_QUEUE", "" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
except Exception: css = ""swallows errors silently. Awarnings.warnor logger call would make a failed CSS read diagnosable. The popup rule also sets onlycolor, notbackground. The PR description says "on a white background", which relies on MapLibre's default. Low confidence.