Skip to content

Commit

Permalink
add argument css to addMouseCoordinates() to allow modification of th…
Browse files Browse the repository at this point in the history
…e div layout. fixes #95
  • Loading branch information
Tim Appelhans committed Nov 20, 2024
1 parent 86daec1 commit 5d44218
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Imports:
geojsonsf,
htmltools (>= 0.3),
htmlwidgets,
jsonlite,
leaflet (>= 2.0.1),
methods,
raster,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### ✨ features and improvements

* `addMouseCoordinates()` gained argument `css` to modify the layout of the div. #95

#### 🐛 bug fixes

#### 💬 documentation etc
Expand Down
55 changes: 34 additions & 21 deletions R/mousecoords.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
#' @param epsg the epsg string to be shown.
#' @param proj4string the proj4string to be shown.
#' @param native.crs logical. whether to use the native crs in the coordinates box.
#' @param align CSS 'text-align' property for the coordinates div. One of
#' 'left', 'center', 'right'. See \url{https://www.w3schools.com/cssref/pr_text_text-align.php}
#' for more details.
#' @param size numeric/integer. Text size of the coordinates div in pixels.
#' @param css list of valid CSS key-value pairs. See e.g.
#' \url{https://www.w3schools.com/cssref/index.php} for possible values.
#'
#' @details
#' If style is set to "detailed", the following information will be displayed:
Expand Down Expand Up @@ -49,6 +47,18 @@
#'
#' removeMouseCoordinates(m)
#'
#' # adjust css
#' leaflet() %>%
#' addProviderTiles("OpenStreetMap") %>%
#' addMouseCoordinates(
#' css = list(
#' "font-size" = "30px",
#' "text-align" = "center",
#' "background-color" = "#ff000080",
#' "color" = "rgb(255, 255, 255)"
#' )
#' )
#'
#' @export addMouseCoordinates
#' @name addMouseCoordinates
#' @rdname addMouseCoordinates
Expand All @@ -58,9 +68,7 @@ addMouseCoordinates <- function(map,
epsg = NULL,
proj4string = NULL,
native.crs = FALSE,
align = 'left',
size = '9px',
opacity = 0.7) {
css = list()) {

if (inherits(map, "mapview")) map <- mapview2leaflet(map)
stopifnot(inherits(map, c("leaflet", "leaflet_proxy", "mapdeck")))
Expand Down Expand Up @@ -93,6 +101,24 @@ addMouseCoordinates <- function(map,
clipboardDependency()
)

css_dflt = list(
'position' = 'relative'
, 'bottomleft' = '0px'
, 'background-color' = 'rgba(255, 255, 255, 0.7)'
, 'box-shadow' = '0 0 2px #bbb'
, 'background-clip' = 'padding-box'
, 'margin' = '0'
, 'padding-left' = '5px'
, 'padding-right' = '5px'
, 'color' = '#333'
, 'font-size' = '9px'
, 'font-family' = '\"Helvetica Neue\", Arial, Helvetica, sans-serif'
, 'text-align' = 'left'
, 'z-index' = '700'
)

css = utils::modifyList(css_dflt, css)

map <- htmlwidgets::onRender(
map,
paste0(
Expand All @@ -110,20 +136,7 @@ addMouseCoordinates <- function(map,
$(el).append(newDiv);
//provide ID and style
newDiv.addClass('lnlt');
newDiv.css({
'position': 'relative',
'bottomleft': '0px',
'background-color': 'rgba(255, 255, 255, ", opacity, ")',
'box-shadow': '0 0 2px #bbb',
'background-clip': 'padding-box',
'margin': '0',
'padding-left': '5px',
'padding-right': '5px',
'color': '#333',
'font': '", size, "px/1.5 \"Helvetica Neue\", Arial, Helvetica, sans-serif',
'text-align': '", align, "',
'z-index': '700',
});
newDiv.css(", jsonlite::toJSON(css, auto_unbox = TRUE), ");
return newDiv;
}
Expand Down
23 changes: 15 additions & 8 deletions man/addMouseCoordinates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d44218

Please sign in to comment.