-
Notifications
You must be signed in to change notification settings - Fork 11
Perceived performance improvement with Mapbox Static Images API #331
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
Open
yen-tt
wants to merge
28
commits into
release/v1.1.0
Choose a base branch
from
dev/mapbox-static-image
base: release/v1.1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ab88584
Mapbox Component (#267)
alextaing 8c677c6
mapbox component jest tests (#311)
yen-tt 69327c1
add react-dom as peer dep (#313)
yen-tt 731c9bd
docs(MapboxMap): add storybook stories (#312)
tatimblin 0d78e7e
use createRoot when possible for rendering pin components (#319)
oshi97 f585a30
Revert "use createRoot when possible for rendering pin components (#3…
oshi97 2c8a500
display mapbox static image until the interactive map is loaded
a926516
function for url, change interface
7b89a4f
add story for static image before load
0bb3220
refactor from hook to component
261facb
mock response to be error instead of delay
8faf428
remove msw setup, story use MapboxStaticImage component
28727e4
wait for map to load for non static map stories
bb25f48
ts
91089ce
update percy config in story
3d5a491
test
2c70739
hide getCoordinate and onDrag control
8ad916a
for snapshot of dynamic map with play function
7ab0c0c
Merge branch 'develop' into dev/mapbox-static-image
44f0e4a
update package lock for test-site
def501e
remove play function -- doesnt deplay snapshot
3538bfb
test dev wcag gh workflow with mapbox api key input
57f505f
test
139ee22
remove logs
6b48f80
update workflow to point back to v1 branch, run WCAG in feature branc…
06c7725
Fix wcag github action mapbox issues (#337)
yen-tt 1ead573
Fix visual coverage test in Github Actions (#339)
yen-tt ffe64e5
periods
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { CSSProperties, useMemo, useRef, useState } from 'react'; | ||
import { MapboxOptions, LngLatLike } from 'mapbox-gl'; | ||
import useLayoutEffect from 'use-isomorphic-layout-effect'; | ||
|
||
/** | ||
* Props for {@link MapboxStaticImage}. | ||
*/ | ||
export interface MapboxStaticImageProps { | ||
/** Mapbox access token. */ | ||
mapboxAccessToken: string, | ||
/** Map customization for Mapbox static image. */ | ||
mapboxOptions: Pick<MapboxOptions, 'center' | 'zoom' | 'style'> | ||
} | ||
|
||
/** | ||
* Renders a static map image using Mapbox Static Image API. | ||
* | ||
* @internal | ||
*/ | ||
export function MapboxStaticImage({ | ||
mapboxAccessToken, | ||
mapboxOptions | ||
}: MapboxStaticImageProps): JSX.Element | null { | ||
const mapContainer = useRef<HTMLDivElement>(null); | ||
const [divDimension, setDivDimension] = useState<{ width: number, height: number }>(); | ||
useLayoutEffect(() => { | ||
if (mapContainer.current) { | ||
setDivDimension({ | ||
width: mapContainer.current.clientWidth, | ||
height: mapContainer.current.clientHeight | ||
}); | ||
} | ||
}, []); | ||
|
||
const staticMapboxStyle: CSSProperties | undefined = useMemo(() => { | ||
const { style, center, zoom } = mapboxOptions; | ||
if (!divDimension || !center || !zoom || typeof style !== 'string') { | ||
return undefined; | ||
} | ||
const { width, height } = divDimension; | ||
const url = getMapboxStaticImageUrl({ mapboxAccessToken, style, center, zoom, width, height }); | ||
if (!url) { | ||
return undefined; | ||
} | ||
return { | ||
backgroundImage: `url(${url})` | ||
}; | ||
}, [divDimension, mapboxAccessToken, mapboxOptions]); | ||
|
||
return <div ref={mapContainer} className='col-span-full row-span-full' style={staticMapboxStyle}/>; | ||
} | ||
|
||
/** | ||
* The configuration for Mapbox Static Image API url. | ||
*/ | ||
interface MapboxStaticImageUrlConfig { | ||
/** Mapbox access token. */ | ||
mapboxAccessToken: string, | ||
/** Mapbox stylesheet url to apply to the static map. */ | ||
style: string, | ||
/** Center point of the static map. */ | ||
center: LngLatLike, | ||
/** Zoom level of the static map. */ | ||
zoom: number, | ||
/** Width (in pixels) of the image. */ | ||
width: number, | ||
/** Height (in pixels) of the image. */ | ||
height: number | ||
} | ||
|
||
function getMapboxStaticImageUrl(urlConfig: MapboxStaticImageUrlConfig): string | undefined { | ||
const { mapboxAccessToken, style, center, zoom, width, height } = urlConfig; | ||
// Mapbox Static Image API only support width and height between 1-1280 pixels. | ||
if (width > 1280 || height > 1280) { | ||
return undefined; | ||
} | ||
const stylesheet = style.split('mapbox://styles/')[1].split('?')[0]; | ||
const centerAndZoom = `${center[0]},${center[1]},${zoom}`; | ||
const dimension = `${width}x${height}`; | ||
return `https://api.mapbox.com/styles/v1/${stylesheet}/static/${centerAndZoom}/${dimension}?access_token=${mapboxAccessToken}`; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.