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(Drawer,Flyout): alignment #2910

Merged
merged 1 commit into from
Aug 5, 2024
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
13 changes: 10 additions & 3 deletions packages/gamut/src/Drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ export interface DrawerProps extends Omit<BoxProps, 'width'> {
* Whether the drawer should be open.
*/
expanded?: boolean;

/**
* Which edge of the drawer content container is aligned to during the animation.
*/
alignContentContainer?: 'left' | 'right';
}

export const Drawer: React.FC<DrawerProps> = ({
children,
expanded,
alignContentContainer = 'right',
...props
}) => {
const isDesktop = useMedia(`(min-width: ${breakpoints.sm})`);
Expand All @@ -31,18 +37,19 @@ export const Drawer: React.FC<DrawerProps> = ({
bg="background-current"
exit={{ width: 0 }}
initial={{ width: 0 }}
overflowX="hidden"
overflowY="auto"
overflow="clip"
position="relative"
transition={{ duration: timingValues.slow / 1000 }}
{...props}
>
<Box
height="100%"
left="0"
{...{ [alignContentContainer]: 0 }}
position="absolute"
maxWidth="100%"
minWidth={fullWidth}
overflowY="auto"
overflowX="hidden"
>
{children}
</Box>
Expand Down
1 change: 1 addition & 0 deletions packages/gamut/src/Flyout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const Flyout: React.FC<FlyoutProps> = ({
flexDirection={openFrom === 'left' ? 'row' : 'row-reverse'}
position="fixed"
top={0}
alignContentContainer={openFrom === 'left' ? 'right' : 'left'}
{...{ [openFrom]: 0 }}
>
<FlexBox
Expand Down
32 changes: 31 additions & 1 deletion packages/gamut/src/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ const findScrollingParent = ({
) {
return parentElement;
}
return findScrollingParent(parentElement);
return findScrollingParent(parentElement); // parent of this parent is used via prop destructure
}
return null;
};

const findResizingParent = ({
parentElement,
}: HTMLElement): HTMLElement | null => {
if (parentElement) {
const { overflow, overflowY, overflowX } = getComputedStyle(parentElement);
if ([overflow, overflowY, overflowX].some((val) => val === 'clip')) {
return parentElement;
}
return findResizingParent(parentElement); // parent of this parent is used via prop destructure
}
return null;
};
Expand Down Expand Up @@ -92,6 +105,23 @@ export const Popover: React.FC<PopoverProps> = ({
return () => scrollingParent.removeEventListener('scroll', handler);
}, [targetRef]);

useEffect(() => {
// handles movement of target within a clipped container e.g. Drawer
if (!targetRef.current || typeof ResizeObserver === 'undefined') {
return;
}
const resizingParent = findResizingParent(targetRef.current as HTMLElement);
if (!resizingParent?.addEventListener) {
return;
}
const handler = () => {
setTargetRect(targetRef?.current?.getBoundingClientRect());
};
const ro = new ResizeObserver(handler);
ro.observe(resizingParent);
return () => ro.unobserve(resizingParent);
}, [targetRef]);

useEffect(() => {
if (targetRect) {
const inView =
Expand Down
9 changes: 7 additions & 2 deletions packages/styleguide/stories/Atoms/Drawer.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ Our Drawers are [controlled components](https://reactjs.org/docs/forms.html#cont

<Canvas>
<Story name="Drawer">
{() => {
{({ alignContentContainer }) => {
const [expanded, setExpanded] = useState(false);
return (
<FlexBox bg="paleYellow" height="20rem">
<Drawer expanded={expanded}>Drawer content in here!</Drawer>
<Drawer
expanded={expanded}
alignContentContainer={alignContentContainer}
>
Drawer content in here!
</Drawer>
<StrokeButton
onClick={() => setExpanded((previousExpanded) => !previousExpanded)}
>
Expand Down
Loading