Repository navigation
feat(dnd): support dragging collection items from handles only - #10738
reidbarber wants to merge 45 commits into
Conversation
…rDragSource?: 'item' | 'dragButton'`
|
Build successful! 🎉 |
|
|
||
| // Playwright's Firefox reports an empty pointerType for mouse presses, causing | ||
| // usePress to treat the release as virtual and start accessible dragging. | ||
| const itDrag = it.skipIf(/firefox/i.test(navigator.userAgent)); |
There was a problem hiding this comment.
Skipping Firefox in the browser tests due to Playwright issues around pointerType. Everything works as expected in Firefox (non-Playwright) though. We could default the pointerType to 'mouse' in usePress to fix this, but I really didn't want to change usePress in this PR.
|
Build successful! 🎉 |
catherine
left a comment
There was a problem hiding this comment.
I need to loop back to check out/test the Stories, but this is looking good to me -- I also liked term swap from "dragButton" to "handle"
…andle on hover and keyboard focus. For mobile, always allow dragging from item
|
Build successful! 🎉 |
|
Build successful! 🎉 |
|
Build successful! 🎉 |
LFDanLu
left a comment
There was a problem hiding this comment.
do we want to consider adding this support to RAC ListBox as well? I know interactive elements are not supported inside ListBox items but maybe a user may want to set drag handle only dragging so that the text inside a listbox item is copyable via mouse click + drag.
| // Spans the start padding and drag handle, stopping before the expand button. | ||
| default: 14, | ||
| // Also spans the checkbox's marginStart, which doesn't scale. | ||
| isCheckboxVisible: '[calc(0.875rem * var(--s2-scale) + 0.5rem)]' |
There was a problem hiding this comment.
is the checkbox's marginStart not just 8?
There was a problem hiding this comment.
We could do calc(${size(14)} + ${space(8)}) to make it easier to read, here, but just 22 here would scale the margin too, which isn't right on touch devices.
|
|
||
| GridList supports drag and drop interactions when the `dragAndDropHooks` prop is provided using the <TypeLink links={docs.links} type={docs.exports.useDragAndDrop} /> hook. Users can drop data on the list as a whole, on individual items, insert new items between existing ones, or reorder items. React Aria supports drag and drop via mouse, touch, keyboard, and screen reader interactions. See the [drag and drop guide](dnd?component=GridList) to learn more. | ||
|
|
||
| If items contain interactive elements such as text fields, set `pointerDragSource: 'handle'` to restrict mouse and touch dragging to the drag handle. |
There was a problem hiding this comment.
might be worth a direct link to the specific section in the dnd docs, same for the other mentions in the other RAC and S2 components
@LFDanLu I guess it would be more like a fake button where you would restrict pointer dragging to that element's content area? It wouldn't be keyboard-focusable, which would be strange, and it seems hacky for the user to have to style it as focused when the item itself is what's focused. Also it would require a completely different API, so I would in favor of holding off. |
|
Build successful! 🎉 |
|
Build successful! 🎉 |
|
Build successful! 🎉 |
snowystinger
left a comment
There was a problem hiding this comment.
Test that asserts you can't drag from elsewhere? i see we check !draggable, but what happens if we try to drag anyways? or is this truly a good enough check? (some ignorance on my end about dragging)
| ); | ||
| } | ||
|
|
||
| // Firefox doesn't isolate focus and hover state between pages, so these tests are flaky when test files run in parallel. |
There was a problem hiding this comment.
I believe I've set concurrency to 1 so that they can't be run in parallel anymore since FF was so very bad, so can remove this comment now i hope
setting was fileParallelism: false,
hopefully means we can stop skipping it as well
| * Native drag props, only provided via context by collection items whose drag button is the | ||
| * native drag source (e.g. pointerDragSource="handle"). Drag event handlers remain on the item. | ||
| */ | ||
| nativeDragProps?: ButtonNativeDragProps; |
There was a problem hiding this comment.
anything wrong with exposing the draggable attributes on buttons in general?
There was a problem hiding this comment.
hmm I guess not, I'll add them.
| <GridList aria-label="Test" selectionMode="multiple" dragAndDropHooks={dragAndDropHooks}> | ||
| <GridListItem id="cat" textValue="Cat"> | ||
| <Button slot="drag">≡</Button> | ||
| <ShadowText text="Cat" /> |
There was a problem hiding this comment.
why shadow text? what about shadow button?
| } | ||
| }); | ||
|
|
||
| // Expands the handle's touch target without affecting layout. The pseudo-element is positioned |
There was a problem hiding this comment.
probably should apply this in the starters as well, they're a little small
| // Expands the handle's touch target without affecting layout. The pseudo-element is positioned | ||
| // relative to the item (or cell), spanning its full height from the start edge to | ||
| // --dragHandleHitAreaWidth, so it doesn't overlap the selection checkbox. | ||
| const dragHandleHitArea = css(` | ||
| @media not ((hover: hover) and (pointer: fine)) { | ||
| &::before { | ||
| content: ""; | ||
| position: absolute; | ||
| top: 0; | ||
| bottom: 0; | ||
| inset-inline-start: 0; | ||
| width: var(--dragHandleHitAreaWidth); | ||
| } | ||
| } | ||
| `); |
There was a problem hiding this comment.
…ox hover/focus tests
|
@snowystinger In that ListView screenshot, is touch device emulation on? It should only appear in that case. |
|
Ahhh, that makes sense |
|
Build successful! 🎉 |
## API Changes
react-aria-components/react-aria-components:Button-Button {
+Button extends HTMLAttributes {
aria-controls?: string
aria-current?: boolean | 'true' | 'false' | 'page' | 'step' | 'location' | 'date' | 'time'
aria-describedby?: string
aria-disabled?: boolean | 'true' | 'false'
aria-expanded?: boolean | 'true' | 'false'
aria-haspopup?: boolean | 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog' | 'true' | 'false'
aria-label?: string
aria-labelledby?: string
aria-pressed?: boolean | 'true' | 'false' | 'mixed'
autoFocus?: boolean
children?: ChildrenOrFunction<ButtonRenderProps>
className?: ClassNameOrFunction<ButtonRenderProps> = 'react-aria-Button'
excludeFromTabOrder?: boolean
form?: string
formAction?: ButtonHTMLAttributes<HTMLButtonElement>['formAction']
formEncType?: string
formMethod?: string
formNoValidate?: boolean
formTarget?: string
id?: string
isDisabled?: boolean
isPending?: boolean
name?: string
onBlur?: (FocusEvent<Target>) => void
onClick?: (MouseEvent<FocusableElement>) => void
onFocus?: (FocusEvent<Target>) => void
onFocusChange?: (boolean) => void
onHoverChange?: (boolean) => void
onHoverEnd?: (HoverEvent) => void
onHoverStart?: (HoverEvent) => void
onKeyDown?: (KeyboardEvent) => void
onKeyUp?: (KeyboardEvent) => void
onPress?: (PressEvent) => void
onPressChange?: (boolean) => void
onPressEnd?: (PressEvent) => void
onPressStart?: (PressEvent) => void
onPressUp?: (PressEvent) => void
preventFocusOnPress?: boolean
render?: DOMRenderFunction<keyof React.JSX.IntrinsicElements, ButtonRenderProps>
slot?: string | null
style?: StyleOrFunction<ButtonRenderProps>
type?: 'button' | 'submit' | 'reset' = 'button'
value?: string
}/react-aria-components:DragAndDropOptions DragAndDropOptions <T = {}> {
acceptedDragTypes?: 'all' | Array<string | symbol> = 'all'
dropTargetDelegate?: DropTargetDelegate
getAllowedDropOperations?: () => Array<DropOperation>
getDropOperation?: (DropTarget, DragTypes, Array<DropOperation>) => DropOperation
getItems?: (Set<Key>, Array<T>) => Array<DragItem> = () => []
isDisabled?: boolean
onDragEnd?: (DraggableCollectionEndEvent) => void
onDragMove?: (DraggableCollectionMoveEvent) => void
onDragStart?: (DraggableCollectionStartEvent) => void
onDrop?: (DroppableCollectionDropEvent) => void
onDropActivate?: (DroppableCollectionActivateEvent) => void
onDropEnter?: (DroppableCollectionEnterEvent) => void
onDropExit?: (DroppableCollectionExitEvent) => void
onInsert?: (DroppableCollectionInsertDropEvent) => void
onItemDrop?: (DroppableCollectionOnItemDropEvent) => void
onMove?: (DroppableCollectionReorderEvent) => void
onReorder?: (DroppableCollectionReorderEvent) => void
onRootDrop?: (DroppableCollectionRootDropEvent) => void
+ pointerDragSource?: 'item' | 'handle' = 'item'
renderDragPreview?: (Array<DragItem>) => JSX.Element | {
element: JSX.Element
x: number
y: number
renderDropIndicator?: (DropTarget) => JSX.Element
shouldAcceptItemDrop?: (ItemDropTarget, DragTypes) => boolean
}@react-spectrum/s2/@react-spectrum/s2:DragAndDropOptions DragAndDropOptions <T = {}> {
acceptedDragTypes?: 'all' | Array<string | symbol> = 'all'
dropTargetDelegate?: DropTargetDelegate
getAllowedDropOperations?: () => Array<DropOperation>
getDropOperation?: (DropTarget, DragTypes, Array<DropOperation>) => DropOperation
getItems?: (Set<Key>, Array<T>) => Array<DragItem> = () => []
isDisabled?: boolean
onDragEnd?: (DraggableCollectionEndEvent) => void
onDragMove?: (DraggableCollectionMoveEvent) => void
onDragStart?: (DraggableCollectionStartEvent) => void
onDrop?: (DroppableCollectionDropEvent) => void
onDropActivate?: (DroppableCollectionActivateEvent) => void
onDropEnter?: (DroppableCollectionEnterEvent) => void
onDropExit?: (DroppableCollectionExitEvent) => void
onInsert?: (DroppableCollectionInsertDropEvent) => void
onItemDrop?: (DroppableCollectionOnItemDropEvent) => void
onMove?: (DroppableCollectionReorderEvent) => void
onReorder?: (DroppableCollectionReorderEvent) => void
onRootDrop?: (DroppableCollectionRootDropEvent) => void
+ pointerDragSource?: 'item' | 'handle' = 'item'
renderDragPreview?: (Array<DragItem>) => JSX.Element | {
element: JSX.Element
x: number
y: number
shouldAcceptItemDrop?: (ItemDropTarget, DragTypes) => boolean
} |

Closes #9739
Adds
pointerDragSource: 'item' | 'handle'touseDragAndDrop, allowing collections with editable content to restrict mouse and touch dragging to the dedicated handle, instead of the entire item. The default is'item', which is the existing behavior. This applies to RAC components (GridList, Table, and Tree), and S2 components (ListView, TableView, and TreeView).In
pointerDragSource: "handle"mode, native drag handlers are applied to the drag button while preserving the whole-item default preview and existing keyboard/screen-reader interactions.For the S2 components specifically:
cursor: grabis applied when the handle is hovered.cursor: grabbingis applied when the handle is pressed. Note that browsers do not allow manually setting this when a native drag is happening (it is instead determined by the currentdropEffect).As always, naming is up for debate. We could do
dragSource: 'item' | 'handle', although it doesn't reflect that this setting doesn't affect keyboard interactivity.✅ Pull Request Checklist:
📝 Test Instructions:
Test stories and docs for S2 and RAC collection components with DnD. Test across browsers and device types.
React Spectrum docs
React Aria docs
🧢 Your Project: