Skip to content

Commit 381202a

Browse files
committed
add modal example for testing
1 parent 84d6817 commit 381202a

7 files changed

Lines changed: 1860 additions & 0 deletions

File tree

examples/modal/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Instructions
2+
3+
1. Run `yarn compile`
4+
2. Open `index.html`

examples/modal/bundle.js

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2+
const bodyScrollLock = require('../../lib/bodyScrollLock.js');
3+
4+
const disableBodyScrollButton = document.querySelector('.disableBodyScroll');
5+
const enableBodyScrollButton = document.querySelector('.enableBodyScroll');
6+
const statusElement = document.querySelector('.bodyScrollLockStatus');
7+
const modalElement = document.querySelector('.modal');
8+
const scrollTargetElement = document.querySelector('.scrollTarget');
9+
10+
disableBodyScrollButton.onclick = function() {
11+
console.info('disableBodyScrollButton');
12+
13+
// show modal
14+
modalElement.classList.add('active');
15+
16+
bodyScrollLock.disableBodyScroll(scrollTargetElement);
17+
18+
statusElement.innerHTML = ' &mdash; Scroll Locked';
19+
statusElement.style.color = 'red';
20+
};
21+
22+
enableBodyScrollButton.onclick = function() {
23+
console.info('enableBodyScrollButton');
24+
25+
// hide modal
26+
modalElement.classList.remove('active');
27+
28+
bodyScrollLock.enableBodyScroll(scrollTargetElement);
29+
30+
statusElement.innerHTML = ' &mdash; Scroll Unlocked';
31+
statusElement.style.color = '';
32+
};
33+
34+
},{"../../lib/bodyScrollLock.js":2}],2:[function(require,module,exports){
35+
(function (global, factory) {
36+
if (typeof define === "function" && define.amd) {
37+
define(['exports'], factory);
38+
} else if (typeof exports !== "undefined") {
39+
factory(exports);
40+
} else {
41+
var mod = {
42+
exports: {}
43+
};
44+
factory(mod.exports);
45+
global.bodyScrollLock = mod.exports;
46+
}
47+
})(this, function (exports) {
48+
'use strict';
49+
50+
Object.defineProperty(exports, "__esModule", {
51+
value: true
52+
});
53+
54+
function _toConsumableArray(arr) {
55+
if (Array.isArray(arr)) {
56+
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
57+
arr2[i] = arr[i];
58+
}
59+
60+
return arr2;
61+
} else {
62+
return Array.from(arr);
63+
}
64+
}
65+
66+
// Older browsers don't support event options, feature detect it.
67+
68+
// Adopted and modified solution from Bohdan Didukh (2017)
69+
// https://stackoverflow.com/questions/41594997/ios-10-safari-prevent-scrolling-behind-a-fixed-overlay-and-maintain-scroll-posi
70+
71+
var hasPassiveEvents = false;
72+
if (typeof window !== 'undefined') {
73+
var passiveTestOptions = {
74+
get passive() {
75+
hasPassiveEvents = true;
76+
return undefined;
77+
}
78+
};
79+
window.addEventListener('testPassive', null, passiveTestOptions);
80+
window.removeEventListener('testPassive', null, passiveTestOptions);
81+
}
82+
83+
var isIosDevice = typeof window !== 'undefined' && window.navigator && window.navigator.platform && (/iP(ad|hone|od)/.test(window.navigator.platform) || window.navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1);
84+
85+
86+
var locks = [];
87+
var documentListenerAdded = false;
88+
var initialClientY = -1;
89+
var previousBodyOverflowSetting = void 0;
90+
var previousBodyPaddingRight = void 0;
91+
92+
// returns true if `el` should be allowed to receive touchmove events.
93+
var allowTouchMove = function allowTouchMove(el) {
94+
return locks.some(function (lock) {
95+
if (lock.options.allowTouchMove && lock.options.allowTouchMove(el)) {
96+
return true;
97+
}
98+
99+
return false;
100+
});
101+
};
102+
103+
var preventDefault = function preventDefault(rawEvent) {
104+
var e = rawEvent || window.event;
105+
106+
// For the case whereby consumers adds a touchmove event listener to document.
107+
// Recall that we do document.addEventListener('touchmove', preventDefault, { passive: false })
108+
// in disableBodyScroll - so if we provide this opportunity to allowTouchMove, then
109+
// the touchmove event on document will break.
110+
if (allowTouchMove(e.target)) {
111+
return true;
112+
}
113+
114+
// Do not prevent if the event has more than one touch (usually meaning this is a multi touch gesture like pinch to zoom).
115+
if (e.touches.length > 1) return true;
116+
117+
if (e.preventDefault) e.preventDefault();
118+
119+
return false;
120+
};
121+
122+
var setOverflowHidden = function setOverflowHidden(options) {
123+
// Setting overflow on body/documentElement synchronously in Desktop Safari slows down
124+
// the responsiveness for some reason. Setting within a setTimeout fixes this.
125+
setTimeout(function () {
126+
// If previousBodyPaddingRight is already set, don't set it again.
127+
if (previousBodyPaddingRight === undefined) {
128+
var _reserveScrollBarGap = !!options && options.reserveScrollBarGap === true;
129+
var scrollBarGap = window.innerWidth - document.documentElement.clientWidth;
130+
131+
if (_reserveScrollBarGap && scrollBarGap > 0) {
132+
previousBodyPaddingRight = document.body.style.paddingRight;
133+
document.body.style.paddingRight = scrollBarGap + 'px';
134+
}
135+
}
136+
137+
// If previousBodyOverflowSetting is already set, don't set it again.
138+
if (previousBodyOverflowSetting === undefined) {
139+
previousBodyOverflowSetting = document.body.style.overflow;
140+
document.body.style.overflow = 'hidden';
141+
}
142+
});
143+
};
144+
145+
var restoreOverflowSetting = function restoreOverflowSetting() {
146+
// Setting overflow on body/documentElement synchronously in Desktop Safari slows down
147+
// the responsiveness for some reason. Setting within a setTimeout fixes this.
148+
setTimeout(function () {
149+
if (previousBodyPaddingRight !== undefined) {
150+
document.body.style.paddingRight = previousBodyPaddingRight;
151+
152+
// Restore previousBodyPaddingRight to undefined so setOverflowHidden knows it
153+
// can be set again.
154+
previousBodyPaddingRight = undefined;
155+
}
156+
157+
if (previousBodyOverflowSetting !== undefined) {
158+
document.body.style.overflow = previousBodyOverflowSetting;
159+
160+
// Restore previousBodyOverflowSetting to undefined
161+
// so setOverflowHidden knows it can be set again.
162+
previousBodyOverflowSetting = undefined;
163+
}
164+
});
165+
};
166+
167+
// https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Problems_and_solutions
168+
var isTargetElementTotallyScrolled = function isTargetElementTotallyScrolled(targetElement) {
169+
return targetElement ? targetElement.scrollHeight - targetElement.scrollTop <= targetElement.clientHeight : false;
170+
};
171+
172+
var handleScroll = function handleScroll(event, targetElement) {
173+
var clientY = event.targetTouches[0].clientY - initialClientY;
174+
175+
if (allowTouchMove(event.target)) {
176+
return false;
177+
}
178+
179+
if (targetElement && targetElement.scrollTop === 0 && clientY > 0) {
180+
// element is at the top of its scroll.
181+
return preventDefault(event);
182+
}
183+
184+
if (isTargetElementTotallyScrolled(targetElement) && clientY < 0) {
185+
// element is at the top of its scroll.
186+
return preventDefault(event);
187+
}
188+
189+
event.stopPropagation();
190+
return true;
191+
};
192+
193+
var disableBodyScroll = exports.disableBodyScroll = function disableBodyScroll(targetElement, options) {
194+
if (isIosDevice) {
195+
// targetElement must be provided, and disableBodyScroll must not have been
196+
// called on this targetElement before.
197+
if (!targetElement) {
198+
// eslint-disable-next-line no-console
199+
console.error('disableBodyScroll unsuccessful - targetElement must be provided when calling disableBodyScroll on IOS devices.');
200+
return;
201+
}
202+
203+
if (targetElement && !locks.some(function (lock) {
204+
return lock.targetElement === targetElement;
205+
})) {
206+
var lock = {
207+
targetElement: targetElement,
208+
options: options || {}
209+
};
210+
211+
locks = [].concat(_toConsumableArray(locks), [lock]);
212+
213+
targetElement.ontouchstart = function (event) {
214+
if (event.targetTouches.length === 1) {
215+
// detect single touch.
216+
initialClientY = event.targetTouches[0].clientY;
217+
}
218+
};
219+
targetElement.ontouchmove = function (event) {
220+
if (event.targetTouches.length === 1) {
221+
// detect single touch.
222+
handleScroll(event, targetElement);
223+
}
224+
};
225+
226+
if (!documentListenerAdded) {
227+
document.addEventListener('touchmove', preventDefault, hasPassiveEvents ? { passive: false } : undefined);
228+
documentListenerAdded = true;
229+
}
230+
}
231+
} else {
232+
setOverflowHidden(options);
233+
var _lock = {
234+
targetElement: targetElement,
235+
options: options || {}
236+
};
237+
238+
locks = [].concat(_toConsumableArray(locks), [_lock]);
239+
}
240+
};
241+
242+
var clearAllBodyScrollLocks = exports.clearAllBodyScrollLocks = function clearAllBodyScrollLocks() {
243+
if (isIosDevice) {
244+
// Clear all locks ontouchstart/ontouchmove handlers, and the references.
245+
locks.forEach(function (lock) {
246+
lock.targetElement.ontouchstart = null;
247+
lock.targetElement.ontouchmove = null;
248+
});
249+
250+
if (documentListenerAdded) {
251+
document.removeEventListener('touchmove', preventDefault, hasPassiveEvents ? { passive: false } : undefined);
252+
documentListenerAdded = false;
253+
}
254+
255+
locks = [];
256+
257+
// Reset initial clientY.
258+
initialClientY = -1;
259+
} else {
260+
restoreOverflowSetting();
261+
locks = [];
262+
}
263+
};
264+
265+
var enableBodyScroll = exports.enableBodyScroll = function enableBodyScroll(targetElement) {
266+
if (isIosDevice) {
267+
if (!targetElement) {
268+
// eslint-disable-next-line no-console
269+
console.error('enableBodyScroll unsuccessful - targetElement must be provided when calling enableBodyScroll on IOS devices.');
270+
return;
271+
}
272+
273+
targetElement.ontouchstart = null;
274+
targetElement.ontouchmove = null;
275+
276+
locks = locks.filter(function (lock) {
277+
return lock.targetElement !== targetElement;
278+
});
279+
280+
if (documentListenerAdded && locks.length === 0) {
281+
document.removeEventListener('touchmove', preventDefault, hasPassiveEvents ? { passive: false } : undefined);
282+
283+
documentListenerAdded = false;
284+
}
285+
} else {
286+
locks = locks.filter(function (lock) {
287+
return lock.targetElement !== targetElement;
288+
});
289+
if (!locks.length) {
290+
restoreOverflowSetting();
291+
}
292+
}
293+
};
294+
});
295+
296+
297+
},{}]},{},[1]);

0 commit comments

Comments
 (0)