Skip to content

Commit

Permalink
feat: add snap offset
Browse files Browse the repository at this point in the history
  • Loading branch information
jahnli committed Nov 27, 2024
1 parent b1d82ce commit b7b65e7
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/split/src/Split.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,34 +186,43 @@ export default defineComponent({
snapped = true
}

if (
newASizeInPx >= minA
&& newBSizeInPx >= minB
&& (minA === 0 || newASizeInPx <= maxA)
&& (minB === 0 || newBSizeInPx <= maxB)
) {
if (typeof aSize === 'number') {
panelSizes.value[triggerIndex.value]
= newASizeInPx / containerElSizeRef.value
}
else {
panelSizes.value[triggerIndex.value] = `${newASizeInPx}px`
}
const halfSize = containerElSizeRef.value / 2
if (Math.abs(newASizeInPx - halfSize) <= snapOffset) {
newASizeInPx = halfSize
newBSizeInPx = aSizeInPx + bSizeInPx - halfSize
snapped = true
}

if (bSize === 'auto') {
panelSizes.value[triggerIndex.value + 1] = `${newBSizeInPx}px`
}
else if (typeof bSize === 'number') {
panelSizes.value[triggerIndex.value + 1]
= newBSizeInPx / containerElSizeRef.value
}
else {
panelSizes.value[triggerIndex.value + 1] = `${newBSizeInPx}px`
}
newASizeInPx = Math.max(
minA,
Math.min(maxA || containerElSizeRef.value, newASizeInPx)
)
newBSizeInPx = Math.max(
minB,
Math.min(maxB || containerElSizeRef.value, newBSizeInPx)
)

if (typeof aSize === 'number') {
panelSizes.value[triggerIndex.value]
= newASizeInPx / containerElSizeRef.value
}
else {
panelSizes.value[triggerIndex.value] = `${newASizeInPx}px`
}

if (!snapped) {
dragStartPos.value = currentPosition
}
if (bSize === 'auto') {
panelSizes.value[triggerIndex.value + 1] = `${newBSizeInPx}px`
}
else if (typeof bSize === 'number') {
panelSizes.value[triggerIndex.value + 1]
= newBSizeInPx / containerElSizeRef.value
}
else {
panelSizes.value[triggerIndex.value + 1] = `${newBSizeInPx}px`
}

if (!snapped) {
dragStartPos.value = currentPosition
}
}

Expand Down

0 comments on commit b7b65e7

Please sign in to comment.