-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ice/appear': minor | ||
--- | ||
|
||
chore: weex support for ice appear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; | ||
}, [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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
hi,@linbudu599 weex 下 addEventListener、removeEventListener 对 onAppear 都加了一层 wrapper,所以这里 removeEventListener 是不是就不生效了