Skip to content

Commit

Permalink
Merge pull request #773 from sheinsight/perf-card-group7
Browse files Browse the repository at this point in the history
perf: CardGroup refactor
  • Loading branch information
saint3347 authored Oct 30, 2024
2 parents b1f5b69 + ea37a4e commit 706268a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sheinx",
"private": true,
"version": "3.4.5-beta.7",
"version": "3.4.5-beta.8",
"description": "A react library developed with sheinx",
"module": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
15 changes: 12 additions & 3 deletions packages/base/src/card-group/item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useLayoutEffect, useMemo, useState, } from 'react';
import React, { useContext } from 'react';
import { useInView, usePersistFn } from '@sheinx/hooks';
import classNames from 'classnames';
import { CardGroupContext } from './card-group-context';
Expand All @@ -11,6 +11,12 @@ const Item = <V,>(props: CardGroupItemProps<V>) => {
const { container } = useContext(CardGroupContext);
const classes = props.jssStyle?.cardGroup?.();

const { ref: itemRef, isInView } = useInView<HTMLDivElement>({
rootMargin: `${container?.offsetHeight || 500}px`,
root: container,
threshold:[0, 1]
})

const handleChange = usePersistFn((_: any, checked: boolean) => {
if (props.onChange) props.onChange(checked, props.value!);
});
Expand All @@ -19,7 +25,7 @@ const Item = <V,>(props: CardGroupItemProps<V>) => {
if (!props.placeholder) return content;
if (!container) return content;
return (
<Lazyload container={container} placeholder={props.placeholder}>
<Lazyload container={container} placeholder={props.placeholder} isInView={isInView}>
{content}
</Lazyload>
);
Expand All @@ -43,8 +49,11 @@ const Item = <V,>(props: CardGroupItemProps<V>) => {
</>
);

const hiddenStyle = isInView ? undefined : { visibility: 'hidden' };
const itemStyle = { ...props.style, ...hiddenStyle } as React.CSSProperties;

return (
<div className={cls} style={props.style}>
<div ref={itemRef} className={cls} style={itemStyle}>
{renderChildren(content)}
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/base/src/card-group/lazyload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface LazyloadProps {
placeholder?: React.ReactNode;
container?: HTMLElement;
offset?: number;
isInView?: boolean;
}

const Lazyload = (props: LazyloadProps) => {
Expand All @@ -29,7 +30,7 @@ const Lazyload = (props: LazyloadProps) => {
};
}, []);

if (ready) return <>{props.children}</>;
if (ready && props.isInView) return <>{props.children}</>;
return (
<span ref={placeholderRef} style={{ border: '1px solid transparent' }}>
{props.placeholder}
Expand Down

0 comments on commit 706268a

Please sign in to comment.