{"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/** @public */\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\nlet isStandardScrollBehavior = (\n  options: any\n): options is StandardBehaviorOptions =>\n  options === Object(options) && Object.keys(options).length !== 0\n\nlet isCustomScrollBehavior = <T = unknown>(\n  options: any\n): options is CustomBehaviorOptions<T> =>\n  typeof options === 'object' ? typeof options.behavior === 'function' : false\n\nlet 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\n// Some people might use both \"auto\" and \"ponyfill\" modes in the same file, so we also provide a named export so\n// that imports in userland code (like if they use native smooth scrolling on some browsers, and the ponyfill for everything else)\n// the named export allows this `import {auto as autoScrollIntoView, ponyfill as smoothScrollIntoView} from ...`\n/** @public */\nexport default function scrollIntoView<T>(\n  target: Element,\n  options: CustomBehaviorOptions<T>\n): T\n/** @public */\nexport default function scrollIntoView(\n  target: Element,\n  options?: StandardBehaviorOptions | boolean\n): void\n/** @public */\nexport default function scrollIntoView<T = unknown>(\n  target: Element,\n  options?: StandardBehaviorOptions | CustomBehaviorOptions<T> | boolean\n): T | void {\n  // Browsers treats targets that aren't in the dom as a no-op and so should we\n  let isTargetAttached =\n    target.isConnected ||\n    target.ownerDocument!.documentElement!.contains(target)\n\n  if (isCustomScrollBehavior<T>(options)) {\n    return options.behavior(isTargetAttached ? compute(target, options) : [])\n  }\n\n  // Don't do anything if using a standard behavior on an element that is not in the document\n  if (!isTargetAttached) {\n    return\n  }\n\n  // @TODO see if it's possible to avoid this assignment\n  let computeOptions = getOptions(options)\n  let actions = compute(target, computeOptions)\n  let canSmoothScroll = 'scrollBehavior' in document.body.style\n\n  actions.forEach(({ el, top, left }) => {\n    // browser implements the new Element.prototype.scroll API that supports `behavior`\n    // and guard window.scroll with supportsScrollBehavior\n    if (el.scroll && canSmoothScroll) {\n      el.scroll({ top, left, behavior: computeOptions.behavior })\n    } else {\n      el.scrollTop = top\n      el.scrollLeft = left\n    }\n  })\n}\n"],"names":["getOptions","options","block","inline","Object","keys","length","isStandardScrollBehavior","target","isTargetAttached","isConnected","ownerDocument","documentElement","contains","behavior","isCustomScrollBehavior","compute","computeOptions","actions","canSmoothScroll","document","body","style","forEach","_ref","el","top","left","scroll","scrollTop","scrollLeft"],"mappings":"kIA6BA,IAUIA,EAAcC,IAEA,IAAZA,EACK,CAAEC,MAAO,MAAOC,OAAQ,WAZjCF,IAEAA,IAAYG,OAAOH,IAA4C,IAAhCG,OAAOC,KAAKJ,GAASK,OAahDC,CAAyBN,GAEpBA,EAIF,CAAEC,MAAO,QAASC,OAAQ,0BAiBX,SACtBK,EACAP,GAGA,IAAIQ,EACFD,EAAOE,aACPF,EAAOG,cAAeC,gBAAiBC,SAASL,GAE9C,GA1CJP,IAEmB,iBAAZA,GAAmD,mBAArBA,EAAQa,SAwCzCC,CAA0Bd,GACrB,OAAAA,EAAQa,SAASL,EAAmBO,EAAAA,QAAQR,EAAQP,GAAW,IAIxE,IAAKQ,EACH,OAIE,IAAAQ,EAAiBjB,EAAWC,GAC5BiB,EAAUF,EAAAA,QAAQR,EAAQS,GAC1BE,EAAkB,mBAAoBC,SAASC,KAAKC,MAExDJ,EAAQK,SAAQC,IAAuB,IAAtBC,GAAEA,EAAIC,IAAAA,EAAAC,KAAKA,GAAWH,EAGjCC,EAAGG,QAAUT,EACfM,EAAGG,OAAO,CAAEF,MAAKC,OAAMb,SAAUG,EAAeH,YAEhDW,EAAGI,UAAYH,EACfD,EAAGK,WAAaH,EAClB,GAEJ"}