@tldraw/editor
Version:
tldraw infinite canvas SDK (editor).
8 lines (7 loc) • 2.08 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/lib/hooks/useViewportHeight.ts"],
"sourcesContent": ["import { useLayoutEffect, useState } from 'react'\n\n/*!\n * BSD License: https://github.com/outline/rich-markdown-editor/blob/main/LICENSE\n * Copyright (c) 2020 General Outline, Inc (https://www.getoutline.com/) and individual contributors.\n *\n * Returns the height of the viewport.\n * This is mainly to account for virtual keyboards on mobile devices.\n *\n * N.B. On iOS, you have to take into account the offsetTop as well so that you get an accurate position\n * while using the virtual keyboard.\n */\n/** @public */\nexport function useViewportHeight(): number {\n\tconst visualViewport = window.visualViewport\n\tconst [height, setHeight] = useState<number>(() =>\n\t\tvisualViewport ? visualViewport.height + visualViewport.offsetTop : window.innerHeight\n\t)\n\n\tuseLayoutEffect(() => {\n\t\tconst handleResize = () => {\n\t\t\tconst visualViewport = window.visualViewport\n\t\t\tsetHeight(() =>\n\t\t\t\tvisualViewport ? visualViewport.height + visualViewport.offsetTop : window.innerHeight\n\t\t\t)\n\t\t}\n\n\t\twindow.visualViewport?.addEventListener('resize', handleResize)\n\t\twindow.visualViewport?.addEventListener('scroll', handleResize)\n\n\t\treturn () => {\n\t\t\twindow.visualViewport?.removeEventListener('resize', handleResize)\n\t\t\twindow.visualViewport?.removeEventListener('scroll', handleResize)\n\t\t}\n\t}, [])\n\treturn height\n}\n"],
"mappings": "AAAA,SAAS,iBAAiB,gBAAgB;AAE1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWO,SAAS,oBAA4B;AAC3C,QAAM,iBAAiB,OAAO;AAC9B,QAAM,CAAC,QAAQ,SAAS,IAAI;AAAA,IAAiB,MAC5C,iBAAiB,eAAe,SAAS,eAAe,YAAY,OAAO;AAAA,EAC5E;AAEA,kBAAgB,MAAM;AACrB,UAAM,eAAe,MAAM;AAC1B,YAAMA,kBAAiB,OAAO;AAC9B;AAAA,QAAU,MACTA,kBAAiBA,gBAAe,SAASA,gBAAe,YAAY,OAAO;AAAA,MAC5E;AAAA,IACD;AAEA,WAAO,gBAAgB,iBAAiB,UAAU,YAAY;AAC9D,WAAO,gBAAgB,iBAAiB,UAAU,YAAY;AAE9D,WAAO,MAAM;AACZ,aAAO,gBAAgB,oBAAoB,UAAU,YAAY;AACjE,aAAO,gBAAgB,oBAAoB,UAAU,YAAY;AAAA,IAClE;AAAA,EACD,GAAG,CAAC,CAAC;AACL,SAAO;AACR;",
"names": ["visualViewport"]
}