diff --git a/dist/float-sidebar.js b/dist/float-sidebar.js index 036efd1..bdeb22e 100644 --- a/dist/float-sidebar.js +++ b/dist/float-sidebar.js @@ -1,6 +1,6 @@ /*! - * float-sidebar - Lightweight (2kb gzipped), zero-dependency javascript library for making float sidebars based on the finite state machine - * @version v1.2.3 + * float-sidebar - A lightweight (2kb gzipped), zero-dependency javascript library for making a sidebar float + * @version v1.2.4 * @link https://github.com/vursen/FloatSidebar.js * @author Sergey Vinogradov * @license The MIT License (MIT) @@ -110,13 +110,11 @@ return /******/ (function(modules) { // webpackBootstrap __webpack_require__.r(__webpack_exports__); // CONCATENATED MODULE: ./src/fsm-states.js -/* harmony default export */ var fsm_states = ({ - START: 'START', - TOP_FIXED: 'TOP_FIXED', - UNFIXED: 'UNFIXED', - BOTTOM_FIXED: 'BOTTOM_FIXED', - FINISH: 'FINISH' -}); +var START = 'START'; +var TOP_FIXED = 'TOP_FIXED'; +var UNFIXED = 'UNFIXED'; +var BOTTOM_FIXED = 'BOTTOM_FIXED'; +var FINISH = 'FINISH'; // CONCATENATED MODULE: ./src/fsm-actions.js var _states$START$states$; @@ -124,31 +122,31 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope -/* harmony default export */ var fsm_actions = (_states$START$states$ = {}, _defineProperty(_states$START$states$, fsm_states.START, function (_d, _ref) { +/* harmony default export */ var fsm_actions = (_states$START$states$ = {}, _defineProperty(_states$START$states$, START, function (_d, _ref) { var $sideInner = _ref.$sideInner; $sideInner.style.position = 'absolute'; $sideInner.style.top = '0'; $sideInner.style.bottom = 'auto'; -}), _defineProperty(_states$START$states$, fsm_states.TOP_FIXED, function (d, _ref2) { +}), _defineProperty(_states$START$states$, TOP_FIXED, function (d, _ref2) { var $sideInner = _ref2.$sideInner; $sideInner.style.position = 'fixed'; $sideInner.style.top = d.topSpacing + 'px'; $sideInner.style.bottom = 'auto'; -}), _defineProperty(_states$START$states$, fsm_states.UNFIXED, function (d, _ref3) { +}), _defineProperty(_states$START$states$, UNFIXED, function (d, _ref3) { var $sideInner = _ref3.$sideInner; $sideInner.style.position = 'absolute'; $sideInner.style.top = d.sideInnerTop - d.startPoint + 'px'; $sideInner.style.bottom = 'auto'; -}), _defineProperty(_states$START$states$, fsm_states.BOTTOM_FIXED, function (d, _ref4) { +}), _defineProperty(_states$START$states$, BOTTOM_FIXED, function (d, _ref4) { var $sideInner = _ref4.$sideInner; $sideInner.style.position = 'fixed'; $sideInner.style.top = 'auto'; $sideInner.style.bottom = d.bottomSpacing + 'px'; -}), _defineProperty(_states$START$states$, fsm_states.FINISH, function (_d, _ref5) { +}), _defineProperty(_states$START$states$, FINISH, function (_d, _ref5) { var $sideInner = _ref5.$sideInner; $sideInner.style.position = 'absolute'; @@ -162,93 +160,113 @@ function fsm_transitions_defineProperty(obj, key, value) { if (key in obj) { Obj -/* harmony default export */ var fsm_transitions = (fsm_transitions_states$START$states$ = {}, fsm_transitions_defineProperty(fsm_transitions_states$START$states$, fsm_states.START, [{ - to: fsm_states.FINISH, +/* harmony default export */ var fsm_transitions = (fsm_transitions_states$START$states$ = {}, fsm_transitions_defineProperty(fsm_transitions_states$START$states$, START, [{ + to: FINISH, when: function when(d) { - return [d.isSideInnerFitsPath === true, d.viewportTop + d.sideInnerHeight >= d.finishPoint]; + return [d.isSideInnerFitsPath === true, d.viewportTop + d.sideInnerHeight > d.finishPoint]; } }, { - to: fsm_states.BOTTOM_FIXED, + to: BOTTOM_FIXED, when: function when(d) { - return [d.isSideInnerFitsPath === true, d.isSideInnerFitsViewport === false, d.viewportBottom >= d.sideInnerBottom + d.bottomSpacing]; + return [d.isSideInnerFitsPath === true, d.isSideInnerFitsViewport === false, d.viewportBottom > d.sideInnerBottom + d.bottomSpacing]; } }, { - to: fsm_states.TOP_FIXED, + to: TOP_FIXED, when: function when(d) { - return [d.isSideInnerFitsPath === true, d.isSideInnerFitsViewport === true, d.viewportTop >= d.startPoint - d.topSpacing]; + return [d.isSideInnerFitsPath === true, d.isSideInnerFitsViewport === true, d.viewportTop > d.startPoint - d.topSpacing]; } -}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, fsm_states.TOP_FIXED, [{ - to: fsm_states.START, +}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, TOP_FIXED, [{ + to: START, when: function when(d) { return [d.isSideInnerFitsPath === false]; } }, { - to: fsm_states.START, + to: START, when: function when(d) { return [d.viewportTop <= d.startPoint - d.topSpacing]; } }, { - to: fsm_states.FINISH, + to: FINISH, when: function when(d) { - return [d.sideInnerBottom >= d.finishPoint]; + return [d.sideInnerBottom > d.finishPoint]; } }, { - to: fsm_states.UNFIXED, + to: UNFIXED, when: function when(d) { return [d.scrollDirection === 'down', d.isSideInnerFitsViewport === false]; } -}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, fsm_states.UNFIXED, [{ - to: fsm_states.START, +}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, UNFIXED, [{ + to: START, when: function when(d) { return [d.isSideInnerFitsPath === false]; } }, { - to: fsm_states.TOP_FIXED, + to: START, when: function when(d) { - return [d.viewportTop <= d.sideInnerTop - d.topSpacing]; + return [d.viewportTop <= d.startPoint - d.topSpacing]; + } +}, { + to: FINISH, + when: function when(d) { + return [d.viewportTop + d.sideInnerHeight > d.finishPoint]; } }, { - to: fsm_states.TOP_FIXED, + to: TOP_FIXED, + when: function when(d) { + return [d.scrollDirection === 'up', d.viewportTop <= d.sideInnerTop - d.topSpacing]; + } +}, { + to: TOP_FIXED, when: function when(d) { return [d.isSideInnerFitsViewport === true, d.viewportBottom >= d.sideInnerBottom + d.bottomSpacing]; } }, { - to: fsm_states.BOTTOM_FIXED, + to: BOTTOM_FIXED, when: function when(d) { - return [d.isSideInnerFitsViewport === false, d.viewportBottom >= d.sideInnerBottom + d.bottomSpacing]; + return [d.isSideInnerFitsViewport === false, d.viewportBottom > d.sideInnerBottom + d.bottomSpacing]; } -}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, fsm_states.BOTTOM_FIXED, [{ - to: fsm_states.START, +}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, BOTTOM_FIXED, [{ + to: START, when: function when(d) { return [d.isSideInnerFitsPath === false]; } }, { - to: fsm_states.UNFIXED, + to: START, + when: function when(d) { + return [d.isSideInnerFitsPath === true, d.sideInnerTop <= d.startPoint - d.topSpacing]; + } +}, { + to: UNFIXED, when: function when(d) { return [d.scrollDirection === 'up']; } }, { - to: fsm_states.TOP_FIXED, + to: TOP_FIXED, when: function when(d) { return [d.isSideInnerFitsViewport === true]; } }, { - to: fsm_states.FINISH, + to: FINISH, when: function when(d) { - return [d.sideInnerBottom >= d.finishPoint]; + return [d.sideInnerBottom > d.finishPoint]; } -}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, fsm_states.FINISH, [{ - to: fsm_states.START, +}]), fsm_transitions_defineProperty(fsm_transitions_states$START$states$, FINISH, [{ + to: START, when: function when(d) { return [d.isSideInnerFitsPath === false]; } }, { - to: fsm_states.BOTTOM_FIXED, + to: START, + when: function when(d) { + return [d.viewportTop <= d.startPoint - d.topSpacing]; + } +}, { + to: BOTTOM_FIXED, when: function when(d) { - return [d.sideInnerBottom + d.bottomSpacing <= d.finishPoint, d.viewportBottom <= d.finishPoint]; + return [d.sideInnerBottom + d.bottomSpacing <= d.finishPoint, d.viewportBottom < d.finishPoint]; } }, { - to: fsm_states.TOP_FIXED, + to: TOP_FIXED, when: function when(d) { return [d.viewportTop <= d.sideInnerTop - d.topSpacing]; } @@ -374,31 +392,33 @@ function createDimensionObserver(callback, _ref) { }; }; - var tick = requestAnimationFrameThrottle(function () { + var tick = function tick() { var dimensions = computeDimensions(); callback(prevDimensions, dimensions); prevDimensions = dimensions; - }); + }; + + var throttledTick = requestAnimationFrameThrottle(tick); var start = function start() { - $viewport.addEventListener('scroll', tick); - $viewport.addEventListener('resize', tick); + $viewport.addEventListener('scroll', throttledTick); + $viewport.addEventListener('resize', throttledTick); tick(); }; var stop = function stop() { - $viewport.removeEventListener('scroll', tick); - $viewport.removeEventListener('resize', tick); + $viewport.removeEventListener('scroll', throttledTick); + $viewport.removeEventListener('resize', throttledTick); }; return { start: start, stop: stop, tick: tick }; } /* harmony default export */ var dimension_observer = (createDimensionObserver); -// CONCATENATED MODULE: ./src/index.js +// CONCATENATED MODULE: ./src/float-sidebar.js @@ -415,10 +435,12 @@ function FloatSidebar(options) { var topSpacing = options.topSpacing || 0; var bottomSpacing = options.bottomSpacing || 0; + var onStateChange = options.onStateChange; + var fsm = src_fsm({ actions: fsm_actions, transitions: fsm_transitions, - initialState: fsm_states.START + initialState: START }); var dimensionObserver = dimension_observer(function (prevDimensions, dimensions) { @@ -430,6 +452,10 @@ function FloatSidebar(options) { $sideOuter: $sideOuter, $relative: $relative }); + + if (onStateChange) { + onStateChange(transition.to); + } } updateSideOuterHeight(prevDimensions, dimensions); @@ -472,7 +498,7 @@ function FloatSidebar(options) { return { forceUpdate: forceUpdate, destroy: destroy }; } -/* harmony default export */ var src = __webpack_exports__["default"] = (FloatSidebar); +/* harmony default export */ var float_sidebar = __webpack_exports__["default"] = (FloatSidebar); /***/ }) /******/ ])["default"]; diff --git a/dist/float-sidebar.min.js b/dist/float-sidebar.min.js index 937b5d2..8935a11 100644 --- a/dist/float-sidebar.min.js +++ b/dist/float-sidebar.min.js @@ -1,8 +1,8 @@ /*! - * float-sidebar - Lightweight (2kb gzipped), zero-dependency javascript library for making float sidebars based on the finite state machine - * @version v1.2.3 + * float-sidebar - A lightweight (2kb gzipped), zero-dependency javascript library for making a sidebar float + * @version v1.2.4 * @link https://github.com/vursen/FloatSidebar.js * @author Sergey Vinogradov * @license The MIT License (MIT) */ -!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.FloatSidebar=n():t.FloatSidebar=n()}(window,(function(){return function(t){var n={};function e(o){if(n[o])return n[o].exports;var i=n[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:o})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(e.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var i in t)e.d(o,i,function(n){return t[n]}.bind(null,i));return o},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=0)}([function(t,n,e){"use strict";e.r(n);var o,i="START",r="TOP_FIXED",s="UNFIXED",u="BOTTOM_FIXED",a="FINISH";function p(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}var c,f=(p(o={},i,(function(t,n){var e=n.$sideInner;e.style.position="absolute",e.style.top="0",e.style.bottom="auto"})),p(o,r,(function(t,n){var e=n.$sideInner;e.style.position="fixed",e.style.top=t.topSpacing+"px",e.style.bottom="auto"})),p(o,s,(function(t,n){var e=n.$sideInner;e.style.position="absolute",e.style.top=t.sideInnerTop-t.startPoint+"px",e.style.bottom="auto"})),p(o,u,(function(t,n){var e=n.$sideInner;e.style.position="fixed",e.style.top="auto",e.style.bottom=t.bottomSpacing+"px"})),p(o,a,(function(t,n){var e=n.$sideInner;e.style.position="absolute",e.style.top="auto",e.style.bottom="0"})),o);function d(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}var l=(d(c={},i,[{to:a,when:function(t){return[!0===t.isSideInnerFitsPath,t.viewportTop+t.sideInnerHeight>=t.finishPoint]}},{to:u,when:function(t){return[!0===t.isSideInnerFitsPath,!1===t.isSideInnerFitsViewport,t.viewportBottom>=t.sideInnerBottom+t.bottomSpacing]}},{to:r,when:function(t){return[!0===t.isSideInnerFitsPath,!0===t.isSideInnerFitsViewport,t.viewportTop>=t.startPoint-t.topSpacing]}}]),d(c,r,[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:i,when:function(t){return[t.viewportTop<=t.startPoint-t.topSpacing]}},{to:a,when:function(t){return[t.sideInnerBottom>=t.finishPoint]}},{to:s,when:function(t){return["down"===t.scrollDirection,!1===t.isSideInnerFitsViewport]}}]),d(c,s,[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:r,when:function(t){return[t.viewportTop<=t.sideInnerTop-t.topSpacing]}},{to:r,when:function(t){return[!0===t.isSideInnerFitsViewport,t.viewportBottom>=t.sideInnerBottom+t.bottomSpacing]}},{to:u,when:function(t){return[!1===t.isSideInnerFitsViewport,t.viewportBottom>=t.sideInnerBottom+t.bottomSpacing]}}]),d(c,u,[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:s,when:function(t){return["up"===t.scrollDirection]}},{to:r,when:function(t){return[!0===t.isSideInnerFitsViewport]}},{to:a,when:function(t){return[t.sideInnerBottom>=t.finishPoint]}}]),d(c,a,[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:u,when:function(t){return[t.sideInnerBottom+t.bottomSpacing<=t.finishPoint,t.viewportBottom<=t.finishPoint]}},{to:r,when:function(t){return[t.viewportTop<=t.sideInnerTop-t.topSpacing]}}]),c);var h=function(t){var n=t.actions,e=t.transitions,o=t.initialState;return{findTransitionFor:function(){for(var t=arguments.length,n=Array(t),i=0;it?"up":"notChanged"),l=c.top,h=f.bottom,m=h-l,w=p.height+s+u=1&&(e.style.height=n.sideOuterHeight+"px")};return requestAnimationFrame((function(){e.style.willChange="height",o.style.width="inherit",o.style.transform="translateZ(0)",o.style.willChange="transform",p.start()})),{forceUpdate:function(){p.tick()},destroy:function(){p.stop()}}}}]).default})); \ No newline at end of file +!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.FloatSidebar=n():t.FloatSidebar=n()}(window,(function(){return function(t){var n={};function e(o){if(n[o])return n[o].exports;var i=n[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:o})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(e.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var i in t)e.d(o,i,function(n){return t[n]}.bind(null,i));return o},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=0)}([function(t,n,e){"use strict";e.r(n);var o,i="START";function r(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}var s,u=(r(o={},i,(function(t,n){var e=n.$sideInner;e.style.position="absolute",e.style.top="0",e.style.bottom="auto"})),r(o,"TOP_FIXED",(function(t,n){var e=n.$sideInner;e.style.position="fixed",e.style.top=t.topSpacing+"px",e.style.bottom="auto"})),r(o,"UNFIXED",(function(t,n){var e=n.$sideInner;e.style.position="absolute",e.style.top=t.sideInnerTop-t.startPoint+"px",e.style.bottom="auto"})),r(o,"BOTTOM_FIXED",(function(t,n){var e=n.$sideInner;e.style.position="fixed",e.style.top="auto",e.style.bottom=t.bottomSpacing+"px"})),r(o,"FINISH",(function(t,n){var e=n.$sideInner;e.style.position="absolute",e.style.top="auto",e.style.bottom="0"})),o);function a(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}var p=(a(s={},i,[{to:"FINISH",when:function(t){return[!0===t.isSideInnerFitsPath,t.viewportTop+t.sideInnerHeight>t.finishPoint]}},{to:"BOTTOM_FIXED",when:function(t){return[!0===t.isSideInnerFitsPath,!1===t.isSideInnerFitsViewport,t.viewportBottom>t.sideInnerBottom+t.bottomSpacing]}},{to:"TOP_FIXED",when:function(t){return[!0===t.isSideInnerFitsPath,!0===t.isSideInnerFitsViewport,t.viewportTop>t.startPoint-t.topSpacing]}}]),a(s,"TOP_FIXED",[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:i,when:function(t){return[t.viewportTop<=t.startPoint-t.topSpacing]}},{to:"FINISH",when:function(t){return[t.sideInnerBottom>t.finishPoint]}},{to:"UNFIXED",when:function(t){return["down"===t.scrollDirection,!1===t.isSideInnerFitsViewport]}}]),a(s,"UNFIXED",[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:i,when:function(t){return[t.viewportTop<=t.startPoint-t.topSpacing]}},{to:"FINISH",when:function(t){return[t.viewportTop+t.sideInnerHeight>t.finishPoint]}},{to:"TOP_FIXED",when:function(t){return["up"===t.scrollDirection,t.viewportTop<=t.sideInnerTop-t.topSpacing]}},{to:"TOP_FIXED",when:function(t){return[!0===t.isSideInnerFitsViewport,t.viewportBottom>=t.sideInnerBottom+t.bottomSpacing]}},{to:"BOTTOM_FIXED",when:function(t){return[!1===t.isSideInnerFitsViewport,t.viewportBottom>t.sideInnerBottom+t.bottomSpacing]}}]),a(s,"BOTTOM_FIXED",[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:i,when:function(t){return[!0===t.isSideInnerFitsPath,t.sideInnerTop<=t.startPoint-t.topSpacing]}},{to:"UNFIXED",when:function(t){return["up"===t.scrollDirection]}},{to:"TOP_FIXED",when:function(t){return[!0===t.isSideInnerFitsViewport]}},{to:"FINISH",when:function(t){return[t.sideInnerBottom>t.finishPoint]}}]),a(s,"FINISH",[{to:i,when:function(t){return[!1===t.isSideInnerFitsPath]}},{to:i,when:function(t){return[t.viewportTop<=t.startPoint-t.topSpacing]}},{to:"BOTTOM_FIXED",when:function(t){return[t.sideInnerBottom+t.bottomSpacing<=t.finishPoint,t.viewportBottomt?"up":"notChanged"),h=c.top,I=d.bottom,v=I-h,w=p.height+s+u=1&&(e.style.height=n.sideOuterHeight+"px")};return requestAnimationFrame((function(){e.style.willChange="height",o.style.width="inherit",o.style.transform="translateZ(0)",o.style.willChange="transform",h.start()})),{forceUpdate:function(){h.tick()},destroy:function(){h.stop()}}}}]).default})); \ No newline at end of file diff --git a/package.json b/package.json index 3f8b834..7f30c1f 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "url": "https://github.com/vursen/FloatSidebar.js/issues" }, "homepage": "https://github.com/vursen/FloatSidebar.js", - "version": "1.2.3", + "version": "1.2.4", "main": "./dist/float-sidebar.min.js", "module": "./dist/float-sidebar.js", "devDependencies": {