Skip to content

Commit b50a04c

Browse files
committed
feat(utils): Export additional positioning types
1 parent c83d578 commit b50a04c

File tree

8 files changed

+83
-56
lines changed

8 files changed

+83
-56
lines changed

packages/transition/src/useFixedPositioning.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "react";
88
import { TransitionProps } from "react-transition-group/Transition";
99
import {
10+
BELOW_CENTER_ANCHOR,
1011
FixedPositionOptions,
1112
getFixedPosition,
1213
getViewportSize,
@@ -164,7 +165,7 @@ export function useFixedPositioning({
164165
getOptions,
165166
onResize,
166167
onScroll,
167-
anchor: currentAnchor = {},
168+
anchor = BELOW_CENTER_ANCHOR,
168169
initialX,
169170
initialY,
170171
xMargin = 0,
@@ -192,10 +193,6 @@ export function useFixedPositioning({
192193
return;
193194
}
194195

195-
const anchor = {
196-
x: currentAnchor.x || "center",
197-
y: currentAnchor.y || "below",
198-
};
199196
const overrides =
200197
typeof getOptions === "function" ? getOptions(node) : {};
201198
const opts: FixedPositionOptions = {
@@ -229,23 +226,22 @@ export function useFixedPositioning({
229226
setStyle(style);
230227
},
231228
[
232-
currentAnchor.x,
233-
currentAnchor.y,
234-
disableSwapping,
235-
disableVHBounds,
236-
fixedTo,
229+
element,
237230
getOptions,
238231
initialX,
239232
initialY,
240-
onPositionChange,
241-
preventOverlap,
242-
transformOrigin,
243-
vhMargin,
244-
vwMargin,
245-
width,
246233
xMargin,
234+
vwMargin,
247235
yMargin,
248-
element,
236+
vhMargin,
237+
width,
238+
transformOrigin,
239+
preventOverlap,
240+
disableSwapping,
241+
disableVHBounds,
242+
anchor,
243+
fixedTo,
244+
onPositionChange,
249245
]
250246
);
251247

packages/utils/src/positioning/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PositionAnchor } from "./types";
1+
import type { PositionAnchor } from "./types";
22

33
export const ABOVE_LEFT_ANCHOR: PositionAnchor = {
44
x: "left",

packages/utils/src/positioning/createHorizontalPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getRightCoord,
77
XCoordConfig,
88
} from "./getCoord";
9-
import { FixedPositionOptions, HorizontalPosition } from "./types";
9+
import type { FixedPositionOptions, HorizontalPosition } from "./types";
1010

1111
/**
1212
* @internal

packages/utils/src/positioning/createVerticalPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
YCoordConfig,
88
} from "./getCoord";
99
import { getViewportSize } from "./getViewportSize";
10-
import { FixedPositionOptions, VerticalPosition } from "./types";
10+
import type { FixedPositionOptions, VerticalPosition } from "./types";
1111

1212
/**
1313
* @internal

packages/utils/src/positioning/getElementRect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Coords } from "./types";
1+
import type { Coords } from "./types";
22

33
function applyCoords(coord: number | undefined): string {
44
return typeof coord === "number" ? `${coord}px` : "";

packages/utils/src/positioning/getFixedPosition.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { BELOW_CENTER_ANCHOR } from "./constants";
12
import { createHorizontalPosition } from "./createHorizontalPosition";
23
import { createVerticalPosition } from "./createVerticalPosition";
34
import { findSizingContainer } from "./findSizingContainer";
45
import { getElementRect } from "./getElementRect";
56
import { getTransformOrigin } from "./getTransformOrigin";
67
import { getViewportSize } from "./getViewportSize";
7-
import { FixedPosition, FixedPositionOptions } from "./types";
8+
import type { FixedPosition, FixedPositionOptions } from "./types";
89

910
/**
1011
* One of the most complicated functions in this project that will attempt to
@@ -40,7 +41,7 @@ import { FixedPosition, FixedPositionOptions } from "./types";
4041
export function getFixedPosition({
4142
container,
4243
element,
43-
anchor: propAnchor = {},
44+
anchor = BELOW_CENTER_ANCHOR,
4445
initialX,
4546
initialY,
4647
vwMargin = 16,
@@ -54,10 +55,6 @@ export function getFixedPosition({
5455
disableVHBounds = false,
5556
}: FixedPositionOptions): FixedPosition {
5657
container = findSizingContainer(container);
57-
const anchor = {
58-
x: propAnchor.x || "center",
59-
y: propAnchor.y || "below",
60-
};
6158

6259
if (process.env.NODE_ENV !== "production") {
6360
if (widthType !== "auto" && anchor.x !== "center") {

packages/utils/src/positioning/getTransformOrigin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PositionAnchor } from "./types";
1+
import type { PositionAnchor } from "./types";
22

33
/**
44
* This is a simple util that'll generate a css `transform-origin` string so

packages/utils/src/positioning/types.ts

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export type SimplePosition = "above" | "below" | "left" | "right";
6767
*
6868
* If this is set to `"equal"` or `"min"` and the horizontal anchor is not set
6969
* to `"center"`, an error will be thrown.
70+
*
71+
* @defaultValue `"auto"`
7072
*/
7173
export type PositionWidth = "auto" | "equal" | "min";
7274

@@ -88,54 +90,55 @@ export interface InitialCoords {
8890
initialY?: number;
8991
}
9092

91-
export interface FixedPositionOptions extends InitialCoords {
92-
/**
93-
* The container element that the `element` should be fixed to.
94-
*/
95-
container: HTMLElement | null;
96-
97-
/**
98-
* The element that is fixed to a `container` element.
99-
*/
100-
element: HTMLElement | null;
101-
93+
/** @remarks \@since 4.0.0 */
94+
export interface CalculateFixedPositionOptions extends InitialCoords {
10295
/**
10396
* The configuration to anchor the fixed element to the container element.
97+
*
98+
* @defaultValue `BELOW_CENTER_ANCHOR`
10499
*/
105-
anchor?: Partial<PositionAnchor>;
100+
anchor?: PositionAnchor;
106101

107102
/**
108103
* The viewwidth margin to apply so that the element doesn't need to be
109104
* directly on the screen edge.
105+
*
106+
* @defaultValue `16`
110107
*/
111108
vwMargin?: number;
112109

113110
/**
114111
* The viewwidth margin to apply so that the element doesn't need to be
115112
* directly on the screen edge.
113+
*
114+
* @defaultValue `16`
116115
*/
117116
vhMargin?: number;
118117

119118
/**
120119
* The container width margin to apply so that the element doesn't need to be
121120
* directly on the container's edge.
121+
*
122+
* @defaultValue `0`
122123
*/
123124
xMargin?: number;
124125

125126
/**
126127
* The container height margin to apply so that the element doesn't need to be
127128
* directly on the container's edge
129+
*
130+
* @defaultValue `0`
128131
*/
129132
yMargin?: number;
130133

131-
/**
132-
* @see PositionWidth
133-
*/
134+
/** {@inheritDoc PositionWidth} */
134135
width?: PositionWidth;
135136

136137
/**
137138
* Boolean if the style object should include the `transformOrigin` value
138139
* based on the x and y positions.
140+
*
141+
* @defaultValue `false`
139142
*/
140143
transformOrigin?: boolean;
141144

@@ -144,24 +147,42 @@ export interface FixedPositionOptions extends InitialCoords {
144147
* container element. This is useful for autocomplete menus or other
145148
* components that retain focus on the container element while the fixed
146149
* element becomes visible.
150+
*
151+
* @defaultValue `false`
147152
*/
148153
preventOverlap?: boolean;
149154

150155
/**
151156
* Boolean if the auto-swapping behavior should be disabled. It's normally
152157
* recommended to not disable this since it'll allow elements to appear off
153158
* screen.
159+
*
160+
* @defaultValue `false`
154161
*/
155162
disableSwapping?: boolean;
156163

157164
/**
158165
* Boolean if the fixed positioning should no longer prevent the fixed element
159166
* to be positioned within the viewport. This is nice if you want to allow for
160167
* full page scrolling instead and manually set a max-height on your element.
168+
*
169+
* @defaultValue `false`
161170
*/
162171
disableVHBounds?: boolean;
163172
}
164173

174+
export interface FixedPositionOptions extends CalculateFixedPositionOptions {
175+
/**
176+
* The container element that the `element` should be fixed to.
177+
*/
178+
container: HTMLElement | null;
179+
180+
/**
181+
* The element that is fixed to a `container` element.
182+
*/
183+
element: HTMLElement | null;
184+
}
185+
165186
/**
166187
* This is more of a private interface that is used to help show how fixed
167188
* elements are placed within the viewport. Most of the time the `top` and
@@ -182,6 +203,30 @@ export interface Coords {
182203
minWidth?: number;
183204
}
184205

206+
/**
207+
* The style object that should be applied to the fixed element so it will be
208+
* fixed to the container element. This will be `undefined` if the container
209+
* element doesn't exist within the DOM.
210+
*
211+
* Note: The fixed element styles **will not** contain styles for `z-index` or
212+
* `background-color` so you'll need to add that manually.
213+
*
214+
* @remarks \@since 4.0.0
215+
*/
216+
export interface FixedPositionStyle extends Coords {
217+
/**
218+
* This will be `"fixed"` unless
219+
* {@link CalculateFixedPositionOptions.disableVHBounds} is set to `true`.
220+
*/
221+
position: "fixed" | "absolute";
222+
223+
/**
224+
* This will be `undefined` unless
225+
* {@link CalculateFixedPositionOptions.transformOrigin} is set to `true`
226+
*/
227+
transformOrigin?: string;
228+
}
229+
185230
/**
186231
* Since the position can be "swapped" to help fit the fixed element within the
187232
* viewport, this interface is used to contain the calculated positions as well
@@ -201,17 +246,6 @@ export interface FixedPosition {
201246
* applied.
202247
*/
203248
actualY: VerticalPosition;
204-
205-
/**
206-
* The style object that should be applied to the fixed element so it will be
207-
* fixed to the container element. This will be `undefined` if the container
208-
* element doesn't exist within the DOM.
209-
*
210-
* Note: The fixed element styles **will not** contain styles for `z-index` or
211-
* `background-color` so you'll need to add that manually.
212-
*/
213-
style?: Coords & {
214-
position: "fixed" | "absolute";
215-
transformOrigin?: string;
216-
};
249+
/** {@inheritDoc FixedPositionStyle} */
250+
style?: FixedPositionStyle;
217251
}

0 commit comments

Comments
 (0)