Skip to content

Commit

Permalink
Merge pull request #596 from LifeSG/MOL-16856
Browse files Browse the repository at this point in the history
[MOL-16856][CWH] Update dropdown field align to center in mobile scre…
  • Loading branch information
qroll authored Oct 16, 2024
2 parents 0bf5724 + 5706cc3 commit 80517a2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/shared/dropdown-wrapper/element-with-dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
FloatingFocusManager,
FloatingPortal,
Middleware,
OpenChangeReason,
Placement,
autoUpdate,
Expand All @@ -17,6 +18,7 @@ import {
} from "@floating-ui/react";
import { useRef } from "react";
import { useResizeDetector } from "react-resize-detector";
import { MediaWidths } from "../../media";
import { useFloatingChild } from "../../overlay/use-floating-context";
import { DropdownContainer } from "./element-with-dropdown.styles";
import { DropdownAlignmentType } from "./types";
Expand Down Expand Up @@ -77,6 +79,20 @@ export const ElementWithDropdown = ({
targetRef: elementRef,
handleHeight: false,
});
const center: Middleware = {
name: "center",
fn: ({ x, rects }) => {
const noGapInBetween =
x === 0 || x + rects.floating.width === window.innerWidth;
const isMobileScreen = window.innerWidth < MediaWidths.mobileL;
return {
x:
noGapInBetween && isMobileScreen
? (window.innerWidth - rects.floating.width) / 2
: x,
};
},
};
const { refs, floatingStyles, context } = useFloating({
open: isOpen,
onOpenChange: (open, _event, reason) => {
Expand Down Expand Up @@ -110,6 +126,7 @@ export const ElementWithDropdown = ({
});
},
}),
center,
],
});
const parentZIndex = useFloatingChild();
Expand Down

0 comments on commit 80517a2

Please sign in to comment.