This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support appear component * chore: modify version * fix: should disappear * chore: export VisibilityChangeEvent * feat: rax-compat use @ice/appear * feat: need not give childref * chore: update lock * chore: update README and package * chore: update README * test: add test for appear * chore: update lock
- Loading branch information
1 parent
16e0afa
commit 39a9f78
Showing
13 changed files
with
150 additions
and
44 deletions.
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
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 | ||
|
||
## v0.1.0 | ||
|
||
- [feat] support VisibilityChange. |
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,24 @@ | ||
# @ice/appear | ||
|
||
React component for appear and disappear. | ||
|
||
## Usage | ||
|
||
```bash | ||
npm i @ice/appear | ||
``` | ||
|
||
```jsx | ||
import VisibilityChange from '@ice/appear'; | ||
|
||
export default function Home() { | ||
return <VisibilityChange | ||
onAppear={() => { | ||
console.log('onAppear') | ||
}} | ||
onDisappear={() => { | ||
console.log('ondisAppear') | ||
}} | ||
>show something</VisibilityChange> | ||
} | ||
``` |
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,23 @@ | ||
{ | ||
"name": "@ice/appear", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "./esm/index.js", | ||
"types": "./esm/index.d.ts", | ||
"scripts": { | ||
"watch": "tsc -w", | ||
"build": "tsc", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18", | ||
"react-dom": "^18" | ||
} | ||
} |
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,39 @@ | ||
import { Children, useRef, useEffect, useCallback } from 'react'; | ||
import { isFunction } from './type'; | ||
import { observerElement, VisibilityChangeEvent } from './visibility'; | ||
|
||
function VisibilityChange(props: any) { | ||
const { | ||
onAppear, | ||
onDisappear, | ||
children, | ||
} = props; | ||
|
||
const ref = useRef(null); | ||
|
||
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 HTMLElement); | ||
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; |
1 change: 1 addition & 0 deletions
1
...s/rax-compat/src/intersection-observer.ts → packages/appear/src/intersection-observer.ts
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
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,3 @@ | ||
export function isFunction(obj: any): obj is Function { | ||
return typeof obj === 'function'; | ||
} |
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
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,22 @@ | ||
import { it, describe } from 'vitest'; | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import VisibilityChange from '../src/index'; | ||
|
||
describe('visibilytyChange', () => { | ||
it('appear', () => { | ||
return new Promise(resolve => { | ||
function App() { | ||
return (<VisibilityChange | ||
onAppear={() => { | ||
resolve(); | ||
}} | ||
> | ||
<span>content</span> | ||
</VisibilityChange>); | ||
} | ||
|
||
render(<App />); | ||
}); | ||
}); | ||
}); |
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,12 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"rootDir": "src", | ||
"outDir": "esm" | ||
}, | ||
"allowJs": true, | ||
"include": [ | ||
"src" | ||
] | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39a9f78
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.
Successfully deployed to the following URLs:
ice-v3 – ./
ice-v3-git-release-next-ice-v3.vercel.app
ice-v3.vercel.app
ice-v3-ice-v3.vercel.app