Belatedly, I didn't see any answers using Mapbox, despite Mapbox has more functionality such as padding, animation, and max zoom.
const coordinates = [<coordiantes>...]
// Create a 'LngLatBounds' with the first coordinate.
const bounds = new mapboxgl.LngLatBounds(
coordinates[0],
coordinates[0]
);
// Extend the 'LngLatBounds' to include every coordinate in the bounds result.
for (const coord of coordinates) {
bounds.extend(coord);
}
// Note there are other options such as easeing animation and maxZoom
map.fitBounds(bounds, {
padding: 20
});
Demo