make input field expand vertically

hi, i want to make my input field expand vertically when the text overflows, i attached a quick video snippet to show you what i mean
23 Replies
Jochem
Jochem•2y ago
what you want is a textarea instead of an input
MarkBoots
MarkBoots•2y ago
you could also use a regular div with contenteditable so it can auto grow with the content. but then you''ll need some extra work if you want to handle it just like other form elements here an example from https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/textbox_role
<!-- Multi-line text area -->
<div id="yourtextbox">Label</div>
<div
role="textbox"
contenteditable="true"
aria-multiline="true"
aria-labelledby="yourtextbox"
aria-required="true">
</div>
<!-- Multi-line text area -->
<div id="yourtextbox">Label</div>
<div
role="textbox"
contenteditable="true"
aria-multiline="true"
aria-labelledby="yourtextbox"
aria-required="true">
</div>
with this you can style the element just to way you like it
/bin/son-arg
/bin/son-argOP•2y ago
well, i want something more like whatsapp for example:
/bin/son-arg
/bin/son-argOP•2y ago
than this:
/bin/son-arg
/bin/son-argOP•2y ago
oh okay, i see, i'll check this out, thanks can i have a placeholder in this contenteditable div?
/bin/son-arg
/bin/son-argOP•2y ago
like this
/bin/son-arg
/bin/son-argOP•2y ago
then it dissapears when a user types
MarkBoots
MarkBoots•2y ago
no really, but you could use a pseudo element which hides when element is :not(:empty)
/bin/son-arg
/bin/son-argOP•2y ago
ah okay, i'll try that discord has something similar
MarkBoots
MarkBoots•2y ago
something like this. I only did not find a way yet to get placeholder back when you delete all text. because there will be some elements left in the html that make it not empty so probaly some more js needed https://codepen.io/MarkBoots/pen/KKBpeVL
/bin/son-arg
/bin/son-argOP•2y ago
ohh, okay, thanks a lot for that! 🙂 by the way, just a quick question, i noticed, on mine and yours, whenever i paste in anything, that literally gets pasted
/bin/son-arg
/bin/son-argOP•2y ago
for example, code from vscode gets pasted literally as code:
/bin/son-arg
/bin/son-argOP•2y ago
how can i kind of "sanitize" this and have it just paste as text? if u try to copy code from vscode and paste into discord, it doesn't have the same behavior; it pastes as plain text as expected
MarkBoots
MarkBoots•2y ago
yea, probably because it is just content of a div, it will paste styling from the clipboard too. hm difficult not sure if i have a good solution for that right away.
MarkBoots
MarkBoots•2y ago
you can see it here, it copies the inline styling. then we would need js to remove it when pasting Have to think about that one
/bin/son-arg
/bin/son-argOP•2y ago
hmm yeah, interesting too
MarkBoots
MarkBoots•2y ago
i've updated the pen a bit. not sure if it covers everything
/bin/son-arg
/bin/son-argOP•2y ago
dang, yeah, it seems to work well! seems like pretty complex stuff too. if you don't mind explaining, how did you manage to get that done? or maybe reference to some doc which i can further read on this
MarkBoots
MarkBoots•2y ago
well the input event is quite easy, it just checks if there is content and set the place holder on/off the paste event i got from here https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event. It will grab only the text from the clipboard.
Element: paste event - Web APIs | MDN
The paste event is fired when the user has initiated a "paste" action through the browser's user interface.
/bin/son-arg
/bin/son-argOP•2y ago
thanks a lot for your great help! 🙂
MarkBoots
MarkBoots•2y ago
np, good luck. make sure you fork the pen if you think you need it later for refference, (I might delete it later)
/bin/son-arg
/bin/son-argOP•2y ago
ah okay, for sure, got it
MarkBoots
MarkBoots•2y ago
pen removed, back up
Want results from more Discord servers?
Add your server