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 all 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 挂载
6 changes: 5 additions & 1 deletion packages/ui/message/src/MessageAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export interface MessageAPIOptions extends ToastAPIOptions {}

export interface MessageOptions extends Omit<MessageProps, 'destroy' | 'visible'> {}

export const message = new MessageAPI({ component: MessageComponent })
export const createMessage = (options?: Omit<MessageAPIOptions, 'component'>) => {
return new MessageAPI({ component: MessageComponent, ...options })
}

export const message = createMessage({})
52 changes: 52 additions & 0 deletions packages/ui/message/stories/custom.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useMemo, useState } from 'react'
import { createMessage } from '../src'
import Button from '@hi-ui/button'

/**
* @title message 属性自定义
*
*/

export const Custom = () => {
const [container, setContainer] = useState<HTMLElement | null>(null)
const message = useMemo(
() =>
createMessage({
container,
zIndex: 1000,
}),
[container]
)
return (
<>
<h1>Custom</h1>
<div className="message-custom__wrap">
<div
ref={(e) => {
setContainer(e)
}}
id="ddd"
style={{
width: 700,
height: 400,
background: 'rgb(245, 247, 250)',
zIndex: 1500,
// Need add it
position: 'relative',
overflow: 'hidden',
}}
></div>
<Button
onClick={() => {
message.open({
title: '欢迎使用 HiUI 组件库',
type: 'success',
})
}}
>
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 './custom.stories'

export default {
title: 'FeedBack/Message',
Expand Down
12 changes: 5 additions & 7 deletions packages/ui/toast/src/ToastAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(this.options.container)
}

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

initManager() {
this.container = Container.getContainer(this.selector)
initManager(container?: HTMLElement) {
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) => {
if (!this.container) {
this.initManager()
this.initManager(this.options.container as HTMLElement)
}

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

Expand Down
23 changes: 18 additions & 5 deletions packages/ui/toast/src/ToastManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export class ToastManager extends Component<ToastManagerProps, ToastManagerState
})
}

private getStyle = (placement: ToastPlacement): React.CSSProperties => {
private getStyle = (
placement: ToastPlacement = 'top',
container?: HTMLElement,
zIndex?: number
): React.CSSProperties => {
let top: string | undefined
let bottom: string | undefined
let transform: string | undefined
Expand All @@ -88,8 +92,8 @@ export class ToastManager extends Component<ToastManagerProps, ToastManagerState

return {
// @DesignToken zIndex: `toast`
zIndex: 1010,
position: 'fixed',
zIndex: zIndex ?? 1010,
position: container ? 'absolute' : 'fixed',
pointerEvents: 'none',
transform,
top,
Expand All @@ -101,12 +105,12 @@ export class ToastManager extends Component<ToastManagerProps, ToastManagerState

render() {
const { queue } = this.state
const { prefixCls, placement, component: As, render } = this.props
const { prefixCls, placement, component: As, render, container, zIndex } = this.props

return (
<div
className={cx(`${prefixCls}-manager`, `${prefixCls}-manager--placement-${placement}`)}
style={this.getStyle(placement!)}
style={this.getStyle(placement, container, zIndex)}
>
{queue.map((notice) => {
if (As) return <As {...notice} />
Expand Down Expand Up @@ -140,4 +144,13 @@ export interface ToastManagerProps {
* 放置 toast 的位置
*/
placement?: ToastPlacement

/**
* 指定 portal 的容器
*/
container?: HTMLElement
/**
* 自定义css展示层级
*/
zIndex?: number
}