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(dropdown): add horizontal repositioning when space is not available #1822

Merged
Merged
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
35 changes: 31 additions & 4 deletions packages/sage-react/lib/Dropdown/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export const Dropdown = ({
};

const positionElement = () => {
let direction = null;
let directionX = null;
let directionY = null;
const el = wrapperRef.current;
// Elements
const button = el;
Expand All @@ -90,11 +91,13 @@ export const Dropdown = ({
const panelHeight = getHeight(panel);
const enoughSpaceAbove = panelHeight + buttonDimensions.bottom > window.innerHeight;
const enoughSpaceBelow = panelHeight + buttonDimensions.bottom < window.innerHeight;
const enoughSpaceLeft = panelDimensions.width < buttonDimensions.left;
const enoughSpaceRight = panelDimensions.width < window.innerWidth - buttonDimensions.right;

if (!enoughSpaceBelow && enoughSpaceAbove) {
direction = 'above';
directionY = 'above';
} else if (!enoughSpaceAbove && enoughSpaceBelow) {
direction = 'below';
directionY = 'below';
}
const rect = wrapperRef.current.getBoundingClientRect();
const coords = {
Expand All @@ -112,7 +115,7 @@ export const Dropdown = ({
coords.left = 'initial';
}

if (direction === 'above') {
if (directionY === 'above') {
coords.top = ((buttonDimensions.height / 4) + panelDimensions.height) * -1;
coords.left = 'initial';
if (isPinned) {
Expand All @@ -121,6 +124,30 @@ export const Dropdown = ({
}
}

// Check if there is enough space to the left or right
// CHECK ISPINNED
if (!enoughSpaceRight && enoughSpaceLeft) {
directionX = 'left';
} else if (!enoughSpaceLeft && enoughSpaceRight) {
directionX = 'right';
}

if (directionX === 'left') {
coords.left = 'initial';
coords.right = 0;
if (isPinned) {
coords.right = window.innerWidth - buttonDimensions.right + inlineBoxOffset;
}
}

if (directionX === 'right') {
coords.left = 0;
coords.right = 'initial';
if (isPinned) {
coords.left = buttonDimensions.left + inlineBoxOffset;
}
}

setPanelCoords(coords);
};

Expand Down
60 changes: 45 additions & 15 deletions packages/sage-system/lib/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,49 +190,79 @@ Sage.dropdown = (function () {
}

function positionElement(el) {
let direction = null;

let directionX = null;
let directionY = null;

// Elements
const button = el;
const panel = el.lastElementChild
const panel = el.lastElementChild;
const win = panel.ownerDocument.defaultView;
const docEl = window.document.documentElement;

panel.style.top = ''; // resets the style

panel.style.left = ''; // resets the style
panel.style.right = ''; // resets the style

// Dimensions
const buttonDimensions = button.getBoundingClientRect();
const panelDimensions = panel.getBoundingClientRect();

const panelNewLoc = {
top: ( buttonDimensions.height / 2 ) + panelDimensions.height
}

top: (buttonDimensions.height / 2) + panelDimensions.height,
left: (buttonDimensions.width / 2) + panelDimensions.width,
};

const viewport = {
top: docEl.scrollTop,
bottom: window.pageYOffset + docEl.clientHeight,
}

left: docEl.scrollLeft,
right: window.pageXOffset + docEl.clientWidth,
};

const offset = {
top: panelDimensions.top + win.pageYOffset,
left: panelDimensions.left + win.pageXOffset,
bottom: (panelDimensions.top + win.pageYOffset)
bottom: (panelDimensions.top + win.pageYOffset),
right: (panelDimensions.left + win.pageXOffset),
};

const panelHeight = getHeight(panel);
const enoughSpaceAbove = viewport.top < ( offset.top + panelHeight);
const panelWidth = panelDimensions.width;
const enoughSpaceAbove = viewport.top < (offset.top + panelHeight);
const enoughSpaceBelow = viewport.bottom > (offset.bottom + panelHeight);

const enoughSpaceLeft = viewport.left < (offset.left + panelWidth);
const enoughSpaceRight = viewport.right > (offset.right + panelWidth);

// Check if there is enough space to the top or bottom
if (!enoughSpaceBelow && enoughSpaceAbove) {
direction = 'above';
directionY = 'above';
} else if (!enoughSpaceAbove && enoughSpaceBelow) {
direction = 'below';
directionY = 'below';
}

if ( direction == 'above') {
if (directionY === 'above') {
panel.style.top = `-${panelNewLoc.top}px`;
} else if (directionY === 'below') {
// find test case
// panel.style.top = 0;
}

// Check if there is enough space to the left or right
if (!enoughSpaceRight && enoughSpaceLeft) {
directionX = 'left';
} else if (!enoughSpaceLeft && enoughSpaceRight) {
directionX = 'right';
}

if (directionX === 'left') {
panel.style.left = 'inherit';
panel.style.right = 0;
} else if (directionX === 'right') {
panel.style.left = 0;
panel.style.right = 'inherit';
}
}

function open(el) {
el.setAttribute("aria-expanded", "true");
positionElement(el);
Expand Down
Loading