UNPKG

react-fixed-bottom

Version:

React component to make fixed:bottom styles usable in Safari mobile

17 lines (14 loc) 4.9 kB
import t from"react"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */var e=function(t,o){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])})(t,o)};var o=function(){return(o=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)},n="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},r=/^\s+|\s+$/g,i=/^[-+]0x[0-9a-f]+$/i,f=/^0b[01]+$/i,a=/^0o[0-7]+$/i,u=parseInt,c="object"==typeof n&&n&&n.Object===Object&&n,l="object"==typeof self&&self&&self.Object===Object&&self,s=c||l||Function("return this")(),p=Object.prototype.toString,d=Math.max,m=Math.min,h=function(){return s.Date.now()};function v(t,e,o){var n,r,i,f,a,u,c=0,l=!1,s=!1,p=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function v(e){var o=n,i=r;return n=r=void 0,c=e,f=t.apply(i,o)}function w(t){return c=t,a=setTimeout(O,e),l?v(t):f}function g(t){var o=t-u;return void 0===u||o>=e||o<0||s&&t-c>=i}function O(){var t=h();if(g(t))return S(t);a=setTimeout(O,function(t){var o=e-(t-u);return s?m(o,i-(t-c)):o}(t))}function S(t){return a=void 0,p&&n?v(t):(n=r=void 0,f)}function j(){var t=h(),o=g(t);if(n=arguments,r=this,u=t,o){if(void 0===a)return w(u);if(s)return a=setTimeout(O,e),v(u)}return void 0===a&&(a=setTimeout(O,e)),f}return e=b(e)||0,y(o)&&(l=!!o.leading,i=(s="maxWait"in o)?d(b(o.maxWait)||0,e):i,p="trailing"in o?!!o.trailing:p),j.cancel=function(){void 0!==a&&clearTimeout(a),c=0,n=u=r=a=void 0},j.flush=function(){return void 0===a?f:S(h())},j}function y(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function b(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==p.call(t)}(t))return NaN;if(y(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=y(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(r,"");var o=f.test(t);return o||a.test(t)?u(t.slice(2),o?2:8):i.test(t)?NaN:+t}var w=function(t,e,o){var n=!0,r=!0;if("function"!=typeof t)throw new TypeError("Expected a function");return y(o)&&(n="leading"in o?!!o.leading:n,r="trailing"in o?!!o.trailing:r),v(t,e,{leading:n,maxWait:e,trailing:r})};var g=function(n){function r(e){var o=n.call(this,e)||this;return o.state={bottom:o.props.offset||0},o.isSafariMobile=function(){if("undefined"==typeof window||void 0===window.navigator)return!1;var t=window.navigator.userAgent;return/iP(ad|od|hone)/i.test(t)&&/WebKit/i.test(t)&&!/(CriOS|FxiOS|OPiOS|mercury)/i.test(t)}(),o.anchorRef=t.createRef(),o.computeOffsetBottom=function(){if(o.anchorRef.current){var t=o.anchorRef.current.getBoundingClientRect().bottom,e=o.props.offset,n=void 0===e?0:e;Math.floor(Number(t))>window.innerHeight?o.setState({bottom:n+44}):o.setState({bottom:n})}},o.handleScroll=w(o.computeOffsetBottom,200),o}return function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}(r,n),r.prototype.componentDidMount=function(){this.isSafariMobile&&(window.addEventListener("scroll",this.handleScroll),this.deferredComputeOffsetBottom=setTimeout(this.computeOffsetBottom))},r.prototype.componentWillUnmount=function(){this.isSafariMobile&&(this.handleScroll.cancel(),window.removeEventListener("scroll",this.handleScroll),window.clearTimeout(this.deferredComputeOffsetBottom))},r.prototype.render=function(){var e=this.state.bottom,n=this.props,r=n.children,i=n.offset,f=t.cloneElement(r,{style:o({},r.props.style,{bottom:e,position:"fixed"})});return t.createElement(t.Fragment,null,f,this.isSafariMobile&&t.createElement("div",{ref:this.anchorRef,style:{position:"fixed",bottom:i}}))},r}(t.PureComponent);export{g as FixedBottom}; //# sourceMappingURL=index.esm.js.map