-
Notifications
You must be signed in to change notification settings - Fork 52
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d4d6c66
feat(react-dom): add FadeIn
manudeli a743248
Update packages/react-dom/src/FadeIn.tsx
manudeli 145e048
Update packages/react-dom/src/FadeIn.tsx
manudeli 1e1238f
chore: update
manudeli c581697
feat(react-dom): add inViewOptions as FadeInProps
manudeli af99fd1
chore: update
manudeli d288710
fix(react-dom): expose style prop for FadeIn with type & runtime rest…
manudeli d9f598e
Merge branch 'main' into react-dom/FadeIn
manudeli 450a3f9
Merge branch 'main' into react-dom/FadeIn
manudeli 324f428
fix(react-dom): add Override utility-type & use it to restrict type
manudeli 5d12190
fix(react-dom): expose only required options for InView
manudeli 0ae4863
Merge branch 'main' into react-dom/FadeIn
manudeli 7b55958
fix(react-dom): add useFadeIn & convert as render prop
manudeli 15f62f7
Merge branch 'main' into react-dom/FadeIn
manudeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@suspensive/react-dom": minor | ||
--- | ||
|
||
feat(react-dom): add `<FadeIn/>` |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'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() { | ||
return ( | ||
<div> | ||
<ErrorBoundary fallback={({ error }) => <>{error.message}</>}> | ||
{Array.from({ length: 20 }).map((_, i) => { | ||
const userId = i + 1 | ||
return ( | ||
<Suspense | ||
key={userId} | ||
clientOnly | ||
fallback={ | ||
<FadeIn delay={200} duration={1000}> | ||
{(fadeIn) => ( | ||
<div {...fadeIn}> | ||
<div role="status" className="mb-6 animate-pulse space-y-2"> | ||
<div className="h-4 w-[42px] rounded-sm bg-gray-300 dark:bg-gray-600" /> | ||
<div className="h-2 w-[34px] rounded-sm bg-gray-300 dark:bg-gray-600" /> | ||
<div className="h-2 w-[344px] rounded-sm bg-gray-300 dark:bg-gray-600" /> | ||
<div className="h-2 w-[344px] rounded-sm bg-gray-300 dark:bg-gray-600" /> | ||
<div className="h-4 w-[42px] rounded-sm bg-gray-300 dark:bg-gray-600" /> | ||
<div className="h-4 w-[34px] rounded-sm bg-gray-300 dark:bg-gray-600" /> | ||
</div> | ||
</div> | ||
)} | ||
</FadeIn> | ||
} | ||
> | ||
<SuspenseQuery {...query.user(userId)}> | ||
{({ data: user }) => ( | ||
<FadeIn duration={200} triggerOnce> | ||
{(fadeIn) => ( | ||
<div {...fadeIn} 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> | ||
<div className="mb-6" /> | ||
</div> | ||
)} | ||
</FadeIn> | ||
)} | ||
</SuspenseQuery> | ||
</Suspense> | ||
) | ||
})} | ||
</ErrorBoundary> | ||
</div> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { FadeIn } from './FadeIn' | ||
import { mockAllIsIntersecting } from './test-utils' | ||
|
||
describe('<FadeIn/>', () => { | ||
it('renders children with correct styles when in view', () => { | ||
mockAllIsIntersecting(false) | ||
const mockChild = vi.fn() | ||
render( | ||
<FadeIn duration={300} timingFunction="ease-in"> | ||
{(fadeIn) => ( | ||
<div {...fadeIn} data-testid="fade-in-child"> | ||
{mockChild(fadeIn.style)} | ||
</div> | ||
)} | ||
</FadeIn> | ||
) | ||
expect(mockChild).toHaveBeenCalledWith({ opacity: 0, willChange: 'opacity', transition: 'opacity 300ms ease-in' }) | ||
mockAllIsIntersecting(true) | ||
const child = screen.getByTestId('fade-in-child') | ||
expect(child).toHaveStyle({ opacity: '1', willChange: 'opacity', transition: 'opacity 300ms ease-in' }) | ||
expect(mockChild).toHaveBeenCalledWith({ opacity: 1, willChange: 'opacity', transition: 'opacity 300ms ease-in' }) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ComponentProps } from 'react' | ||
import type { FadeIn } from './FadeIn' | ||
|
||
describe('<FadeIn/>', () => { | ||
it('type check', () => { | ||
expectTypeOf<keyof ComponentProps<typeof FadeIn>>().toEqualTypeOf< | ||
'root' | 'rootMargin' | 'threshold' | 'triggerOnce' | 'delay' | 'children' | 'duration' | 'timingFunction' | ||
>() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { type ReactNode } from 'react' | ||
import { type FadeInOptions, useFadeIn } from './useFadeIn' | ||
|
||
type FadeInProps = FadeInOptions & { | ||
children: (fadeInResult: ReturnType<typeof useFadeIn>) => ReactNode | ||
} | ||
/** | ||
* A component that fades in when it comes into view. | ||
*/ | ||
export function FadeIn({ | ||
duration = 200, | ||
timingFunction = 'linear', | ||
delay, | ||
root, | ||
rootMargin, | ||
threshold, | ||
triggerOnce, | ||
children, | ||
}: FadeInProps) { | ||
const result = useFadeIn({ delay, duration, root, rootMargin, threshold, timingFunction, triggerOnce }) | ||
return <>{children(result)}</> | ||
} |
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,2 +1,4 @@ | ||
export { InView } from './InView' | ||
export { useInView } from './useInView' | ||
export { FadeIn } from './FadeIn' | ||
export { useFadeIn } from './useFadeIn' |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { type CSSProperties, useMemo } from 'react' | ||
import { type InViewOptions, useInView } from './useInView' | ||
import type { OmitKeyof } from './utility-types' | ||
|
||
export type FadeInOptions = OmitKeyof< | ||
InViewOptions, | ||
'fallbackInView' | 'initialInView' | 'skip' | 'onChange' | 'trackVisibility' | ||
> & { | ||
/** | ||
* The duration in milliseconds of the animation. | ||
* @default 200 | ||
*/ | ||
duration?: number | ||
/** | ||
* The timing function of the animation. | ||
* @default 'linear' | ||
*/ | ||
timingFunction?: CSSProperties['transitionTimingFunction'] | ||
} | ||
type FadeInResult = Pick<ReturnType<typeof useInView>, 'ref'> & { | ||
style: { | ||
opacity: 0 | 1 | ||
willChange: 'opacity' | ||
transition: `opacity ${number}ms ${Required<CSSProperties>['transitionTimingFunction']}` | ||
} | ||
} | ||
export function useFadeIn({ | ||
duration = 200, | ||
timingFunction = 'linear', | ||
delay, | ||
root, | ||
rootMargin, | ||
threshold, | ||
triggerOnce, | ||
}: FadeInOptions): FadeInResult { | ||
const { inView, ref } = useInView({ delay, root, rootMargin, threshold, triggerOnce }) | ||
return useMemo<FadeInResult>( | ||
() => ({ | ||
ref, | ||
style: { | ||
opacity: inView ? 1 : 0, | ||
willChange: 'opacity', | ||
transition: `opacity ${duration}ms ${timingFunction}` as const, | ||
}, | ||
}), | ||
[inView, duration, timingFunction] | ||
) | ||
} |
176 changes: 176 additions & 0 deletions
176
packages/react-dom/src/utility-types/OmitKeyof.test-d.ts
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 |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import type { OmitKeyof } from './OmitKeyof' | ||
|
||
describe('OmitKeyof', () => { | ||
it("'s string key type check", () => { | ||
type A = { | ||
x: string | ||
y: number | ||
} | ||
|
||
type ExpectedType = { | ||
x: string | ||
} | ||
|
||
// Bad point | ||
// 1. original Omit can use 'z' as type parameter with no type error | ||
// 2. original Omit have no auto complete for 2nd type parameter | ||
expectTypeOf<Omit<A, 'z' | 'y'>>().toEqualTypeOf<ExpectedType>() | ||
|
||
// Solution | ||
|
||
// 1. strictly | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 'z' as type parameter with type error because A don't have key 'z' | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
'z' | 'y' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 'z' as type parameter with type error because A don't have key 'z' | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
'z' | 'y', | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments | ||
'strictly' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
|
||
// 2. safely | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 'z' as type parameter type error with strictly parameter or default parameter | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
'z' | 'y' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// With 'safely', OmitKeyof can use 'z' as type parameter like original Omit but This support autocomplete too yet for DX. | ||
'z' | 'y', | ||
'safely' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
}) | ||
|
||
it("'s number key type check", () => { | ||
type A = { | ||
[1]: string | ||
[2]: number | ||
} | ||
|
||
type ExpectedType = { | ||
[1]: string | ||
} | ||
|
||
// Bad point | ||
// 1. original Omit can use 3 as type parameter with no type error | ||
// 2. original Omit have no auto complete for 2nd type parameter | ||
expectTypeOf<Omit<A, 3 | 2>>().toEqualTypeOf<ExpectedType>() | ||
|
||
// Solution | ||
|
||
// 1. strictly | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 3 as type parameter with type error because A don't have key 3 | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
3 | 2 | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 3 as type parameter with type error because A don't have key 3 | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
3 | 2, | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments | ||
'strictly' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
|
||
// 2. safely | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 3 as type parameter type error with strictly parameter or default parameter | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
3 | 2 | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// With 'safely', OmitKeyof can use 3 as type parameter like original Omit but This support autocomplete too yet for DX. | ||
3 | 2, | ||
'safely' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
}) | ||
|
||
it("'s symbol key type check", () => { | ||
const symbol1 = Symbol() | ||
const symbol2 = Symbol() | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const symbol3 = Symbol() | ||
|
||
type A = { | ||
[symbol1]: string | ||
[symbol2]: number | ||
} | ||
|
||
type ExpectedType = { | ||
[symbol1]: string | ||
} | ||
|
||
// Bad point | ||
// 1. original Omit can use symbol3 as type parameter with no type error | ||
// 2. original Omit have no auto complete for 2nd type parameter | ||
expectTypeOf<Omit<A, typeof symbol3 | typeof symbol2>>().toEqualTypeOf<ExpectedType>() | ||
|
||
// Solution | ||
|
||
// 1. strictly | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3 | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
typeof symbol3 | typeof symbol2 | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3 | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
typeof symbol3 | typeof symbol2, | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments | ||
'strictly' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
|
||
// 2. safely | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use symbol3 as type parameter type error with strictly parameter or default parameter | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
typeof symbol3 | typeof symbol2 | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// With 'safely', OmitKeyof can use symbol3 as type parameter like original Omit but This support autocomplete too yet for DX. | ||
typeof symbol3 | typeof symbol2, | ||
'safely' | ||
> | ||
>().toEqualTypeOf<ExpectedType>() | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.