amis-rpc-design/node_modules/scroll-into-view-if-needed/dist/index.cjs.map

1 line
6.7 KiB
Plaintext
Raw Normal View History

2023-10-07 19:42:30 +08:00
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { compute } from 'compute-scroll-into-view'\nimport type {\n Options as BaseOptions,\n ScrollAction,\n} from 'compute-scroll-into-view'\n\n/** @public */\nexport type Options<T = unknown> =\n | StandardBehaviorOptions\n | CustomBehaviorOptions<T>\n\n/**\n * Only scrolls if the `node` is partially out of view:\n * ```ts\n * scrollIntoView(node, { scrollMode: 'if-needed' })\n * ```\n * Skips scrolling `overflow: hidden` elements:\n * ```ts\n * scrollIntoView(node, { skipOverflowHiddenElements: true })\n * ```\n * When scrolling is needed do the least and smoothest scrolling possible:\n * ```ts\n * scrollIntoView(node, {\n * behavior: 'smooth',\n * scrollMode: 'if-needed',\n * block: 'nearest',\n * inline: 'nearest',\n * })\n * ```\n * @public\n */\nexport interface StandardBehaviorOptions extends BaseOptions {\n /**\n * @defaultValue 'auto\n */\n behavior?: ScrollBehavior\n}\n\n/** @public */\nexport interface CustomBehaviorOptions<T = unknown> extends BaseOptions {\n behavior: CustomScrollBehaviorCallback<T>\n}\n\n/** @public */\nexport type CustomScrollBehaviorCallback<T = unknown> = (\n actions: ScrollAction[]\n) => T\n\nconst isStandardScrollBehavior = (\n options: any\n): options is StandardBehaviorOptions =>\n options === Object(options) && Object.keys(options).length !== 0\n\nconst isCustomScrollBehavior = <T = unknown>(\n options: any\n): options is CustomBehaviorOptions<T> =>\n typeof options === 'object' ? typeof options.behavior === 'function' : false\n\nconst getOptions = (options: any): StandardBehaviorOptions => {\n // Handle alignToTop for legacy reasons, to be compatible with the spec\n if (options === false) {\n return { block: 'end', inline: 'nearest' }\n }\n\n if (isStandardScrollBehavior(options)) {\n // compute.ts ensures the defaults are block: 'center' and inline: 'nearest', to conform to the spec\n return options\n }\n\n // if options = {}, options = true or options = null, based on w3c web platform test\n return { block: 'start', inline: 'nearest' }\n}\n\nconst getScrollMargins = (target: Element) => {\n const computedStyle = window.getComputedStyle(target)\n return {\n top: parseFloat(computedStyle.scrollMarginTop) || 0,\n right: parseFloat(computedStyle.scrollMarginRight) || 0,\n bottom: parseFloat(computedStyle.scrollMarginBottom) || 0,\n left: parseFloat(computedStyle.scrollMarginLeft) || 0,\n }\n}\n\n// Determine if the element is part of the document (including shadow dom)\n// Derived from code of Andy Desmarais\n// https://terodox.tech/how-to-tell-if-an-element-is-in-the-dom-including-the-shadow-dom/\nconst isInDocument = (element: Node) => {\n let currentElement = element\n while (currentElement && currentElement.parentNode) {\n if (currentElement.parentNode === document) {\n return true\n } else if (currentElement.parentNode instanceof ShadowRoot) {\n currentElement = (currentElement.parentNode as ShadowRoot).host\n } else {\n currentElement = currentElement.parentNode\n }\n }\n return false\n}\n\n/**\n * Scrolls the given element into view, with options for when, and how.\n * Supports the same `options` as [`Element.prototype.scrollIntoView`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) with additions such as `scrollMode`, `behavior: Function` and `skipOverflowHiddenElements`.\n * @public\n */\nfunction scrollIntoView(\n target: Element,\n options?: StandardBehaviorOptions | boolean\n): void\n/**\n * Scrolls the given element into view, with options for when, and how.\n * Supports the same `options` as [`Element.prototype.scrollIntoView`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) with additions such as `scrollMode`, `behavior: Function` and `skipOverflowHiddenElements`.\n *\n * You can set the expected return type for `behavior: Function`:\n * ```ts\n * await scrollIntoView<Promise<boolean[]>>(node, {\n * behavior: async actions => {\n * return Promis