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

[Feature] Add MapboxMap, MapboxMarker, MapboxPopup components #184

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
76 changes: 76 additions & 0 deletions packages/ui/organisms/MapboxMap/MapboxMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Base, BaseProps } from '@studiometa/js-toolkit';
import mapbox from 'mapbox-gl';
import { MapboxMarker } from './MapboxMarker.js';
import { MapboxPopup } from './MapboxPopup.js';

export interface MapboxMapProps extends BaseProps {
$children: {
MapboxMarker: MapboxMarker[];
};
$options: {
accessToken: string;
zoom: number;
center: [number, number];
};
}

export class MapboxMap <T extends BaseProps = BaseProps> extends Base<T & MapboxMapProps> {

Check failure on line 17 in packages/ui/organisms/MapboxMap/MapboxMap.ts

View workflow job for this annotation

GitHub Actions / Build

Class static side 'typeof MapboxMap' incorrectly extends base class static side 'typeof Base'.

Check failure on line 17 in packages/ui/organisms/MapboxMap/MapboxMap.ts

View workflow job for this annotation

GitHub Actions / Code-Quality

Delete `Β·`
static config = {
name: 'MapboxMap',
emits: ['map-load'],
options: {
accessToken: String,
zoom: Number,
center: { type: Array, default: () => [0, 0] },
},
components: {
MapboxMarker: (mapboxMap) => {
return new Promise((resolve) => {
if (mapboxMap.isLoaded) {
resolve(MapboxMarker);
} else {
mapboxMap.$on('map-load', () => {
resolve(MapboxMarker);
});
}
});
},
MapboxPopup: (mapboxMap) => {
return new Promise((resolve) => {
if (mapboxMap.isLoaded) {
resolve(MapboxPopup);
} else {
mapboxMap.$on('map-load', () => {
resolve(MapboxPopup);
});
}
});
},
},
};

map: mapbox.Map | null = null;

isLoaded = false;

get mapboxOptions() {
return {
container: this.$el,
center: this.$options.center,
accessToken: this.$options.accessToken,
zoom: this.$options.zoom,
};
}

async mounted() {
this.map = new mapbox.Map(this.mapboxOptions);
this.map.on('load', () => {
this.isLoaded = true;
this.$emit('map-load');
});
}

destroyed() {
this.map?.remove();
}
}
41 changes: 41 additions & 0 deletions packages/ui/organisms/MapboxMap/MapboxMarker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Base, getClosestParent } from '@studiometa/js-toolkit';
import mapbox from 'mapbox-gl';
import { MapboxMap } from './MapboxMap.js';

export class MapboxMarker extends Base {
static config = {
name: 'MapboxMarker',
options: {
lng: Number,
lat: Number,
// Marker options. (https://docs.mapbox.com/mapbox-gl-js/api/markers#marker)
markerOptions: Object,
},
};

marker: mapbox.Marker | null = null;

get map() {
return getClosestParent(this, MapboxMap)?.map;

Check failure on line 19 in packages/ui/organisms/MapboxMap/MapboxMarker.ts

View workflow job for this annotation

GitHub Actions / Build

Argument of type 'typeof MapboxMap' is not assignable to parameter of type 'BaseConstructor<Base<BaseProps>>'.

Check failure on line 19 in packages/ui/organisms/MapboxMap/MapboxMarker.ts

View workflow job for this annotation

GitHub Actions / Build

Property 'map' does not exist on type 'Base<BaseProps>'.
}

get markerOptions() {
if (this.$options.markerOptions) {
return this.$options.markerOptions;
}

return {};
}

get lngLat() {
return [this.$options.lng, this.$options.lat];
}

mounted() {
this.marker = new mapbox.Marker(this.markerOptions).setLngLat(this.lngLat).addTo(this.map);

Check failure on line 35 in packages/ui/organisms/MapboxMap/MapboxMarker.ts

View workflow job for this annotation

GitHub Actions / Build

Argument of type 'unknown[]' is not assignable to parameter of type 'LngLatLike'.
}

destroyed() {
this.marker?.remove();
}
}
45 changes: 45 additions & 0 deletions packages/ui/organisms/MapboxMap/MapboxPopup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Base, getClosestParent } from '@studiometa/js-toolkit';
import mapbox from 'mapbox-gl';
import { MapboxMap } from './MapboxMap.js';

export class MapboxPopup extends Base {
static config = {
name: 'MapboxPopup',
options: {
lng: Number,
lat: Number,
// Marker options. (https://docs.mapbox.com/mapbox-gl-js/api/markers#popup)
popupOptions: Object,
},
};

popup: mapbox.Popup | null = null;

get map() {
return getClosestParent(this, MapboxMap)?.map;

Check failure on line 19 in packages/ui/organisms/MapboxMap/MapboxPopup.ts

View workflow job for this annotation

GitHub Actions / Build

Argument of type 'typeof MapboxMap' is not assignable to parameter of type 'BaseConstructor<Base<BaseProps>>'.

Check failure on line 19 in packages/ui/organisms/MapboxMap/MapboxPopup.ts

View workflow job for this annotation

GitHub Actions / Build

Property 'map' does not exist on type 'Base<BaseProps>'.
}

get popupOptions() {
if (this.$options.markerOptions) {
return this.$options.markerOptions;
}

return {};
}

get lngLat() {
return [this.$options.lng, this.$options.lat];
}

mounted() {
this.popup = new mapbox.Popup()
.setLngLat(this.lngLat)

Check failure on line 36 in packages/ui/organisms/MapboxMap/MapboxPopup.ts

View workflow job for this annotation

GitHub Actions / Build

Argument of type 'unknown[]' is not assignable to parameter of type 'LngLatLike'.
.setHTML('<h1>Hello World!</h1>')
.setMaxWidth('300px')
.addTo(this.map);
}

destroyed() {
this.popup?.remove();
}
}
3 changes: 3 additions & 0 deletions packages/ui/organisms/MapboxMap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './MapboxMap.js';
export * from './MapboxMarker.js';
export * from './MapboxPopup.js';
5 changes: 5 additions & 0 deletions packages/ui/organisms/MapboxMap/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@studiometa/ui-mapbox-map",
"type": "module",
"version": "0.0.1"
}
1 change: 1 addition & 0 deletions packages/ui/organisms/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './Frame/index.js';
export * from './MapboxMap/index.js';
4 changes: 3 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"homepage": "https://github.com/studiometa/ui#readme",
"dependencies": {
"@studiometa/js-toolkit": "^2.10.2",
"deepmerge": "^4.2.2"
"@types/mapbox-gl": "^3.1.0",
"deepmerge": "^4.2.2",
"mapbox-gl": "^3.2.0"
}
}
Loading