UNPKG

4.35 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getTetherAttachments = getTetherAttachments;
7exports.getScrollbarWidth = getScrollbarWidth;
8exports.setScrollbarWidth = setScrollbarWidth;
9exports.isBodyOverflowing = isBodyOverflowing;
10exports.getOriginalBodyPadding = getOriginalBodyPadding;
11exports.conditionallyUpdateScrollbar = conditionallyUpdateScrollbar;
12exports.mapToCssModules = mapToCssModules;
13function getTetherAttachments(placement) {
14 var attachments = {};
15 switch (placement) {
16 case 'top':
17 case 'top center':
18 attachments = {
19 attachment: 'bottom center',
20 targetAttachment: 'top center'
21 };
22 break;
23 case 'bottom':
24 case 'bottom center':
25 attachments = {
26 attachment: 'top center',
27 targetAttachment: 'bottom center'
28 };
29 break;
30 case 'left':
31 case 'left center':
32 attachments = {
33 attachment: 'middle right',
34 targetAttachment: 'middle left'
35 };
36 break;
37 case 'right':
38 case 'right center':
39 attachments = {
40 attachment: 'middle left',
41 targetAttachment: 'middle right'
42 };
43 break;
44 case 'top left':
45 attachments = {
46 attachment: 'bottom left',
47 targetAttachment: 'top left'
48 };
49 break;
50 case 'top right':
51 attachments = {
52 attachment: 'bottom right',
53 targetAttachment: 'top right'
54 };
55 break;
56 case 'bottom left':
57 attachments = {
58 attachment: 'top left',
59 targetAttachment: 'bottom left'
60 };
61 break;
62 case 'bottom right':
63 attachments = {
64 attachment: 'top right',
65 targetAttachment: 'bottom right'
66 };
67 break;
68 case 'right top':
69 attachments = {
70 attachment: 'top left',
71 targetAttachment: 'top right'
72 };
73 break;
74 case 'right bottom':
75 attachments = {
76 attachment: 'bottom left',
77 targetAttachment: 'bottom right'
78 };
79 break;
80 case 'left top':
81 attachments = {
82 attachment: 'top right',
83 targetAttachment: 'top left'
84 };
85 break;
86 case 'left bottom':
87 attachments = {
88 attachment: 'bottom right',
89 targetAttachment: 'bottom left'
90 };
91 break;
92 default:
93 attachments = {
94 attachment: 'top center',
95 targetAttachment: 'bottom center'
96 };
97 }
98
99 return attachments;
100}
101
102var tetherAttachements = exports.tetherAttachements = ['top', 'bottom', 'left', 'right', 'top left', 'top center', 'top right', 'right top', 'right middle', 'right bottom', 'bottom right', 'bottom center', 'bottom left', 'left top', 'left middle', 'left bottom'];
103
104// https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/js/src/modal.js#L436-L443
105function getScrollbarWidth() {
106 var scrollDiv = document.createElement('div');
107 // .modal-scrollbar-measure styles // https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/scss/_modal.scss#L106-L113
108 scrollDiv.style.position = 'absolute';
109 scrollDiv.style.top = '-9999px';
110 scrollDiv.style.width = '50px';
111 scrollDiv.style.height = '50px';
112 scrollDiv.style.overflow = 'scroll';
113 document.body.appendChild(scrollDiv);
114 var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
115 document.body.removeChild(scrollDiv);
116 return scrollbarWidth;
117}
118
119function setScrollbarWidth(padding) {
120 document.body.style.paddingRight = padding > 0 ? padding + 'px' : null;
121}
122
123function isBodyOverflowing() {
124 return document.body.clientWidth < window.innerWidth;
125}
126
127function getOriginalBodyPadding() {
128 return parseInt(window.getComputedStyle(document.body, null).getPropertyValue('padding-right') || 0, 10);
129}
130
131function conditionallyUpdateScrollbar() {
132 var scrollbarWidth = getScrollbarWidth();
133 // https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/js/src/modal.js#L420
134 var fixedContent = document.querySelectorAll('.navbar-fixed-top, .navbar-fixed-bottom, .is-fixed')[0];
135 var bodyPadding = fixedContent ? parseInt(fixedContent.style.paddingRight || 0, 10) : 0;
136
137 if (isBodyOverflowing()) {
138 setScrollbarWidth(bodyPadding + scrollbarWidth);
139 }
140}
141
142function mapToCssModules(className, cssModule) {
143 if (!cssModule) return className;
144 return className.split(' ').map(function (c) {
145 return cssModule[c] || c;
146 }).join(' ');
147}
\No newline at end of file