Skip to content

Dismiss gallery lightbox on background click #752

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

Merged
merged 2 commits into from
Sep 7, 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
25 changes: 25 additions & 0 deletions src/components/sidebar/tabs/queue/ResultGallery.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
fullScreen
circular
:showThumbnails="false"
:pt="{
mask: {
onMousedown: onMaskMouseDown,
onMouseup: onMaskMouseUp,
'data-mask': true
}
}"
>
<template #item="{ item }">
<ComfyImage
Expand Down Expand Up @@ -40,6 +47,24 @@ const props = defineProps<{
activeIndex: number
}>()

let maskMouseDownTarget: EventTarget | null = null

const onMaskMouseDown = (event: MouseEvent) => {
maskMouseDownTarget = event.target
}

const onMaskMouseUp = (event: MouseEvent) => {
const maskEl = document.querySelector('[data-mask]')
if (
galleryVisible.value &&
maskMouseDownTarget === event.target &&
maskMouseDownTarget === maskEl
) {
galleryVisible.value = false
handleVisibilityChange(false)
}
}

watch(
() => props.activeIndex,
(index) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { mount } from '@vue/test-utils'
import { expect, describe, it } from 'vitest'
import ResultGallery from '../ResultGallery.vue'
import Galleria from 'primevue/galleria'
import ComfyImage from '@/components/common/ComfyImage.vue'
import PrimeVue from 'primevue/config'
import { ResultItemImpl } from '@/stores/queueStore'

type ResultGalleryProps = typeof ResultGallery.__props

describe('ResultGallery', () => {
let mockResultItem: ResultItemImpl

beforeEach(() => {
mockResultItem = {
filename: 'test.jpg',
type: 'images',
nodeId: 'test',
mediaType: 'images',
url: 'https://picsum.photos/200/300',
urlWithTimestamp: 'https://picsum.photos/200/300?t=123456',
supportsPreview: true
}
})

const mountResultGallery = (props: ResultGalleryProps, options = {}) => {
return mount(ResultGallery, {
global: {
plugins: [PrimeVue],
components: { Galleria, ComfyImage }
},
props,
...options
})
}

const clickElement = async (element: Element) => {
element.dispatchEvent(new MouseEvent('mousedown'))
element.dispatchEvent(new MouseEvent('mouseup'))
}

it('is dismissed when overlay mask is clicked', async () => {
const wrapper = mountResultGallery({
activeIndex: 0,
allGalleryItems: [mockResultItem]
})
wrapper.vm.galleryVisible = true
await wrapper.vm.$nextTick()
expect(wrapper.findComponent(Galleria).exists()).toBe(true)
expect(wrapper.vm.galleryVisible).toBe(true)

// Since Galleria uses teleport, we need to query the mask in the global document
const mask = document.querySelector('[data-mask]')
expect(mask).not.toBeNull()

// Click the overlay mask to dismiss the gallery
await clickElement(mask)
await wrapper.vm.$nextTick()
expect(wrapper.vm.galleryVisible).toBe(false)
})

it('is not dismissed when gallery is clicked', async () => {
const wrapper = mountResultGallery({
activeIndex: 0,
allGalleryItems: [mockResultItem]
})
wrapper.vm.galleryVisible = true
await wrapper.vm.$nextTick()
expect(wrapper.findComponent(Galleria).exists()).toBe(true)
expect(wrapper.vm.galleryVisible).toBe(true)

// Since Galleria uses teleport, we need to query the mask in the global document
const gallery = document.querySelector('.p-galleria-content')
expect(gallery).not.toBeNull()

// The gallery should not be dismissed when the gallery itself is clicked
await clickElement(gallery)
await wrapper.vm.$nextTick()
expect(wrapper.vm.galleryVisible).toBe(true)
})
})
Loading