@scrolia/react
Version:
A headless scrollbar component
1 lines • 8.97 kB
Source Map (JSON)
{"version":3,"file":"length.mjs","names":["_total: number","_view: number","scrollbarLengthNext: number","result: OnSetLengthResult | undefined","length: number"],"sources":["../../src/hooks/length.ts"],"sourcesContent":["import type { Axis, OnSetLengthResult, Plugin } from \"#/@types/options\";\n\nimport * as React from \"react\";\n\nimport { useScrollCore } from \"#/contexts/scrollcore\";\nimport { tryPlugin } from \"#/functions/plugin\";\n\ntype SetLengthOptions = {\n axis: Axis;\n disabled: boolean;\n page: boolean;\n plugins: Plugin[];\n contentRef: React.RefObject<HTMLElement | null>;\n hvTrack: boolean;\n hvThumb: boolean;\n total: React.RefObject<number>;\n view: React.RefObject<number>;\n viewOffset: React.RefObject<number>;\n scrollbarLength: number;\n setScrollbarLength: React.Dispatch<React.SetStateAction<number>>;\n};\n\nconst setLengthFn = ({\n axis,\n disabled,\n page,\n plugins,\n contentRef,\n hvTrack,\n hvThumb,\n total,\n view,\n viewOffset,\n scrollbarLength,\n setScrollbarLength,\n}: SetLengthOptions): void => {\n let _total: number = 0;\n let _view: number = 0;\n\n if (page) {\n if (axis === \"x\") {\n _total = document.body.scrollWidth;\n _view = window.innerWidth;\n } else {\n _total = document.body.scrollHeight;\n _view = window.innerHeight;\n }\n } else if (contentRef.current) {\n if (axis === \"x\") {\n _total = contentRef.current.scrollWidth;\n _view = contentRef.current.clientWidth;\n } else {\n _total = contentRef.current.scrollHeight;\n _view = contentRef.current.clientHeight;\n }\n }\n\n if (total.current === _total && view.current === _view) return void 0;\n\n total.current = _total;\n view.current = _view;\n\n const scrollbarLengthNext: number = (_view / _total) * _view;\n\n let result: OnSetLengthResult | undefined;\n\n for (const plugin of plugins) {\n if (!plugin.onSetLength) continue;\n\n result =\n tryPlugin(plugin, plugin.onSetLength, {\n axis,\n isDisabled: disabled,\n isPage: page,\n isDefined: hvTrack && hvThumb,\n total: _total,\n view: _view,\n viewOffset: viewOffset.current,\n scrollbarLengthPrev: scrollbarLength,\n scrollbarLengthNext:\n result?.scrollbarLength ?? scrollbarLengthNext,\n }) ?? result;\n }\n\n let length: number;\n\n if (result?.scrollbarLength) {\n length = result.scrollbarLength;\n } else {\n length = scrollbarLengthNext;\n }\n\n // functions\n if (_view >= _total) {\n setScrollbarLength(0);\n } else {\n setScrollbarLength(length);\n }\n};\n\n/** Hook for setting the length of the scrollbar. */\nconst useLengthHandler = (): void => {\n const {\n options: { disabled, page, plugins },\n contentRef,\n x,\n y,\n } = useScrollCore();\n\n React.useEffect((): (() => void) => {\n const setLength = (): void => {\n x.hvTrack &&\n x.hvThumb &&\n setLengthFn({\n axis: \"x\",\n disabled,\n page,\n plugins,\n contentRef,\n hvTrack: x.hvTrack,\n hvThumb: x.hvThumb,\n total: x.total,\n view: x.view,\n viewOffset: x.viewOffset,\n scrollbarLength: x.scrollbarLength,\n setScrollbarLength: x.setScrollbarLength,\n });\n };\n\n setLength();\n\n if (page) {\n window.addEventListener(\"resize\", setLength);\n window.addEventListener(\"scroll\", setLength);\n } else if (contentRef.current) {\n contentRef.current.addEventListener(\"resize\", setLength);\n contentRef.current.addEventListener(\"scroll\", setLength);\n }\n\n return (): void => {\n if (page) {\n window.removeEventListener(\"resize\", setLength);\n window.removeEventListener(\"scroll\", setLength);\n } else if (contentRef.current) {\n contentRef.current.removeEventListener(\"resize\", setLength);\n contentRef.current.removeEventListener(\"scroll\", setLength);\n }\n };\n }, [\n disabled,\n page,\n plugins,\n contentRef,\n\n x.hvTrack,\n x.hvThumb,\n x.total,\n x.view,\n x.viewOffset,\n x.scrollbarLength,\n x.setScrollbarLength,\n ]);\n\n React.useEffect((): (() => void) => {\n const setLength = (): void => {\n y.hvTrack &&\n y.hvThumb &&\n setLengthFn({\n axis: \"y\",\n disabled,\n page,\n plugins,\n contentRef,\n hvTrack: y.hvTrack,\n hvThumb: y.hvThumb,\n total: y.total,\n view: y.view,\n viewOffset: y.viewOffset,\n scrollbarLength: y.scrollbarLength,\n setScrollbarLength: y.setScrollbarLength,\n });\n };\n\n setLength();\n\n if (page) {\n window.addEventListener(\"resize\", setLength);\n window.addEventListener(\"scroll\", setLength);\n } else if (contentRef.current) {\n contentRef.current.addEventListener(\"resize\", setLength);\n contentRef.current.addEventListener(\"scroll\", setLength);\n }\n\n return (): void => {\n if (page) {\n window.removeEventListener(\"resize\", setLength);\n window.removeEventListener(\"scroll\", setLength);\n } else if (contentRef.current) {\n contentRef.current.removeEventListener(\"resize\", setLength);\n contentRef.current.removeEventListener(\"scroll\", setLength);\n }\n };\n }, [\n disabled,\n page,\n plugins,\n contentRef,\n\n y.hvTrack,\n y.hvThumb,\n y.total,\n y.view,\n y.viewOffset,\n y.scrollbarLength,\n y.setScrollbarLength,\n ]);\n};\n\nexport { useLengthHandler };\n"],"mappings":";;;;AAsBA,MAAM,cAAc,CAAC,EACjB,MACA,UACA,MACA,SACA,YACA,SACA,SACA,OACA,MACA,YACA,iBACA,oBACe,KAAW;CAC1B,IAAIA,SAAiB;CACrB,IAAIC,QAAgB;AAEpB,KAAI,KACA,KAAI,SAAS,KAAK;AACd,WAAS,SAAS,KAAK;AACvB,UAAQ,OAAO;CAClB,OAAM;AACH,WAAS,SAAS,KAAK;AACvB,UAAQ,OAAO;CAClB;UACM,WAAW,QAClB,KAAI,SAAS,KAAK;AACd,WAAS,WAAW,QAAQ;AAC5B,UAAQ,WAAW,QAAQ;CAC9B,OAAM;AACH,WAAS,WAAW,QAAQ;AAC5B,UAAQ,WAAW,QAAQ;CAC9B;AAGL,KAAI,MAAM,YAAY,UAAU,KAAK,YAAY,MAAO,aAAY;AAEpE,OAAM,UAAU;AAChB,MAAK,UAAU;CAEf,MAAMC,sBAA+B,QAAQ,SAAU;CAEvD,IAAIC;AAEJ,MAAK,MAAM,UAAU,SAAS;AAC1B,OAAK,OAAO,YAAa;AAEzB,WACI,UAAU,QAAQ,OAAO,aAAa;GAClC;GACA,YAAY;GACZ,QAAQ;GACR,WAAW,WAAW;GACtB,OAAO;GACP,MAAM;GACN,YAAY,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