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(Appear): fix listener attach for ref callback #6432

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/silver-donuts-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/appear': patch
---

fix(appear): fixup listener attach for ref callback
29 changes: 19 additions & 10 deletions packages/appear/src/web/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { Children, useRef, useEffect, useCallback } from 'react';
import type { MutableRefObject } from 'react';

import type { Ref, RefObject } from 'react';
import { isFunction } from './type';
import { observerElement, VisibilityChangeEvent } from './visibility';

export function isFunction(obj: any): obj is Function {
return typeof obj === 'function';
interface Node {
_nativeNode?: Node;
}

function VisibilityChange(props: any) {
export default function VisibilityChange(props: any) {
const { onAppear, onDisappear, children } = props;

const defaultRef: MutableRefObject<Node> = useRef<Node>();
const ref: MutableRefObject<Node> = children && children.ref ? children.ref : defaultRef;
const defaultRef: Ref<Node> = useRef<Node>();

const ref: RefObject<Node> = children.ref
? typeof children.ref === 'object'
? children.ref
: defaultRef
: defaultRef;

useEffect(() => {
if (typeof children.ref === 'function') {
children.ref(ref.current);
}
}, [ref, children]);

const listen = useCallback(
(eventName: string, handler: () => {}) => {
(eventName: string, handler: EventListenerOrEventListenerObject) => {
const { current } = ref;
// Rax components will set custom ref by useImperativeHandle.
// So We should get eventTarget by _nativeNode.
Expand All @@ -40,5 +51,3 @@ function VisibilityChange(props: any) {

return Children.only({ ...children, ref });
}

export default VisibilityChange;
3 changes: 3 additions & 0 deletions packages/appear/src/web/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isFunction(obj: any): obj is Function {
return typeof obj === 'function';
}