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

[v8] Align type names with base map libraries #2474

Merged
merged 1 commit into from
Jan 26, 2025
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
18 changes: 18 additions & 0 deletions docs/upgrade-guide.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Upgrade Guide

## Upgrading to v8.0

- All imports from `react-map-gl` should be replaced with one of the following endpoints:
+ If using with `mapbox-gl@>=3.5.0`: import from `react-map-gl/mapbox`
+ If using with `mapbox-gl@<3.5.0`: import from `react-map-gl/mapbox-legacy`
- Some TypeScript types have been renamed to align with the official types from the base map libraries:

| Old name | New name |
|----------|----------|
| `MapStyle` | `StyleSpecification` |
| `Fog` | `FogSpecification` |
| `Light` | `LightSpecification` |
| `Terrain` | `TerrainSpecification` |
| `Projection` | `ProjectionSpecification` |
| `*Layer` | `*LayerSpecification` |
| `*SourceRaw` | `*SourceSpecification` |


## Upgrading to v7.1

- `maplibre-gl` users no longer need to install `mapbox-gl` or a placeholder package as dependency. Change your imports to the new endpoint `react-map-gl/maplibre`. Components imported from here do not require setting the `mapLib` prop, and use the types defined by `maplibre-gl`.
Expand Down
6 changes: 3 additions & 3 deletions examples/mapbox/filter/src/map-style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {FillLayer} from 'react-map-gl/mapbox';
import type {FillLayerSpecification} from 'react-map-gl/mapbox';

