Skip to content

Commit adf13b3

Browse files
committed
fix sizing of geocoder in Quarto dash
1 parent 314b723 commit adf13b3

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

R/search.R

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ get_geocoder_dependencies <- function() {
566566
#' @param limit The maximum number of results to show. Defaults to 5.
567567
#' @param min_length The minimum number of characters the user must enter before results are shown. Defaults to 2.
568568
#' @param language The language to use for the geocoder. Per the Mapbox documentation, "Options are IETF language tags comprised of a mandatory ISO 639-1 language code and optionally one or more IETF subtags for country or script."
569+
#' @param types A vector of feature types to limit to which the search should be
570+
#' limited. Available options include `'country'`, `'region'`, `'postcode'`,
571+
#' `'district'`, `'place'`, `'locality'`, `'neighborhood'`, `'address'`, `'street'`, `'block'`, `'address'`. and `'secondary_address'`. If left blank, all types will be searched.
572+
#' @param country A character string or vector of ISO 3166 alpha-2 country codes within which you would like to limit your search.
573+
#' @param worldview Returns features intended for different regional or cultural groups. The US (\code{'us'}) world view is returned by default.
574+
#' @param width The width of the input field. Can be specified as a number (pixels) or a valid CSS width value (e.g., "300px", "100%"). Defaults to 100%.
569575
#'
570576
#' @return A Mapbox geocoder widget as a Shiny input
571577
#' @export
@@ -577,8 +583,11 @@ mapboxGeocoderInput <- function(
577583
proximity = NULL,
578584
limit = 5,
579585
min_length = 2,
580-
language = NULL
581-
586+
language = NULL,
587+
types = NULL,
588+
country = NULL,
589+
worldview = NULL,
590+
width = "100%"
582591
) {
583592

584593
access_token <- get_mb_access_token(access_token)
@@ -602,15 +611,41 @@ mapboxGeocoderInput <- function(
602611
latitude = proximity[2]
603612
)
604613
}
614+
615+
if (!is.null(types)) {
616+
types <- paste0(types, collapse = ",")
617+
}
605618

606619
options <- list()
607620

608-
options <- modifyList(options, list(proximity = proximity, placeholder = placeholder, bbox = bbox, limit = limit, minLength = min_length, language = language))
621+
options <- modifyList(options, list(
622+
proximity = proximity,
623+
placeholder = placeholder,
624+
bbox = bbox,
625+
limit = limit,
626+
minLength = min_length,
627+
language = language,
628+
types = types,
629+
countries = country,
630+
worldview = worldview
631+
))
632+
633+
# Process width value
634+
width_style <- if(is.numeric(width)) {
635+
paste0(width, "px")
636+
} else {
637+
width
638+
}
609639

640+
# Add width to options as well
641+
options$customWidth <- width_style
642+
610643
div(id = inputId,
611644
class = "mapbox-geocoder",
645+
style = paste0("width: ", width_style, ";"),
612646
`data-access-token` = access_token,
613647
`data-options` = jsonlite::toJSON(options, auto_unbox = TRUE),
648+
`data-width` = width_style,
614649
get_geocoder_dependencies())
615650
}
616651

inst/www/mapboxGeocoder.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
}
66

77
.mapboxgl-ctrl-geocoder {
8-
width: 100%;
98
font-size: 15px;
109
line-height: 20px;
11-
max-width: 1000px;
10+
max-width: none !important; /* Override Mapbox's default max-width */
1211
z-index: 10010;
1312
position: relative; /* Ensure the parent has position for absolute children anchoring */
1413
}
@@ -39,6 +38,9 @@
3938
/* Additional fixes for container issues */
4039
.mapboxgl-ctrl-geocoder {
4140
position: relative; /* Ensure parent has position context */
41+
min-width: unset !important; /* Override Mapbox's default min-width of 240px */
42+
width: 100%; /* Ensure the geocoder takes the width of its container */
43+
transition: none !important; /* Disable transitions that might affect width */
4244
}
4345

4446
/* Ensure the container for suggestions has no overflow restrictions */

inst/www/mapboxGeocoderBinding.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,48 @@ $.extend(mapboxGeocoderBinding, {
66
initialize: function(el) {
77
var accessToken = $(el).data("access-token");
88
var options = $(el).data("options");
9+
var width = $(el).data("width") || "100%";
910
mapboxgl.accessToken = accessToken;
11+
12+
// Clean undefined values to avoid issues
13+
var cleanOptions = {};
14+
for (var key in options) {
15+
if (options[key] !== null && options[key] !== undefined && key !== "customWidth") {
16+
cleanOptions[key] = options[key];
17+
}
18+
}
19+
1020
var geocoder = new MapboxGeocoder({
1121
accessToken: mapboxgl.accessToken,
1222
mapboxgl: mapboxgl,
13-
...options
23+
...cleanOptions
1424
});
25+
1526
geocoder.addTo(el);
27+
28+
// Apply custom width to the geocoder container
29+
setTimeout(function() {
30+
var geocoderContainer = $(el).find('.mapboxgl-ctrl-geocoder');
31+
if (geocoderContainer.length) {
32+
geocoderContainer.css({
33+
'min-width': 'unset',
34+
'width': width
35+
});
36+
}
37+
}, 0);
38+
1639
// Capture the result event and send data to Shiny
1740
geocoder.on('result', function(e) {
1841
$(el).data("geocoder-result", e.result); // Store the result in jQuery data
1942
$(el).trigger('change');
2043
});
44+
2145
// Add event listener for the 'clear' event
2246
geocoder.on('clear', function() {
2347
$(el).data("geocoder-result", null); // Reset the result to null
2448
$(el).trigger('change');
2549
});
50+
2651
$(el).data("geocoder", geocoder);
2752
},
2853
getValue: function(el) {

man/mapboxGeocoderInput.Rd

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)