@@ -38,11 +38,28 @@ import {
3838 ISelectedItem
3939} from './types' ;
4040
41+ /**
42+ * Returns the document object from the window or document prop. To maintain SSR compatibility, use within useEffect hooks to reference correct document object.
43+ *
44+ * @param win
45+ * @param doc
46+ * @returns Document
47+ */
48+ function getDocument ( win ?: IUseMenuProps [ 'window' ] , doc ?: IUseMenuProps [ 'document' ] ) {
49+ let _document : IUseMenuProps [ 'document' ] ;
50+
51+ if ( doc ) {
52+ _document = doc ;
53+ } else {
54+ _document = win ? win . document : window . document ;
55+ }
56+ return _document ;
57+ }
58+
4159export const useMenu = < T extends HTMLElement = HTMLElement , M extends HTMLElement = HTMLElement > ( {
4260 defaultExpanded = false ,
4361 defaultFocusedValue,
4462 document : documentProp ,
45- environment,
4663 focusedValue,
4764 idPrefix,
4865 isExpanded,
@@ -61,10 +78,6 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
6178 const isSelectedItemsControlled = selectedItems !== undefined ;
6279 const isFocusedValueControlled = focusedValue !== undefined ;
6380
64- const _window = windowProp || environment ;
65- let _document : Document | ShadowRoot = _window ? _window . document : window . document ;
66- _document = documentProp ? documentProp : _document ;
67-
6881 const menuItems = useMemo (
6982 ( ) =>
7083 rawItems . reduce ( ( items , item : MenuItem ) => {
@@ -406,6 +419,7 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
406419 const handleBlur = useCallback (
407420 ( event : React . FocusEvent ) => {
408421 setTimeout ( ( ) => {
422+ const _document = getDocument ( windowProp , documentProp ) ;
409423 // Timeout is required to ensure blur is handled after focus
410424 const activeElement = _document . activeElement ;
411425 const isMenuOrTriggerFocused =
@@ -426,12 +440,13 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
426440 } ) ;
427441 } ,
428442 [
429- _document . activeElement ,
430443 closeMenu ,
431444 controlledIsExpanded ,
445+ documentProp ,
432446 menuRef ,
433447 returnFocusToTrigger ,
434- triggerRef
448+ triggerRef ,
449+ windowProp
435450 ]
436451 ) ;
437452
@@ -527,7 +542,7 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
527542 event . preventDefault ( ) ;
528543
529544 if ( item . href ) {
530- triggerLink ( event . target as HTMLAnchorElement , _window || window ) ;
545+ triggerLink ( event . target as HTMLAnchorElement , windowProp || window ) ;
531546 }
532547
533548 returnFocusToTrigger ( isTransitionItem ) ;
@@ -599,7 +614,6 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
599614 }
600615 } ,
601616 [
602- _window ,
603617 getNextFocusedValue ,
604618 getSelectedItems ,
605619 isExpandedControlled ,
@@ -608,7 +622,8 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
608622 onChange ,
609623 returnFocusToTrigger ,
610624 rtl ,
611- state . nestedPathIds
625+ state . nestedPathIds ,
626+ windowProp
612627 ]
613628 ) ;
614629
@@ -647,16 +662,18 @@ export const useMenu = <T extends HTMLElement = HTMLElement, M extends HTMLEleme
647662 * Respond to clicks outside the open menu
648663 */
649664 useEffect ( ( ) => {
665+ const _document = getDocument ( windowProp , documentProp ) as Document ;
666+
650667 if ( controlledIsExpanded ) {
651- ( _document as Document ) . addEventListener ( 'keydown' , handleMenuKeyDown , true ) ;
668+ _document . addEventListener ( 'keydown' , handleMenuKeyDown , true ) ;
652669 } else if ( ! controlledIsExpanded ) {
653- ( _document as Document ) . removeEventListener ( 'keydown' , handleMenuKeyDown , true ) ;
670+ _document . removeEventListener ( 'keydown' , handleMenuKeyDown , true ) ;
654671 }
655672
656673 return ( ) => {
657674 ( _document as Document ) . removeEventListener ( 'keydown' , handleMenuKeyDown , true ) ;
658675 } ;
659- } , [ controlledIsExpanded , _document , handleMenuKeyDown ] ) ;
676+ } , [ controlledIsExpanded , documentProp , handleMenuKeyDown , windowProp ] ) ;
660677
661678 /**
662679 * When the menu is opened, this effect sets focus on the current menu item using `focusedValue`
0 commit comments