react-measured
Version:
Core functionality for react-measured
104 lines (103 loc) • 3.49 kB
TypeScript
import React from 'react';
interface BoundingBox {
readonly top: number;
readonly right: number;
readonly bottom: number;
readonly left: number;
readonly width: number;
readonly height: number;
}
declare type BoundingBoxProviderChildrenFunction = (props: BoundingBox) => React.ReactNode;
declare type CheckerTransform<T> = (value: T) => BoundingBox;
declare type CheckerChangeHandler = (boundingBox: BoundingBox | undefined) => void;
interface Checker<T> {
(element: T, onChange: CheckerChangeHandler, transform?: CheckerTransform<BoundingBox>): () => void;
clear: () => void;
}
interface CheckerItem<T> {
element: T;
transform?: CheckerTransform<BoundingBox>;
onChange: CheckerChangeHandler;
}
interface InNextFrame {
(callback: () => void): void;
cancel: () => void;
}
declare const createInNextFrame: () => InNextFrame;
declare const createAnimationFrameChecker: <T>(
check: (items: Set<CheckerItem<T>>, measurements: Map<CheckerItem<T>, BoundingBox>, next: () => void) => void,
) => Checker<T>;
/// <reference types="react" />
declare type UseBoundingBox<T> = (
ref: React.RefObject<T | null | undefined>,
onChange?: CheckerChangeHandler,
transform?: CheckerTransform<BoundingBox>,
) => BoundingBox | undefined;
declare const createUseBoundingBox: <T>(checker: Checker<T>) => UseBoundingBox<T>;
declare const discardPosition: CheckerTransform<BoundingBox>;
declare const discardSize: CheckerTransform<BoundingBox>;
declare const renderChildren: (
children:
| string
| number
| boolean
| {}
| React.ReactElement<
any,
| string
| ((
props: any,
) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null)
| (new (props: any) => React.Component<any, any, any>)
>
| React.ReactNodeArray
| React.ReactPortal
| BoundingBoxProviderChildrenFunction
| null
| undefined,
boundingBox: BoundingBox | undefined,
) => React.ReactNode;
interface MeasuredProps {
children?: BoundingBoxProviderChildrenFunction | React.ReactNode;
positionOnly?: boolean;
sizeOnly?: boolean;
onBoundingBoxChange?: (props: BoundingBox | undefined) => void;
}
declare type Measured<I> = <T extends MeasurableComponentType<I>>(type: T) => MeasuredComponentType<T>;
declare type MeasurableComponentType<I> = I extends HTMLElement
? keyof React.ReactHTML | (React.ComponentType<React.RefAttributes<I> & React.PropsWithChildren<{}>>)
: React.ComponentType<React.RefAttributes<I>>;
declare type MeasuredComponentType<T extends keyof React.ReactHTML | React.ComponentType> = React.FC<
Omit<React.ComponentProps<T>, 'ref'> & MeasuredProps
>;
declare function createMeasured<I>(useBoundingBox: UseBoundingBox<I>): Measured<I>;
declare const areBoundingBoxesEqual: (
boundingBox: BoundingBox,
previousBoundingBox: BoundingBox | undefined,
precision?: number,
) => boolean;
declare type ValueFactory<T> = () => T;
declare function lazyValue<T>(valueFactory: ValueFactory<T>): ValueFactory<T>;
export {
createAnimationFrameChecker,
InNextFrame,
createInNextFrame,
MeasuredProps,
Measured,
MeasurableComponentType,
MeasuredComponentType,
createMeasured,
UseBoundingBox,
createUseBoundingBox,
BoundingBox,
BoundingBoxProviderChildrenFunction,
CheckerTransform,
CheckerChangeHandler,
Checker,
CheckerItem,
areBoundingBoxesEqual,
discardPosition,
discardSize,
lazyValue,
renderChildren,
};