From 8dd4192207dbd883e7b70b1b9bd11349b655cc4c Mon Sep 17 00:00:00 2001 From: tyh2001 <1469442737@qq.com> Date: Sun, 29 Jan 2023 15:07:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=81=E8=A3=85=E6=96=B0=E7=9A=84=20?= =?UTF-8?q?message=20hook=20=F0=9F=8F=B7=EF=B8=8F=F0=9F=8F=B7=EF=B8=8F?= =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/fighting-design/_hooks/index.ts | 1 + .../fighting-design/_hooks/use-eject/index.ts | 129 ++++++++++++++++++ .../_hooks/use-eject/interface.d.ts | 31 +++++ .../fighting-design/message/src/message.vue | 110 +++------------ .../notification/src/notification.vue | 108 +++------------ start/src/App.vue | 37 ++--- 6 files changed, 211 insertions(+), 205 deletions(-) create mode 100644 packages/fighting-design/_hooks/use-eject/index.ts create mode 100644 packages/fighting-design/_hooks/use-eject/interface.d.ts diff --git a/packages/fighting-design/_hooks/index.ts b/packages/fighting-design/_hooks/index.ts index ff683fbff2..320e905855 100644 --- a/packages/fighting-design/_hooks/index.ts +++ b/packages/fighting-design/_hooks/index.ts @@ -17,3 +17,4 @@ export * from './use-visible' export * from './use-tabs-nav-style' export * from './use-button' export * from './use-tips' +export * from './use-eject' diff --git a/packages/fighting-design/_hooks/use-eject/index.ts b/packages/fighting-design/_hooks/use-eject/index.ts new file mode 100644 index 0000000000..9e7748023b --- /dev/null +++ b/packages/fighting-design/_hooks/use-eject/index.ts @@ -0,0 +1,129 @@ +import { useList, useTips } from '..' +import { onMounted, nextTick, ref, computed } from 'vue' +import type { Ref, CSSProperties } from 'vue' +import type { MessageProps } from '../../message' +import type { NotificationProps } from '../../notification' +import type { UseEjectReturn } from './interface' + +export * from './interface.d' + +/** + * 针对 message notification 组件公共方法封装 + * + * @param prop props 参数 + * @param component 组件名 + * @param el 元素节点 + */ +export const useEject = ( + prop: MessageProps | NotificationProps, + component: 'message' | 'notification', + el: Ref +): UseEjectReturn => { + + /** 元素的高度 */ + const elHeight = ref(0) + + /** 控制显示隐藏 */ + const visible = ref(false) + + /** 计时器 */ + const timer = ref() + + const { classes, styles } = useList(prop, component) + const { getSiblingOffset, removeInstance } = useTips() + + /** 类名列表 */ + const classList = classes(['type', 'placement', 'round'], `f-${component}`) + + /** 样式列表 */ + const styleList = styles(['color', 'background', 'zIndex'], 'zIndex') + + onMounted((): void => { + nextTick((): void => { + /** + * 设置元素的高度 + * + * @see getBoundingClientRec https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRec + */ + elHeight.value = el.value ? el.value.getBoundingClientRect().height : 0 + }) + }) + + /** 判断方位 */ + const isPosition = computed((): boolean => prop.placement.includes(component === 'message' ? 'top' : 'right')) + + /** 计算组件之间偏移量 */ + const siblingOffset = computed((): number => getSiblingOffset(prop.placement, prop.id, !isPosition.value)) + + /** 计算偏移量 */ + const offset = computed((): number => prop.offset + siblingOffset.value) + + /** 底部偏移量 */ + const bottom = computed((): number => elHeight.value + offset.value) + + /** 位置偏移量样式列表 */ + const offsetStyle = computed((): CSSProperties => { + const styles: CSSProperties = {} + + if (prop.placement.includes('bottom')) { + styles.bottom = offset.value + 'px' + } else { + styles.top = offset.value + 'px' + } + + return styles + }) + + /** 清除计时器 */ + const clearTimer = (): void => { + if (!timer.value) return + clearTimeout(timer.value) + } + + /** 关闭提示框 */ + const closeMessage = (): void => { + clearTimer() + visible.value = false + } + + /** + * 关闭提示框之后的回调 + * + * 移除组件实例 + */ + const closeMessageEnd = (): void => { + removeInstance(prop.placement, prop.id) + } + + /** + * 开始计时 + * + * 到时间隐藏提示框 + */ + const startTime = (): void => { + if (!prop.duration) return + timer.value = setTimeout((): void => { + closeMessage() + }, prop.duration) + } + + /** 初始化之后开始计时 并且展示提示 */ + onMounted((): void => { + startTime() + visible.value = true + }) + + return { + classList, + styleList, + elHeight, + bottom, + offsetStyle, + isPosition, + visible, + clearTimer, + closeMessage, + closeMessageEnd, + startTime + } +} diff --git a/packages/fighting-design/_hooks/use-eject/interface.d.ts b/packages/fighting-design/_hooks/use-eject/interface.d.ts new file mode 100644 index 0000000000..bbfd3f9493 --- /dev/null +++ b/packages/fighting-design/_hooks/use-eject/interface.d.ts @@ -0,0 +1,31 @@ +import type { ComputedRef, CSSProperties, Ref } from 'vue' +import type { ClassList } from '../../_interface' + +/** + * useEject 返回值类型接口 + * + * @param classList 类名列表 + * @param styleList 样式列表 + * @param elHeight 元素的高度 + * @param bottom 底部偏移量 + * @param offsetStyle 位置偏移量样式列表 + * @param isPosition 判断方位 + * @param visible 控制显示隐藏 + * @param clearTimer 清除计时器 + * @param closeMessage 关闭提示框 + * @param closeMessageEnd 关闭提示框之后的回调 + * @param startTime 开始计时 + */ +export interface UseEjectReturn { + classList: ComputedRef + styleList: ComputedRef + elHeight: Ref + bottom: ComputedRef + offsetStyle: ComputedRef + isPosition: ComputedRef + visible: Ref + clearTimer: () => void + closeMessage: () => void + closeMessageEnd: () => void + startTime: () => void +} diff --git a/packages/fighting-design/message/src/message.vue b/packages/fighting-design/message/src/message.vue index fdea13c7ff..4cfbe68af7 100644 --- a/packages/fighting-design/message/src/message.vue +++ b/packages/fighting-design/message/src/message.vue @@ -1,110 +1,32 @@ - -