-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
appendToPlugin.js
64 lines (61 loc) · 1.74 KB
/
appendToPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @license
*
* Copyright IBM Corp. 2019
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* @param {object} config Plugin configuration.
* @returns {Plugin} A Flatpickr plugin to put adjust the position of calendar dropdown.
*/
export default (config) => (fp) => {
/**
* Adjusts the floating meun position after Flatpicker sets it.
*/
const handlePreCalendarPosition = () => {
Promise.resolve().then(() => {
const {
calendarContainer,
config: fpConfig,
_positionElement: positionElement,
} = fp;
const { appendTo } = fpConfig;
const {
left: containerLeft,
top: containerTop,
} = appendTo.getBoundingClientRect();
const {
left: refLeft,
bottom: refBottom,
} = positionElement.getBoundingClientRect();
if (
(appendTo !== appendTo.ownerDocument.body ||
containerLeft !== 0 ||
containerTop !== 0) &&
appendTo.ownerDocument.defaultView
.getComputedStyle(appendTo)
.getPropertyValue('position') === 'static'
) {
throw new Error(
'Floating menu container must not have `position:static`.'
);
}
// `2` for negative mergin on calendar dropdown
calendarContainer.style.top = `${refBottom - containerTop + 2}px`;
calendarContainer.style.left = `${refLeft - containerLeft}px`;
});
};
/**
* Registers this Flatpickr plugin.
*/
const register = () => {
fp.loadedPlugins.push('carbonFlatpickrAppendToPlugin');
};
return {
appendTo: config.appendTo,
onReady: register,
onPreCalendarPosition: handlePreCalendarPosition,
};
};