generated from eternallycyf/ims-monorepo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(ims-view-pc): add MutateObserver
- Loading branch information
1 parent
d23c2c5
commit 4ced5f0
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/ims-view-pc/src/components/MutateObserver/MutateObserver.tsx
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,26 @@ | ||
import { useMutateObserver } from '@ims-view/hooks'; | ||
import React, { useLayoutEffect } from 'react'; | ||
import './index.less'; | ||
import type { MutationObserverProps } from './interface'; | ||
|
||
const MutateObserver: React.FC<MutationObserverProps> = (props) => { | ||
const { options, onMutate = () => {}, children } = props; | ||
|
||
const elementRef = React.useRef<HTMLElement>(null); | ||
|
||
const [target, setTarget] = React.useState<HTMLElement>(); | ||
|
||
useMutateObserver(target!, onMutate, options); | ||
|
||
useLayoutEffect(() => { | ||
setTarget(elementRef.current!); | ||
}, []); | ||
|
||
if (!children) { | ||
return null; | ||
} | ||
|
||
return React.cloneElement(children, { ref: elementRef }); | ||
}; | ||
|
||
export default MutateObserver; |
32 changes: 32 additions & 0 deletions
32
packages/ims-view-pc/src/components/MutateObserver/demo/index.tsx
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,32 @@ | ||
import { MutateObserver } from 'ims-view-pc'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export default function App() { | ||
const [className, setClassName] = useState('aaa'); | ||
|
||
useEffect(() => { | ||
setTimeout(() => setClassName('bbb'), 2000); | ||
}, []); | ||
|
||
const callback = function (mutationsList: MutationRecord[]) { | ||
console.log(mutationsList); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<MutateObserver onMutate={callback}> | ||
<div id="container"> | ||
<div className={className}> | ||
{className === 'aaa' ? ( | ||
<div>aaa</div> | ||
) : ( | ||
<div> | ||
<p>bbb</p> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</MutateObserver> | ||
</div> | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/ims-view-pc/src/components/MutateObserver/index.less
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,2 @@ | ||
.MutateObserver { | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/ims-view-pc/src/components/MutateObserver/index.md
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,41 @@ | ||
--- | ||
title: MutateObserver | ||
description: MutateObserver | ||
toc: content | ||
group: | ||
title: 其他 | ||
--- | ||
|
||
# MutateObserver | ||
|
||
## 介绍 | ||
|
||
`MutateObserver` 组件可以将内容渲染到指定的节点上,通常用于弹出层、全局提示等场景。 | ||
|
||
## 引入 | ||
|
||
```js | ||
import { MutateObserver } from 'ims-view-pc'; | ||
``` | ||
|
||
## 代码演示 | ||
|
||
<code src='./demo/index.tsx'></code> | ||
|
||
## API | ||
|
||
### Props | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| -------- | --------------------------- | ----------------------------- | ------ | | ||
| options | MutationObserver 的配置项 | MutationObserverInit | - | | ||
| onMutate | MutationObserver 的回调函数 | (mutations, observer) => void | - | | ||
| children | 需要观察的节点 | React.ReactElement | - | | ||
|
||
### 类型定义 | ||
|
||
组件导出以下类型定义: | ||
|
||
```ts | ||
import type { MutateObserverProps } from 'ims-view-pc'; | ||
``` |
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,2 @@ | ||
import MutateObserver from './MutateObserver'; | ||
export default MutateObserver; |
5 changes: 5 additions & 0 deletions
5
packages/ims-view-pc/src/components/MutateObserver/interface.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface MutationObserverProps { | ||
options?: MutationObserverInit; | ||
onMutate?: (mutations: MutationRecord[], observer: MutationObserver) => void; | ||
children: React.ReactElement; | ||
} |
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