Skip to content

Commit

Permalink
Close modal on current stlye
Browse files Browse the repository at this point in the history
  • Loading branch information
el committed Feb 1, 2022
1 parent 2f1a676 commit 06135be
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
2 changes: 2 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export declare class MapboxStyleSwitcherControl implements IControl {
getDefaultPosition(): string;
onAdd(map: MapboxMap): HTMLElement;
onRemove(): void;
private closeModal;
private openModal;
private onDocumentClick;
}
export {};
22 changes: 15 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class MapboxStyleSwitcherControl implements IControl
styleElement.addEventListener("click", event =>
{
const srcElement = event.srcElement as HTMLButtonElement;
this.closeModal();
if (srcElement.classList.contains("active"))
{
return;
Expand All @@ -83,8 +84,6 @@ export class MapboxStyleSwitcherControl implements IControl
}
const style = JSON.parse(srcElement.dataset.uri!);
this.map!.setStyle(style);
this.mapStyleContainer!.style.display = "none";
this.styleButton!.style.display = "block";
const elms = this.mapStyleContainer!.getElementsByClassName("active");
while (elms[0])
{
Expand All @@ -110,8 +109,7 @@ export class MapboxStyleSwitcherControl implements IControl
{
return;
}
this.styleButton!.style.display = "none";
this.mapStyleContainer!.style.display = "block";
this.openModal();
});

document.addEventListener("click", this.onDocumentClick);
Expand All @@ -133,13 +131,29 @@ export class MapboxStyleSwitcherControl implements IControl
this.map = undefined;
}

private onDocumentClick(event: MouseEvent): void
private closeModal(): void
{
if (this.controlContainer && !this.controlContainer.contains(event.target as Element)
&& this.mapStyleContainer && this.styleButton)
if (this.mapStyleContainer && this.styleButton)
{
this.mapStyleContainer.style.display = "none";
this.styleButton.style.display = "block";
}
}

private openModal(): void
{
if (this.mapStyleContainer && this.styleButton)
{
this.mapStyleContainer.style.display = "block";
this.styleButton.style.display = "none";
}
}

private onDocumentClick(event: MouseEvent): void
{
if (this.controlContainer && !this.controlContainer.contains(event.target as Element))
{
this.closeModal();
}
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mapbox-gl-style-switcher",
"version": "1.0.10",
"version": "1.0.11",
"description": "Mapbox GL JS Style Switcher",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 06135be

Please sign in to comment.