Add labels to DND to describe where the component will be dropped

This commit is contained in:
Andrew Kingston 2021-09-16 15:08:42 +01:00
parent a05dc7e295
commit 2fc9672bfa
3 changed files with 46 additions and 27 deletions

View File

@ -1,6 +1,7 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import IndicatorSet from "./IndicatorSet.svelte" import IndicatorSet from "./IndicatorSet.svelte"
import Indicator from "./Indicator.svelte"
import DNDPositionIndicator from "./DNDPositionIndicator.svelte" import DNDPositionIndicator from "./DNDPositionIndicator.svelte"
import { builderStore } from "stores" import { builderStore } from "stores"
@ -106,6 +107,7 @@
const child = getDOMNodeForComponent(e.target) const child = getDOMNodeForComponent(e.target)
const bounds = child.getBoundingClientRect() const bounds = child.getBoundingClientRect()
dropInfo = { dropInfo = {
name: element.dataset.name,
droppableInside: element.dataset.droppableInside === "true", droppableInside: element.dataset.droppableInside === "true",
bounds, bounds,
} }
@ -160,6 +162,10 @@
prefix="Inside" prefix="Inside"
/> />
{#if dropMode !== "inside" && dropInfo} <DNDPositionIndicator
<DNDPositionIndicator bounds={dropInfo.bounds} mode={dropMode} zIndex="940" /> dropInfo={dropMode !== "inside" ? dropInfo : null}
{/if} mode={dropMode}
color="var(--spectrum-global-color-static-green-500)"
zIndex="940"
transition
/>

View File

@ -1,33 +1,42 @@
<script> <script>
export let bounds import Indicator from "./Indicator.svelte"
export let dropInfo
export let mode export let mode
export let zIndex export let zIndex
export let color
export let transition
$: x = bounds?.left $: dimensions = getDimensions(dropInfo?.bounds, mode)
$: y = getYPos(bounds, mode) $: prefix = mode === "above" ? "Above" : "Below"
$: width = bounds?.width $: text = `${prefix} ${dropInfo?.name}`
$: valid = bounds != null
const getYPos = (bounds, mode) => { const getDimensions = (bounds, mode) => {
if (!bounds || !mode) { if (!bounds || !mode) {
return null return null
} }
const { top, height } = bounds const { left, top, width, height } = bounds
return mode === "above" ? top - 2 : top + height return {
top: mode === "above" ? top - 4 : top + height,
left: left - 2,
width: width + 4,
}
} }
</script> </script>
{#if valid} {#key mode}
<div {#if dimensions}
class="indicator" <Indicator
style={`top:${y}px;left:${x}px;width:${width}px;z-index:${zIndex};`} left={dimensions.left}
top={dimensions.top}
width={dimensions.width}
height={0}
{text}
{zIndex}
{color}
{transition}
flip={mode === "below"}
rounded
/> />
{/if} {/if}
{/key}
<style>
.indicator {
position: absolute;
height: 2px;
background: var(--spectrum-global-color-static-green-500);
}
</style>

View File

@ -9,6 +9,9 @@
export let color export let color
export let zIndex export let zIndex
export let transition = false export let transition = false
export let flip = false
$: flipped = flip || top < 20
</script> </script>
<div <div
@ -18,11 +21,11 @@
}} }}
out:fade={{ duration: transition ? 130 : 0 }} out:fade={{ duration: transition ? 130 : 0 }}
class="indicator" class="indicator"
class:flipped={top < 20} class:flipped
style="top: {top}px; left: {left}px; width: {width}px; height: {height}px; --color: {color}; --zIndex: {zIndex};" style="top: {top}px; left: {left}px; width: {width}px; height: {height}px; --color: {color}; --zIndex: {zIndex};"
> >
{#if text} {#if text}
<div class="text" class:flipped={top < 20}> <div class="text" class:flipped>
{text} {text}
</div> </div>
{/if} {/if}
@ -63,6 +66,7 @@
} }
.text.flipped { .text.flipped {
border-top-left-radius: 4px; border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
transform: translateY(0%); transform: translateY(0%);
top: -2px; top: -2px;
} }