Skip to content

Commit

Permalink
refactor: improve types and internal codebase consistency (#2948)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Jun 24, 2024
1 parent ad06eb6 commit 39c8556
Show file tree
Hide file tree
Showing 35 changed files with 1,081 additions and 1,101 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-monkeys-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/react": patch
---

fix(useInteractions): split prop getter dependencies so that only the relevant element changes when necessary. Previously `reference` and `floating` prop getters had dependencies grouped.
5 changes: 5 additions & 0 deletions .changeset/brown-ducks-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/react": patch
---

fix(useFocus): prevent SyntheticEvent warning on React <17
5 changes: 5 additions & 0 deletions .changeset/calm-carpets-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/react": patch
---

fix(inner): allow to make Derivable
9 changes: 9 additions & 0 deletions .changeset/weak-apples-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@floating-ui/react-dom': patch
'@floating-ui/react': patch
'@floating-ui/utils': patch
'@floating-ui/core': patch
'@floating-ui/dom': patch
---

refactor: improve types and internal codebase consistency. All documented types are now exported.
9 changes: 2 additions & 7 deletions packages/core/src/computePosition.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {computeCoordsFromPlacement} from './computeCoordsFromPlacement';
import type {
ComputePosition,
ComputePositionReturn,
Middleware,
MiddlewareData,
} from './types';
import type {ComputePosition, Middleware, MiddlewareData} from './types';

/**
* Computes the `x` and `y` coordinates that will place the floating element
Expand All @@ -17,7 +12,7 @@ export const computePosition: ComputePosition = async (
reference,
floating,
config,
): Promise<ComputePositionReturn> => {
) => {
const {
placement = 'bottom',
strategy = 'absolute',
Expand Down
18 changes: 7 additions & 11 deletions packages/core/src/detectOverflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,34 @@ import type {
RootBoundary,
} from './types';

export type DetectOverflowOptions = Partial<{
export interface DetectOverflowOptions {
/**
* The clipping element(s) or area in which overflow will be checked.
* @default 'clippingAncestors'
*/
boundary: Boundary;

boundary?: Boundary;
/**
* The root clipping area in which overflow will be checked.
* @default 'viewport'
*/
rootBoundary: RootBoundary;

rootBoundary?: RootBoundary;
/**
* The element in which overflow is being checked relative to a boundary.
* @default 'floating'
*/
elementContext: ElementContext;

elementContext?: ElementContext;
/**
* Whether to check for overflow using the alternate element's boundary
* (`clippingAncestors` boundary only).
* @default false
*/
altBoundary: boolean;

altBoundary?: boolean;
/**
* Virtual padding for the resolved overflow detection offsets.
* @default 0
*/
padding: Padding;
}>;
padding?: Padding;
}

/**
* Resolves with an object of overflow side offsets that determine how much the
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ export type {
Strategy,
VirtualElement,
} from '@floating-ui/utils';
// This export exists only for backwards compatibility. It will be removed in
// the next major version.
export {rectToClientRect} from '@floating-ui/utils';
1 change: 0 additions & 1 deletion packages/core/src/middleware/arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface ArrowOptions {
* @default undefined
*/
element: any;

/**
* The padding between the arrow element and the floating element edges.
* Useful when the floating element has rounded corners.
Expand Down
55 changes: 25 additions & 30 deletions packages/core/src/middleware/autoPlacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,31 @@ export function getPlacementList(
});
}

export type AutoPlacementOptions = Partial<
DetectOverflowOptions & {
/**
* The axis that runs along the alignment of the floating element. Determines
* whether to check for most space along this axis.
* @default false
*/
crossAxis: boolean;

/**
* Choose placements with a particular alignment.
* @default undefined
*/
alignment: Alignment | null;

/**
* Whether to choose placements with the opposite alignment if the preferred
* alignment does not fit.
* @default true
*/
autoAlignment: boolean;

/**
* Which placements are allowed to be chosen. Placements must be within the
* `alignment` option if explicitly set.
* @default allPlacements (variable)
*/
allowedPlacements: Array<Placement>;
}
>;
export interface AutoPlacementOptions extends DetectOverflowOptions {
/**
* The axis that runs along the alignment of the floating element. Determines
* whether to check for most space along this axis.
* @default false
*/
crossAxis?: boolean;
/**
* Choose placements with a particular alignment.
* @default undefined
*/
alignment?: Alignment | null;
/**
* Whether to choose placements with the opposite alignment if the preferred
* alignment does not fit.
* @default true
*/
autoAlignment?: boolean;
/**
* Which placements are allowed to be chosen. Placements must be within the
* `alignment` option if explicitly set.
* @default allPlacements (variable)
*/
allowedPlacements?: Array<Placement>;
}

