-
Notifications
You must be signed in to change notification settings - Fork 15
feat(menu)!: allow users to specify both window and document env #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
packages/menu/src/useMenu.ts
Outdated
let _document: Document | ShadowRoot = _window ? _window.document : window.document; | ||
_document = documentProp ? documentProp : _document; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let _document: Document | ShadowRoot = _window ? _window.document : window.document; | |
_document = documentProp ? documentProp : _document; | |
let _document: IUseMenuProps['document']; | |
if (documentProp) { | |
_document = documentProp | |
} else { | |
_document = _window ? _window.document : window.document; | |
} |
...for improved readability and type reuse. Repeated ternaries are difficult to read and usually a signal for refactor.
packages/menu/src/useMenu.ts
Outdated
}: IUseMenuProps<T, M>): IUseMenuReturnValue => { | ||
const prefix = `${useId(idPrefix)}-`; | ||
const triggerId = `${prefix}menu-trigger`; | ||
const isExpandedControlled = isExpanded !== undefined; | ||
const isSelectedItemsControlled = selectedItems !== undefined; | ||
const isFocusedValueControlled = focusedValue !== undefined; | ||
|
||
const _window = windowProp || environment; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall there being problems with initializing this outside of the handlers because it potentially references globals
in a way that breaks SSR. The logic should be restored throughout to get the window
and/or document
as needed – probably via a utility function.
packages/menu/src/types.ts
Outdated
/** | ||
* Sets the environment where the menu is rendered | ||
* @deprecated use `window` prop instead. | ||
* */ | ||
environment?: Window; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not deprecate; prefer the immediate feat(menu)!: ...
breaking change in react-containers
const _document = getDocument(windowProp, documentProp) as Document; | ||
|
||
if (controlledIsExpanded) { | ||
win.document.addEventListener('keydown', handleMenuKeyDown, true); | ||
_document.addEventListener('keydown', handleMenuKeyDown, true); | ||
} else if (!controlledIsExpanded) { | ||
win.document.removeEventListener('keydown', handleMenuKeyDown, true); | ||
_document.removeEventListener('keydown', handleMenuKeyDown, true); | ||
} | ||
|
||
return () => { | ||
win.document.removeEventListener('keydown', handleMenuKeyDown, true); | ||
(_document as Document).removeEventListener('keydown', handleMenuKeyDown, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth coercing the getDocument
return value to Document
so you don't need to repeatedly coerce throughout?
Description
This PR updates
useMenu
to allow users to specify bothwindow
anddocument
environment independently.💥 BREAKING CHANGE:
The
environment
prop has been removed and replaced with the optionalwindow
anddocument
props to improve clarity and flexibility.Detail
optional
window anddocument
propswindow
andwindow.document
ifprops.window
and/orprops.document
areundefined
.Checklist
npm start
)💂♂️ includes new unit tests