Skip to content
Open
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
17 changes: 16 additions & 1 deletion sandbox/pages/SandboxToaster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
title="KToaster"
>
<div class="ktoaster-sandbox">
<div class="toast-overlay" />

<!-- Props -->
<SandboxTitleComponent
is-subtitle
Expand Down Expand Up @@ -152,7 +154,7 @@ const openToaster = (argument: string) => {
title: 'Timeout',
message: 'This is toaster with timeout (10s)',
appearance: 'info',
timeoutMilliseconds: 10000,
timeoutMilliseconds: 1000000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: why is this changing to 1,000 seconds? (I know this is sandbox only)

}
break
}
Expand All @@ -168,10 +170,23 @@ const multipleToastersHandler = () => {

<style lang="scss" scoped>
.ktoaster-sandbox {
height: 100%;
position: relative;

.horizontal-container {
display: flex;
flex-wrap: wrap;
gap: $kui-space-50;
}

.toast-overlay {
background-color: red;
bottom: 16px;
height: 100px;
position: absolute;
right: 16px;
width: 200px;
z-index: 2;
}
}
</style>
25 changes: 2 additions & 23 deletions src/components/KToaster/KToaster.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import KToaster from '@/components/KToaster/KToaster.vue'

const toastersContainerId = 'kongponents-toaster-container'

describe('KToaster', () => {
it('renders toaster', () => {
const toasts = []
Expand Down Expand Up @@ -50,29 +52,6 @@ describe('KToaster', () => {
cy.get('.toaster .toaster-close-icon').should('be.visible')
})

it('renders toast with correct default z-index', () => {

cy.mount(KToaster, {
props: {
toasterState: [{ title: 'I have a toast' }],
},
})

cy.get('.k-toaster').should('have.css', 'z-index', '10000')
})

it('renders toast with custom z-index', () => {
cy.mount(KToaster, {
props: {
toasterState: [{ title: 'I have a toast' }],
zIndex: 9999,
},
})

cy.get('.k-toaster').should('have.css', 'z-index', '9999')
})


it('shows close button even if content is long', () => {
const longTitle = 'title'.repeat(20)
const longMessage = 'message'.repeat(20)
Expand Down
2 changes: 0 additions & 2 deletions src/components/KToaster/KToaster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type ToastIcon = typeof InfoIcon | typeof CheckCircleIcon | typeof WarningIcon |

const {
toasterState = [],
zIndex = 10000,
} = defineProps<ToasterProps>()

const emit = defineEmits<ToasterEmits>()
Expand Down Expand Up @@ -120,7 +119,6 @@ const getToastIcon = (appearance?: ToasterAppearance): ToastIcon => {
display: flex;
flex-direction: column;
gap: var(--kui-space-50, $kui-space-50);
z-index: v-bind('zIndex');

.toaster {
align-items: flex-start;
Expand Down
19 changes: 19 additions & 0 deletions src/components/KToaster/ToastManager.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,23 @@ describe('ToastManager', () => {
toastManager2.destroy()
toastManager3.destroy()
})

it('creates toasters container with correct default z-index', () => {
const toastManager = new ToastManager()
const container = document.getElementById(toastersContainerId)
expect(container).to.have.css('z-index', '10000')

// Cleanup
toastManager.destroy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would it make sense to call destroy in an afterEach hook for the whole file?

})

it('creates toasters container with correct z-index when provided', () => {
const zIndex = 9999
const toastManager = new ToastManager({ zIndex })
const container = document.getElementById(toastersContainerId)
expect(container).to.have.css('z-index', String(zIndex))

// Cleanup
toastManager.destroy()
})
})
7 changes: 4 additions & 3 deletions src/components/KToaster/ToastManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ const defaultZIndex = 10000
* Get or create the shared toaster container element.
* Increments the reference count for tracking active instances.
*/
function getOrCreateSharedContainer(): HTMLElement {
function getOrCreateSharedContainer(zIndex: number): HTMLElement {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: make this an object interface in case we need to add more properties later

Suggested change
function getOrCreateSharedContainer(zIndex: number): HTMLElement {
function getOrCreateSharedContainer({ zIndex }: { zIndex: number }): HTMLElement {

Also, should the default value be enforced here?

let container = document.getElementById(toasterContainerId)

if (!container) {
container = document.createElement('div')
container.id = toasterContainerId
container.setAttribute(containerInstanceCountAttribute, '0')
container.style.zIndex = String(zIndex)
document.body.appendChild(container)
}

Expand Down Expand Up @@ -73,7 +74,8 @@ export default class ToastManager {
}

// Get or create the shared container
this.sharedContainer = getOrCreateSharedContainer()
const zIndex = options?.zIndex ? options.zIndex : defaultZIndex
this.sharedContainer = getOrCreateSharedContainer(zIndex)

// Create this instance's wrapper div
this.instanceWrapper = document.createElement('div')
Expand All @@ -83,7 +85,6 @@ export default class ToastManager {
// Create the KToaster VNode
this.toaster = createVNode(KToaster, {
toasterState: this.toasts.value,
zIndex: options?.zIndex ? options.zIndex : defaultZIndex,
onClose: (key: string) => this.close(key),
})

Expand Down
3 changes: 1 addition & 2 deletions src/types/toaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export interface ToasterProps {
toasterState: Array<Toast & { key: string }>

/**
* The z-index of the toaster.
* @default 10000
* @deprecated zIndex provided through ToasterOptions is set on the shared container on initialization. This prop is no longer used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the @default 10000 comment in place along with this new @deprecated

*/
zIndex?: number
}
Expand Down
Loading