/**
* Optimizes the visibility of the floating element by choosing the placement
Expand Down
79 changes: 36 additions & 43 deletions packages/core/src/middleware/flip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,42 @@ import type {DetectOverflowOptions} from '../detectOverflow';
import {detectOverflow} from '../detectOverflow';
import type {Derivable, Middleware} from '../types';

export type FlipOptions = Partial<
DetectOverflowOptions & {
/**
* The axis that runs along the side of the floating element. Determines
* whether overflow along this axis is checked to perform a flip.
* @default true
*/
mainAxis: boolean;

/**
* The axis that runs along the alignment of the floating element. Determines
* whether overflow along this axis is checked to perform a flip.
* @default true
*/
crossAxis: boolean;

/**
* Placements to try sequentially if the preferred `placement` does not fit.
* @default [oppositePlacement] (computed)
*/
fallbackPlacements: Array<Placement>;

/**
* What strategy to use when no placements fit.
* @default 'bestFit'
*/
fallbackStrategy: 'bestFit' | 'initialPlacement';

/**
* Whether to allow fallback to the perpendicular axis of the preferred
* placement, and if so, which side direction along the axis to prefer.
* @default 'none' (disallow fallback)
*/
fallbackAxisSideDirection: 'none' | 'start' | 'end';

/**
* Whether to flip to placements with the opposite alignment if they fit
* better.
* @default true
*/
flipAlignment: boolean;
}
>;
export interface FlipOptions extends DetectOverflowOptions {
/**
* The axis that runs along the side of the floating element. Determines
* whether overflow along this axis is checked to perform a flip.
* @default true
*/
mainAxis?: boolean;
/**
* The axis that runs along the alignment of the floating element. Determines
* whether overflow along this axis is checked to perform a flip.
* @default true
*/
crossAxis?: boolean;
/**
* Placements to try sequentially if the preferred `placement` does not fit.
* @default [oppositePlacement] (computed)
*/
fallbackPlacements?: Array<Placement>;
/**
* What strategy to use when no placements fit.
* @default 'bestFit'
*/
fallbackStrategy?: 'bestFit' | 'initialPlacement';
/**
* Whether to allow fallback to the perpendicular axis of the preferred
* placement, and if so, which side direction along the axis to prefer.
* @default 'none' (disallow fallback)
*/
fallbackAxisSideDirection?: 'none' | 'start' | 'end';
/**
* Whether to flip to placements with the opposite alignment if they fit
* better.
* @default true
*/
flipAlignment?: boolean;
}

/**
* Optimizes the visibility of the floating element by flipping the `placement`
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/middleware/hide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ function isAnySideFullyClipped(overflow: SideObject) {
return sides.some((side) => overflow[side] >= 0);
}

export type HideOptions = Partial<
DetectOverflowOptions & {
/**
* The strategy used to determine when to hide the floating element.
*/
strategy: 'referenceHidden' | 'escaped';
}
>;
export interface HideOptions extends DetectOverflowOptions {
/**
* The strategy used to determine when to hide the floating element.
*/
strategy?: 'referenceHidden' | 'escaped';
}

/**
* Provides data to hide the floating element in applicable situations, such as
Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/middleware/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,23 @@ export function getRectsByLine(rects: Array<ClientRectObject>) {
return groups.map((rect) => rectToClientRect(getBoundingRect(rect)));
}

export type InlineOptions = Partial<{
export interface InlineOptions {
/**
* Viewport-relative `x` coordinate to choose a `ClientRect`.
* @default undefined
*/
x: number;

x?: number;
/**
* Viewport-relative `y` coordinate to choose a `ClientRect`.
* @default undefined
*/
y: number;

y?: number;
/**
* Represents the padding around a disjoined rect when choosing it.
* @default 2
*/
padding: Padding;
}>;
padding?: Padding;
}

/**
* Provides improved positioning for inline reference elements that can span
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/middleware/offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import type {Derivable, Middleware, MiddlewareState} from '../types';

type OffsetValue =
| number
| Partial<{
| {
/**
* The axis that runs along the side of the floating element. Represents
* the distance (gutter or margin) between the reference and floating
* element.
* @default 0
*/
mainAxis: number;
mainAxis?: number;
/**
* The axis that runs along the alignment of the floating element.
* Represents the skidding between the reference and floating element.
* @default 0
*/
crossAxis: number;
crossAxis?: number;
/**
* The same axis as `crossAxis` but applies only to aligned placements
* and inverts the `end` alignment. When set to a number, it overrides the
Expand All @@ -34,8 +34,8 @@ type OffsetValue =
* the reverse.
* @default null
*/
alignmentAxis: number | null;
}>;
alignmentAxis?: number | null;
};

// For type backwards-compatibility, the `OffsetOptions` type was also
// Derivable.
Expand Down
Loading

0 comments on commit 39c8556

Please sign in to comment.