Skip to content

Commit 22f17a0

Browse files
Merge pull request #9 from davidruvolo51/dev
new function toggle_elem 🎉
2 parents 8686015 + de7e71d commit 22f17a0

File tree

10 files changed

+87
-36
lines changed

10 files changed

+87
-36
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: browsertools
22
Type: Package
33
Title: a collection of js handlers and console tools
4-
Version: 0.1.5
4+
Version: 0.1.6
55
Author: dcruvolo
66
Maintainer: dcruvolo <yourself@somewhere.net>
77
Description: This package provides a series of js handlers for

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export("scroll_to")
2222
export("set_element_attribute")
2323
export("show_elem")
2424
export("toggle_css")
25+
export("toggle_elem")
2526
importFrom(htmltools, htmlDependency)
2627
importFrom(shiny, getDefaultReactiveDomain, removeResourcePath, addResourcePath, observeEvent, runApp)
2728
importFrom(jsonlite, fromJSON)

R/handlers.R

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,8 @@ show_elem <- function(elem, css = "browsertools-hidden") {
424424
#' Toggles a css state of an html element
425425
#' @return Toggles a css state of an html element
426426
#' @param elem the id or class name of an html element
427-
#' @param css a string containing the class to remove from
428-
#' an html element
429-
#' @keywords browsertools, attribute, value
427+
#' @param css a string containing the class to remove from an html element
428+
#' @keywords browsertools toggle css
430429
#' @examples
431430
#' toggle_css(elem = "#mydiv", css = "mytheme")
432431
#' @references \url{https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle}
@@ -441,4 +440,28 @@ toggle_css <- function(elem, css) {
441440
# send
442441
session <- getDefaultReactiveDomain()
443442
session$sendCustomMessage("toggle_css", list(elem = elem, css = css))
443+
}
444+
445+
446+
#' \code{toggle_elem}
447+
#'
448+
#' Toggle the visibility of an html element
449+
#' @return Toggle the visibility of an html element
450+
#' @param elem the id or class name of an html element
451+
#' @param css a string containing the class to remove from an html element
452+
#' (default: browsertools-hidden; package default)
453+
#' @keywords browsertools toggle element
454+
#' @examples
455+
#' toggle_elem(elem = "#mydiv")
456+
#' @importFrom shiny getDefaultReactiveDomain
457+
#' @export
458+
toggle_elem <- function(elem, css = "browsertools-hidden") {
459+
460+
# validate
461+
if (is.null(elem)) stop("argument 'elem' is undefined")
462+
if (is.null(css)) stop("argument 'css' is undefined")
463+
464+
# send
465+
session <- getDefaultReactiveDomain()
466+
session$sendCustomMessage("toggle_elem", list(elem = elem, css = css))
444467
}

R/use_browsertools.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use_browsertools <- function() {
1010
htmlDependency(
1111
name = "browsertools",
12-
version = "0.1.5",
12+
version = "0.1.6",
1313
src = "browsertools/",
1414
package = "browsertools",
1515
script = "js/browsertools.min.js",

README.md

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ This package offers the following functions. With the exception of the `use_brow
5959
| `scroll_to` | `x`, `y` | scroll to a position in a page (default: `0, 0` = top of document)
6060
| `show_elem` | `elem`, `css` | show an element by removing a class name or `hidden`
6161
| `toggle_css` | `elem`, `css` | toggle a css class
62+
| `toggle_elem` | `elem`, `css` | toggle the visibility of an element
6263

6364
## Development
6465

@@ -93,27 +94,4 @@ When all changes are made and the css/js files are built, you can build the pack
9394
yarn uninstall # uninstalls pkg
9495
yarn document # builds pkg documentation
9596
yarn package # builds and installs pkg
96-
```
97-
98-
99-
### Dependencies
100-
101-
This package uses the following R dependencies.
102-
103-
- htmltools (>= 0.4.0): htmlDependency
104-
- shiny (>= 1.4.0): getDefaultReactiveDomain, removeResourcePath, addResourcePath, observeEvent
105-
- jsonlite (>= 1.6.0): fromJSON
106-
- rlang (>= 0.4.6): list2
107-
- purrr (>= 0.3.4): map
108-
109-
And the following dev dependencies
110-
111-
- @babel/cli: ^7.8.3,
112-
- @babel/core: ^7.9.6,
113-
- @babel/preset-env: ^7.9.6,
114-
- autoprefixer: ^9.7.6,
115-
- babel-preset-minify: ^0.5.1,
116-
- cssnano: ^4.1.10,
117-
- parcel: 1.12.4,
118-
- postcss-modules: ^2.0.0,
119-
- sass: ^1.26.5
97+
```

inst/browsertools/js/browsertools.min.js

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

inst/browsertools/js/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,28 @@ function toggle_css(elem, css) {
383383
// register
384384
Shiny.addCustomMessageHandler("toggle_css", function (value) {
385385
toggle_css(value.elem, value.css);
386+
});
387+
388+
////////////////////////////////////////
389+
390+
// TOGGLE ELEMENT
391+
// @param elem: an element to select (e.g., ID, class, tag, etc.)
392+
// @param css: a css class to add/remove (visibility class)
393+
function toggle_elem(elem, css) {
394+
try {
395+
let el = document.querySelector(elem);
396+
// if hidden, then unhide. Else, hide.
397+
if ([...el.classList].indexOf(css) > -1) {
398+
show_elem(elem, css);
399+
} else {
400+
hide_elem(elem, css);
401+
}
402+
} catch (e) {
403+
send_error("toggle_elem", e);
404+
}
405+
}
406+
407+
// register
408+
Shiny.addCustomMessageHandler("toggle_elem", function (value) {
409+
toggle_elem(value.elem, value.css);
386410
});

man/toggle_css.Rd

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/toggle_elem.Rd

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browsertools",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"author": "dcruvolo",
55
"license": "ISC",
66
"description": "a collection of tools for use in shiny",

0 commit comments

Comments
 (0)