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

[useAnchorPositioning] Add OffsetFunction for sideOffset and alignOffset #1223

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions docs/reference/generated/menu-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Additional offset along the alignment axis of the element."
},
Expand All @@ -16,9 +16,9 @@
"description": "Which side of the anchor element to align the popup against.\nMay automatically change to avoid collisions."
},
"sideOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Distance between the anchor and the popup."
"description": "Distance between the anchor and the popup in pixels.\nAlso accepts a function that returns a number to read the dimensions of the anchor and popup,\nalong with its side and alignment.\n\n- `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.\n- `data.popup`: the dimensions of the popup element with properties `width` and `height`.\n- `data.side`: which side of the anchor element the popup is aligned against.\n- `data.align`: how the popup is aligned relative to the specified side."
},
"arrowPadding": {
"type": "number",
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/generated/popover-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Additional offset along the alignment axis of the element."
},
Expand All @@ -18,9 +18,9 @@
"description": "Which side of the anchor element to align the popup against.\nMay automatically change to avoid collisions."
},
"sideOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Distance between the anchor and the popup."
"description": "Distance between the anchor and the popup in pixels.\nAlso accepts a function that returns a number to read the dimensions of the anchor and popup,\nalong with its side and alignment.\n\n- `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.\n- `data.popup`: the dimensions of the popup element with properties `width` and `height`.\n- `data.side`: which side of the anchor element the popup is aligned against.\n- `data.align`: how the popup is aligned relative to the specified side."
},
"arrowPadding": {
"type": "number",
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/generated/preview-card-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Additional offset along the alignment axis of the element."
},
Expand All @@ -18,9 +18,9 @@
"description": "Which side of the anchor element to align the popup against.\nMay automatically change to avoid collisions."
},
"sideOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Distance between the anchor and the popup."
"description": "Distance between the anchor and the popup in pixels.\nAlso accepts a function that returns a number to read the dimensions of the anchor and popup,\nalong with its side and alignment.\n\n- `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.\n- `data.popup`: the dimensions of the popup element with properties `width` and `height`.\n- `data.side`: which side of the anchor element the popup is aligned against.\n- `data.align`: how the popup is aligned relative to the specified side."
},
"arrowPadding": {
"type": "number",
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/generated/select-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Additional offset along the alignment axis of the element."
},
Expand All @@ -18,9 +18,9 @@
"description": "Which side of the anchor element to align the popup against.\nMay automatically change to avoid collisions."
},
"sideOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Distance between the anchor and the popup."
"description": "Distance between the anchor and the popup in pixels.\nAlso accepts a function that returns a number to read the dimensions of the anchor and popup,\nalong with its side and alignment.\n\n- `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.\n- `data.popup`: the dimensions of the popup element with properties `width` and `height`.\n- `data.side`: which side of the anchor element the popup is aligned against.\n- `data.align`: how the popup is aligned relative to the specified side."
},
"arrowPadding": {
"type": "number",
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/generated/tooltip-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Additional offset along the alignment axis of the element."
},
Expand All @@ -18,9 +18,9 @@
"description": "Which side of the anchor element to align the popup against.\nMay automatically change to avoid collisions."
},
"sideOffset": {
"type": "number",
"type": "number | (data) => number",
"default": "0",
"description": "Distance between the anchor and the popup."
"description": "Distance between the anchor and the popup in pixels.\nAlso accepts a function that returns a number to read the dimensions of the anchor and popup,\nalong with its side and alignment.\n\n- `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.\n- `data.popup`: the dimensions of the popup element with properties `width` and `height`.\n- `data.side`: which side of the anchor element the popup is aligned against.\n- `data.align`: how the popup is aligned relative to the specified side."
Comment on lines +21 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-12-24 at 7 24 35 pm

},
"arrowPadding": {
"type": "number",
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/overrides/common.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"props": {
"alignOffset": {
"type": "number | (data) => number"
},
"anchor": {
"type": "React.Ref | Element | VirtualElement | (() => Element | VirtualElement | null) | null"
},
Expand All @@ -26,6 +29,9 @@
},
"render": {
"type": "React.ReactElement | (props, state) => React.ReactElement"
},
"sideOffset": {
"type": "number | (data) => number"
}
},
"types": {
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/menu/positioner/MenuPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ MenuPositioner.propTypes /* remove-proptypes */ = {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset: PropTypes.number,
alignOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* An element to position the popup against.
* By default, the popup will be positioned against the trigger.
Expand Down Expand Up @@ -258,10 +258,17 @@ MenuPositioner.propTypes /* remove-proptypes */ = {
*/
side: PropTypes.oneOf(['bottom', 'inline-end', 'inline-start', 'left', 'right', 'top']),
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset: PropTypes.number,
sideOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* Whether to maintain the menu in the viewport after
* the anchor element is scrolled out of view.
Expand Down
20 changes: 16 additions & 4 deletions packages/react/src/menu/positioner/useMenuPositioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type {
FloatingEvents,
} from '@floating-ui/react';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { type Boundary, type Side, useAnchorPositioning } from '../../utils/useAnchorPositioning';
import {
useAnchorPositioning,
type Boundary,
type OffsetFunction,
type Side,
} from '../../utils/useAnchorPositioning';
import type { GenericHTMLProps } from '../../utils/types';
import { useMenuRootContext } from '../root/MenuRootContext';

Expand Down Expand Up @@ -121,10 +126,17 @@ export namespace useMenuPositioner {
*/
side?: Side;
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset?: number;
sideOffset?: number | OffsetFunction;
/**
* How to align the popup relative to the specified side.
*/
Expand All @@ -133,7 +145,7 @@ export namespace useMenuPositioner {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset?: number;
alignOffset?: number | OffsetFunction;
/**
* An element or a rectangle that delimits the area that the popup is confined to.
* @default 'clipping-ancestors'
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/popover/positioner/PopoverPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ PopoverPositioner.propTypes /* remove-proptypes */ = {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset: PropTypes.number,
alignOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* An element to position the popup against.
* By default, the popup will be positioned against the trigger.
Expand Down Expand Up @@ -202,10 +202,17 @@ PopoverPositioner.propTypes /* remove-proptypes */ = {
*/
side: PropTypes.oneOf(['bottom', 'inline-end', 'inline-start', 'left', 'right', 'top']),
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset: PropTypes.number,
sideOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* Whether to maintain the popup in the viewport after
* the anchor element is scrolled out of view.
Expand Down
20 changes: 16 additions & 4 deletions packages/react/src/popover/positioner/usePopoverPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type {
FloatingRootContext,
} from '@floating-ui/react';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { type Boundary, type Side, useAnchorPositioning } from '../../utils/useAnchorPositioning';
import {
useAnchorPositioning,
type Boundary,
type OffsetFunction,
type Side,
} from '../../utils/useAnchorPositioning';
import type { GenericHTMLProps } from '../../utils/types';
import { InteractionType } from '../../utils/useEnhancedClickHandler';

Expand Down Expand Up @@ -95,10 +100,17 @@ export namespace usePopoverPositioner {
*/
side?: Side;
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset?: number;
sideOffset?: number | OffsetFunction;
/**
* How to align the popup relative to the specified side.
* @default 'center'
Expand All @@ -108,7 +120,7 @@ export namespace usePopoverPositioner {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset?: number;
alignOffset?: number | OffsetFunction;
/**
* An element or a rectangle that delimits the area that the popup is confined to.
* @default 'clipping-ancestors'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ PreviewCardPositioner.propTypes /* remove-proptypes */ = {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset: PropTypes.number,
alignOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* An element to position the popup against.
* By default, the popup will be positioned against the trigger.
Expand Down Expand Up @@ -216,10 +216,17 @@ PreviewCardPositioner.propTypes /* remove-proptypes */ = {
*/
side: PropTypes.oneOf(['bottom', 'inline-end', 'inline-start', 'left', 'right', 'top']),
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset: PropTypes.number,
sideOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* Whether to maintain the popup in the viewport after
* the anchor element is scrolled out of view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type {
FloatingRootContext,
} from '@floating-ui/react';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { Boundary, useAnchorPositioning, type Side } from '../../utils/useAnchorPositioning';
import {
useAnchorPositioning,
type Boundary,
type OffsetFunction,
type Side,
} from '../../utils/useAnchorPositioning';
import type { GenericHTMLProps } from '../../utils/types';
import { usePreviewCardRootContext } from '../root/PreviewCardContext';

Expand Down Expand Up @@ -97,10 +102,17 @@ export namespace usePreviewCardPositioner {
*/
side?: Side;
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset?: number;
sideOffset?: number | OffsetFunction;
/**
* How to align the popup relative to the specified side.
* @default 'center'
Expand All @@ -110,7 +122,7 @@ export namespace usePreviewCardPositioner {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset?: number;
alignOffset?: number | OffsetFunction;
/**
* An element or a rectangle that delimits the area that the popup is confined to.
* @default 'clipping-ancestors'
Expand Down
13 changes: 10 additions & 3 deletions packages/react/src/select/positioner/SelectPositioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ SelectPositioner.propTypes /* remove-proptypes */ = {
* Additional offset along the alignment axis of the element.
* @default 0
*/
alignOffset: PropTypes.number,
alignOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* An element to position the popup against.
* By default, the popup will be positioned against the trigger.
Expand Down Expand Up @@ -238,10 +238,17 @@ SelectPositioner.propTypes /* remove-proptypes */ = {
*/
side: PropTypes.oneOf(['bottom', 'inline-end', 'inline-start', 'left', 'right', 'top']),
/**
* Distance between the anchor and the popup.
* Distance between the anchor and the popup in pixels.
* Also accepts a function that returns a number to read the dimensions of the anchor and popup,
* along with its side and alignment.
*
* - `data.anchor`: the dimensions of the anchor element with properties `width` and `height`.
* - `data.popup`: the dimensions of the popup element with properties `width` and `height`.
* - `data.side`: which side of the anchor element the popup is aligned against.
* - `data.align`: how the popup is aligned relative to the specified side.
* @default 0
*/
sideOffset: PropTypes.number,
sideOffset: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
/**
* Whether to maintain the select menu in the viewport after
* the anchor element is scrolled out of view.
Expand Down
Loading
Loading