Skip to content
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
43 changes: 25 additions & 18 deletions app/components/ButtonBackground.vue
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
<template>
<button
v-bind="previewAttributes"
class="rounded-emphasis relative h-20 w-24 cursor-pointer overflow-hidden focus:outline-hidden"
:class="active ? 'ring-1 ring-violet-500 dark:ring-violet-400' : ''"
>
<img
v-if="thumbnail"
:src="thumbnail"
alt=""
loading="eager"
decoding="async"
draggable="false"
class="absolute inset-0 size-full object-cover"
/>
<div class="relative h-20 w-24">
<button
v-bind="{ ...previewAttributes, ...$attrs }"
type="button"
class="rounded-emphasis relative size-full cursor-pointer overflow-hidden focus:outline-hidden focus-visible:ring-2 focus-visible:ring-violet-500"
:class="active ? 'ring-1 ring-violet-500 dark:ring-violet-400' : ''"
>
<img
v-if="thumbnail"
:src="thumbnail"
alt=""
loading="eager"
decoding="async"
draggable="false"
class="absolute inset-0 size-full object-cover"
/>

<slot />
</button>

<button
v-if="custom"
type="button"
aria-label="Delete custom background"
@click="$emit('delete')"
class="absolute -top-2 -right-2 z-10 inline-flex size-6 items-center justify-center rounded-full bg-zinc-800 shadow-sm active:bg-zinc-400 dark:bg-zinc-400 dark:active:bg-zinc-500"
class="absolute top-1 right-1 z-10 inline-flex size-6 cursor-pointer items-center justify-center rounded-full bg-zinc-800 text-white shadow-sm hover:bg-zinc-700 focus-visible:ring-2 focus-visible:ring-violet-500 focus-visible:outline-hidden active:bg-zinc-600"
>
<XIcon class="size-4 text-white" />
</button>

<slot />
</button>
</div>
</template>

<script setup>
import { computed } from 'vue';
import { XIcon } from 'lucide-vue-next';

defineOptions({ inheritAttrs: false });

const props = defineProps({
custom: Boolean,
active: Boolean,
Expand Down
36 changes: 36 additions & 0 deletions tests/components/ButtonBackground.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { mount } from '@vue/test-utils';
import { describe, expect, it, vi } from 'vitest';
import ButtonBackground from '~/components/ButtonBackground.vue';

describe('ButtonBackground', () => {
it('deletes a custom background without selecting it', async () => {
const select = vi.fn();
const wrapper = mount(ButtonBackground, {
props: { custom: true },
attrs: { onClick: select },
});

await wrapper.get('button[aria-label="Delete custom background"]').trigger('click');

expect(wrapper.emitted('delete')).toHaveLength(1);
expect(select).not.toHaveBeenCalled();
expect(wrapper.find('button button').exists()).toBe(false);
});

it('keeps selection attributes and events on the preview button', async () => {
const select = vi.fn();
const wrapper = mount(ButtonBackground, {
props: {
attributes: { style: { backgroundColor: 'red' } },
},
attrs: { 'data-ref': 'background-test', onClick: select },
});

const preview = wrapper.get('button[data-ref="background-test"]');
await preview.trigger('click');

expect(preview.element.style.backgroundColor).toBe('red');
expect(select).toHaveBeenCalledOnce();
expect(wrapper.find('[aria-label="Delete custom background"]').exists()).toBe(false);
});
});
Loading