Skip to content

Commit fb2658d

Browse files
authored
Merge pull request #1 from react-component/MutateObserver
feat: Mutate observer
2 parents 6b77a40 + 15af3bc commit fb2658d

22 files changed

Lines changed: 194 additions & 721 deletions

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# rc-portal
1+
# rc-mutate-observer
22

33
React 18 supported Portal Component.
44

@@ -31,11 +31,21 @@ open http://localhost:8000
3131

3232
## Usage
3333

34-
```js | pure
35-
import Portal from 'rc-portal';
34+
```tsx
35+
import React from 'react';
36+
import MutateObserver from './src';
3637

37-
const Demo = () => {
38-
return <Portal open>Hello World</Portal>;
38+
const onMutate = (mutations: MutationRecord[], observer: MutationObserver) => {
39+
console.log(mutation);
40+
console.log(observer);
41+
};
42+
43+
const Demo: React.FC = () => {
44+
return (
45+
<MutateObserver onMutate={onMutate}>
46+
<div>test</div>
47+
</MutateObserver>
48+
);
3949
};
4050

4151
export default Demo;
@@ -47,8 +57,7 @@ We use typescript to create the Type definition. You can view directly in IDE. B
4757

4858
### Portal
4959

50-
| Prop | Description | Type | Default |
51-
| ------------ | ---------------------------------- | ------------------------ | ------------- |
52-
| getContainer | Customize portal container element | Element \| () => Element | document.body |
53-
| open | Show the portal | boolean | false |
54-
| autoLock | Lock screen scroll when open | boolean | false |
60+
| Prop | Description | Type | Default |
61+
| -------- | ---------------------------------------------------------------------------------------------------------------- | -------------------- | ------- |
62+
| onMutate | A function which will be called on each DOM change that qualifies given the observed node or subtree and options | MutationCallback | - |
63+
| options | An object providing options that describe which DOM mutations should be reported to mutationObserver's callback | MutationObserverInit | - |

docs/demo/basic.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
## basic
22

3-
43
<code src="../examples/basic.tsx" />

docs/demo/getContainer.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/examples/basic.less

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/examples/basic.tsx

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,20 @@
1-
import React, { version } from 'react';
2-
import classNames from 'classnames';
3-
import Portal from '../../src';
4-
import './basic.less';
1+
import MutateObserver from '../../src';
2+
import React, { useCallback } from 'react';
53

6-
export default () => {
7-
const [show, setShow] = React.useState(true);
8-
const [customizeContainer, setCustomizeContainer] = React.useState(false);
9-
const [lock, setLock] = React.useState(true);
4+
const App: React.FC = () => {
5+
const [flag, setFlag] = React.useState<boolean>(true);
106

11-
const divRef = React.useRef<HTMLDivElement>(null);
12-
13-
React.useEffect(
14-
() => () => {
15-
console.log('Demo unmount!!');
16-
},
17-
[],
18-
);
19-
20-
const getContainer = customizeContainer ? () => divRef.current : undefined;
21-
const contentCls = customizeContainer ? '' : 'abs';
7+
const onMutate = useCallback((mutations: MutationRecord[]) => {
8+
console.log(mutations);
9+
}, []);
2210

2311
return (
24-
<React.StrictMode>
25-
<div style={{ height: '200vh' }}>
26-
<div style={{ border: '2px solid red' }}>
27-
Real Version: {version}
28-
<button onClick={() => setShow(!show)}>
29-
show: {show.toString()}
30-
</button>
31-
<button onClick={() => setCustomizeContainer(!customizeContainer)}>
32-
customize container: {customizeContainer.toString()}
33-
</button>
34-
<button onClick={() => setLock(!lock)}>
35-
lock scroll: {lock.toString()}
36-
</button>
37-
<div
38-
id="customize"
39-
ref={divRef}
40-
style={{ border: '1px solid green', minHeight: 10 }}
41-
/>
42-
</div>
43-
44-
<Portal open={show} getContainer={getContainer} autoLock={lock}>
45-
<p className={classNames(contentCls, 'root')}>Hello Root</p>
46-
<Portal open={show} getContainer={getContainer} autoLock={lock}>
47-
<p className={classNames(contentCls, 'parent')}>Hello Parent</p>
48-
<Portal open={show} getContainer={getContainer} autoLock={lock}>
49-
<p className={classNames(contentCls, 'children')}>
50-
Hello Children
51-
</p>
52-
</Portal>
53-
</Portal>
54-
</Portal>
55-
</div>
56-
</React.StrictMode>
12+
<MutateObserver onMutate={onMutate}>
13+
<button className={flag ? 'aaa' : 'bbb'} onClick={() => setFlag(!flag)}>
14+
click
15+
</button>
16+
</MutateObserver>
5717
);
5818
};
19+
20+
export default App;

docs/examples/getContainer.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: rc-portal
2+
title: rc-mutate-observer
33
---
44

55
<embed src="../README.md"></embed>

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "@rc-component/portal",
3-
"version": "1.0.3",
4-
"description": "React Portal Component",
2+
"name": "@rc-component/mutate-observer",
3+
"version": "0.0.1",
4+
"description": "React MutateObserver Component",
55
"keywords": [
66
"react",
77
"react-component",
8-
"react-portal"
8+
"mutate-observer"
99
],
10-
"homepage": "https://github.com/react-component/portal",
10+
"homepage": "https://github.com/react-component/mutate-observer",
1111
"bugs": {
12-
"url": "https://github.com/react-component/portal/issues"
12+
"url": "https://github.com/react-component/mutate-observer/issues"
1313
},
1414
"repository": {
1515
"type": "git",
16-
"url": "https://github.com/react-component/portal.git"
16+
"url": "https://github.com/react-component/mutate-observer.git"
1717
},
1818
"license": "MIT",
19-
"author": "smith3816@gmail.com",
19+
"author": "574980606@qq.com",
2020
"main": "./lib/index",
2121
"module": "./es/index",
2222
"typings": "es/index.d.ts",

src/Context.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Portal.tsx

Lines changed: 0 additions & 138 deletions
This file was deleted.

0 commit comments

Comments
 (0)