scroll-into-view-if-needed
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -34,2 +34,5 @@ import type { Options as Options_2 } from 'compute-scroll-into-view' | ||
export declare interface StandardBehaviorOptions extends Options_2 { | ||
/** | ||
* @defaultValue 'auto | ||
*/ | ||
behavior?: ScrollBehavior | ||
@@ -36,0 +39,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import t from"compute-scroll-into-view";function e(t){return!1===t?{block:"end",inline:"nearest"}:function(t){return t===Object(t)&&0!==Object.keys(t).length}(t)?t:{block:"start",inline:"nearest"}}function o(o,n){const r=o.isConnected||o.ownerDocument.documentElement.contains(o);if(function(t){return"object"==typeof t&&"function"==typeof t.behavior}(n))return n.behavior(r?t(o,n):[]);if(!r)return;const c=e(n);return function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto";const o="scrollBehavior"in document.body.style;t.forEach((t=>{let{el:n,top:r,left:c}=t;n.scroll&&o?n.scroll({top:r,left:c,behavior:e}):(n.scrollTop=r,n.scrollLeft=c)}))}(t(o,c),c.behavior)}export{o as default}; | ||
import e from"compute-scroll-into-view";let t=e=>!1===e?{block:"end",inline:"nearest"}:(e=>e===Object(e)&&0!==Object.keys(e).length)(e)?e:{block:"start",inline:"nearest"};function o(o,l){let n=o.isConnected||o.ownerDocument.documentElement.contains(o);if((e=>"object"==typeof e&&"function"==typeof e.behavior)(l))return l.behavior(n?e(o,l):[]);if(!n)return;let r=t(l),c=e(o,r),i="scrollBehavior"in document.body.style;c.forEach((e=>{let{el:t,top:o,left:l}=e;t.scroll&&i?t.scroll({top:o,left:l,behavior:r.behavior}):(t.scrollTop=o,t.scrollLeft=l)}))}export{o as default}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "scroll-into-view-if-needed", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Ponyfill for upcoming Element.scrollIntoView() APIs like scrollMode: if-needed, behavior: smooth and block: center", | ||
@@ -61,3 +61,3 @@ "keywords": [ | ||
"dependencies": { | ||
"compute-scroll-into-view": "^2.0.1" | ||
"compute-scroll-into-view": "^2.0.2" | ||
}, | ||
@@ -64,0 +64,0 @@ "devDependencies": { |
@@ -12,2 +12,5 @@ import compute from 'compute-scroll-into-view' | ||
export interface StandardBehaviorOptions extends BaseOptions { | ||
/** | ||
* @defaultValue 'auto | ||
*/ | ||
behavior?: ScrollBehavior | ||
@@ -26,36 +29,13 @@ } | ||
function isStandardScrollBehavior( | ||
let isStandardScrollBehavior = ( | ||
options: any | ||
): options is StandardBehaviorOptions { | ||
return options === Object(options) && Object.keys(options).length !== 0 | ||
} | ||
): options is StandardBehaviorOptions => | ||
options === Object(options) && Object.keys(options).length !== 0 | ||
function isCustomScrollBehavior<T>( | ||
let isCustomScrollBehavior = <T>( | ||
options: any | ||
): options is CustomBehaviorOptions<T> { | ||
if (typeof options === 'object') { | ||
return typeof options.behavior === 'function' | ||
} | ||
return false | ||
} | ||
): options is CustomBehaviorOptions<T> => | ||
typeof options === 'object' ? typeof options.behavior === 'function' : false | ||
function defaultBehavior( | ||
actions: ScrollAction[], | ||
behavior: ScrollBehavior = 'auto' | ||
) { | ||
const canSmoothScroll = 'scrollBehavior' in document.body.style | ||
actions.forEach(({ el, top, left }) => { | ||
// browser implements the new Element.prototype.scroll API that supports `behavior` | ||
// and guard window.scroll with supportsScrollBehavior | ||
if (el.scroll && canSmoothScroll) { | ||
el.scroll({ top, left, behavior }) | ||
} else { | ||
el.scrollTop = top | ||
el.scrollLeft = left | ||
} | ||
}) | ||
} | ||
function getOptions(options: any): StandardBehaviorOptions { | ||
let getOptions = (options: any): StandardBehaviorOptions => { | ||
// Handle alignToTop for legacy reasons, to be compatible with the spec | ||
@@ -79,3 +59,3 @@ if (options === false) { | ||
/** @public */ | ||
function scrollIntoView<T>( | ||
export default function scrollIntoView<T>( | ||
target: Element, | ||
@@ -85,3 +65,3 @@ options: CustomBehaviorOptions<T> | ||
/** @public */ | ||
function scrollIntoView( | ||
export default function scrollIntoView( | ||
target: Element, | ||
@@ -91,8 +71,8 @@ options?: StandardBehaviorOptions | boolean | ||
/** @public */ | ||
function scrollIntoView<T>( | ||
export default function scrollIntoView<T>( | ||
target: Element, | ||
options?: StandardBehaviorOptions | CustomBehaviorOptions<T> | boolean | ||
) { | ||
): T | void { | ||
// Browsers treats targets that aren't in the dom as a no-op and so should we | ||
const isTargetAttached = | ||
let isTargetAttached = | ||
target.isConnected || | ||
@@ -111,9 +91,16 @@ target.ownerDocument!.documentElement!.contains(target) | ||
// @TODO see if it's possible to avoid this assignment | ||
const computeOptions = getOptions(options) | ||
return defaultBehavior( | ||
compute(target, computeOptions), | ||
computeOptions.behavior | ||
) | ||
let computeOptions = getOptions(options) | ||
let actions = compute(target, computeOptions) | ||
let canSmoothScroll = 'scrollBehavior' in document.body.style | ||
actions.forEach(({ el, top, left }) => { | ||
// browser implements the new Element.prototype.scroll API that supports `behavior` | ||
// and guard window.scroll with supportsScrollBehavior | ||
if (el.scroll && canSmoothScroll) { | ||
el.scroll({ top, left, behavior: computeOptions.behavior }) | ||
} else { | ||
el.scrollTop = top | ||
el.scrollLeft = left | ||
} | ||
}) | ||
} | ||
export default scrollIntoView |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
36243
153