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

Popper: Add positioning props #3186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Popover: Allow to disable autoUpdate and set positionStrategy
Vladimir Shushkov authored and Vladimir Shushkov committed Oct 21, 2024
commit ab97c65ed580905a254f06cd11451c5831e3b07e
27 changes: 27 additions & 0 deletions packages/react/popper/src/Popper.stories.tsx
Original file line number Diff line number Diff line change
@@ -445,6 +445,33 @@ export const Chromatic = () => {
};
Chromatic.parameters = { chromatic: { disable: false } };

export const WithAbsolutePositionStrategy = () => {
return (
<div
style={{
padding: '300px',
height: '200vh',
width: '200vw',
}}
>
<Popper.Root>
<Popper.Anchor className={anchorClass({ size: 'small' })}>#1</Popper.Anchor>
<Portal asChild>
<Popper.Content
positionStrategy="absolute"
disablePositionUpdate
className={contentClass({ size: 'small' })}
sideOffset={5}
>
<Popper.Arrow className={arrowClass()} width={10} height={5} />
Some content
</Popper.Content>
</Portal>
</Popper.Root>
</div>
);
};

const Scrollable = (props: any) => (
<div
style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '200vh' }}
22 changes: 14 additions & 8 deletions packages/react/popper/src/Popper.tsx
Original file line number Diff line number Diff line change
@@ -121,7 +121,9 @@ interface PopperContentProps extends PrimitiveDivProps {
collisionPadding?: number | Partial<Record<Side, number>>;
sticky?: 'partial' | 'always';
hideWhenDetached?: boolean;
disablePositionUpdate?: boolean;
updatePositionStrategy?: 'optimized' | 'always';
positionStrategy?: 'fixed' | 'absolute';
onPlaced?: () => void;
}

@@ -139,6 +141,9 @@ const PopperContent = React.forwardRef<PopperContentElement, PopperContentProps>
collisionPadding: collisionPaddingProp = 0,
sticky = 'partial',
hideWhenDetached = false,
// default to `fixed` strategy to avoid focus scroll issues
positionStrategy = 'fixed',
disablePositionUpdate = false,
updatePositionStrategy = 'optimized',
onPlaced,
...contentProps
@@ -172,15 +177,16 @@ const PopperContent = React.forwardRef<PopperContentElement, PopperContentProps>
};

const { refs, floatingStyles, placement, isPositioned, middlewareData } = useFloating({
// default to `fixed` strategy so users don't have to pick and we also avoid focus scroll issues
strategy: 'fixed',
strategy: positionStrategy,
placement: desiredPlacement,
whileElementsMounted: (...args) => {
const cleanup = autoUpdate(...args, {
animationFrame: updatePositionStrategy === 'always',
});
return cleanup;
},
whileElementsMounted: disablePositionUpdate
? undefined
: (...args) => {
const cleanup = autoUpdate(...args, {
animationFrame: updatePositionStrategy === 'always',
});
return cleanup;
},
elements: {
reference: context.anchor,
},