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

add as AppearInViewport component prop #78

Merged
merged 1 commit into from
Sep 7, 2023
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
6 changes: 5 additions & 1 deletion app/gallery/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function GalleryPage() {
<AppearInViewport
className={titleCn}
transition={{delay: 0.5, duration: TransitionDuration.LONG}}
as="h1"
>
SALSAVIVA GALLERY
</AppearInViewport>
Expand All @@ -67,7 +68,10 @@ export default function GalleryPage() {
</div>
<Gallery />
<div className={galleryTextItem}>
<AppearInViewport className={subTitleCn}>
<AppearInViewport
className={subTitleCn}
as="h3"
>
Whant more? Follow us on social media!
</AppearInViewport>
<SocialIcons iconSize="2x" />
Expand Down
23 changes: 14 additions & 9 deletions components/shared/AppearInViewport/AppearInViewport.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
'use client';

import {HTMLMotionProps, Variants, m} from 'framer-motion';
import {ReactNode, forwardRef} from 'react';
import {forwardRef} from 'react';
import TransitionDuration from '@/lib/framerMotion/TransitionDuration';

// eslint-disable-next-line jsdoc/require-jsdoc
type SafeHTMLMotionProps = Omit<
HTMLMotionProps<'div'>,
type As = 'div' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';

// eslint-disable-next-line jsdoc/require-jsdoc
type SafeHTMLMotionProps<T extends As = As> = Omit<
HTMLMotionProps<T>,
'initial' | 'whileInView' | 'viewport' | 'ref'
>;

/**
* Props.
*/
type AppearOnScreenProps = {
children: ReactNode;
} & SafeHTMLMotionProps;
type AppearOnScreenProps<T extends As = As> = {
as?: T;
} & SafeHTMLMotionProps<T>;

const defaultVariants: Variants = {
visible: {opacity: 1},
Expand All @@ -29,11 +32,13 @@ const defaultTransition = {duration: TransitionDuration.MEDIUM, delay: 0.3};
* @returns React component.
*/
const AppearInViewport = forwardRef<HTMLDivElement, AppearOnScreenProps>(function AppearInViewport(
{children, variants, transition, ...rest},
{children, variants, transition, as = 'div', ...rest},
ref,
) {
const Component = m[as];

return (
<m.div
<Component
initial="hidden"
whileInView="visible"
viewport={{once: true}}
Expand All @@ -43,7 +48,7 @@ const AppearInViewport = forwardRef<HTMLDivElement, AppearOnScreenProps>(functio
{...rest}
>
{children}
</m.div>
</Component>
);
});

Expand Down