npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yh-react-waterfall

v0.1.0

Published

waterfall layout ; 瀑布流布局

Downloads

6

Readme

yh-react-waterfall 瀑布流布局

轻松实现瀑布流布局

简介

  • 瀑布流布局总归会有要用到的时候,于是自己写了个。
  • 一开始我觉得我写的这个应该可以自动获取每个元素宽高,实际写一遍才发现不行,因为如果是图片,外层 div 没定高,则图片未加载时只能获取错误的高度。但是瀑布流必须要继续走,不能等着它加载完,所以仍需要规定每个元素的高度。

快速上手

  • 需要 react 支持 hook 即可。
  • 将 waterfall 包裹 map 的列表即可使用。
  • 必传 column 列数 、 itemWidth 每个元素宽度 、data-height 每个元素高度
import React from "react";
import Waterfall from "./components";

//模拟数据
const imglist = [
	"http://dummyimage.com/200x100",
	"http://dummyimage.com/200x200",
	"http://dummyimage.com/200x100",
	"http://dummyimage.com/200x500",
	"http://dummyimage.com/200x800",
];
let arr: Array<string> = [];
for (let i = 0; i < 100; i++) {
	arr = arr.concat(imglist);
}
///////////////////////
function App() {
	return (
		<Waterfall
			style={{
				border: "1px solid black",
			}}
			column={3}
			//220是200宽,左右padding 10
			itemWidth={220} //瀑布流需要每个宽度相等,高度可以不相等 单位px 如果rem自行换算
		>
			{arr.map((v, i) => {
				const height = parseFloat(v.slice(v.length - 3, v.length));
				return (
					<div
						key={i}
						style={{
							padding: "10px",
							boxSizing: "content-box",
						}}
						//这个是图片高度+上下padding 20  必传项!!!!
						data-height={height + 20} //高度必须固定,因为图片异步加载,会导致div塌缩,从而高度计算错误
					>
						<img src={v} alt=""></img>
					</div>
				);
			})}
		</Waterfall>
	);
}

export default App;

传递参数

interface WaterfallProps {
	//外层容器样式 ,需要自己定外层容器宽高,默认宽高800px
	style?: CSSProperties;
	//外层容器类名
	classnames?: string;
	//列数 必传
	column: number;
	//每个子元素宽度,需要定宽 必传
	itemWidth: number;
	//容器虚拟渲染高度,需要大于容器高度,用于在用户没滚到最底层时加载
	forceHeight?: number;
	//监听滚动函数,参数是强制刷新,使得可以继续对滚动进行判断
	scrollCallback?: (v: React.Dispatch<React.SetStateAction<number>>) => void;
	//拿到外层容器的ref
	wrapperRefCallback?: (v: RefObject<HTMLDivElement>) => void;
}

注意!!

  • 必须为数组的 react 元素才可以工作!目前暂时非数组无法工作。也就是一般使用 map 或者用 Array 包一下。

  • 不满意外层容器宽高,通过 style 进行设置。

  • column * itemWidth 需要 < 外层容器宽