Skip to content

Commit fea11e3

Browse files
committed
Work in Progress (nearly complete; needs docs and testing): Add autogrow plugin
1 parent 6737d3c commit fea11e3

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

code-input.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,13 @@ var codeInput = {
555555
*/
556556
syncSize() {
557557
// Synchronise the size of the pre/code and textarea elements
558-
this.textareaElement.style.height = getComputedStyle(this.getStyledHighlightingElement()).height;
559-
this.textareaElement.style.width = getComputedStyle(this.getStyledHighlightingElement()).width;
558+
const height = getComputedStyle(this.getStyledHighlightingElement()).height;
559+
this.textareaElement.style.height = height;
560+
this.textareaElement.style.setProperty("--code-input_synced-height", height);
561+
562+
const width = getComputedStyle(this.getStyledHighlightingElement()).width;
563+
this.textareaElement.style.width = width;
564+
this.textareaElement.style.setProperty("--code-input_synced-width", width);
560565
}
561566

562567
/**

docs/interface/css/_index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ title = 'Styling `code-input` elements with CSS'
1414
* For now, elements on top of `code-input` elements should have a CSS `z-index` at least 3 greater than the `code-input` element.
1515

1616
Please do **not** use `className` in JavaScript referring to code-input elements, because the code-input library needs to add its own classes to code-input elements for easier progressive enhancement. You can, however, use `classList` and `style` as much as you want - it will make your code cleaner anyway.
17+
18+
## Methods of resizing
19+
20+
`code-input` elements default to having a fixed height and filling the width of their container while the code inside scrolls, and you can set the ordinary CSS properties (for example `height` and `width`) to change this size. You can also make the size more flexible using CSS:
21+
22+
* The [CSS `resize` property (see link)](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/resize) can be used on `code-input` elements to give them the manual resizing handle `textarea`s often come with, when the web browser supports it. For example, `<code-input style="resize: both;"`... gives:
23+
![A syntax-highlighted code input element with diagonal lines in the corner, which can be dragged to resize the element.](resize-both-screenshot.png)
24+
* You can also make a `code-input` element resize automatically to fit its contents - use [the Autosize plugin](../../plugins/#playground-preset-autosize) for that.
5.79 KB
Loading

plugins/autogrow.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Make code-input elements resize automatically and fit their content live using CSS
3+
* classes, optionally between a minimum and maximum size specified using CSS variables.
4+
*
5+
* Files: autogrow.css
6+
*/
7+
8+
/* Inspired greatly by https://css-tricks.com/the-cleanest-trick-for-autogrowing-textareas/
9+
and the fact code-input.js already implements the article's main structure. */
10+
11+
code-input.code-input_autogrow_height {
12+
--code-input_autogrow_min-height: 0px;
13+
--code-input_autogrow_max-height: calc(infinity * 1px);
14+
height: max-content;
15+
max-height: var(--code-input_autogrow_max-height);
16+
}
17+
code-input.code-input_autogrow_height textarea {
18+
height: var(--code-input_autogrow_min-height)!important; /* So minimum height possible while containing highlighted code */
19+
min-height: max(var(--code-input_synced-height), calc(100% - var(--padding-top, 16px) - var(--padding-bottom, 16px)));
20+
}
21+
22+
code-input.code-input_autogrow_width {
23+
--code-input_autogrow_min-width: 100px;
24+
--code-input_autogrow_max-width: 100%;
25+
width: max-content; /* Using unset rather than max-content makes always 100% width. */
26+
max-width: var(--code-input_autogrow_max-width);
27+
}
28+
29+
code-input.code-input_autogrow_width textarea {
30+
width: var(--code-input_autogrow_min-width)!important; /* So minimum width possible while containing highlighted code */
31+
min-width: max(var(--code-input_synced-width), calc(100% - var(--padding-left, 16px) - var(--padding-right, 16px)));
32+
}

0 commit comments

Comments
 (0)