Skip to content

Commit

Permalink
feat(react-dom): add FadeIn
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli committed Nov 18, 2024
1 parent fbb937b commit c30a173
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/visualization/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<li>
<Link href="/react-dom/InView">{`<InView/>`}</Link>
</li>
<li>
<Link href="/react-dom/FadeIn">{`<FadeIn/>`}</Link>
</li>
</details>
</li>
<li>
Expand Down
25 changes: 25 additions & 0 deletions examples/visualization/src/app/react-dom/FadeIn/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use client'

import { Delay } from '@suspensive/react'
import { FadeIn } from '@suspensive/react-dom'
import { Spinner } from '~/components/uis'

export default function Page() {
return (
<>
<Delay
ms={3000}
fallback={
<FadeIn delay={300} duration={1000}>
<Spinner />
</FadeIn>
}
>
Head:{' '}
<FadeIn as="span" duration={300}>
<h1>hello</h1>
</FadeIn>
</Delay>
</>
)
}
1 change: 0 additions & 1 deletion examples/visualization/src/app/react-dom/InView/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default function Page() {
return (
<div>
{Array.from({ length: 200 }).map((_, i) => (
// eslint-disable-next-line @eslint-react/no-duplicate-key
<InView key={i} threshold={0.8} delay={200} triggerOnce initialInView>
{({ inView, ref }) => (
<div ref={ref}>
Expand Down
34 changes: 34 additions & 0 deletions packages/react-dom/src/FadeIn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { type CSSProperties, type ComponentPropsWithoutRef, type ElementType } from 'react'
import { useInView } from './useInView'

type FadInBaseProps = {
delay?: number
duration?: number
timingFunction: CSSProperties['animationTimingFunction']
}
type FadeInProps<TAs extends ElementType> = {
as?: TAs
} & Omit<ComponentPropsWithoutRef<TAs>, 'as' | keyof FadInBaseProps> &
FadInBaseProps

export function FadeIn<TAs extends ElementType = 'div'>({
as,
delay = 0,
duration = 200,
timingFunction = 'linear',
...rest
}: FadeInProps<TAs>) {
const Component = as || 'div'
const { inView, ref } = useInView()
return (
<Component
{...rest}
ref={ref}
style={{
opacity: inView ? 1 : 0,
willChange: 'opacity',
transition: `opacity ${duration}ms ${timingFunction} ${delay}ms`,
}}
/>
)
}
1 change: 1 addition & 0 deletions packages/react-dom/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { InView } from './InView'
export { useInView } from './useInView'
export { FadeIn } from './FadeIn'

0 comments on commit c30a173

Please sign in to comment.