forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(DatePicker): fix calendar dropdown position
This change inroduces a Flatpickr plugin that ensures that the calendar dropdown is put in the correct floating position even if the floating menu container is changed via `appendTo` and the floating menu container has significant offset from `<body>`. Fixes carbon-design-system#4818.
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/react/src/components/DatePicker/plugins/appendToPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
}; | ||
}; |