From ab24d57f8fbb9872450075ec19f497583ce1529e Mon Sep 17 00:00:00 2001 From: Mike Lebeau Date: Fri, 20 Oct 2023 11:35:20 +0200 Subject: [PATCH] fix: update snackbar behavior -> remove stackable behavior --- packages/components/snackbar/src/notify.ts | 70 ++++++++----------- packages/components/snackbar/src/snackbar.ts | 6 ++ packages/components/snackbar/src/snackbar.vue | 4 ++ .../snackbar/stories/snackbar-use.stories.mdx | 1 + .../snackbar/stories/snackbar.stories.ts | 31 ++++++++ .../components/snackbar/test/snackbar.spec.ts | 10 ++- 6 files changed, 78 insertions(+), 44 deletions(-) diff --git a/packages/components/snackbar/src/notify.ts b/packages/components/snackbar/src/notify.ts index c86e94da..99182381 100644 --- a/packages/components/snackbar/src/notify.ts +++ b/packages/components/snackbar/src/notify.ts @@ -1,65 +1,51 @@ -import { createVNode, render, type AppContext, type VNode } from 'vue' +import { + createVNode, + render, + type AppContext, + type VNode, + ref, + type Ref, +} from 'vue' import Snackbar from './snackbar.vue' import type { PuikSnackbarOptions } from './snackbar' -const notifications: VNode[] = [] - -const GAP = 16 - -let seed = 1 +const currentNotification: Ref = ref(null) +const PUIK_SNACKBAR_ID = 'puik-snackbar-id' const notify = ( options: PuikSnackbarOptions, context: AppContext | null = null ) => { - const id = `puik-snackbar_${seed++}` const customOnClose = options.onClose - let offset = options.offset || 32 + const offset = options.offset || 32 - notifications.forEach(({ el }) => { - offset += (el?.offsetHeight || 0) + GAP - }) - const appendTo: HTMLElement | null = document.body + const documentBody: HTMLElement | null = document.body const props = { ...options, offset, - id, - onClose: () => close(id, customOnClose), + id: PUIK_SNACKBAR_ID, + onClose: () => { + currentNotification.value = null + return customOnClose + }, } - const notification = createVNode(Snackbar, props) - notification.appContext = context ?? notify._context - const container = document.createElement('div') - - notification.props!.onDestroy = () => render(null, container) - - render(notification, container) - notifications.push(notification) - appendTo.appendChild(container.firstElementChild!) - - const close = (id: string, customClose?: () => void) => { - const idx = notifications.findIndex(({ props }) => props?.id === id) - if (idx === -1) return + const newNotification = createVNode(Snackbar, props) + newNotification.appContext = context ?? notify._context - const { el } = notifications[idx] - if (!el) return - - customClose?.() - - const removedHeight = el?.offsetHeight - - notifications.splice(idx, 1) - const len = notifications.length + const container = document.createElement('div') - if (len < 1) return + newNotification.props!.onDestroy = () => render(null, container) + render(newNotification, container) + documentBody.appendChild(container.firstElementChild!) - for (let i = idx; i < len; i++) { - const { el, component } = notifications[i] - const pos = parseInt(el?.style.bottom, 10) - removedHeight - GAP - component!.props.offset = pos - } + if (currentNotification.value) { + const curNot = document.getElementById(PUIK_SNACKBAR_ID) + curNot?.remove() } + + currentNotification.value = newNotification } notify._context = null diff --git a/packages/components/snackbar/src/snackbar.ts b/packages/components/snackbar/src/snackbar.ts index 46e51e64..9b9bccb4 100644 --- a/packages/components/snackbar/src/snackbar.ts +++ b/packages/components/snackbar/src/snackbar.ts @@ -16,6 +16,7 @@ export interface PuikSnackbarOptions { action?: SnackbarAction duration?: number offset?: number + hasCloseButton?: boolean onClose?: () => void } @@ -49,6 +50,11 @@ export const snackbarProps = buildProps({ required: false, default: 0, }, + hasCloseButton: { + type: Boolean, + required: false, + default: true, + }, onClose: { type: Function, required: false, diff --git a/packages/components/snackbar/src/snackbar.vue b/packages/components/snackbar/src/snackbar.vue index 50d5cc60..97db7ece 100644 --- a/packages/components/snackbar/src/snackbar.vue +++ b/packages/components/snackbar/src/snackbar.vue @@ -7,6 +7,7 @@ >