Skip to content

Commit

Permalink
Fix(Appear): fix listener attach for ref callback (#6432)
Browse files Browse the repository at this point in the history
* fix(appear): fixup listener attach for ref callback

* fix(appear): remove unused directive
  • Loading branch information
linbudu599 authored Aug 2, 2023
1 parent 11dd752 commit f0d7906
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
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';
}

0 comments on commit f0d7906

Please sign in to comment.