-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb14459
commit a8af968
Showing
6 changed files
with
146 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { isFunction, wrap } from 'lodash'; | ||
import React, { useRef, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import cx from 'classnames'; | ||
import { sortableContainer, sortableElement, sortableHandle } from 'react-sortable-hoc'; | ||
|
||
import './style.less'; | ||
|
||
export const DragHandle = sortableHandle(({ className, ...restProps }) => ( | ||
<div className={cx('drag-handle', className)} {...restProps} /> | ||
)); | ||
|
||
export const SortableContainerWrapper = sortableContainer(({ children }) => children); | ||
|
||
export const SortableElement = sortableElement(({ children }) => children); | ||
|
||
export function SortableContainer({ disabled, containerProps, children, ...wrapperProps }) { | ||
const containerRef = useRef(); | ||
const [isDragging, setIsDragging] = useState(false); | ||
|
||
wrapperProps = { ...wrapperProps }; | ||
containerProps = { ...containerProps }; | ||
|
||
if (disabled) { | ||
// Disabled state: | ||
// - forbid drag'n'drop (and therefore no need to hook events | ||
// - don't override anything on container element | ||
wrapperProps.shouldCancelStart = () => true; | ||
} else { | ||
// Enabled state: | ||
|
||
// - use container element as a default helper element | ||
wrapperProps.helperContainer = wrap(wrapperProps.helperContainer, helperContainer => ( | ||
isFunction(helperContainer) ? | ||
helperContainer(containerRef.current) : | ||
containerRef.current | ||
)); | ||
|
||
// - hook drag start/end events | ||
wrapperProps.updateBeforeSortStart = wrap(wrapperProps.updateBeforeSortStart, (updateBeforeSortStart, ...args) => { | ||
setIsDragging(true); | ||
if (isFunction(updateBeforeSortStart)) { | ||
updateBeforeSortStart(...args); | ||
} | ||
}); | ||
wrapperProps.onSortEnd = wrap(wrapperProps.onSortEnd, (onSortEnd, ...args) => { | ||
setIsDragging(false); | ||
if (isFunction(onSortEnd)) { | ||
onSortEnd(...args); | ||
} | ||
}); | ||
|
||
// - update container element: add classes and take a ref | ||
containerProps.className = cx( | ||
'sortable-container', | ||
{ 'sortable-container-dragging': isDragging }, | ||
containerProps.className, | ||
); | ||
containerProps.ref = containerRef; | ||
} | ||
|
||
// order of props matters - we override some of them | ||
return ( | ||
<SortableContainerWrapper {...wrapperProps}> | ||
<div {...containerProps}>{children}</div> | ||
</SortableContainerWrapper> | ||
); | ||
} | ||
|
||
SortableContainer.propTypes = { | ||
disabled: PropTypes.bool, | ||
containerProps: PropTypes.object, // eslint-disable-line react/forbid-prop-types | ||
children: PropTypes.node, | ||
}; | ||
|
||
SortableContainer.defaultProps = { | ||
disabled: false, | ||
containerProps: {}, | ||
children: null, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.drag-handle { | ||
vertical-align: bottom; | ||
cursor: move; | ||
|
||
display: inline-flex; | ||
align-items: stretch; | ||
justify-content: center; | ||
|
||
&:before { | ||
content: ''; | ||
display: block; | ||
width: 6px; | ||
|
||
background: | ||
linear-gradient(90deg, transparent 0px, white 1px, white 2px) center, | ||
linear-gradient(transparent 0px, white 1px, white 2px) center, | ||
#111111; | ||
background-size: 2px 2px; | ||
} | ||
} | ||
|
||
.sortable-container { | ||
transition: background-color 200ms ease-out; | ||
transition-delay: 300ms; // short pause before returning to original bgcolor | ||
|
||
&.sortable-container-dragging { | ||
transition-delay: 0s; | ||
background-color: #f6f8f9; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters