@@ -31,6 +31,7 @@ import { checkInputableElement, RovingTabIndexProvider } from "../../accessibili
31
31
// of doing reusable widgets like dialog boxes & menus where we go and
32
32
// pass in a custom control as the actual body.
33
33
34
+ const WINDOW_PADDING = 10 ;
34
35
const ContextualMenuContainerId = "mx_ContextualMenu_Container" ;
35
36
36
37
function getOrCreateContainer ( ) : HTMLDivElement {
@@ -247,21 +248,49 @@ export default class ContextMenu extends React.PureComponent<IProps, IState> {
247
248
248
249
if ( chevronFace === ChevronFace . Top || chevronFace === ChevronFace . Bottom ) {
249
250
chevronOffset . left = props . chevronOffset ;
250
- } else if ( position . top !== undefined ) {
251
- const target = position . top ;
252
-
253
- // By default, no adjustment is made
254
- let adjusted = target ;
251
+ } else {
252
+ chevronOffset . top = props . chevronOffset ;
253
+ }
255
254
256
- // If we know the dimensions of the context menu, adjust its position
257
- // such that it does not leave the (padded) window.
258
- if ( contextMenuRect ) {
259
- const padding = 10 ;
260
- adjusted = Math . min ( position . top , document . body . clientHeight - contextMenuRect . height - padding ) ;
255
+ // If we know the dimensions of the context menu, adjust its position to
256
+ // keep it within the bounds of the (padded) window
257
+ const { windowWidth, windowHeight } = UIStore . instance ;
258
+ if ( contextMenuRect ) {
259
+ if ( position . top !== undefined ) {
260
+ position . top = Math . min (
261
+ position . top ,
262
+ windowHeight - contextMenuRect . height - WINDOW_PADDING ,
263
+ ) ;
264
+ // Adjust the chevron if necessary
265
+ if ( chevronOffset . top !== undefined ) {
266
+ chevronOffset . top = props . chevronOffset + props . top - position . top ;
267
+ }
268
+ } else if ( position . bottom !== undefined ) {
269
+ position . bottom = Math . min (
270
+ position . bottom ,
271
+ windowHeight - contextMenuRect . height - WINDOW_PADDING ,
272
+ ) ;
273
+ if ( chevronOffset . top !== undefined ) {
274
+ chevronOffset . top = props . chevronOffset + props . bottom - position . bottom ;
275
+ }
276
+ }
277
+ if ( position . left !== undefined ) {
278
+ position . left = Math . min (
279
+ position . left ,
280
+ windowWidth - contextMenuRect . width - WINDOW_PADDING ,
281
+ ) ;
282
+ if ( chevronOffset . left !== undefined ) {
283
+ chevronOffset . left = props . chevronOffset + props . left - position . left ;
284
+ }
285
+ } else if ( position . right !== undefined ) {
286
+ position . right = Math . min (
287
+ position . right ,
288
+ windowWidth - contextMenuRect . width - WINDOW_PADDING ,
289
+ ) ;
290
+ if ( chevronOffset . left !== undefined ) {
291
+ chevronOffset . left = props . chevronOffset + props . right - position . right ;
292
+ }
261
293
}
262
-
263
- position . top = adjusted ;
264
- chevronOffset . top = Math . max ( props . chevronOffset , props . chevronOffset + target - adjusted ) ;
265
294
}
266
295
267
296
let chevron ;
0 commit comments