@canlooks/can-ui 0.0.38 → 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.
@@ -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({ ref, columnCount, gap, columnGap, rowGap, ...props }: WaterfallProps): import("@emotion/react/jsx-runtime").JSX.Element;
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({ ref, columnCount = { xs: 4 }, gap = { xs: 0 }, columnGap, rowGap, ...props }) {
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 columnCountNum = (0, utils_1.useResponsiveValue)(columnCount);
14
+ const syncColumnCount = (0, utils_1.useSync)((0, utils_1.useResponsiveValue)(columnCount));
15
15
  const container = (0, react_1.useRef)(null);
16
- const items = (0, react_1.useRef)(new Map());
17
- (0, react_1.useLayoutEffect)(() => {
18
- const resizeObserver = new ResizeObserver(() => {
19
- const heights = Array(columnCountNum).fill(0);
20
- for (const [, el] of items.current) {
21
- if (el) {
22
- const order = heights.indexOf(Math.min(...heights));
23
- heights[order] += el.offsetHeight;
24
- el.style.order = order + '';
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
- for (const [, el] of items.current) {
30
- el && resizeObserver.observe(el);
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
- return () => {
33
- resizeObserver.disconnect();
34
- };
35
- }, [props.children, columnCountNum]);
36
- return ((0, jsx_runtime_1.jsx)(transportStyle_1.TransportStyle, { ...props, ref: (0, utils_1.cloneRef)(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) => {
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: r => {
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, useEffect } from 'react';
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({ ref, columnCount, gap, columnGap, rowGap, ...props }: WaterfallProps): import("@emotion/react/jsx-runtime").JSX.Element;
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, useLayoutEffect, useRef } from 'react';
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({ ref, columnCount = { xs: 4 }, gap = { xs: 0 }, columnGap, rowGap, ...props }) {
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 columnCountNum = useResponsiveValue(columnCount);
11
+ const syncColumnCount = useSync(useResponsiveValue(columnCount));
12
12
  const container = useRef(null);
13
- const items = useRef(new Map());
14
- useLayoutEffect(() => {
15
- const resizeObserver = new ResizeObserver(() => {
16
- const heights = Array(columnCountNum).fill(0);
17
- for (const [, el] of items.current) {
18
- if (el) {
19
- const order = heights.indexOf(Math.min(...heights));
20
- heights[order] += el.offsetHeight;
21
- el.style.order = order + '';
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
- for (const [, el] of items.current) {
27
- el && resizeObserver.observe(el);
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
- return () => {
30
- resizeObserver.disconnect();
31
- };
32
- }, [props.children, columnCountNum]);
33
- return (_jsx(TransportStyle, { ...props, ref: cloneRef(ref, container), css: useStyle({ columnCount, columnGap, rowGap }), className: clsx(classes.root, props.className), children: Children.map(props.children, (child, i) => {
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: r => {
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.38",
3
+ "version": "0.0.39",
4
4
  "author": "C.CanLiang <canlooks@gmail.com>",
5
5
  "description": "My ui framework",
6
6
  "license": "MIT",