这是一个高性能的无限滚动网格组件,可为 React 提供基于动量的滚动,实现流畅的触摸/鼠标交互。
A high-performance, infinite scrolling grid component for React that provides smooth touch/mouse interactions with momentum-based scrolling. Perfect for displaying large datasets in a grid format with custom cell renderers.
This is the component that powers the interactive grid on thiings.co - A growing collection of 1200+ free 3D icons, generated with AI.
Experience ThiingsGrid in action with interactive examples and copy-paste ready code.
This component is currently part of this repository. To use it in your project:
lib/ThiingsGrid.tsx file to your projectnpm install react react-domimport ThiingsGrid, { type ItemConfig } from './path/to/ThiingsGrid';
const MyCell = ({ gridIndex, position }: ItemConfig) => (
);
const App = () => (
);| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
gridSize |
number |
✅ | - | Size of each grid cell in pixels |
renderItem |
(config: ItemConfig) => ReactNode |
✅ | - | Function to render each grid cell |
className |
string |
❌ | - | CSS class name for the container |
initialPosition |
Position |
❌ | { x: 0, y: 0 } |
Initial scroll position |
The renderItem function receives an ItemConfig object with:
| Property | Type | Description |
|---|---|---|
gridIndex |
number |
Unique index for the grid cell |
position |
Position |
Grid coordinates { x: number, y: number } |
isMoving |
boolean |
Whether the grid is currently being moved/scrolled |
type Position = {
x: number;
y: number;
};import ThiingsGrid, { type ItemConfig } from "./ThiingsGrid";
const SimpleNumberCell = ({ gridIndex }: ItemConfig) => (
);
export const SimpleNumbers = () => (
);const ColorfulCell = ({ gridIndex }: ItemConfig) => {
const colors = [
"bg-red-300",
"bg-green-300",
"bg-blue-300",
"bg-yellow-300",
"bg-pink-300",
"bg-cyan-300",
];
const colorClass = colors[gridIndex % colors.length];
return (
);
};
export const ColorfulGrid = () => (
);const CardCell = ({ gridIndex, position, isMoving }: ItemConfig) => (
);
export const CardLayout = () => (
);Always use absolute positioning within your cell components for optimal performance:
// ✅ Good
const MyCell = ({ gridIndex }: ItemConfig) => (
);
// ❌ Avoid - can cause layout issues
const MyCell = ({ gridIndex }: ItemConfig) => (
);For better performance with complex cells:
const OptimizedCell = React.memo(({ gridIndex, isMoving }: ItemConfig) => {
// Expensive calculations here
const computedValue = useMemo(() => {
return expensiveCalculation(gridIndex);
}, [gridIndex]);
return (
);
});Ensure the ThiingsGrid has a defined container size:
// ✅ Good - explicit container size
// ✅ Good - CSS classes with defined dimensions
The gridIndex is calculated based on the grid position using a custom algorithm that provides unique indices for each cell position.
You can access the current grid position programmatically:
const MyComponent = () => {
const gridRef = useRef(null);
const getCurrentPosition = () => {
if (gridRef.current) {
const position = gridRef.current.publicGetCurrentPosition();
console.log('Current position:', position);
}
};
return (
);
};const useResponsiveGridSize = () => {
const [gridSize, setGridSize] = useState(80);
useEffect(() => {
const handleResize = () => {
const width = window.innerWidth;
if (width window.removeEventListener('resize', handleResize);
}, []);
return gridSize;
};
const ResponsiveGrid = () => {
const gridSize = useResponsiveGridSize();
return (
);
};The component handles:
# Install dependencies
npm install
# Start development server
npm run dev
# Build for production
npm run buildsrc/
├── examples/ # Example implementations
│ ├── SimpleNumbers.tsx
│ ├── ColorfulGrid.tsx
│ ├── EmojiFun.tsx
│ └── CardLayout.tsx
├── App.tsx # Main demo application
├── Playground.tsx # Example viewer
├── SourceCode.tsx # Source code display
└── Sidebar.tsx # Example navigation
lib/
└── ThiingsGrid.tsx # Main componentgit checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)MIT License - see the LICENSE file for details.
暂无开放 Issues,或尚未同步最近议题。