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

chore: weex support for ice appear #6391

Merged
merged 3 commits into from
Aug 1, 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/dry-pets-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/appear': minor
---

chore: weex support for ice appear
47 changes: 10 additions & 37 deletions packages/appear/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import { Children, useRef, useEffect, useCallback } from 'react';
import type { Ref } from 'react';
import { isFunction } from './type';
import { observerElement, VisibilityChangeEvent } from './visibility';
import type * as React from 'react';
import WebAppear from './web';
import WeexAppear from './weex';
import type { AppearProps } from './typings.js';

function VisibilityChange(props: any) {
const {
onAppear,
onDisappear,
children,
} = props;
let Appear: React.ForwardRefExoticComponent<AppearProps & React.RefAttributes<any>>;

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

const listen = useCallback((eventName: string, handler: Function) => {
const { current } = ref;
// Rax components will set custom ref by useImperativeHandle.
// So We should get eventTarget by _nativeNode.
// https://github.com/raxjs/rax-components/blob/master/packages/rax-textinput/src/index.tsx#L151
if (current && isFunction(handler)) {
const eventTarget = current._nativeNode || current;
observerElement(eventTarget as Element);
eventTarget.addEventListener(eventName, handler);
}
return () => {
const { current } = ref;
if (current) {
const eventTarget = current._nativeNode || current;
eventTarget.removeEventListener(eventName, handler);
}
};
}, [ref]);

useEffect(() => listen(VisibilityChangeEvent.appear, onAppear), [ref, onAppear, listen]);
useEffect(() => listen(VisibilityChangeEvent.disappear, onDisappear), [ref, onDisappear, listen]);

return Children.only({ ...children, ref });
if (import.meta.target === 'weex') {
Appear = WeexAppear;
} else {
Appear = WebAppear as any;
}

export default VisibilityChange;
export default Appear;
11 changes: 11 additions & 0 deletions packages/appear/src/runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="@ice/pkg/types" />

interface ImportMeta {
// The build target
target: 'weex' | 'web';
}


interface Node {
_nativeNode: Node;
}
3 changes: 0 additions & 3 deletions packages/appear/src/type.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/appear/src/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type * as React from 'react';

export interface AppearProps {
children: React.ReactElement;
/**
* Triggered when the element enters the visible area.
* @param {CustomEvent} e
* @returns {void}
*/
onAppear?: (e: CustomEvent) => void;
/**
* Triggered when the element leaves the visible area.
* @param {CustomEvent} e
* @returns {void}
*/
onDisappear?: (e: CustomEvent) => void;
}
44 changes: 44 additions & 0 deletions packages/appear/src/web/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Children, useRef, useEffect, useCallback } from 'react';
import type { MutableRefObject } from 'react';

import { observerElement, VisibilityChangeEvent } from './visibility';

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

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 listen = useCallback(
(eventName: string, handler: () => {}) => {
const { current } = ref;
// Rax components will set custom ref by useImperativeHandle.
// So We should get eventTarget by _nativeNode.
// https://github.com/raxjs/rax-components/blob/master/packages/rax-textinput/src/index.tsx#L151
if (current && isFunction(handler)) {
const eventTarget = current._nativeNode || current;
observerElement(eventTarget as Element);
eventTarget.addEventListener(eventName, handler);
}
return () => {
const { current } = ref;
if (current) {
const eventTarget = current._nativeNode || current;
eventTarget.removeEventListener(eventName, handler);
}
};
},
[ref],
);

useEffect(() => listen(VisibilityChangeEvent.appear, onAppear), [ref, onAppear, listen]);
useEffect(() => listen(VisibilityChangeEvent.disappear, onDisappear), [ref, onDisappear, listen]);

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

export default VisibilityChange;
36 changes: 36 additions & 0 deletions packages/appear/src/weex/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { ForwardedRef } from 'react';
import { useEffect, useRef, forwardRef, cloneElement, Children } from 'react';
import type { AppearProps } from '../typings';

const WeexAppear = forwardRef<any, AppearProps>((props, ref) => {
const internalRef = useRef<HTMLDivElement>(null);
const childrenRef: ForwardedRef<HTMLDivElement> = ref ?? internalRef;

const { children, onAppear, onDisappear } = props;

useEffect(() => {
// Use copy of childrenRef to avoid ref value changed in cleanup phase.
const nodeRef = typeof childrenRef === 'object' ? childrenRef.current : null;

// Return early if onAppear callback not specified.
onAppear && nodeRef?.addEventListener('appear', (e: CustomEvent) => onAppear(e));

return () => {
onAppear && nodeRef?.removeEventListener('appear', (e: CustomEvent) => onAppear(e));
Copy link

Choose a reason for hiding this comment

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

hi,@linbudu599 weex 下 addEventListener、removeEventListener 对 onAppear 都加了一层 wrapper,所以这里 removeEventListener 是不是就不生效了

};
}, [childrenRef, onAppear]);

useEffect(() => {
const nodeRef = typeof childrenRef === 'object' ? childrenRef.current : null;

onDisappear && nodeRef?.addEventListener('disappear', (e: CustomEvent) => onDisappear(e));

return () => {
onDisappear && nodeRef?.removeEventListener('disappear', (e: CustomEvent) => onDisappear(e));
};
}, [childrenRef, onDisappear]);

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

export default WeexAppear;