Skip to content

Commit

Permalink
feat(layer): 简单封装
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Sep 1, 2022
1 parent 687e6be commit 1cec2ef
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './map';
export * from './marker';
export * from './source';
export * from './layer';
export * from './hooks/useMap';
2 changes: 2 additions & 0 deletions src/layer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Layer } from './layer';
export type { LayerProps } from './types';
23 changes: 23 additions & 0 deletions src/layer/layer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect, useState } from 'react';

import { useMap } from '@/hooks/useMap';

import type { FC } from 'react';
import type { AnyLayer } from 'mapbox-gl';
import type { LayerProps } from './types';

export const Layer: FC<LayerProps> = (props) => {
const { before, ...rest } = props;
const map = useMap();
const [layer, setLayer] = useState<AnyLayer>();

useEffect(() => {
if (map) {
map.addLayer(rest as AnyLayer, before);

setLayer(map.getLayer(props.id));
}
}, [map]);

return null;
};
85 changes: 85 additions & 0 deletions src/layer/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import type {
Layer,
BackgroundLayout,
BackgroundPaint,
CircleLayout,
CirclePaint,
FillLayout,
FillPaint,
FillExtrusionLayout,
FillExtrusionPaint,
HeatmapLayout,
HeatmapPaint,
LineLayout,
LinePaint,
RasterLayout,
RasterPaint,
SkyLayout,
SkyPaint,
SymbolLayout,
SymbolPaint,
HillshadeLayout,
HillshadePaint,
} from 'mapbox-gl';

export type LayerType =
| 'background'
| 'circle'
| 'fill-extrusion'
| 'fill'
| 'heatmap'
| 'hillshade'
| 'line'
| 'raster'
| 'symbol'
| 'sky';

export interface LayoutPaint {
background: {
layout: BackgroundLayout;
paint: BackgroundPaint;
};
circle: {
layout: CircleLayout;
paint: CirclePaint;
};
'fill-extrusion': {
layout: FillExtrusionLayout;
paint: FillExtrusionPaint;
};
fill: {
layout: FillLayout;
paint: FillPaint;
};
heatmap: {
layout: HeatmapLayout;
paint: HeatmapPaint;
};
hillshade: {
layout: HillshadeLayout;
paint: HillshadePaint;
};
line: {
layout: LineLayout;
paint: LinePaint;
};
raster: {
layout: RasterLayout;
paint: RasterPaint;
};
symbol: {
layout: SymbolLayout;
paint: SymbolPaint;
};
sky: {
layout: SkyLayout;
paint: SkyPaint;
};
}

export interface LayerProps<T extends LayerType> extends Omit<Layer, 'layout' | 'paint'> {
type: T;
before?: string;
layout?: LayoutPaint[T];
paint?: LayoutPaint[T];
}
3 changes: 1 addition & 2 deletions src/map/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ReactElement } from 'react';
import type { MapboxOptions, MapEventType } from 'mapbox-gl';

export interface MapOptions extends MapboxOptions {}
Expand Down Expand Up @@ -72,5 +71,5 @@ export type PropKey = KeysOfUnion<MapboxOptions>;

export interface MapProps extends Omit<MapOptions, 'container'>, Partial<MapEvents> {
className?: string;
children?: ReactElement;
children?: any;
}

0 comments on commit 1cec2ef

Please sign in to comment.