-
-
Notifications
You must be signed in to change notification settings - Fork 498
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
fix: elementOverflow
code
#1104
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,47 @@ | ||
export function elementOverflow(element: HTMLElement, container: HTMLElement) { | ||
// Default for `getContainer`, meant for use in suggestion menus. Traverses | ||
// ancestors of the passed element until a suggestion menu root is found. | ||
const defaultGetContainer = (element: HTMLElement) => { | ||
let container: HTMLElement = element; | ||
|
||
while ( | ||
!container.classList.contains("bn-suggestion-menu") && | ||
!container.classList.contains("bn-grid-suggestion-menu") && | ||
container.parentElement | ||
) { | ||
container = container.parentElement; | ||
} | ||
|
||
if ( | ||
!container.classList.contains("bn-suggestion-menu") && | ||
!container.classList.contains("bn-grid-suggestion-menu") | ||
) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make explicit with return undefined (instead of empty return statement) |
||
|
||
return container; | ||
}; | ||
|
||
export function elementOverflow( | ||
element: HTMLElement, | ||
getContainer?: () => HTMLElement | undefined | ||
) { | ||
let container = getContainer?.(); | ||
if (!container) { | ||
container = defaultGetContainer(element); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just say |
||
if (!container) { | ||
return "none"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this happen or should it be an error? |
||
|
||
const elementRect = element.getBoundingClientRect(); | ||
const parentRect = container.getBoundingClientRect(); | ||
const containerRect = container.getBoundingClientRect(); | ||
|
||
if (!containerRect) { | ||
return "none"; | ||
} | ||
|
||
const topOverflow = elementRect.top < parentRect.top; | ||
const bottomOverflow = elementRect.bottom > parentRect.bottom; | ||
const topOverflow = elementRect.top < containerRect.top; | ||
const bottomOverflow = elementRect.bottom > containerRect.bottom; | ||
|
||
return topOverflow && bottomOverflow | ||
? "both" | ||
|
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 think you can just use https://developer.mozilla.org/en-US/docs/Web/API/Element/closest