Skip to content

Commit 407f2d2

Browse files
committed
Improve documentation compatibility with small screens
1 parent 124d622 commit 407f2d2

File tree

3 files changed

+120
-49
lines changed

3 files changed

+120
-49
lines changed

docs/assets/css/style.scss

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,70 @@
4646
}
4747

4848
.header-buttons .btn, .header-buttons .btn+btn {
49-
margin: 0.5em 1em;
49+
margin: 0.5em 0;
5050
width: 100%;
5151
}
5252

53+
@media only screen and (max-width: 1000px) {
54+
.header-content {
55+
max-width: 100%;
56+
width: 100%;
57+
padding: 0 6rem;
58+
margin: 0 auto;
59+
}
60+
61+
.header-options {
62+
flex-direction: column;
63+
gap: 1em;
64+
}
65+
66+
.header-buttons {
67+
border-left: none;
68+
padding: 0;
69+
}
70+
}
71+
72+
@media only screen and (max-width: 780px) {
73+
.header-content {
74+
max-width: 100%;
75+
width: 100%;
76+
padding: 0 4rem;
77+
}
78+
}
79+
80+
@media only screen and (max-width: 672px) {
81+
.header-content {
82+
max-width: 100%;
83+
width: 100%;
84+
padding: 0 2rem;
85+
}
86+
}
87+
88+
@media only screen and (max-width: 520px) {
89+
.header-content {
90+
max-width: 100%;
91+
width: 100%;
92+
padding: 0;
93+
}
94+
95+
.install-instructions {
96+
display: none;
97+
}
98+
}
99+
53100
.map-preview {
54101
margin: 0 auto;
55102
border-radius: 0.3rem;
103+
height: 300px;
104+
width: 100%;
105+
max-width: 600px;
56106
}
57107

58108
#map-main-preview-area {
59109
border: 1px solid #ddd;
60110
background-color: #f3f6fa;
61-
width: 620px;
111+
width: 100%;
112+
max-width: 620px;
62113
padding: 10px;
63114
margin: 0 auto;
64115
border-radius: 0.3rem;
@@ -75,13 +126,15 @@
75126
#map-main-preview-info {
76127
display: flex;
77128
flex-direction: row;
78-
gap: 3em;
129+
flex-wrap: wrap;
130+
gap: 0.5em 3em;
79131
font-size: 0.8em;
80132
}
81133

82134
#map-main-preview-info div {
83135
display: flex;
84136
flex-direction: row;
137+
flex-wrap: nowrap;
85138
gap: 0.5em;
86139
}
87140

docs/index.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ minimum bounding rectangle.
2121
<label class="map-main-preview-fit-label" for="map-main-preview-fit-bbox">Bounding Box</label>
2222
</fieldset>
2323

24-
<div id="map-main-preview" class="map-preview" style="width: 600px; height: 300px;"></div>
24+
<div id="map-main-preview" class="map-preview"></div>
2525

2626
<div id="map-main-preview-info">
2727
<div>
@@ -118,7 +118,7 @@ This padding object is identical to the [Mapbox GL JS `paddingOptions` object](h
118118
);
119119
```
120120

121-
<div id="map-padding-preview" class="map-preview" style="width: 600px; height: 300px;"></div>
121+
<div id="map-padding-preview" class="map-preview"></div>
122122

123123
#### Bearing Preference
124124

@@ -136,7 +136,7 @@ This defaults to `0` which prefers to point the map in a northerly bearing.
136136
);
137137
```
138138

139-
<div id="map-bearing-preview" class="map-preview" style="width: 600px; height: 300px;"></div>
139+
<div id="map-bearing-preview" class="map-preview"></div>
140140

141141
### Additional Features and Documentation
142142

docs/src/main.ts

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import 'maplibre-gl/dist/maplibre-gl.css';
1010

1111
window.addEventListener("load", (event) => {
1212
setupMapMainPreview();
13-
setupPaddingMap();
14-
setupBearingMap();
13+
addMap('map-padding-preview', {padding: {top: 50, bottom: 50, left: 100, right: 100}});
14+
addMap('map-bearing-preview', {preferredBearing: 45});
1515
setupCodePreview();
1616
});
1717

@@ -22,15 +22,18 @@ function setupMapMainPreview() {
2222
top: 10,
2323
bottom: 10
2424
}
25-
const bestFit = mapFitFeatures(
26-
sampleData as GeoJSON.FeatureCollection,
27-
[600, 300],
28-
{maxZoom: 8, padding});
29-
const boundingBox = turf.bbox(sampleData as GeoJSON.FeatureCollection);
25+
const mapId = 'map-main-preview';
26+
27+
const previewMap = addMap(mapId, {padding});
3028

