Skip to content

Commit

Permalink
Merge pull request #9 from davidruvolo51/dev
Browse files Browse the repository at this point in the history
new function toggle_elem 🎉
  • Loading branch information
davidruvolo51 authored Jun 16, 2020
2 parents 8686015 + de7e71d commit 22f17a0
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 36 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: browsertools
Type: Package
Title: a collection of js handlers and console tools
Version: 0.1.5
Version: 0.1.6
Author: dcruvolo
Maintainer: dcruvolo <yourself@somewhere.net>
Description: This package provides a series of js handlers for
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export("scroll_to")
export("set_element_attribute")
export("show_elem")
export("toggle_css")
export("toggle_elem")
importFrom(htmltools, htmlDependency)
importFrom(shiny, getDefaultReactiveDomain, removeResourcePath, addResourcePath, observeEvent, runApp)
importFrom(jsonlite, fromJSON)
Expand Down
29 changes: 26 additions & 3 deletions R/handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ show_elem <- function(elem, css = "browsertools-hidden") {
#' Toggles a css state of an html element
#' @return Toggles a css state of an html element
#' @param elem the id or class name of an html element
#' @param css a string containing the class to remove from
#' an html element
#' @keywords browsertools, attribute, value
#' @param css a string containing the class to remove from an html element
#' @keywords browsertools toggle css
#' @examples
#' toggle_css(elem = "#mydiv", css = "mytheme")
#' @references \url{https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle}
Expand All @@ -441,4 +440,28 @@ toggle_css <- function(elem, css) {
# send
session <- getDefaultReactiveDomain()
session$sendCustomMessage("toggle_css", list(elem = elem, css = css))
}


#' \code{toggle_elem}
#'
#' Toggle the visibility of an html element
#' @return Toggle the visibility of an html element
#' @param elem the id or class name of an html element
#' @param css a string containing the class to remove from an html element
#' (default: browsertools-hidden; package default)
#' @keywords browsertools toggle element
#' @examples
#' toggle_elem(elem = "#mydiv")
#' @importFrom shiny getDefaultReactiveDomain
#' @export
toggle_elem <- function(elem, css = "browsertools-hidden") {

# validate
if (is.null(elem)) stop("argument 'elem' is undefined")
if (is.null(css)) stop("argument 'css' is undefined")

# send
session <- getDefaultReactiveDomain()
session$sendCustomMessage("toggle_elem", list(elem = elem, css = css))
}
2 changes: 1 addition & 1 deletion R/use_browsertools.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use_browsertools <- function() {
htmlDependency(
name = "browsertools",
version = "0.1.5",
version = "0.1.6",
src = "browsertools/",
package = "browsertools",
script = "js/browsertools.min.js",
Expand Down
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ This package offers the following functions. With the exception of the `use_brow
| `scroll_to` | `x`, `y` | scroll to a position in a page (default: `0, 0` = top of document)
| `show_elem` | `elem`, `css` | show an element by removing a class name or `hidden`
| `toggle_css` | `elem`, `css` | toggle a css class
| `toggle_elem` | `elem`, `css` | toggle the visibility of an element

## Development

Expand Down Expand Up @@ -93,27 +94,4 @@ When all changes are made and the css/js files are built, you can build the pack
yarn uninstall # uninstalls pkg
yarn document # builds pkg documentation
yarn package # builds and installs pkg
```


### Dependencies

This package uses the following R dependencies.

- htmltools (>= 0.4.0): htmlDependency
- shiny (>= 1.4.0): getDefaultReactiveDomain, removeResourcePath, addResourcePath, observeEvent
- jsonlite (>= 1.6.0): fromJSON
- rlang (>= 0.4.6): list2
- purrr (>= 0.3.4): map

And the following dev dependencies

- @babel/cli: ^7.8.3,
- @babel/core: ^7.9.6,
- @babel/preset-env: ^7.9.6,
- autoprefixer: ^9.7.6,
- babel-preset-minify: ^0.5.1,
- cssnano: ^4.1.10,
- parcel: 1.12.4,
- postcss-modules: ^2.0.0,
- sass: ^1.26.5
```
2 changes: 1 addition & 1 deletion inst/browsertools/js/browsertools.min.js

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

24 changes: 24 additions & 0 deletions inst/browsertools/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,28 @@ function toggle_css(elem, css) {
// register
Shiny.addCustomMessageHandler("toggle_css", function (value) {
toggle_css(value.elem, value.css);
});

////////////////////////////////////////

// TOGGLE ELEMENT
// @param elem: an element to select (e.g., ID, class, tag, etc.)
// @param css: a css class to add/remove (visibility class)
function toggle_elem(elem, css) {
try {
let el = document.querySelector(elem);
// if hidden, then unhide. Else, hide.
if ([...el.classList].indexOf(css) > -1) {
show_elem(elem, css);
} else {
hide_elem(elem, css);
}
} catch (e) {
send_error("toggle_elem", e);
}
}

// register
Shiny.addCustomMessageHandler("toggle_elem", function (value) {
toggle_elem(value.elem, value.css);
});
9 changes: 4 additions & 5 deletions man/toggle_css.Rd

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

26 changes: 26 additions & 0 deletions man/toggle_elem.Rd

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browsertools",
"version": "0.1.5",
"version": "0.1.6",
"author": "dcruvolo",
"license": "ISC",
"description": "a collection of tools for use in shiny",
Expand Down

0 comments on commit 22f17a0

Please sign in to comment.