1
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
export default function useDelay(callback: VoidFunction): () => void;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import * as React from 'react';
|
||||
import { useEvent } from '@rc-component/util';
|
||||
import raf from "@rc-component/util/es/raf";
|
||||
export default function useDelay(callback) {
|
||||
const idRef = React.useRef(0);
|
||||
const clearRaf = () => {
|
||||
raf.cancel(idRef.current);
|
||||
};
|
||||
React.useEffect(() => clearRaf, []);
|
||||
const triggerFn = useEvent(() => {
|
||||
clearRaf();
|
||||
idRef.current = raf(callback);
|
||||
});
|
||||
return triggerFn;
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import type { Key } from 'react';
|
||||
export type ItemHeightData = [key: Key, height: number, column?: number];
|
||||
export type ItemPositions = Map<Key, {
|
||||
column: number;
|
||||
top: number;
|
||||
}>;
|
||||
/**
|
||||
* Auto arrange the items in the masonry layout.
|
||||
* Always get stable positions by order
|
||||
* instead of dynamic adjust for next item height.
|
||||
*/
|
||||
export default function usePositions(itemHeights: ItemHeightData[], columnCount: number, verticalGutter: number): readonly [ItemPositions, number];
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
||||
// Disabled the rule since `fill` is safe here
|
||||
// but `Array.from` will increase bundle size.
|
||||
/* eslint-disable unicorn/no-new-array */
|
||||
import * as React from 'react';
|
||||
/**
|
||||
* Auto arrange the items in the masonry layout.
|
||||
* Always get stable positions by order
|
||||
* instead of dynamic adjust for next item height.
|
||||
*/
|
||||
export default function usePositions(itemHeights, columnCount, verticalGutter) {
|
||||
// ==================== Auto Order ====================
|
||||
const [orderItemPositions, orderTotalHeight] = React.useMemo(() => {
|
||||
const columnHeights = new Array(columnCount).fill(0);
|
||||
const itemPositions = new Map();
|
||||
for (let i = 0; i < itemHeights.length; i += 1) {
|
||||
const [itemKey, itemHeight, itemColumn] = itemHeights[i];
|
||||
let targetColumnIndex = itemColumn ?? columnHeights.indexOf(Math.min.apply(Math, _toConsumableArray(columnHeights)));
|
||||
targetColumnIndex = Math.min(targetColumnIndex, columnCount - 1);
|
||||
const top = columnHeights[targetColumnIndex];
|
||||
itemPositions.set(itemKey, {
|
||||
column: targetColumnIndex,
|
||||
top
|
||||
});
|
||||
columnHeights[targetColumnIndex] += itemHeight + verticalGutter;
|
||||
}
|
||||
return [itemPositions, Math.max(0, Math.max.apply(Math, _toConsumableArray(columnHeights)) - verticalGutter)];
|
||||
}, [columnCount, itemHeights, verticalGutter]);
|
||||
// ====================== Return ======================
|
||||
return [orderItemPositions, orderTotalHeight];
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import * as React from 'react';
|
||||
export default function useRefs(): readonly [(key: React.Key, element: HTMLDivElement | null) => void, (key: React.Key) => HTMLDivElement | null | undefined];
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import * as React from 'react';
|
||||
export default function useRefs() {
|
||||
const ref = React.useRef(null);
|
||||
if (ref.current === null) {
|
||||
ref.current = new Map();
|
||||
}
|
||||
const setRef = (key, element) => {
|
||||
ref.current.set(key, element);
|
||||
};
|
||||
const getRef = key => ref.current.get(key);
|
||||
return [setRef, getRef];
|
||||
}
|
||||
Reference in New Issue
Block a user