Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions examples/03-ui-components/13-custom-ui/MUISuggestionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,26 @@ function MUISuggestionMenuItem(
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(
`.MuiPaper-root:has([aria-label="suggestion-menu"])`
)!
);
const overflow = elementOverflow(itemRef.current, () => {
if (!itemRef.current) {
return;
}

let container: HTMLElement = itemRef.current;

while (
!container.classList.contains("MuiPaper-root") &&
container.parentElement
) {
container = container.parentElement;
}

if (!container.classList.contains("MuiPaper-root")) {
return;
}

return container;
});

if (overflow === "top") {
itemRef.current.scrollIntoView(true);
Expand Down Expand Up @@ -140,7 +154,7 @@ export function MUISuggestionMenu(
<Paper
sx={{
height: "fit-content",
maxHeight: "100%",
maxHeight: "inherit",
maxWidth: 360,
overflow: "auto",
}}>
Expand Down
5 changes: 3 additions & 2 deletions packages/ariakit/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@

.bn-ariakit .bn-suggestion-menu {
height: fit-content;
max-height: 100%;
max-height: inherit;
overflow-y: auto;
}

.bn-ariakit .bn-color-picker-dropdown {
Expand Down Expand Up @@ -97,7 +98,7 @@
gap: 7px;
height: fit-content;
justify-items: center;
max-height: min(500px, 100%);
max-height: inherit;
overflow-y: auto;
padding: 20px;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/ariakit/src/suggestionMenu/SuggestionMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const SuggestionMenuItem = forwardRef<
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(".bn-suggestion-menu")!
);
const overflow = elementOverflow(itemRef.current);

if (overflow === "top") {
itemRef.current.scrollIntoView(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const GridSuggestionMenuItem = forwardRef<
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(".bn-grid-suggestion-menu")!
);
const overflow = elementOverflow(itemRef.current);

if (overflow === "top") {
itemRef.current.scrollIntoView(true);
Expand Down
5 changes: 1 addition & 4 deletions packages/mantine/src/suggestionMenu/SuggestionMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export const SuggestionMenuItem = forwardRef<
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(".bn-suggestion-menu")!
);
const overflow = elementOverflow(itemRef.current);

if (overflow === "top") {
itemRef.current.scrollIntoView(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export const GridSuggestionMenuItem = forwardRef<
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(".bn-grid-suggestion-menu")!
);
const overflow = elementOverflow(itemRef.current);

if (overflow === "top") {
itemRef.current.scrollIntoView(true);
Expand Down
46 changes: 42 additions & 4 deletions packages/react/src/util/elementOverflow.ts
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") &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just say getContainer = defaultGetContainer in the function signature?

if (!container) {
return "none";
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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"
Expand Down
5 changes: 3 additions & 2 deletions packages/shadcn/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@

.bn-shadcn .bn-suggestion-menu {
height: fit-content;
max-height: 100%;
max-height: inherit;
overflow-y: auto;
}

.bn-shadcn .bn-suggestion-menu-item[aria-selected="true"],
Expand All @@ -123,7 +124,7 @@
gap: 7px;
height: fit-content;
justify-items: center;
max-height: min(500px, 100%);
max-height: inherit;
overflow-y: auto;
padding: 20px;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/shadcn/src/suggestionMenu/SuggestionMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export const SuggestionMenuItem = forwardRef<
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(".bn-suggestion-menu")!
);
const overflow = elementOverflow(itemRef.current);
if (overflow === "top") {
itemRef.current.scrollIntoView(true);
} else if (overflow === "bottom") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const GridSuggestionMenuItem = forwardRef<
return;
}

const overflow = elementOverflow(
itemRef.current,
document.querySelector(".bn-grid-suggestion-menu")!
);
const overflow = elementOverflow(itemRef.current);

if (overflow === "top") {
itemRef.current.scrollIntoView(true);
Expand Down
Loading