Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: CardGroup refactor #773

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading