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 87% rename from src/index.js rename to src/index.ts index e43e18e..38294e5 100644 --- a/src/index.js +++ 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} } 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" ] }