Skip to content

Commit

Permalink
✨ feat(ims-view-pc): add Portal
Browse files Browse the repository at this point in the history
  • Loading branch information
eternallycyf committed Apr 27, 2024
1 parent d689bf9 commit 6967260
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/ims-view-pc/src/components/Portal/Portal.tsx
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;
}
24 changes: 24 additions & 0 deletions packages/ims-view-pc/src/components/Portal/demo/index.tsx
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>
);
};
2 changes: 2 additions & 0 deletions packages/ims-view-pc/src/components/Portal/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.portal-wrapper {
}
40 changes: 40 additions & 0 deletions packages/ims-view-pc/src/components/Portal/index.md
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';
```
2 changes: 2 additions & 0 deletions packages/ims-view-pc/src/components/Portal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Portal from './Portal';
export default Portal;
4 changes: 4 additions & 0 deletions packages/ims-view-pc/src/components/Portal/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface PortalProps {
attach?: HTMLElement | string;
children: React.ReactNode;
}
2 changes: 2 additions & 0 deletions packages/ims-view-pc/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export * from './components/ExportButton/interface';
export { default as Icon } from './components/Icon';
export { default as Marker } from './components/Marker';
export * from './components/Marker/interface';
export { default as Portal } from './components/Portal';
export * from './components/Portal/interface';
export { default as SectionTitle } from './components/SectionTitle';
export * from './components/SectionTitle/interface';
export { default as Sticky } from './components/Sticky';
Expand Down

0 comments on commit 6967260

Please sign in to comment.