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

feat(react-dom): add <FadeIn/> #1362

Merged
merged 14 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/fresh-flies-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react-dom": minor
---

feat(react-dom): add `<FadeIn/>`
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
68 changes: 68 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,68 @@
'use client'

import { ErrorBoundary, Suspense } from '@suspensive/react'
import { FadeIn } from '@suspensive/react-dom'
import { SuspenseQuery, queryOptions } from '@suspensive/react-query'
import axios from 'axios'
import { delay } from '~/utils'

const query = {
user: (userId: number) =>
queryOptions({
queryKey: ['users', userId],
queryFn: () =>
delay(3000).then(() =>
axios
.get<{
id: number
username: string
maidenName: string
age: number
gender: string
email: string
image: 'https://dummyjson.com/icon/emilys/128'
userAgent: string
}>(`https://dummyjson.com/users/${userId}`)
.then(({ data }) => data)
),
}),
}

export default function Page() {
const userId = 1

return (
<ErrorBoundary fallback={({ error }) => <>{error.message}</>}>
<Suspense
clientOnly
fallback={
<FadeIn delay={200} duration={1000}>
{skeleton}
</FadeIn>
}
>
<SuspenseQuery {...query.user(userId)}>
{({ data: user }) => (
<FadeIn duration={200} className="max-w-[344px]">
<h1 className="text-lg font-bold">{user.username}</h1>
<p className="text-xs">{user.userAgent}</p>
<p>{user.age}</p>
<p>{user.maidenName}</p>
</FadeIn>
)}
</SuspenseQuery>
</Suspense>
</ErrorBoundary>
)
}

const skeleton = (
<div role="status" className="animate-pulse space-y-2">
<div className="h-4 w-[42px] rounded-sm bg-gray-300 dark:bg-gray-600"></div>
<div className="h-2 w-[34px] rounded-sm bg-gray-300 dark:bg-gray-600"></div>
<div className="h-2 w-[344px] rounded-sm bg-gray-300 dark:bg-gray-600"></div>
<div className="h-2 w-[344px] rounded-sm bg-gray-300 dark:bg-gray-600"></div>
<div className="h-4 w-[42px] rounded-sm bg-gray-300 dark:bg-gray-600"></div>
<div className="h-4 w-[34px] rounded-sm bg-gray-300 dark:bg-gray-600"></div>
</div>
)
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']
}
manudeli marked this conversation as resolved.
Show resolved Hide resolved
type FadeInProps<TAs extends ElementType> = {
as?: TAs
} & Omit<ComponentPropsWithoutRef<TAs>, 'as' | keyof FadInBaseProps> &
FadInBaseProps
manudeli marked this conversation as resolved.
Show resolved Hide resolved
manudeli marked this conversation as resolved.
Show resolved Hide resolved

export function FadeIn<TAs extends ElementType = 'div'>({
as,
delay = 0,
duration = 200,
Copy link
Collaborator

@gwansikk gwansikk Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am concerned about whether setting the default value of delay to 200ms is a good idea. The referenced KakaoPay article derived 200ms based on examples from KakaoPay monitoring, but I’m not sure if this metric is applicable to us or our users.

Since general users are likely to stick with the default values, how about setting it based on popular and well-founded conventions?

ref: https://www.nngroup.com/articles/progress-indicators/

@manudeli @kangju2000

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree your opinion. Do we need to make context provider to set up default prop of FadeIn?

Copy link
Collaborator

@gwansikk gwansikk Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to make context provider to set up default prop of FadeIn?

Yes, having a provider could help define the scope and manage it consistently!


This was simply a suggestion to set a default delay value.
According to an article by the "UX Research Group Nielsen Norman," animations under one second can be distracting and negatively impact the user experience. Providing a best-practice default value would seem like a good approach.

"Using repeated animations for any element that takes less than one second to load can be distracting. 
Users may struggle to follow what's happening on the screen and may feel uneasy about the flickering content."

timingFunction = 'linear',
...rest
}: FadeInProps<TAs>) {
const Component = as || 'div'
manudeli marked this conversation as resolved.
Show resolved Hide resolved
const { inView, ref } = useInView()
manudeli marked this conversation as resolved.
Show resolved Hide resolved
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'
Loading