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

fix(components): [message]: offset #8379

Merged
merged 3 commits into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 26 additions & 21 deletions packages/components/message/src/message-method.ts
Expand Up @@ -60,26 +60,26 @@ const normalizeOptions = (params?: MessageParams) => {
return normalized as MessageParamsNormalized
}

const getNextOffset = (offset: number) => offset + 48 + 16
sxzz marked this conversation as resolved.
Show resolved Hide resolved

const computeOffset = () => {
if (instances.length === 0) return

instances.reduce((prev, next) => {
next.props.offset = prev
return getNextOffset(prev)
}, instances[0].options.offset)
}

const closeMessage = (instance: MessageQueueItem) => {
const idx = instances.indexOf(instance)
if (idx === -1) return

instances.splice(idx, 1)
const { vnode, handler } = instance
const { handler } = instance
handler.close()

const removedHeight = vnode.el!.offsetHeight
// adjust other instances vertical offset
const len = instances.length
if (len < 1) return
for (let i = idx; i < len; i++) {
const pos =
Number.parseInt(instances[i].vnode.el!.style['top'], 10) -
removedHeight -
16

instances[i].vnode.component!.props.offset = pos
}
computeOffset()
}

const createMessage = (
Expand All @@ -91,18 +91,20 @@ const createMessage = (
const id = `message_${seed++}`
const userOnClose = options.onClose

let verticalOffset = options.offset
instances.forEach(({ vnode: vm }) => {
verticalOffset += (vm.el?.offsetHeight || 0) + 16
})
verticalOffset += 16
let offset: number
const lastInstance = instances[instances.length - 1]
if (lastInstance) {
offset = getNextOffset(lastInstance.vm.offset)
} else {
offset = options.offset
}

const container = document.createElement('div')

const props = {
...options,
zIndex: options.zIndex ?? nextZIndex(),
offset: verticalOffset,
offset,
id,
onClose: () => {
userOnClose?.()
Expand Down Expand Up @@ -142,9 +144,12 @@ const createMessage = (
}

const instance = {
id,
vnode,
vm,
handler,
options: { appendTo, ...options },
props: (vnode.component as any).props,
}

return instance
Expand All @@ -168,8 +173,8 @@ const message: MessageFn &
({ vnode: vm }) => vm.props?.message === normalized.message
)
if (instance) {
;(instance.vnode.component as any).props.repeatNum += 1
;(instance.vnode.component as any).props.type = normalized.type
instance.props.repeatNum += 1
instance.props.type = normalized.type
return instance.handler
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/components/message/src/message.ts
Expand Up @@ -142,9 +142,12 @@ export interface Message extends MessageFn {
}

export type MessageQueueItem = {
id: string
vnode: VNode
handler: MessageHandler
vm: MessageInstance
options: MessageParamsNormalized
props: Mutable<MessageProps>
}

export type MessageQueue = MessageQueueItem[]