File size: 1,747 Bytes
8efb28e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<script lang="ts">
	import { Button, type ButtonVariant, type ButtonSize } from '$lib/components/ui/button';
	import * as Tooltip from '$lib/components/ui/tooltip';
	import type { Component } from 'svelte';
	import { TooltipSide } from '$lib/enums';

	interface Props {
		ariaLabel?: string;
		class?: string;
		disabled?: boolean;
		href?: string;
		icon: Component;
		iconSize?: string;
		onclick?: (e?: MouseEvent) => void;
		size?: ButtonSize;
		stopPropagationOnClick?: boolean;
		tooltip?: string;
		variant?: ButtonVariant;
		tooltipSide?: TooltipSide;
	}

	let {
		icon,
		tooltip,
		variant = 'ghost',
		href = '',
		size = 'sm',
		class: className = '',
		disabled = false,
		iconSize = 'h-3 w-3',
		tooltipSide = TooltipSide.TOP,
		stopPropagationOnClick = false,
		onclick,
		ariaLabel
	}: Props = $props();

	let innerWidth = $state(0);
	const showTooltip = $derived(!!tooltip && innerWidth > 768);
</script>

{#snippet button(props = {})}
	<Button
		{...props}
		{href}
		{variant}
		{size}
		{disabled}
		onclick={(e: MouseEvent) => {
			if (stopPropagationOnClick) e.stopPropagation();

			onclick?.(e);
		}}
		class="h-6 w-6 p-0 {className} flex hover:bg-transparent data-[state=open]:bg-transparent!"
		aria-label={ariaLabel || tooltip}
	>
		{#if icon}
			{@const IconComponent = icon}

			<IconComponent class={iconSize} />
		{/if}
	</Button>
{/snippet}

{#if showTooltip}
	<Tooltip.Root>
		<Tooltip.Trigger>
			<!-- prevent another nested button element -->
			{#snippet child({ props })}
				{@render button(props)}
			{/snippet}
		</Tooltip.Trigger>

		<Tooltip.Content side={tooltipSide}>
			<p>{tooltip}</p>
		</Tooltip.Content>
	</Tooltip.Root>
{:else}
	{@render button({ href })}
{/if}

<svelte:window bind:innerWidth />