export const countiesLayer: FillLayer = {
export const countiesLayer: FillLayerSpecification = {
id: 'counties',
source: '',
type: 'fill',
Expand All @@ -11,7 +11,7 @@ export const countiesLayer: FillLayer = {
}
};
// Highlighted county polygons
export const highlightLayer: FillLayer = {
export const highlightLayer: FillLayerSpecification = {
id: 'counties-highlighted',
type: 'fill',
source: 'counties',
Expand Down
4 changes: 2 additions & 2 deletions examples/mapbox/terrain/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import Map, {Source, Layer} from 'react-map-gl/mapbox';

import ControlPanel from './control-panel';

import type {SkyLayer} from 'react-map-gl/mapbox';
import type {SkyLayerSpecification} from 'react-map-gl/mapbox';

const TOKEN = ''; // Set your mapbox token here

const skyLayer: SkyLayer = {
const skyLayer: SkyLayerSpecification = {
id: 'sky',
type: 'sky',
paint: {
Expand Down
12 changes: 8 additions & 4 deletions examples/mapbox/zoom-to-bounds/src/map-style.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type {GeoJSONSource, FillLayer, LineLayer} from 'react-map-gl/mapbox';
import type {
GeoJSONSourceSpecification,
FillLayerSpecification,
LineLayerSpecification
} from 'react-map-gl/mapbox';

import MAP_STYLE from '../../../map-style-basic-v8.json';

const sfNeighborhoods: GeoJSONSource = {
const sfNeighborhoods: GeoJSONSourceSpecification = {
type: 'geojson',
data: 'https://raw.githubusercontent.com/uber/react-map-gl/master/examples/.data/feature-example-sf.json'
};

const fillLayer: FillLayer = {
const fillLayer: FillLayerSpecification = {
id: 'sf-neighborhoods-fill',
source: 'sf-neighborhoods',
type: 'fill',
Expand All @@ -18,7 +22,7 @@ const fillLayer: FillLayer = {
}
};

const lineLayer: LineLayer = {
const lineLayer: LineLayerSpecification = {
id: 'sf-neighborhoods-outline',
source: 'sf-neighborhoods',
type: 'line',
Expand Down
6 changes: 3 additions & 3 deletions examples/maplibre/filter/src/map-style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {FillLayer} from 'react-map-gl/maplibre';
import type {FillLayerSpecification} from 'react-map-gl/maplibre';

export const countiesLayer: FillLayer = {
export const countiesLayer: FillLayerSpecification = {
id: 'counties',
source: '',
type: 'fill',
Expand All @@ -10,7 +10,7 @@ export const countiesLayer: FillLayer = {
}
};
// Highlighted county polygons
export const highlightLayer: FillLayer = {
export const highlightLayer: FillLayerSpecification = {
id: 'counties-highlighted',
type: 'fill',
source: 'counties',
Expand Down
12 changes: 8 additions & 4 deletions examples/maplibre/zoom-to-bounds/src/map-style.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type {GeoJSONSource, FillLayer, LineLayer} from 'react-map-gl/maplibre';
import type {
GeoJSONSourceSpecification,
FillLayerSpecification,
LineLayerSpecification
} from 'react-map-gl/maplibre';

import MAP_STYLE from '../../../map-style-basic-v8.json';

const sfNeighborhoods: GeoJSONSource = {
const sfNeighborhoods: GeoJSONSourceSpecification = {
type: 'geojson',
data: 'https://raw.githubusercontent.com/visgl/react-map-gl/master/examples/.data/feature-example-sf.json'
};

const fillLayer: FillLayer = {
const fillLayer: FillLayerSpecification = {
id: 'sf-neighborhoods-fill',
source: 'sf-neighborhoods',
type: 'fill',
Expand All @@ -18,7 +22,7 @@ const fillLayer: FillLayer = {
}
};

const lineLayer: LineLayer = {
const lineLayer: LineLayerSpecification = {
id: 'sf-neighborhoods-outline',
source: 'sf-neighborhoods',
type: 'line',
Expand Down
4 changes: 2 additions & 2 deletions modules/main/src/mapbox-legacy/components/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import assert from '../utils/assert';
import {deepEqual} from '../utils/deep-equal';

import type {MapInstance, CustomLayerInterface} from '../types/lib';
import type {AnyLayer} from '../types/style-spec';
import type {LayerSpecification} from '../types/style-spec';

// Omiting property from a union type, see
// https://github.com/microsoft/TypeScript/issues/39556#issuecomment-656925230
type OptionalId<T> = T extends {id: string} ? Omit<T, 'id'> & {id?: string} : T;
type OptionalSource<T> = T extends {source: string} ? Omit<T, 'source'> & {source?: string} : T;

export type LayerProps = (OptionalSource<OptionalId<AnyLayer>> | CustomLayerInterface) & {
export type LayerProps = (OptionalSource<OptionalId<LayerSpecification>> | CustomLayerInterface) & {
/** If set, the layer will be inserted before the specified layer */
beforeId?: string;
};
Expand Down
14 changes: 9 additions & 5 deletions modules/main/src/mapbox-legacy/components/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import type {
ImageSourceImplemtation,
AnySourceImplementation
} from '../types/internal';
import type {AnySource, ImageSource, VectorSource} from '../types/style-spec';
import type {
SourceSpecification,
ImageSourceSpecification,
VectorSourceSpecification
} from '../types/style-spec';
import type {MapInstance} from '../types/lib';

export type SourceProps = AnySource & {
export type SourceProps = SourceSpecification & {
id?: string;
children?: any;
};
Expand Down Expand Up @@ -61,11 +65,11 @@ function updateSource(source: AnySourceImplementation, props: SourceProps, prevP
coordinates: props.coordinates
});
} else if ('setCoordinates' in source && changedKeyCount === 1 && changedKey === 'coordinates') {
source.setCoordinates((props as unknown as ImageSource).coordinates);
source.setCoordinates((props as unknown as ImageSourceSpecification).coordinates);
} else if ('setUrl' in source && changedKey === 'url') {
source.setUrl((props as VectorSource).url);
source.setUrl((props as VectorSourceSpecification).url);
} else if ('setTiles' in source && changedKey === 'tiles') {
source.setTiles((props as VectorSource).tiles);
source.setTiles((props as VectorSourceSpecification).tiles);
} else {
// eslint-disable-next-line
console.warn(`Unable to update <Source> prop: ${changedKey}`);
Expand Down
20 changes: 13 additions & 7 deletions modules/main/src/mapbox-legacy/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
LngLatBoundsLike,
MapGeoJSONFeature
} from '../types/common';
import type {MapStyle, Light, Terrain, Fog, Projection} from '../types/style-spec';
import type {
StyleSpecification,
LightSpecification,
TerrainSpecification,
FogSpecification,
ProjectionSpecification
} from '../types/style-spec';
import type {MapInstance} from '../types/lib';
import type {Transform, MapInstanceInternal} from '../types/internal';
import type {
Expand Down Expand Up @@ -57,31 +63,31 @@
// Styling

/** Mapbox style */
mapStyle?: string | MapStyle | ImmutableLike<MapStyle>;
mapStyle?: string | StyleSpecification | ImmutableLike<StyleSpecification>;
/** Enable diffing when the map style changes
* @default true
*/
styleDiffing?: boolean;
/** The projection property of the style. Must conform to the Projection Style Specification.
* @default 'mercator'
*/
projection?: Projection;
projection?: ProjectionSpecification | ProjectionSpecification['name'];
/** The fog property of the style. Must conform to the Fog Style Specification .
* If `undefined` is provided, removes the fog from the map. */
fog?: Fog;
fog?: FogSpecification;
/** Light properties of the map. */
light?: Light;
light?: LightSpecification;
/** Terrain property of the style. Must conform to the Terrain Style Specification .
* If `undefined` is provided, removes terrain from the map. */
terrain?: Terrain;
terrain?: TerrainSpecification;

/** Default layers to query on pointer events */
interactiveLayerIds?: string[];
/** CSS cursor */
cursor?: string;
};

const DEFAULT_STYLE = {version: 8, sources: {}, layers: []} as MapStyle;
const DEFAULT_STYLE = {version: 8, sources: {}, layers: []} as StyleSpecification;

const pointerEvents = {
mousedown: 'onMouseDown',
Expand Down Expand Up @@ -509,7 +515,7 @@
@param {object} currProps
@returns {bool} true if anything is changed
*/
_updateStyleComponents(nextProps: MapboxProps, currProps: MapboxProps): boolean {

Check warning on line 518 in modules/main/src/mapbox-legacy/mapbox/mapbox.ts

View workflow job for this annotation

GitHub Actions / test-node

Method '_updateStyleComponents' has a complexity of 13. Maximum allowed is 11
const map = this._map;
let changed = false;
if (map.isStyleLoaded()) {
Expand Down
99 changes: 28 additions & 71 deletions modules/main/src/mapbox-legacy/types/style-spec.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,34 @@
/*
* Mapbox Style Specification types
*/
// Layers
import type {
BackgroundLayer,
SkyLayer,
CircleLayer,
FillLayer,
FillExtrusionLayer,
HeatmapLayer,
HillshadeLayer,
LineLayer,
RasterLayer,
SymbolLayer
} from 'mapbox-gl';

export type AnyLayer =
| BackgroundLayer
| CircleLayer
| FillExtrusionLayer
| FillLayer
| HeatmapLayer
| HillshadeLayer
| LineLayer
| RasterLayer
| SymbolLayer
| SkyLayer;

export type {
BackgroundLayer,
SkyLayer,
CircleLayer,
FillLayer,
FillExtrusionLayer,
HeatmapLayer,
HillshadeLayer,
LineLayer,
RasterLayer,
SymbolLayer
};

// Sources
import type {
Projection as ProjectionSpecification,
GeoJSONSourceRaw as GeoJSONSource,
VideoSourceRaw as VideoSource,
ImageSourceRaw as ImageSource,
VectorSource,
RasterSource,
CanvasSourceRaw as CanvasSource,
RasterDemSource
} from 'mapbox-gl';
// Layers
AnyLayer as LayerSpecification,
BackgroundLayer as BackgroundLayerSpecification,
SkyLayer as SkyLayerSpecification,
CircleLayer as CircleLayerSpecification,
FillLayer as FillLayerSpecification,
FillExtrusionLayer as FillExtrusionLayerSpecification,
HeatmapLayer as HeatmapLayerSpecification,
HillshadeLayer as HillshadeLayerSpecification,
LineLayer as LineLayerSpecification,
RasterLayer as RasterLayerSpecification,
SymbolLayer as SymbolLayerSpecification,

export type AnySource =
| GeoJSONSource
| VideoSource
| ImageSource
| CanvasSource
| VectorSource
| RasterSource
| RasterDemSource;
// Sources
AnySourceData as SourceSpecification,
CanvasSourceRaw as CanvasSourceSpecification,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand why we're aliasing some of these specifications?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will match type names from @types/mapbox-gl (unofficial types) with the latest mapbox-gl official type names.

GeoJSONSourceRaw as GeoJSONSourceSpecification,
VideoSourceRaw as VideoSourceSpecification,
ImageSourceRaw as ImageSourceSpecification,
VectorSource as VectorSourceSpecification,
RasterSource as RasterSourceSpecification,
RasterDemSource as RasterDemSourceSpecification,

export type {
GeoJSONSource,
VideoSource,
ImageSource,
CanvasSource,
VectorSource,
RasterSource,
RasterDemSource
};

// Other
export type {Style as MapStyle, Light, Fog, TerrainSpecification as Terrain} from 'mapbox-gl';

export type Projection = ProjectionSpecification | ProjectionSpecification['name'];
// Style
Style as StyleSpecification,
Light as LightSpecification,
Fog as FogSpecification,
TerrainSpecification,
Projection as ProjectionSpecification
} from 'mapbox-gl';
6 changes: 3 additions & 3 deletions modules/main/src/mapbox-legacy/utils/style-utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {ImmutableLike} from '../types/common';
import {MapStyle} from '../types/style-spec';
import {StyleSpecification} from '../types/style-spec';

const refProps = ['type', 'source', 'source-layer', 'minzoom', 'maxzoom', 'filter', 'layout'];

// Prepare a map style object for diffing
// If immutable - convert to plain object
// Work around some issues in older styles that would fail Mapbox's diffing
export function normalizeStyle(
style: string | MapStyle | ImmutableLike<MapStyle>
): string | MapStyle {
style: string | StyleSpecification | ImmutableLike<StyleSpecification>
): string | StyleSpecification {
if (!style) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/react-mapbox/src/components/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import assert from '../utils/assert';
import {deepEqual} from '../utils/deep-equal';

import type {MapInstance, CustomLayerInterface} from '../types/lib';
import type {AnyLayer} from '../types/style-spec';
import type {LayerSpecification} from '../types/style-spec';

// Omiting property from a union type, see
// https://github.com/microsoft/TypeScript/issues/39556#issuecomment-656925230
type OptionalId<T> = T extends {id: string} ? Omit<T, 'id'> & {id?: string} : T;
type OptionalSource<T> = T extends {source: string} ? Omit<T, 'source'> & {source?: string} : T;

export type LayerProps = (OptionalSource<OptionalId<AnyLayer>> | CustomLayerInterface) & {
export type LayerProps = (OptionalSource<OptionalId<LayerSpecification>> | CustomLayerInterface) & {
/** If set, the layer will be inserted before the specified layer */
beforeId?: string;
};
Expand Down
Loading
Loading