Skip to content

Commit

Permalink
解决cls问题
Browse files Browse the repository at this point in the history
  • Loading branch information
master1lan committed Dec 27, 2022
1 parent 852f110 commit 76b5047
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 56 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^7.6.0",
"@types/imagesloaded": "^4.1.2",
"@types/node": "^18.11.18",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@vitejs/plugin-react-swc": "^3.0.0",
Expand Down
13 changes: 10 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 8 additions & 33 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
import React, { useEffect, useRef } from "react";
import { Masonry } from "masonic";
import { useFakerImages } from "./utils/faker/index";
// import Masonry from "@components/masonry";

import Image from "@components/image";

export default function App() {
const lists = useFakerImages(100);
// return <Masonry />;
const url = `https://images.pexels.com/photos/5702958/pexels-photo-5702958.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1`;

return (
<div
style={{
display: "flex",
justifyContent: "center",
}}
>
<div className='feedContainer'>
<Masonry
items={lists}
columnWidth={180}
rowGutter={10}
columnGutter={10}
maxColumnCount={5}
render={FakerCard}
/>
</div>
<div>
<Image url={url} />
</div>
);
}

function FakerCard({ data: { id, image, name } }) {
return (
<section key={id} className='element-item'>
<img src={image} alt='' />
<div className='footer'>
<p>
<span>{name}</span>
</p>
</div>
</section>
);
}
23 changes: 23 additions & 0 deletions src/components/image/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect, useState } from "react";
import { getImageSize } from "./tool";
export default function Image({ url }) {
const [loading, setLoading] = useState<boolean>(true);
const [obj, setObj] = useState({});
useEffect(() => {
getImageSize(url).then((data) => {
setLoading(() => false);
setObj(() => ({
width: data.width,
height: data.height,
}));
});
}, [url]);
if (loading) {
return <div>loading</div>;
}
return (
<div>
<img width={obj.width} height={obj.height} src={url} />
</div>
);
}
22 changes: 22 additions & 0 deletions src/components/image/tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Once } from "@utils/index";
import { resolve } from "path";
/**
* image组件的工具库
*/
export function getImageSize(imageSrc: string) {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = imageSrc;
if (image.complete) {
resolve({
width: image.width,
height: image.height,
});
}
image.onload = () =>
resolve({
width: image.width,
height: image.height,
});
});
}
38 changes: 38 additions & 0 deletions src/components/masonry/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Masonry } from "masonic";
import { useFakerImages } from "@utils/faker/index";
export default function App() {
const lists = useFakerImages(100);

return (
<div
style={{
display: "flex",
justifyContent: "center",
}}
>
<div className='feedContainer'>
<Masonry
items={lists}
columnWidth={180}
rowGutter={10}
columnGutter={10}
maxColumnCount={5}
render={FakerCard}
/>
</div>
</div>
);
}

function FakerCard({ data: { id, image, name } }) {
return (
<section key={id} className='element-item'>
<img src={image} alt='' loading='lazy' />
<div className='footer'>
<p>
<span>{name}</span>
</p>
</div>
</section>
);
}
6 changes: 3 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import App from "./App";
import "./index.less";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
// <React.StrictMode>
<App />
// </React.StrictMode>
);
9 changes: 9 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function Once(fn) {
let tag = fn;
return function () {
if (tag) {
tag.apply(this, arguments);
tag = null;
}
};
}
15 changes: 10 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"target": "ES5",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
Expand All @@ -14,8 +14,13 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
"jsx": "react-jsx",
"baseUrl": ".",
"paths": {
"@utils/*": ["src/utils/*"],
"@components/*": ["src/components/*"],
"@store/*": ["src/store/*"]
}
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
"include": ["src"]
}
9 changes: 0 additions & 9 deletions tsconfig.node.json

This file was deleted.

14 changes: 11 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { defineConfig } from "vite";
import path from "path";
import react from "@vitejs/plugin-react-swc";

// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
"@utils": path.resolve(__dirname, "src/utils"),
"@components": path.resolve(__dirname, "src/components"),
"@store": path.resolve(__dirname, "src/store"),
},
},
plugins: [react()],
})
});

0 comments on commit 76b5047

Please sign in to comment.