Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(form): replace react-sortable-hoc with dndkit
- Loading branch information
Showing
12 changed files
with
294 additions
and
170 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
9 changes: 9 additions & 0 deletions
9
packages/sanity/src/core/form/inputs/arrays/ArrayOfObjectsInput/item/ItemCard.tsx
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,9 @@ | ||
import {Card} from '@sanity/ui' | ||
import styled from 'styled-components' | ||
|
||
export const ItemCard = styled(Card)` | ||
border: 1px solid transparent; | ||
&[aria-selected='true'] { | ||
border-color: var(--card-focus-ring-color); | ||
} | ||
` |
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
16 changes: 10 additions & 6 deletions
16
packages/sanity/src/core/form/inputs/arrays/common/DragHandle.tsx
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 |
---|---|---|
@@ -1,27 +1,31 @@ | ||
import styled from 'styled-components' | ||
import {Button, ButtonProps} from '@sanity/ui' | ||
import React from 'react' | ||
import React, {useContext} from 'react' | ||
import {DragHandleIcon} from '@sanity/icons' | ||
import {DRAG_HANDLE_ATTRIBUTE, sortableHandle} from './sortable' | ||
import {useSortable} from '@dnd-kit/sortable' | ||
|
||
const DragHandleButton = styled(Button)<{grid?: boolean}>` | ||
cursor: ${(props) => (props.grid ? 'move' : 'ns-resize')}; | ||
` | ||
|
||
const DRAG_HANDLE_PROPS = {[DRAG_HANDLE_ATTRIBUTE]: true} | ||
export const SortableItemIdContext = React.createContext<string | null>(null) | ||
|
||
export const DragHandle = sortableHandle(function DragHandle( | ||
export const DragHandle = function DragHandle( | ||
props: { | ||
grid?: boolean | ||
} & ButtonProps | ||
) { | ||
const id = useContext(SortableItemIdContext)! | ||
const {listeners, attributes} = useSortable({id}) | ||
|
||
return ( | ||
<DragHandleButton | ||
icon={DragHandleIcon} | ||
mode="bleed" | ||
data-ui="DragHandleButton" | ||
{...attributes} | ||
{...props} | ||
{...DRAG_HANDLE_PROPS} | ||
{...listeners} | ||
/> | ||
) | ||
}) | ||
} |
45 changes: 45 additions & 0 deletions
45
.../src/core/form/inputs/arrays/common/dndkit-modifier/restrictToParentElementWithMargins.ts
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,45 @@ | ||
import type {ClientRect, Modifier} from '@dnd-kit/core' | ||
import type {Transform} from '@dnd-kit/utilities' | ||
|
||
function restrictToBoundingRect( | ||
transform: Transform, | ||
rect: ClientRect, | ||
boundingRect: ClientRect, | ||
margins: Margins | ||
): Transform { | ||
const value = { | ||
...transform, | ||
} | ||
|
||
const marginY = margins.y || 0 | ||
const marginX = margins.x || 0 | ||
|
||
if (rect.top + value.y <= boundingRect.top + marginY) { | ||
value.y = boundingRect.top - rect.top + marginY | ||
} else if (rect.bottom + value.y >= boundingRect.top + boundingRect.height - marginY) { | ||
value.y = boundingRect.top + boundingRect.height - rect.bottom - marginY | ||
} | ||
|
||
if (rect.left + value.x <= boundingRect.left - marginX) { | ||
value.x = boundingRect.left - rect.left + marginX | ||
} else if (rect.right + value.x >= boundingRect.left + boundingRect.width + marginX) { | ||
value.x = boundingRect.left + boundingRect.width - rect.right + marginX | ||
} | ||
|
||
return value | ||
} | ||
|
||
interface Margins { | ||
x?: number | ||
y?: number | ||
} | ||
|
||
export const restrictToParentElementWithMargins: (margins: Margins) => Modifier = | ||
(margins: Margins) => | ||
({containerNodeRect, draggingNodeRect, transform}) => { | ||
if (!draggingNodeRect || !containerNodeRect) { | ||
return transform | ||
} | ||
|
||
return restrictToBoundingRect(transform, draggingNodeRect, containerNodeRect, margins) | ||
} |
Oops, something went wrong.