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.
- Loading branch information
1 parent
d689bf9
commit 6967260
Showing
7 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, { forwardRef, useEffect, useImperativeHandle, useMemo } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import './index.less'; | ||
import type { PortalProps } from './interface'; | ||
|
||
const Portal = forwardRef((props: PortalProps, ref) => { | ||
const { attach = document.body, children } = props; | ||
|
||
const container = useMemo(() => { | ||
const el = document.createElement('div'); | ||
el.className = `portal-wrapper`; | ||
return el; | ||
}, []); | ||
|
||
useEffect(() => { | ||
const parentElement = getAttach(attach); | ||
parentElement?.appendChild?.(container); | ||
|
||
return () => { | ||
parentElement?.removeChild?.(container); | ||
}; | ||
}, [container, attach]); | ||
|
||
useImperativeHandle(ref, () => container); | ||
|
||
return createPortal(children, container); | ||
}); | ||
|
||
export default Portal; | ||
|
||
export function getAttach(attach: PortalProps['attach']) { | ||
if (typeof attach === 'string') { | ||
return document.querySelector(attach); | ||
} | ||
if (typeof attach === 'object' && attach instanceof window.HTMLElement) { | ||
return attach; | ||
} | ||
|
||
return document.body; | ||
} |
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 @@ | ||
import { Portal } from 'ims-view-pc'; | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
export default () => { | ||
const containerRef = useRef<HTMLElement>(null); | ||
|
||
const content = ( | ||
<div className="btn"> | ||
<button type="button">按钮</button> | ||
</div> | ||
); | ||
|
||
useEffect(() => { | ||
console.log(containerRef); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<Portal attach={document.body} ref={containerRef}> | ||
{content} | ||
</Portal> | ||
</div> | ||
); | ||
}; |
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 @@ | ||
.portal-wrapper { | ||
} |
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,40 @@ | ||
--- | ||
title: Portal | ||
description: Portal | ||
toc: content | ||
group: | ||
title: 其他 | ||
--- | ||
|
||
# Portal | ||
|
||
## 介绍 | ||
|
||
`Portal` 组件可以将内容渲染到指定的节点上,通常用于弹出层、全局提示等场景。 | ||
|
||
## 引入 | ||
|
||
```js | ||
import { Portal } from 'ims-view-pc'; | ||
``` | ||
|
||
## 代码演示 | ||
|
||
<code src='./demo/index.tsx'></code> | ||
|
||
## API | ||
|
||
### Props | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| -------- | ------------------------------ | ----------------------- | ------ | | ||
| attach | 挂载的节点,可以是节点或选择器 | _HTMLElement \| string_ | - | | ||
| children | 需要挂载的内容 | _ReactNode_ | - | | ||
|
||
### 类型定义 | ||
|
||
组件导出以下类型定义: | ||
|
||
```ts | ||
import type { PortalProps } 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 Portal from './Portal'; | ||
export default Portal; |
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,4 @@ | ||
export interface PortalProps { | ||
attach?: HTMLElement | string; | ||
children: React.ReactNode; | ||
} |
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