-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bff46a
commit 425198d
Showing
5 changed files
with
60 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { onResize } from '$lib/utils/resize'; | ||
|
||
export const autoGrowHeight = (textarea: HTMLTextAreaElement, height = 'auto') => { | ||
const resize = () => { | ||
textarea.style.height = height; | ||
textarea.style.height = `${textarea.scrollHeight + 5}px`; | ||
}; | ||
|
||
resize(); | ||
|
||
const onResizeAction = onResize(textarea, resize); | ||
textarea.addEventListener('input', resize); | ||
window.addEventListener('resize', resize); | ||
|
||
return { | ||
destroy: () => { | ||
onResizeAction.destroy?.(); | ||
textarea.removeEventListener('input', resize); | ||
window.removeEventListener('resize', resize); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { ActionReturn } from 'svelte/action'; | ||
|
||
export type OnResize = (rect: DOMRectReadOnly) => unknown; | ||
|
||
export const onResize = <T extends Element>(target: T, eventHandler: OnResize): ActionReturn => { | ||
const observer = new ResizeObserver((entries) => { | ||
for (const entry of entries) { | ||
eventHandler(entry.target.getBoundingClientRect()); | ||
} | ||
}); | ||
|
||
observer.observe(target); | ||
|
||
return { | ||
destroy() { | ||
observer.disconnect(); | ||
}, | ||
}; | ||
}; |