Skip to content

Commit

Permalink
Resize textarea if screen height is small or changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepav committed Jun 5, 2024
1 parent 3305eb2 commit d2f38ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions css/guided-typing.css
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ display-keyboard {
margin-bottom: 1em;
}

.input-holder {
margin: 0;
padding: 0;
}

.settings-icons-holder {
position: fixed;
right: 2px;
Expand Down
18 changes: 17 additions & 1 deletion js/guided-typing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ function checkBrowserFeatures() {
}
}

function sizeTextarea(sectionEl, textarea, keyboard) {
const ELEMENT_SPACING_HEIGHT = 25, // height of padding and borders around elements
TEXTAREA_MIN_HEIGHT = 50; // empirically, one comfortable line of text
const viewportHeight = document.documentElement.clientHeight;
const remainingHeight = viewportHeight - sectionEl.offsetHeight - keyboard.offsetHeight - ELEMENT_SPACING_HEIGHT;
const textareaHeight = Math.max(TEXTAREA_MIN_HEIGHT, Math.min(sectionEl.offsetHeight, remainingHeight));
textarea.style.height = `${textareaHeight}px`;
}

async function main() {
checkBrowserFeatures();

Expand Down Expand Up @@ -337,7 +346,6 @@ async function main() {
if (selected) {
if (!nextEl || !nextEl.classList.contains("input-holder")) {
const textarea = document.createElement("textarea");
textarea.style.height = `${sectionEl.offsetHeight}px`;
// Use the first two words of the section text as a placeholder
const sectionText = sectionEl.innerText;
let breakIdx = sectionText.indexOf(' ');
Expand All @@ -363,6 +371,7 @@ async function main() {
if (textarea.nextElementSibling != keyboard)
textarea.after(keyboard);
sectionEl.scrollIntoView(true);
sizeTextarea(sectionEl, textarea, keyboard);
processTextInput(textarea, expandedText, successCheck, keyboard);
});
textarea.addEventListener('input', ev => {
Expand Down Expand Up @@ -392,6 +401,13 @@ async function main() {
}
}
});
window.addEventListener('resize', () => {
if (keyboard.parentElement) {
const textarea = keyboard.previousElementSibling;
const sectionEl = textarea.parentElement.previousElementSibling;
sizeTextarea(sectionEl, textarea, keyboard);
}
});
}

window.addEventListener('load', main());

0 comments on commit d2f38ef

Please sign in to comment.