Skip to content

Commit 86a82f6

Browse files
authoredJan 29, 2022
types, refactor and changelog for TileComponent
1 parent d124fdb commit 86a82f6

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Once we reach 1.0 all deprecations will be removed and the project will switch t
77

88
## Uncommitted
99

10+
## 0.20.0 - 2022-01-29
11+
### Changes
12+
- Add `tileComponent` prop, which lets you swap out the default `<img />` component for a custom one.
13+
1014
## 0.19.7 - 2021-07-04
1115
### Changes
1216
- Improve `<Draggable />` for cases when controlling its location via `onDragMove`.

‎src/map/Map.tsx

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
MoveEvent,
1010
Point,
1111
Tile,
12+
TileComponent,
1213
TileValues,
1314
WAdd,
1415
WarningType,
@@ -94,28 +95,25 @@ function srcSet(
9495
return dprs.map((dpr) => url(x, y, z, dpr) + (dpr === 1 ? '' : ` ${dpr}x`)).join(', ')
9596
}
9697

97-
const ImgTile = props => {
98-
let tile = props.tile
99-
return <img
98+
const ImgTile: TileComponent = ({ tile, tileLoaded }) => (
99+
<img
100100
src={tile.url}
101101
srcSet={tile.srcSet}
102102
width={tile.width}
103103
height={tile.height}
104104
loading={'lazy'}
105-
onLoad={() => props.imageLoaded(tile.key)}
105+
onLoad={tileLoaded}
106106
alt={''}
107107
style={{
108108
position: 'absolute',
109109
left: tile.left,
110110
top: tile.top,
111111
willChange: 'transform',
112-
// TODO: check this
113-
// transform: tile.transform,
114112
transformOrigin: 'top left',
115113
opacity: 1,
116114
}}
117115
/>
118-
}
116+
)
119117

120118
export class Map extends Component<MapProps, MapReactState> {
121119
static defaultProps = {
@@ -133,7 +131,7 @@ export class Map extends Component<MapProps, MapReactState> {
133131
maxZoom: 18,
134132
limitBounds: 'center',
135133
dprs: [],
136-
tile: ImgTile
134+
tileComponent: ImgTile
137135
}
138136

139137
_containerRef?: HTMLDivElement
@@ -568,7 +566,7 @@ export class Map extends Component<MapProps, MapReactState> {
568566
return minMax
569567
}
570568

571-
imageLoaded = (key: string): void => {
569+
tileLoaded = (key: string): void => {
572570
if (this._loadTracker && key in this._loadTracker) {
573571
this._loadTracker[key] = true
574572

@@ -1226,13 +1224,13 @@ export class Map extends Component<MapProps, MapReactState> {
12261224
transform: `translate(${left}px, ${top}px)`,
12271225
}
12281226

1229-
const Tile = this.props.tile
1227+
const Tile = this.props.tileComponent
12301228

12311229
return (
12321230
<div style={boxStyle} className={boxClassname}>
12331231
<div className="pigeon-tiles" style={tilesStyle}>
12341232
{tiles.map((tile) => (
1235-
<Tile key={tile.key} tile={tile} imageLoaded={this.imageLoaded}/>
1233+
<Tile key={tile.key} tile={tile} tileLoaded={() => this.tileLoaded(tile.key)}/>
12361234
))}
12371235
</div>
12381236
</div>

‎src/types.ts

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ export interface MapProps {
6262
limitBounds?: 'center' | 'edge'
6363

6464
boxClassname?: string
65+
tileComponent?: TileComponent
66+
}
67+
68+
export type TileComponent = (props: TileComponentProps) => JSX.Element
69+
70+
export interface TileComponentProps {
71+
tile: Tile,
72+
tileLoaded: () => void
6573
}
6674

6775
export interface Tile {

0 commit comments

Comments
 (0)
Please sign in to comment.