Skip to content

Commit

Permalink
Merge pull request #10 from marcreichel/fix/#9-textarea-height
Browse files Browse the repository at this point in the history
✨ Introduce padding modifier
  • Loading branch information
marcreichel committed Sep 15, 2022
2 parents 5fc1bfd + 29a7050 commit 1c1fa35
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ To let the `textarea` automatically resize, add the `x-data` and `x-autosize` di
<textarea x-data x-autosize></textarea>
```

That's it!
### ⏬ Additional height

To add additional height to the textarea - which might be necessary in some occasions - you can do so using the
"padding" modifier like so (only `px` values are supported):

```html
<textarea x-data x-autosize.10px></textarea>
```

This adds additional 10px to the textarea height. You can provide any integer which best suits your needs.

## 📄 License

Expand Down
14 changes: 11 additions & 3 deletions dist/alpine-autosize.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/alpine-autosize.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/alpine-autosize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/alpine-autosize.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Alpine Autosize Examples</title>

<script defer src="../dist/alpine-autosize.min.js"></script>
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
</head>
<body x-data>
<h1>Examples</h1>
<h2>Default</h2>
<textarea x-autosize aria-label=""></textarea>
<h2>With padding</h2>
<textarea x-autosize.50px aria-label=""></textarea>
</body>
</html>
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function Autosize(Alpine) {
Alpine.directive('autosize', (el, {}, { cleanup }) => {

Alpine.directive('autosize', (el, { modifiers }, { cleanup }) => {
const attributes = Array.from(el.attributes);

let hasWireModel = false;
Expand All @@ -20,15 +19,21 @@ function Autosize(Alpine) {
el.style.resize = 'none';

const previousMinHeight = el.style.minHeight;
el.style.minHeight = el.getBoundingClientRect().height + 'px';
el.style.minHeight = `${el.getBoundingClientRect().height}px`;

const paddingModifier = modifiers.filter(modifier => modifier.match(/px$/i))[0] || false;
let padding = 0;
if (paddingModifier !== false) {
padding = parseInt(paddingModifier);
}

const handler = (event) => {
const element = event.target;
if (!element.scrollHeight) {
return;
}
element.style.height = '4px';
element.style.height = `${element.scrollHeight}px`;
element.style.height = `${element.scrollHeight + padding}px`;
};

handler({ target: el });
Expand Down

0 comments on commit 1c1fa35

Please sign in to comment.