I am trying to adapt this SO answer regarding how to auto-resize a textarea input via javascript for shiny R. Ideally, I want to avoid helper packages such as shinyJS.
First I tried a pure javascript implementation in which I load the javascript as is into the app (approach 1). Then I tried triggering the javascript function from an observeEvent within shiny (approach 2).
Both approaches are not working.
It seems like I am missing something.
Approach 1:
library(shiny)
jsCode1 <- "
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
}
init();
"
shinyApp(ui =
fluidPage(
tags$script(jsCode1),
tags$head(
tags$style("
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
}
"
)
),
shiny::tagAppendAttributes(
textAreaInput(inputId = "text",
label = "Enter text here",
placeholder = "insert your text here",
width = "100%"),
style = "width: 100%;")
),
server = function(input, output, session) {
}
)
Approach 2:
library(shiny)
jsCode2 <- "
Shiny.addCustomMessageHandler('handler1', init);
function init (el) {
var text = document.getElementById(el);
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
}"
shinyApp(ui =
fluidPage(
tags$script(jsCode2),
tags$head(
tags$style("
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
}
"
)
),
shiny::tagAppendAttributes(
textAreaInput(inputId = "text",
label = "Enter text here",
placeholder = "insert your text here",
width = "100%"),
style = "width: 100%;")
),
server = function(input, output, session) {
observeEvent(input$text,{
session$sendCustomMessage("handler1", message = "text")
})
}
)
See Question&Answers more detail:
os