0.20.1 • Published 3 years ago
@terra-dev/use-element-intersection v0.20.1
@terra-dev/use-element-intersection
https://anchor-storybook.vercel.app/?path=/story/packages-use-element-intersection--basic
API
export function useElementIntersection({
elementRef,
threshold,
root,
rootMargin,
observeOnce = false,
}: ElementIntersectionParams) {}
export interface ElementIntersectionParams extends IntersectionObserverInit {
elementRef: RefObject<HTMLElement>;
observeOnce?: boolean;
}
Examples
__stories__/use-element-intersection.stories.tsx
import { useElementIntersection } from '@terra-dev/use-element-intersection';
import { useRef } from 'react';
import styled from 'styled-components';
export default {
title: 'packages/use-element-intersection',
};
export const Basic = () => {
const elementRef = useRef<HTMLElement>(null);
const intersection = useElementIntersection({ elementRef, threshold: 0.2 });
return (
<div>
<div style={{ backgroundColor: 'red', height: 1500 }} />
<div
ref={elementRef as any}
style={{ backgroundColor: 'white', height: 500 }}
/>
<div style={{ backgroundColor: 'blue', height: 1500 }} />
<Intersecting>
{intersection?.isIntersecting
? 'White block is visible'
: 'White block is invisible'}
</Intersecting>
</div>
);
};
export const Observe_Once = () => {
const elementRef = useRef<HTMLElement>(null);
const intersection = useElementIntersection({
elementRef,
threshold: 0.5,
observeOnce: true,
});
return (
<div>
<div style={{ backgroundColor: 'red', height: 1500 }} />
<div
ref={elementRef as any}
style={{ backgroundColor: 'white', height: 500 }}
/>
<div style={{ backgroundColor: 'blue', height: 1500 }} />
<Intersecting>
{intersection?.isIntersecting
? 'White block is appeared'
: 'White block is still not appeared'}
</Intersecting>
</div>
);
};
const Intersecting = styled.div`
position: fixed;
top: 20px;
right: 20px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
display: grid;
place-content: center;
border-radius: 15px;
padding: 20px;
`;
0.20.1
3 years ago
0.20.0
3 years ago
0.19.0
3 years ago
0.18.0
3 years ago
0.17.0
3 years ago
0.17.1
3 years ago
0.16.0
3 years ago
0.16.0-alpha.4
3 years ago
0.16.0-alpha.1
3 years ago
0.16.0-alpha.3
3 years ago
0.16.0-alpha.2
3 years ago
0.14.0
3 years ago
0.15.0
3 years ago
0.13.0
3 years ago
0.13.1
3 years ago
0.12.2
3 years ago
0.10.0
3 years ago
0.11.0
3 years ago
0.10.1
3 years ago
0.12.0
3 years ago
0.10.2
3 years ago
0.8.0
3 years ago
0.12.1
3 years ago
0.5.0
3 years ago
0.4.0
3 years ago
0.7.0
3 years ago
0.6.0
3 years ago
0.3.0
3 years ago
0.2.0
3 years ago
0.1.0
3 years ago