Skip to content

Commit

Permalink
Merge pull request nathancahill#602 from johsunds/array-snapoffsets
Browse files Browse the repository at this point in the history
Accept array argument for snapOffset
  • Loading branch information
nathancahill authored Nov 30, 2021
2 parents cfb66f9 + f7d2f9b commit 34d2d2a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/react-split/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ SplitWrapper.propTypes = {
expandToMin: PropTypes.bool,
gutterSize: PropTypes.number,
gutterAlign: PropTypes.string,
snapOffset: PropTypes.number,
snapOffset: PropTypes.oneOfType([
PropTypes.number,
PropTypes.arrayOf(PropTypes.number),
]),
dragInterval: PropTypes.number,
direction: PropTypes.string,
cursor: PropTypes.string,
Expand Down
2 changes: 1 addition & 1 deletion packages/splitjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare namespace Split {
gutterAlign?: string

// Snap to minimum size offset in pixels.
snapOffset?: number
snapOffset?: number | number[]

dragInterval?: number

Expand Down
10 changes: 6 additions & 4 deletions packages/splitjs/src/split.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const Split = (idsOption, options = {}) => {
const gutterSize = getOption(options, 'gutterSize', 10)
const gutterAlign = getOption(options, 'gutterAlign', 'center')
const snapOffset = getOption(options, 'snapOffset', 30)
const snapOffsets = Array.isArray(snapOffset) ? snapOffset : ids.map(() => snapOffset)
const dragInterval = getOption(options, 'dragInterval', 1)
const direction = getOption(options, 'direction', HORIZONTAL)
const cursor = getOption(
Expand Down Expand Up @@ -297,20 +298,20 @@ const Split = (idsOption, options = {}) => {
// If within snapOffset of min or max, set offset to min or max.
// snapOffset buffers a.minSize and b.minSize, so logic is opposite for both.
// Include the appropriate gutter sizes to prevent overflows.
if (offset <= a.minSize + snapOffset + this[aGutterSize]) {
if (offset <= a.minSize + a.snapOffset + this[aGutterSize]) {
offset = a.minSize + this[aGutterSize]
} else if (
offset >=
this.size - (b.minSize + snapOffset + this[bGutterSize])
this.size - (b.minSize + b.snapOffset + this[bGutterSize])
) {
offset = this.size - (b.minSize + this[bGutterSize])
}

if (offset >= a.maxSize - snapOffset + this[aGutterSize]) {
if (offset >= a.maxSize - a.snapOffset + this[aGutterSize]) {
offset = a.maxSize + this[aGutterSize]
} else if (
offset <=
this.size - (b.maxSize - snapOffset + this[bGutterSize])
this.size - (b.maxSize - b.snapOffset + this[bGutterSize])
) {
offset = this.size - (b.maxSize + this[bGutterSize])
}
Expand Down Expand Up @@ -589,6 +590,7 @@ const Split = (idsOption, options = {}) => {
size: sizes[i],
minSize: minSizes[i],
maxSize: maxSizes[i],
snapOffset: snapOffsets[i],
i,
}

Expand Down

0 comments on commit 34d2d2a

Please sign in to comment.