-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: InView 컴포넌트 polymorphicForwardRef 적용 (#607)
* fix: InView 컴포넌트 개선 * fix: InView 다형성 적용 * docs: polymorphicForwardRef 문서 개선
- Loading branch information
Showing
33 changed files
with
352 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react/src/components/DebounceWrapper/DebounceWrapper.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,64 @@ | ||
import { Slot } from '../Slot'; | ||
import React from 'react'; | ||
import { | ||
useIntersectionObserver, | ||
UseIntersectionObserverProps, | ||
} from '../../hooks/useIntersectionObserver'; | ||
import { polymorphicForwardRef } from '../../utils/polymorphicForwardRef'; | ||
import { useMergeRefs } from '../../hooks/useMergeRefs'; | ||
|
||
interface InViewProps extends UseIntersectionObserverProps { | ||
children: JSX.Element; | ||
children: React.ReactNode; | ||
} | ||
|
||
/** | ||
* @description `InView`는 `IntersectionObserver`를 선언적으로 활용 할 수 있는 컴포넌트입니다. | ||
* | ||
* 관찰 대상이 `viewport`에 노출될 때(`onIntersectStart`) 혹은 나갈 때(`onIntersectEnd`) 특정 action 함수를 호출 할 수 있는 컴포넌트입니다. | ||
* | ||
* `@modern-kit/react`의 `useIntersectionObserver` 훅을 사용하여 구현되었습니다. | ||
* | ||
* @see https://modern-agile-team.github.io/modern-kit/docs/react/hooks/useIntersectionObserver | ||
* | ||
* @param {InViewProps} props - 컴포넌트에 전달되는 속성들입니다. | ||
* @param {JSX.Element} props.children - 관찰할 자식 요소입니다. | ||
* @param {React.ReactNode} props.children - 관찰할 자식 요소입니다. | ||
* @param {boolean} props.asChild - 자식 요소를 그대로 렌더링할지 여부를 나타냅니다. `true`일 경우 자식 요소가 그대로 렌더링되며, 자식 요소가 관찰 대상이됩니다. | ||
* @param {(entry: IntersectionObserverEntry) => void} props.onIntersectStart - 타겟 요소가 viewport 내에 들어갈 때 호출되는 콜백 함수입니다. | ||
* @param {(entry: IntersectionObserverEntry) => void} props.onIntersectEnd - 타겟 요소가 viewport에서 나갈 때 호출되는 콜백 함수입니다. | ||
* @param {number | number[]} props.threshold - 관찰을 시작할 viewport의 가시성 비율입니다. | ||
* @param {Element | Document | null} props.root - 교차할 때 기준이 되는 root 요소입니다. 기본값은 `null`이며 이는 viewport를 의미합니다. | ||
* @param {string} props.rootMargin - 루트 요소에 대한 마진을 지정합니다. 이는 뷰포트 또는 루트 요소의 경계를 확장하거나 축소하는데 사용됩니다. | ||
* @param {boolean} props.enabled - Observer를 활성화할지 여부를 나타냅니다. `false`일 경우 Observer가 작동하지 않습니다. | ||
* @param {boolean} props.calledOnce - 요소가 교차할 때 콜백을 `한 번`만 호출할지 여부를 나타냅니다. | ||
* | ||
* @returns {JSX.Element} | ||
* | ||
* @example | ||
* ```tsx | ||
* // 기본적으로 div로 감싸지며, 해당 div를 관찰 대상으로 설정합니다. | ||
* // 해당 div가 viewport에 노출되거나 숨겨질 때 onIntersectStart/onIntersectEnd 콜백 함수를 호출합니다. | ||
* <InView onIntersectStart={onIntersectStart} onIntersectEnd={onIntersectEnd}> | ||
* <div>Content1</div> | ||
* </InView> | ||
* ``` | ||
* | ||
* @example | ||
* ```tsx | ||
* // as 속성을 통해 특정 요소로 렌더링할 수 있습니다. | ||
* <InView as="ul" onIntersectStart={onIntersectStart} onIntersectEnd={onIntersectEnd}> | ||
* <li>List Item1</li> | ||
* <li>List Item2</li> | ||
* </InView> | ||
* ``` | ||
*/ | ||
export const InView = ({ children, ...props }: InViewProps) => { | ||
const { ref: intersectionObserverRef } = useIntersectionObserver(props); | ||
export const InView = polymorphicForwardRef<'div', InViewProps>( | ||
({ children, as = 'div', ...props }, ref) => { | ||
const Wrapper = as ?? 'div'; | ||
const { ref: intersectionObserverRef } = useIntersectionObserver(props); | ||
|
||
return <Slot ref={intersectionObserverRef}>{children}</Slot>; | ||
}; | ||
return ( | ||
<Wrapper ref={useMergeRefs(ref, intersectionObserverRef)} {...props}> | ||
{children} | ||
</Wrapper> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react/src/components/OutsideClick/OutsideClick.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.