3129
document.getElementById('map-main-preview-fit-bestFit').addEventListener('change', function() {
3230
const radioButton = this as HTMLInputElement;
3331

32+
const bestFit = mapFitFeatures(
33+
sampleData as GeoJSON.FeatureCollection,
34+
[document.getElementById(mapId).clientWidth, document.getElementById(mapId).clientHeight],
35+
{maxZoom: 8, padding});
36+
3437
if (radioButton.checked) {
3538
previewMap.easeTo({
3639
center: bestFit.center,
@@ -43,22 +46,13 @@ function setupMapMainPreview() {
4346
document.getElementById('map-main-preview-fit-bbox').addEventListener('change', function() {
4447
const radioButton = this as HTMLInputElement;
4548

49+
const boundingBox = turf.bbox(sampleData as GeoJSON.FeatureCollection);
50+
4651
if (radioButton.checked) {
4752
previewMap.fitBounds(boundingBox as LngLatBoundsLike, {padding});
4853
}
4954
});
5055

51-
const previewMap = new Map({
52-
container: 'map-main-preview',
53-
style: 'https://demotiles.maplibre.org/style.json',
54-
center: bestFit.center,
55-
bearing: bestFit.bearing,
56-
zoom: bestFit.zoom,
57-
interactive: false,
58-
});
59-
60-
addSampleDataToMap(previewMap);
61-
6256
function updateMapInfo() {
6357
document.getElementById('map-main-preview-info-center').innerText = `${previewMap.getCenter().toArray().map((coord => coord.toFixed(4))).join(', ')}`;
6458
document.getElementById('map-main-preview-info-zoom').innerText = `${previewMap.getZoom().toFixed(2)}`;
@@ -73,21 +67,24 @@ function setupMapMainPreview() {
7367
}
7468

7569
function setupPaddingMap() {
70+
const padding ={
71+
top: 50,
72+
bottom: 50,
73+
left: 100,
74+
right: 100
75+
}
76+
const mapId = 'map-padding-preview';
77+
7678
const fit = mapFitFeatures(
7779
sampleData as GeoJSON.FeatureCollection,
78-
[600, 300],
80+
[document.getElementById(mapId).clientWidth, document.getElementById(mapId).clientHeight],
7981
{
80-
padding: {
81-
top: 50,
82-
bottom: 50,
83-
left: 100,
84-
right: 100
85-
}
82+
padding
8683
}
8784
);
8885

8986
const paddingMap = new Map({
90-
container: 'map-padding-preview',
87+
container: mapId,
9188
style: 'https://demotiles.maplibre.org/style.json',
9289
center: fit.center,
9390
bearing: fit.bearing,
@@ -96,27 +93,11 @@ function setupPaddingMap() {
9693
});
9794

9895
addSampleDataToMap(paddingMap);
96+
setupMapResize(paddingMap, 'map-padding-preview', padding);
9997
}
10098

10199
function setupBearingMap() {
102-
const fit = mapFitFeatures(
103-
sampleData as GeoJSON.FeatureCollection,
104-
[600, 300],
105-
{
106-
preferredBearing: 180
107-
}
108-
);
109-
110-
const bearingMap = new Map({
111-
container: 'map-bearing-preview',
112-
style: 'https://demotiles.maplibre.org/style.json',
113-
center: fit.center,
114-
bearing: fit.bearing,
115-
zoom: fit.zoom,
116-
interactive: false,
117-
});
118100

119-
addSampleDataToMap(bearingMap);
120101
}
121102

122103
function setupCodePreview() {
@@ -139,6 +120,28 @@ function setupCodePreview() {
139120
});
140121
}
141122

123+
function addMap(mapId, fitOptions) {
124+
const fit = mapFitFeatures(
125+
sampleData as GeoJSON.FeatureCollection,
126+
[document.getElementById(mapId).clientWidth, document.getElementById(mapId).clientHeight],
127+
fitOptions
128+
);
129+
130+
const map = new Map({
131+
container: mapId,
132+
style: 'https://demotiles.maplibre.org/style.json',
133+
center: fit.center,
134+
bearing: fit.bearing,
135+
zoom: fit.zoom,
136+
interactive: false,
137+
});
138+
139+
addSampleDataToMap(map);
140+
setupMapResize(map, mapId, fitOptions.padding || {});
141+
142+
return map;
143+
}
144+
142145
function addSampleDataToMap(map) {
143146
map.on('load', () => {
144147
map.addSource('sample-data', {
@@ -160,4 +163,19 @@ function addSampleDataToMap(map) {
160163
});
161164
}
162165

166+
function setupMapResize(map, id, padding) {
167+
window.addEventListener('resize', () => {
168+
if(window.innerWidth < 720) {
169+
// Window is small enough to begin impacting the width of the maps. Recalculate the map size to fit the new window size.
170+
const fit = mapFitFeatures(
171+
sampleData as GeoJSON.FeatureCollection,
172+
[document.getElementById(id).clientWidth, document.getElementById(id).clientHeight],
173+
{maxZoom: 8, padding});
174+
175+
map.easeTo(fit);
176+
}
177+
178+
});
179+
}
180+
163181

0 commit comments

Comments
 (0)