Skip to content

Commit

Permalink
Backport mantinedev#5213
Browse files Browse the repository at this point in the history
  • Loading branch information
ranquild committed Nov 7, 2023
1 parent cdf1179 commit a358fcf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
24 changes: 24 additions & 0 deletions src/mantine-core/src/Popover/Popover.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,30 @@ export function Inline() {
);
}

export function Size() {
const [opened, setState] = useState(false);

return (
<div style={{ padding: 40 }}>
<Popover
opened={opened}
middlewares={{ shift: true, flip: true, size: true }}
onChange={setState}
>
<Popover.Target>
<button type="button" onClick={() => setState((c) => !c)}>
Toggle popover
</button>
</Popover.Target>

<Popover.Dropdown style={{ overflow: 'auto' }}>
<div style={{ width: 100, height: 2000, background: 'pink' }} />
</Popover.Dropdown>
</Popover>
</div>
);
}

export function PopoverEvents() {
const [opened, setState] = useState(false);
const [toggle1, setToggle1] = useState(false);
Expand Down
1 change: 1 addition & 0 deletions src/mantine-core/src/Popover/Popover.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface PopoverMiddlewares {
shift: boolean;
flip: boolean;
inline?: boolean;
size?: boolean;
}
47 changes: 31 additions & 16 deletions src/mantine-core/src/Popover/use-popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Middleware,
inline,
limitShift,
UseFloatingReturn,
} from '@floating-ui/react';
import { FloatingAxesOffsets, FloatingPosition, useFloatingAutoUpdate } from '../Floating';
import { PopoverWidth, PopoverMiddlewares } from './Popover.types';
Expand All @@ -29,7 +30,10 @@ interface UsePopoverOptions {
arrowOffset: number;
}

function getPopoverMiddlewares(options: UsePopoverOptions) {
function getPopoverMiddlewares(
options: UsePopoverOptions,
getFloating: () => UseFloatingReturn<Element>
) {
const middlewares: Middleware[] = [offset(options.offset)];

if (options.middlewares.shift) {
Expand All @@ -46,6 +50,30 @@ function getPopoverMiddlewares(options: UsePopoverOptions) {

middlewares.push(arrow({ element: options.arrowRef, padding: options.arrowOffset }));

if (options.middlewares.size || options.width === 'target') {
middlewares.push(
size({
apply({ rects, availableWidth, availableHeight }) {
const floating = getFloating();
const styles = floating.refs.floating.current?.style ?? {};

if (options.middlewares.size) {
Object.assign(styles, {
maxWidth: `${availableWidth}px`,
maxHeight: `${availableHeight}px`,
});
}

if (options.width === 'target') {
Object.assign(styles, {
width: `${rects.reference.width}px`,
});
}
},
})
);
}

return middlewares;
}

Expand All @@ -72,22 +100,9 @@ export function usePopover(options: UsePopoverOptions) {
}
};

const floating = useFloating({
const floating: UseFloatingReturn<Element> = useFloating({
placement: options.position,
middleware: [
...getPopoverMiddlewares(options),
...(options.width === 'target'
? [
size({
apply({ rects }) {
Object.assign(floating.refs.floating.current?.style ?? {}, {
width: `${rects.reference.width}px`,
});
},
}),
]
: []),
],
middleware: getPopoverMiddlewares(options, () => floating),
});

useFloatingAutoUpdate({
Expand Down

0 comments on commit a358fcf

Please sign in to comment.