-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
fix: inViewOption does not working #37
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import type { MotionState } from '@/state/motion-state' | ||
import { Feature } from '@/features' | ||
import { frame, inView } from 'framer-motion/dom' | ||
import type { Options } from '@/types' | ||
|
||
function handleHoverEvent( | ||
state: MotionState, | ||
|
@@ -31,18 +32,45 @@ export class InViewGesture extends Feature { | |
super(state) | ||
} | ||
|
||
mount() { | ||
startObserver() { | ||
const element = this.state.element | ||
if (!element) | ||
return | ||
this.unmount() | ||
const { once, ...viewOptions } = this.state.getOptions().inViewOptions || {} | ||
this.unmount = inView( | ||
element, | ||
(entry) => { | ||
handleHoverEvent(this.state, entry, 'Enter') | ||
return (endEvent) => { | ||
handleHoverEvent(this.state, entry, 'Leave') | ||
if (!once) { | ||
return (endEvent) => { | ||
handleHoverEvent(this.state, entry, 'Leave') | ||
} | ||
} | ||
}, | ||
viewOptions, | ||
) | ||
} | ||
|
||
mount() { | ||
this.startObserver() | ||
} | ||
|
||
update(): void { | ||
const { props, prevProps } = this.state.visualElement | ||
const hasOptionsChanged = ['amount', 'margin', 'root'].some( | ||
hasViewportOptionChanged(props as any, prevProps as any), | ||
) | ||
if (hasOptionsChanged) { | ||
this.startObserver() | ||
} | ||
Comment on lines
+59
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance OptimizationThe Recommendation: |
||
} | ||
} | ||
|
||
function hasViewportOptionChanged( | ||
{ inViewOptions = {} }: Options, | ||
{ inViewOptions: prevViewport = {} }: Options = {}, | ||
) { | ||
return (name: keyof typeof inViewOptions) => | ||
inViewOptions[name] !== prevViewport[name] | ||
} |
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.
Error Handling Issue
The
startObserver
method sets up an observer with theinView
function but does not include any error handling mechanisms. This could lead to unhandled exceptions if theinView
function encounters errors, such as invalid arguments or issues with the browser's IntersectionObserver.Recommendation:
Implement try-catch blocks around the
inView
function call to handle potential exceptions gracefully. This will improve the robustness of the observer setup and prevent the application from crashing due to unhandled errors.