@canlooks/can-ui 0.0.37 → 0.0.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/components/image/image.js +0 -3
- package/dist/cjs/components/waterfall/waterfall.d.ts +2 -2
- package/dist/cjs/components/waterfall/waterfall.js +30 -25
- package/dist/esm/components/image/image.js +1 -4
- package/dist/esm/components/waterfall/waterfall.d.ts +2 -2
- package/dist/esm/components/waterfall/waterfall.js +32 -27
- package/package.json +2 -2
|
@@ -18,9 +18,6 @@ exports.Image = (0, react_1.memo)(({ src, fallback, onLoad, onError, renderLoadi
|
|
|
18
18
|
onLoad?.(e);
|
|
19
19
|
setLoading(false);
|
|
20
20
|
};
|
|
21
|
-
(0, react_1.useEffect)(() => {
|
|
22
|
-
console.log(52, imgRef.current.complete);
|
|
23
|
-
}, []);
|
|
24
21
|
const [failed, setFailed] = (0, utils_1.useDerivedState)(() => !src, [src]);
|
|
25
22
|
const errorHandler = (e) => {
|
|
26
23
|
imgProps?.onError?.(e);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { ResponsiveProp } from '../../types';
|
|
2
2
|
import { TransportStyleProps } from '../transportStyle';
|
|
3
|
-
export interface WaterfallProps extends TransportStyleProps {
|
|
3
|
+
export interface WaterfallProps extends Omit<TransportStyleProps, 'padding' | 'paddingLeft' | 'paddingRight' | 'paddingTop' | 'paddingBottom'> {
|
|
4
4
|
/** 布局列数,默认为`{xs: 4}` */
|
|
5
5
|
columnCount?: ResponsiveProp;
|
|
6
6
|
gap?: ResponsiveProp;
|
|
7
7
|
columnGap?: ResponsiveProp;
|
|
8
8
|
rowGap?: ResponsiveProp;
|
|
9
9
|
}
|
|
10
|
-
export declare function Waterfall({
|
|
10
|
+
export declare function Waterfall({ columnCount, gap, columnGap, rowGap, ...props }: WaterfallProps): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
@@ -6,39 +6,44 @@ const react_1 = require("react");
|
|
|
6
6
|
const waterfall_style_1 = require("./waterfall.style");
|
|
7
7
|
const utils_1 = require("../../utils");
|
|
8
8
|
const transportStyle_1 = require("../transportStyle");
|
|
9
|
-
function Waterfall({
|
|
9
|
+
function Waterfall({ columnCount = { xs: 4 }, gap = { xs: 0 }, columnGap, rowGap, ...props }) {
|
|
10
10
|
columnCount = (0, utils_1.toResponsiveValue)(columnCount);
|
|
11
11
|
gap = (0, utils_1.toResponsiveValue)(gap);
|
|
12
12
|
columnGap = (0, utils_1.toResponsiveValue)(columnGap) ?? gap;
|
|
13
13
|
rowGap = (0, utils_1.toResponsiveValue)(rowGap) ?? gap;
|
|
14
|
-
const
|
|
14
|
+
const syncColumnCount = (0, utils_1.useSync)((0, utils_1.useResponsiveValue)(columnCount));
|
|
15
15
|
const container = (0, react_1.useRef)(null);
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
16
|
+
const elements = (0, react_1.useRef)([]);
|
|
17
|
+
elements.current = [];
|
|
18
|
+
const computeItemOrder = () => {
|
|
19
|
+
const heights = Array(syncColumnCount.current).fill(0);
|
|
20
|
+
elements.current.forEach(el => {
|
|
21
|
+
if (el) {
|
|
22
|
+
const order = heights.indexOf(Math.min(...heights));
|
|
23
|
+
heights[order] += el.offsetHeight;
|
|
24
|
+
el.style.order = order + '';
|
|
26
25
|
}
|
|
27
|
-
container.current.style.height = Math.max(...heights) + 1 + 'px';
|
|
28
26
|
});
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
container.current.style.height = Math.max(...heights) + 1 + 'px';
|
|
28
|
+
};
|
|
29
|
+
const resizeObserver = (0, react_1.useRef)(void 0);
|
|
30
|
+
resizeObserver.current ||= new ResizeObserver(computeItemOrder);
|
|
31
|
+
const setItemRefAndObserve = (r) => {
|
|
32
|
+
if (r) {
|
|
33
|
+
elements.current.push(r);
|
|
34
|
+
resizeObserver.current.observe(r);
|
|
31
35
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
};
|
|
37
|
+
const isInitialized = (0, react_1.useRef)(false);
|
|
38
|
+
(0, react_1.useEffect)(() => {
|
|
39
|
+
if (!isInitialized.current) {
|
|
40
|
+
isInitialized.current = true;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
computeItemOrder();
|
|
44
|
+
}, [syncColumnCount.current]);
|
|
45
|
+
return ((0, jsx_runtime_1.jsx)(transportStyle_1.TransportStyle, { ...props, ref: (0, utils_1.cloneRef)(props.ref, container), css: (0, waterfall_style_1.useStyle)({ columnCount, columnGap, rowGap }), className: (0, utils_1.clsx)(waterfall_style_1.classes.root, props.className), children: react_1.Children.map(props.children, (child, i) => {
|
|
37
46
|
const key = (0, react_1.isValidElement)(child) ? child.key ?? i : i;
|
|
38
|
-
return ((0, jsx_runtime_1.jsx)("div", { ref:
|
|
39
|
-
r
|
|
40
|
-
? items.current.set(key, r)
|
|
41
|
-
: items.current.delete(key);
|
|
42
|
-
}, className: waterfall_style_1.classes.item, children: child }, key));
|
|
47
|
+
return ((0, jsx_runtime_1.jsx)("div", { ref: setItemRefAndObserve, className: waterfall_style_1.classes.item, children: child }, key));
|
|
43
48
|
}) }));
|
|
44
49
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
|
|
2
|
-
import { memo, useRef
|
|
2
|
+
import { memo, useRef } from 'react';
|
|
3
3
|
import { classes, style } from './image.style';
|
|
4
4
|
import { clsx, cloneRef, useControlled, useDerivedState } from '../../utils';
|
|
5
5
|
import { ImagePreview } from './imagePreview';
|
|
@@ -15,9 +15,6 @@ export const Image = memo(({ src, fallback, onLoad, onError, renderLoading, alt
|
|
|
15
15
|
onLoad?.(e);
|
|
16
16
|
setLoading(false);
|
|
17
17
|
};
|
|
18
|
-
useEffect(() => {
|
|
19
|
-
console.log(52, imgRef.current.complete);
|
|
20
|
-
}, []);
|
|
21
18
|
const [failed, setFailed] = useDerivedState(() => !src, [src]);
|
|
22
19
|
const errorHandler = (e) => {
|
|
23
20
|
imgProps?.onError?.(e);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { ResponsiveProp } from '../../types';
|
|
2
2
|
import { TransportStyleProps } from '../transportStyle';
|
|
3
|
-
export interface WaterfallProps extends TransportStyleProps {
|
|
3
|
+
export interface WaterfallProps extends Omit<TransportStyleProps, 'padding' | 'paddingLeft' | 'paddingRight' | 'paddingTop' | 'paddingBottom'> {
|
|
4
4
|
/** 布局列数,默认为`{xs: 4}` */
|
|
5
5
|
columnCount?: ResponsiveProp;
|
|
6
6
|
gap?: ResponsiveProp;
|
|
7
7
|
columnGap?: ResponsiveProp;
|
|
8
8
|
rowGap?: ResponsiveProp;
|
|
9
9
|
}
|
|
10
|
-
export declare function Waterfall({
|
|
10
|
+
export declare function Waterfall({ columnCount, gap, columnGap, rowGap, ...props }: WaterfallProps): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
@@ -1,41 +1,46 @@
|
|
|
1
1
|
import { jsx as _jsx } from "@emotion/react/jsx-runtime";
|
|
2
|
-
import { Children, isValidElement,
|
|
2
|
+
import { Children, isValidElement, useEffect, useRef } from 'react';
|
|
3
3
|
import { classes, useStyle } from './waterfall.style';
|
|
4
|
-
import { clsx, cloneRef, toResponsiveValue, useResponsiveValue } from '../../utils';
|
|
4
|
+
import { clsx, cloneRef, toResponsiveValue, useResponsiveValue, useSync } from '../../utils';
|
|
5
5
|
import { TransportStyle } from '../transportStyle';
|
|
6
|
-
export function Waterfall({
|
|
6
|
+
export function Waterfall({ columnCount = { xs: 4 }, gap = { xs: 0 }, columnGap, rowGap, ...props }) {
|
|
7
7
|
columnCount = toResponsiveValue(columnCount);
|
|
8
8
|
gap = toResponsiveValue(gap);
|
|
9
9
|
columnGap = toResponsiveValue(columnGap) ?? gap;
|
|
10
10
|
rowGap = toResponsiveValue(rowGap) ?? gap;
|
|
11
|
-
const
|
|
11
|
+
const syncColumnCount = useSync(useResponsiveValue(columnCount));
|
|
12
12
|
const container = useRef(null);
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
13
|
+
const elements = useRef([]);
|
|
14
|
+
elements.current = [];
|
|
15
|
+
const computeItemOrder = () => {
|
|
16
|
+
const heights = Array(syncColumnCount.current).fill(0);
|
|
17
|
+
elements.current.forEach(el => {
|
|
18
|
+
if (el) {
|
|
19
|
+
const order = heights.indexOf(Math.min(...heights));
|
|
20
|
+
heights[order] += el.offsetHeight;
|
|
21
|
+
el.style.order = order + '';
|
|
23
22
|
}
|
|
24
|
-
container.current.style.height = Math.max(...heights) + 1 + 'px';
|
|
25
23
|
});
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
container.current.style.height = Math.max(...heights) + 1 + 'px';
|
|
25
|
+
};
|
|
26
|
+
const resizeObserver = useRef(void 0);
|
|
27
|
+
resizeObserver.current ||= new ResizeObserver(computeItemOrder);
|
|
28
|
+
const setItemRefAndObserve = (r) => {
|
|
29
|
+
if (r) {
|
|
30
|
+
elements.current.push(r);
|
|
31
|
+
resizeObserver.current.observe(r);
|
|
28
32
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
};
|
|
34
|
+
const isInitialized = useRef(false);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!isInitialized.current) {
|
|
37
|
+
isInitialized.current = true;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
computeItemOrder();
|
|
41
|
+
}, [syncColumnCount.current]);
|
|
42
|
+
return (_jsx(TransportStyle, { ...props, ref: cloneRef(props.ref, container), css: useStyle({ columnCount, columnGap, rowGap }), className: clsx(classes.root, props.className), children: Children.map(props.children, (child, i) => {
|
|
34
43
|
const key = isValidElement(child) ? child.key ?? i : i;
|
|
35
|
-
return (_jsx("div", { ref:
|
|
36
|
-
r
|
|
37
|
-
? items.current.set(key, r)
|
|
38
|
-
: items.current.delete(key);
|
|
39
|
-
}, className: classes.item, children: child }, key));
|
|
44
|
+
return (_jsx("div", { ref: setItemRefAndObserve, className: classes.item, children: child }, key));
|
|
40
45
|
}) }));
|
|
41
46
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canlooks/can-ui",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.39",
|
|
4
4
|
"author": "C.CanLiang <canlooks@gmail.com>",
|
|
5
5
|
"description": "My ui framework",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"scripts": {
|
|
42
42
|
"clean": "npx shx rm -rf dist",
|
|
43
43
|
"build": "npm run clean && tscup && npm run build:documentation",
|
|
44
|
-
"build:core": "tscup",
|
|
44
|
+
"build:core": "npm run clean && tscup",
|
|
45
45
|
"build:documentation": "vite build -c documentation/vite.config.mts && tsc -p documentation/tsconfig.bootstrap.json",
|
|
46
46
|
"dev": "vite -c test/vite.config.mts",
|
|
47
47
|
"dev:documentation": "vite -c documentation/vite.config.mts",
|