From 932966a518da44ff26086a08458313c4c5fa03e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Fri, 29 Oct 2021 11:50:17 +0100 Subject: [PATCH 1/2] Convert source to TypeScript --- .eslintrc.json | 3 ++- src/{index.js => index.ts} | 0 tsconfig.json | 5 ++--- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/{index.js => index.ts} (100%) diff --git a/.eslintrc.json b/.eslintrc.json index 798a9ec..080fd73 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,6 +4,7 @@ "extends": [ "plugin:github/internal", "plugin:github/recommended", - "plugin:github/browser" + "plugin:github/browser", + "plugin:github/typescript" ] } diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts diff --git a/tsconfig.json b/tsconfig.json index 54d6efe..9cf7595 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,10 +6,9 @@ "declaration": true, "outDir": "dist", "removeComments": true, - "preserveConstEnums": true, - "allowJs": true + "preserveConstEnums": true }, "files": [ - "src/index.js" + "src/index.ts" ] } From 048ec6b3ca6ed18184c93fe6af954086589be201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Fri, 29 Oct 2021 11:55:13 +0100 Subject: [PATCH 2/2] Add types to source --- src/index.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index e43e18e..38294e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,16 @@ -export default function autosize(textarea, {viewportMarginBottom = 100} = {}) { - let previousValue = null +interface Subscription { + unsubscribe(): void +} + +export default function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom = 100} = {}): Subscription { + let previousValue: string | null = null let isUserResized = false - let x - let y - let height + let x: number + let y: number + let height: string - function onUserResize(event) { + function onUserResize(event: MouseEvent) { if (x !== event.clientX || y !== event.clientY) { const newHeight = textarea.style.height if (height && height !== newHeight) { @@ -30,10 +34,10 @@ export default function autosize(textarea, {viewportMarginBottom = 100} = {}) { while (el !== document.body && el !== null) { offsetTop += el.offsetTop || 0 - el = el.offsetParent + el = el.offsetParent as HTMLTextAreaElement } - const top = offsetTop - document.defaultView.pageYOffset + const top = offsetTop - document.defaultView!.pageYOffset const bottom = documentElement.clientHeight - (top + textarea.offsetHeight) return {top, bottom} }