Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(message): 支持自定义message配置 #2820

Merged
merged 8 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-items-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(message): 支持指定 portal 挂载
6 changes: 6 additions & 0 deletions .changeset/old-doors-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hi-ui/message": minor
"@hi-ui/toast": minor
---

feat: 支持指定 portal 挂载
4 changes: 4 additions & 0 deletions packages/ui/message/src/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export interface MessageProps extends Omit<HiBaseHTMLProps<'div'>, 'title'> {
* 是否开启自动关闭
*/
autoClose?: boolean
/**
* 指定 portal 的容器
*/
container?: Element | null
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
/**
* 执行销毁,内部使用,勿覆盖。暂不对外暴露
* @private
Expand Down
29 changes: 29 additions & 0 deletions packages/ui/message/stories/container.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import message from '../src'
import Button from '@hi-ui/button'

/**
* @title portal挂载
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
* @desc 挂载在当前div
*
*/
export const Container = () => {
return (
<>
<h1>Basic</h1>
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
<div className="message-container__wrap">
<Button
onClick={() => {
message.open({
title: '欢迎使用 HiUI 组件库',
type: 'success',
container: document.querySelector('.message-container__wrap'),
})
}}
>
Toast
</Button>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/message/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './basic.stories'
export * from './type.stories'
export * from './close.stories'
export * from './auto-close.stories'
export * from './container.stories'

export default {
title: 'FeedBack/Message',
Expand Down
18 changes: 10 additions & 8 deletions packages/ui/toast/src/ToastAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ToastEventOptions } from './types'
import { withDefaultProps } from '@hi-ui/react-utils'
import { uuid } from '@hi-ui/use-id'

export class ToastAPI<T = ToastEventOptions> {
export class ToastAPI<T extends ToastEventOptions> {
static defaultOptions = {
prefixCls: _prefix,
}
Expand All @@ -18,11 +18,9 @@ export class ToastAPI<T = ToastEventOptions> {

constructor(toastAPIOptions: ToastAPIOptions) {
this.options = withDefaultProps(toastAPIOptions, ToastAPI.defaultOptions)

// Ensure that Toast is a singleton.
this.id = uuid()

this.initManager()
// this.initManager()
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
}

static create(options: ToastAPIOptions) {
Expand All @@ -33,17 +31,21 @@ export class ToastAPI<T = ToastEventOptions> {
return `.${this.options.prefixCls}__portal__${this.id}`
}

initManager() {
this.container = Container.getContainer(this.selector)
initManager(container: Element | undefined) {
this.container = Container.getContainer(this.selector, document, container)
this.toastManager = React.createRef<ToastManager>()

render(<ToastManager {...this.options} ref={this.toastManager} />, this.container)
}

open = (props: T) => {
const container = props.container ?? document.body
if (this.container?.parentNode !== container) {
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
this.destroy()
}
if (!this.container) {
this.initManager()
this.initManager(container)
}

return this.toastManager.current?.open(props)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/toast/src/ToastManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ToastManager extends Component<ToastManagerProps, ToastManagerState
return {
// @DesignToken zIndex: `toast`
zIndex: 1010,
position: 'fixed',
position: 'absolute',
xiamiao1121 marked this conversation as resolved.
Show resolved Hide resolved
pointerEvents: 'none',
transform,
top,
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/toast/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface ToastEventOptions {
* toast 默认展示 title
*/
title?: React.ReactNode
/**
* 指定 portal 的容器
*/
container?: Element | undefined
}

export interface ToastOptions extends ToastEventOptions {
Expand Down