|
| 1 | +<script lang="ts"> |
| 2 | + import type { SvelteHTMLElements } from 'svelte/elements'; |
| 3 | + import { transformContext } from 'layerchart'; |
| 4 | +
|
| 5 | + import IconArrowUturnLeft from '~icons/heroicons/arrow-uturn-left'; |
| 6 | + import IconMagnifyingGlassPlus from '~icons/heroicons/magnifying-glass-plus'; |
| 7 | + import IconMagnifyingGlassMinus from '~icons/heroicons/magnifying-glass-minus'; |
| 8 | +
|
| 9 | + import { Button } from '$lib/components/ui/button'; |
| 10 | + import { Tooltip, TooltipContent, TooltipTrigger } from '$lib/components/ui/tooltip'; |
| 11 | + import { cn } from '$lib/utils'; |
| 12 | +
|
| 13 | + type Placement = |
| 14 | + | 'top-left' |
| 15 | + | 'top' |
| 16 | + | 'top-right' |
| 17 | + | 'left' |
| 18 | + | 'center' |
| 19 | + | 'right' |
| 20 | + | 'bottom-left' |
| 21 | + | 'bottom' |
| 22 | + | 'bottom-right'; |
| 23 | +
|
| 24 | + type Actions = 'zoomIn' | 'zoomOut' | 'center' | 'reset' | 'scrollMode'; |
| 25 | + interface Props { |
| 26 | + placement?: Placement; |
| 27 | + orientation?: 'horizontal' | 'vertical'; |
| 28 | + size?: SvelteHTMLElements['svg']['width']; |
| 29 | + show?: Actions[]; |
| 30 | + class?: string; |
| 31 | + } |
| 32 | +
|
| 33 | + let { |
| 34 | + placement = 'top-right', |
| 35 | + orientation = 'vertical', |
| 36 | + size = '16', |
| 37 | + show = ['zoomIn', 'zoomOut', 'center', 'reset', 'scrollMode'], |
| 38 | + ...restProps |
| 39 | + }: Props = $props(); |
| 40 | +
|
| 41 | + const transform = transformContext(); |
| 42 | +</script> |
| 43 | + |
| 44 | +<!-- svelte-ignore a11y_no_static_element_interactions --> |
| 45 | +<div |
| 46 | + class={cn( |
| 47 | + 'bg-surface-100/50 border rounded-full m-1 backdrop-blur z-10 flex p-1', |
| 48 | + orientation === 'vertical' && 'flex-col', |
| 49 | + { |
| 50 | + 'top-left': 'absolute top-0 left-0', |
| 51 | + top: 'absolute top-0 left-1/2 -translate-x-1/2', |
| 52 | + 'top-right': 'absolute top-0 right-0', |
| 53 | + left: 'absolute top-1/2 left-0 -translate-y-1/2', |
| 54 | + center: 'absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2', |
| 55 | + right: 'absolute top-1/2 right-0 -translate-y-1/2', |
| 56 | + 'bottom-left': 'absolute bottom-0 left-0', |
| 57 | + bottom: 'absolute bottom-0 left-1/2 -translate-x-1/2', |
| 58 | + 'bottom-right': 'absolute bottom-0 right-0' |
| 59 | + }[placement], |
| 60 | + restProps.class |
| 61 | + )} |
| 62 | + ondblclick={(e) => { |
| 63 | + // Stop from propagating to TransformContext |
| 64 | + e.stopPropagation(); |
| 65 | + }} |
| 66 | +> |
| 67 | + {#if show.includes('zoomIn')} |
| 68 | + <Tooltip> |
| 69 | + <TooltipTrigger> |
| 70 | + <Button |
| 71 | + on:click={() => transform.zoomIn()} |
| 72 | + size="icon" |
| 73 | + variant="ghost" |
| 74 | + class="rounded-full" |
| 75 | + > |
| 76 | + <IconMagnifyingGlassPlus width={size} height={size} /> |
| 77 | + </Button> |
| 78 | + </TooltipTrigger> |
| 79 | + <TooltipContent side="left">Zoom in</TooltipContent> |
| 80 | + </Tooltip> |
| 81 | + {/if} |
| 82 | + |
| 83 | + {#if show.includes('zoomOut')} |
| 84 | + <Tooltip> |
| 85 | + <TooltipTrigger> |
| 86 | + <Button |
| 87 | + on:click={() => transform.zoomOut()} |
| 88 | + size="icon" |
| 89 | + variant="ghost" |
| 90 | + class="rounded-full" |
| 91 | + > |
| 92 | + <IconMagnifyingGlassMinus width={size} height={size} /> |
| 93 | + </Button> |
| 94 | + </TooltipTrigger> |
| 95 | + <TooltipContent side="left">Zoom out</TooltipContent> |
| 96 | + </Tooltip> |
| 97 | + {/if} |
| 98 | + |
| 99 | + {#if show.includes('reset')} |
| 100 | + <Tooltip> |
| 101 | + <TooltipTrigger> |
| 102 | + <Button on:click={() => transform.reset()} size="icon" variant="ghost" class="rounded-full"> |
| 103 | + <IconArrowUturnLeft width={size} height={size} /> |
| 104 | + </Button> |
| 105 | + </TooltipTrigger> |
| 106 | + <TooltipContent side="left">Reset</TooltipContent> |
| 107 | + </Tooltip> |
| 108 | + {/if} |
| 109 | +</div> |
0 commit comments