react-anchor-link-smooth-scroll
Version:
React component for anchor links using the smoothscroll polyfill.
1 lines • 7.23 kB
JavaScript
(function(a,b){'object'==typeof exports&&'object'==typeof module?module.exports=b(require('react')):'function'==typeof define&&define.amd?define(['react'],b):'object'==typeof exports?exports['anchor-link']=b(require('react')):a['anchor-link']=b(a.react)})('undefined'==typeof self?this:self,function(a){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={i:d,l:!1,exports:{}};return a[d].call(e.exports,e,e.exports,b),e.l=!0,e.exports}var c={};return b.m=a,b.c=c,b.d=function(a,c,d){b.o(a,c)||Object.defineProperty(a,c,{configurable:!1,enumerable:!0,get:d})},b.n=function(a){var c=a&&a.__esModule?function(){return a['default']}:function(){return a};return b.d(c,'a',c),c},b.o=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)},b.p='',b(b.s=0)}([function(a,b,c){'use strict';Object.defineProperty(b,'__esModule',{value:!0});var d=c(1),e=function(a){return a&&a.__esModule?a:{default:a}}(d);b.default=e.default},function(a,b,c){'use strict';function d(a,b){var c={};for(var d in a)0<=b.indexOf(d)||Object.prototype.hasOwnProperty.call(a,d)&&(c[d]=a[d]);return c}function e(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}function f(a,b){if(!a)throw new ReferenceError('this hasn\'t been initialised - super() hasn\'t been called');return b&&('object'==typeof b||'function'==typeof b)?b:a}function g(a,b){if('function'!=typeof b&&null!==b)throw new TypeError('Super expression must either be null or a function, not '+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}Object.defineProperty(b,'__esModule',{value:!0});var h=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a},i=function(){function a(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,'value'in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),j=c(2),k=function(a){return a&&a.__esModule?a:{default:a}}(j),l=function(a){function b(a){e(this,b);var c=f(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,a));return c.smoothScroll=c.smoothScroll.bind(c),c}return g(b,a),i(b,[{key:'componentDidMount',value:function(){c(3).polyfill()}},{key:'smoothScroll',value:function(a){var b=this;a.preventDefault();var c=function(){return 0};'undefined'!=typeof this.props.offset&&(this.props.offset&&this.props.offset.constructor&&this.props.offset.apply?c=this.props.offset:c=function(){return parseInt(b.props.offset)});var d=a.currentTarget.getAttribute('href').slice(1),e=document.getElementById(d),f=e.getBoundingClientRect().top+window.pageYOffset;window.scroll({top:f-c(),behavior:'smooth'}),this.props.onClick&&this.props.onClick(a)}},{key:'render',value:function(){var a=this.props,b=a.offset,c=d(a,['offset']);return k.default.createElement('a',h({},c,{onClick:this.smoothScroll}))}}]),b}(j.Component);b.default=l},function(b){b.exports=a},function(a){(function(){'use strict';function b(){function a(a){return new RegExp(['MSIE ','Trident/','Edge/'].join('|')).test(a)}function b(a,b){this.scrollLeft=a,this.scrollTop=b}function c(a){return 0.5*(1-Math.cos(Math.PI*a))}function e(a){if(null===a||'object'!=typeof a||a.behavior===void 0||'auto'===a.behavior||'instant'===a.behavior)return!0;if('object'==typeof a&&'smooth'===a.behavior)return!1;throw new TypeError('behavior member of ScrollOptions '+a.behavior+' is not a valid value for enumeration ScrollBehavior.')}function f(a,b){return'Y'===b?a.clientHeight+q<a.scrollHeight:'X'===b?a.clientWidth+q<a.scrollWidth:void 0}function g(a,b){var c=l.getComputedStyle(a,null)['overflow'+b];return'auto'===c||'scroll'===c}function h(a){var b=f(a,'Y')&&g(a,'Y'),c=f(a,'X')&&g(a,'X');return b||c}function i(a){var b;do a=a.parentNode,b=a===m.body;while(!1===b&&!1===h(a));return b=null,a}function j(a){var b,d,e,f=p(),g=(f-a.startTime)/n;g=1<g?1:g,b=c(g),d=a.startX+(a.x-a.startX)*b,e=a.startY+(a.y-a.startY)*b,a.method.call(a.scrollable,d,e),(d!==a.x||e!==a.y)&&l.requestAnimationFrame(j.bind(l,a))}function k(a,c,d){var e,f,g,h,i=p();a===m.body?(e=l,f=l.scrollX||l.pageXOffset,g=l.scrollY||l.pageYOffset,h=o.scroll):(e=a,f=a.scrollLeft,g=a.scrollTop,h=b),j({scrollable:e,method:h,startTime:i,startX:f,startY:g,x:c,y:d})}var l=window,m=document;if(!('scrollBehavior'in m.documentElement.style&&!0!==l.__forceSmoothScrollPolyfill__)){var d=l.HTMLElement||l.Element,n=468,o={scroll:l.scroll||l.scrollTo,scrollBy:l.scrollBy,elementScroll:d.prototype.scroll||b,scrollIntoView:d.prototype.scrollIntoView},p=l.performance&&l.performance.now?l.performance.now.bind(l.performance):Date.now,q=a(l.navigator.userAgent)?1:0;l.scroll=l.scrollTo=function(){return void 0===arguments[0]?void 0:!0===e(arguments[0])?void o.scroll.call(l,void 0===arguments[0].left?'object'==typeof arguments[0]?l.scrollX||l.pageXOffset:arguments[0]:arguments[0].left,void 0===arguments[0].top?void 0===arguments[1]?l.scrollY||l.pageYOffset:arguments[1]:arguments[0].top):void k.call(l,m.body,void 0===arguments[0].left?l.scrollX||l.pageXOffset:~~arguments[0].left,void 0===arguments[0].top?l.scrollY||l.pageYOffset:~~arguments[0].top)},l.scrollBy=function(){return void 0===arguments[0]?void 0:e(arguments[0])?void o.scrollBy.call(l,void 0===arguments[0].left?'object'==typeof arguments[0]?0:arguments[0]:arguments[0].left,void 0===arguments[0].top?void 0===arguments[1]?0:arguments[1]:arguments[0].top):void k.call(l,m.body,~~arguments[0].left+(l.scrollX||l.pageXOffset),~~arguments[0].top+(l.scrollY||l.pageYOffset))},d.prototype.scroll=d.prototype.scrollTo=function(){if(void 0!==arguments[0]){if(!0===e(arguments[0])){if('number'==typeof arguments[0]&&void 0===arguments[1])throw new SyntaxError('Value could not be converted');return void o.elementScroll.call(this,void 0===arguments[0].left?'object'==typeof arguments[0]?this.scrollLeft:~~arguments[0]:~~arguments[0].left,void 0===arguments[0].top?void 0===arguments[1]?this.scrollTop:~~arguments[1]:~~arguments[0].top)}var a=arguments[0].left,b=arguments[0].top;k.call(this,this,'undefined'==typeof a?this.scrollLeft:~~a,'undefined'==typeof b?this.scrollTop:~~b)}},d.prototype.scrollBy=function(){return void 0===arguments[0]?void 0:!0===e(arguments[0])?void o.elementScroll.call(this,void 0===arguments[0].left?~~arguments[0]+this.scrollLeft:~~arguments[0].left+this.scrollLeft,void 0===arguments[0].top?~~arguments[1]+this.scrollTop:~~arguments[0].top+this.scrollTop):void this.scroll({left:~~arguments[0].left+this.scrollLeft,top:~~arguments[0].top+this.scrollTop,behavior:arguments[0].behavior})},d.prototype.scrollIntoView=function(){if(!0===e(arguments[0]))return void o.scrollIntoView.call(this,void 0===arguments[0]||arguments[0]);var a=i(this),b=a.getBoundingClientRect(),c=this.getBoundingClientRect();a===m.body?l.scrollBy({left:c.left,top:c.top,behavior:'smooth'}):(k.call(this,a,a.scrollLeft+c.left-b.left,a.scrollTop+c.top-b.top),'fixed'!==l.getComputedStyle(a).position&&l.scrollBy({left:b.left,top:b.top,behavior:'smooth'}))}}}a.exports={polyfill:b}})()}])});