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

fix: inViewOption does not working #37

Merged
merged 1 commit into from
Jan 14, 2025
Merged
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
34 changes: 31 additions & 3 deletions packages/motion/src/features/gestures/in-view/index.ts
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,
Expand Down Expand Up @@ -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,
)
Comment on lines 41 to +52

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 the inView function but does not include any error handling mechanisms. This could lead to unhandled exceptions if the inView 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.

}

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

Choose a reason for hiding this comment

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

Performance Optimization

The update method uses a combination of .some() and a custom function hasViewportOptionChanged to check if observer options have changed, which might be inefficient if the properties list is large or changes infrequently.

Recommendation:
Consider optimizing the check by caching previous properties and comparing directly or using a more efficient method to detect changes. This could reduce the overhead of function calls and iterations, improving the performance of the update method.

}
}

function hasViewportOptionChanged(
{ inViewOptions = {} }: Options,
{ inViewOptions: prevViewport = {} }: Options = {},
) {
return (name: keyof typeof inViewOptions) =>
inViewOptions[name] !== prevViewport[name]
}
Loading