budibase/packages/bbui/src/Icon/Icon.svelte

92 lines
2.0 KiB
Svelte
Raw Normal View History

2021-04-16 12:24:06 +02:00
<script context="module">
export const directions = ["n", "ne", "e", "se", "s", "sw", "w", "nw"]
2021-04-16 12:24:06 +02:00
</script>
<script>
import Tooltip from "../Tooltip/Tooltip.svelte"
import { fade } from "svelte/transition"
export let direction = "n"
export let name = "Add"
export let hidden = false
2021-04-27 16:30:13 +02:00
export let size = "M"
export let hoverable = false
export let disabled = false
export let color
export let tooltip
2021-04-16 12:24:06 +02:00
2021-06-15 20:36:56 +02:00
$: rotation = getRotation(direction)
let showTooltip = false
2021-06-15 20:36:56 +02:00
const getRotation = direction => {
return directions.indexOf(direction) * 45
}
2021-04-16 12:24:06 +02:00
</script>
<div
2022-04-25 13:46:45 +02:00
class="icon"
on:mouseover={() => (showTooltip = true)}
on:focus={() => (showTooltip = true)}
on:mouseleave={() => (showTooltip = false)}
2022-04-25 13:46:45 +02:00
on:click={() => (showTooltip = false)}
2021-04-23 10:33:41 +02:00
>
<svg
on:click
class:hoverable
class:disabled
class="spectrum-Icon spectrum-Icon--size{size}"
focusable="false"
aria-hidden={hidden}
aria-label={name}
style={`transform: rotate(${rotation}deg); ${
color ? `color: ${color};` : ""
}`}
>
<use style="pointer-events: none;" xlink:href="#spectrum-icon-18-{name}" />
</svg>
{#if tooltip && showTooltip}
<div class="tooltip" in:fade={{ duration: 130, delay: 250 }}>
<Tooltip textWrapping direction="bottom" text={tooltip} />
</div>
{/if}
</div>
<style>
2022-04-25 13:46:45 +02:00
.icon {
position: relative;
display: grid;
place-items: center;
}
svg.hoverable {
pointer-events: all;
transition: color var(--spectrum-global-animation-duration-100, 130ms);
}
svg.hoverable:hover {
color: var(--spectrum-alias-icon-color-selected-hover) !important;
cursor: pointer;
}
svg.disabled {
color: var(--spectrum-global-color-gray-500) !important;
pointer-events: none !important;
}
.tooltip {
position: absolute;
pointer-events: none;
left: 50%;
2022-04-25 13:46:45 +02:00
top: calc(100% + 4px);
width: 100vw;
max-width: 150px;
transform: translateX(-50%);
2022-04-25 13:46:45 +02:00
text-align: center;
}
2022-08-17 15:46:38 +02:00
.spectrum-Icon--sizeXS {
width: 10px;
height: 10px;
}
</style>