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

[v7] Fix popup className update in mapbox v1/maplibre #1694

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/components/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export type PopupProps = {
children?: React.ReactNode;
};

// Adapted from https://github.com/mapbox/mapbox-gl-js/blob/v1.13.0/src/ui/popup.js
function getClassList(className: string) {
return new Set(className ? className.trim().split(/\s+/) : []);
}

function Popup(props: PopupProps) {
const map = useContext(MapContext);
const container = useMemo(() => {
Expand Down Expand Up @@ -102,15 +107,24 @@ function Popup(props: PopupProps) {
popup.options.anchor = props.anchor;
popup.setMaxWidth(props.maxWidth);
}
// Adapted from https://github.com/mapbox/mapbox-gl-js/blob/main/src/ui/popup.js
// @ts-ignore
if (popup.options.className !== props.className) {
// @ts-ignore
popup.options.className = props.className;
// @ts-ignore
popup._classList = new Set(props.className ? props.className.trim().split(/\s+/) : []);
const prevClassList = getClassList(popup.options.className);
const nextClassList = getClassList(props.className);

for (const c of prevClassList) {
if (!nextClassList.has(c)) {
popup.removeClassName(c);
}
}
for (const c of nextClassList) {
if (!prevClassList.has(c)) {
popup.addClassName(c);
}
}
// @ts-ignore
popup._updateClassList();
popup.options.className = props.className;
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/src/components/popup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ test('Popup', t => {
t.not(anchor, popup.options.anchor, 'anchor is updated');
t.not(maxWidth, popup.options.maxWidth, 'maxWidth is updated');

act(() => {
map.update(
<Map ref={mapRef}>
<Popup longitude={-122} latitude={38} className="classA">
You are here
</Popup>
</Map>
);
});

t.is(popup.options.className, 'classA', 'className is updated');

restoreMock();

t.end();
Expand Down
7 changes: 7 additions & 0 deletions test/src/utils/mapbox-gl-mock/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ export default class Popup extends Evented {
this.options.offset = value;
return this;
}

addClassName(className) {
this._classList.add(className);
}
removeClassName(className) {
this._classList.remove(className);
}
}
1 change: 1 addition & 0 deletions tsconfig.es5.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"jsx": "react",
"moduleResolution": "node",
"module": "commonjs",
"downlevelIteration": true,
"declaration": true,
"sourceMap": true,
"outDir": "./dist/es5"
Expand Down