-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1517 from clauderic/iframes-support
Add same-origin iframe support
- Loading branch information
Showing
25 changed files
with
529 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@dnd-kit/dom': patch | ||
--- | ||
|
||
Support dragging across same-origin iframes. |
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
28 changes: 28 additions & 0 deletions
28
apps/stories/stories/react/Sortable/Iframe/Iframe.stories.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,28 @@ | ||
import type {Meta, StoryObj} from '@storybook/react'; | ||
|
||
import {IframeLists} from './IframeExample.tsx'; | ||
|
||
const meta: Meta<typeof IframeLists> = { | ||
title: 'React/Sortable/Iframe', | ||
component: IframeLists, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof IframeLists>; | ||
|
||
export const Iframe: Story = { | ||
name: 'Iframe', | ||
args: { | ||
debug: false, | ||
itemCount: 6, | ||
}, | ||
}; | ||
|
||
export const IframeTransformed: Story = { | ||
name: 'Transformed Iframe', | ||
args: { | ||
debug: false, | ||
itemCount: 6, | ||
transform: true, | ||
}, | ||
}; |
170 changes: 170 additions & 0 deletions
170
apps/stories/stories/react/Sortable/Iframe/IframeExample.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,170 @@ | ||
import React, {useEffect, useRef, useState} from 'react'; | ||
import type {PropsWithChildren} from 'react'; | ||
import {DragDropProvider} from '@dnd-kit/react'; | ||
import {useSortable} from '@dnd-kit/react/sortable'; | ||
import {move} from '@dnd-kit/helpers'; | ||
import {defaultPreset} from '@dnd-kit/dom'; | ||
import {Debug} from '@dnd-kit/dom/plugins/debug'; | ||
import AutoFrameComponent from '@measured/auto-frame-component'; | ||
|
||
import {Container, Item} from '../../components/index.ts'; | ||
import {createRange} from '../../../utilities/createRange.ts'; | ||
import {cloneDeep} from '../../../utilities/cloneDeep.ts'; | ||
|
||
const AutoFrame = AutoFrameComponent.default || AutoFrameComponent; | ||
|
||
interface Props { | ||
debug?: boolean; | ||
defaultItems?: Record<string, string[]>; | ||
columnStyle?: Record<string, string>; | ||
itemCount: number; | ||
scrollable?: boolean; | ||
transform?: boolean; | ||
} | ||
|
||
export function IframeLists({ | ||
debug, | ||
defaultItems, | ||
itemCount, | ||
columnStyle, | ||
scrollable, | ||
transform, | ||
}: Props) { | ||
const [items, setItems] = useState( | ||
defaultItems ?? { | ||
host: createRange(itemCount).map((id) => `Host: ${id}`), | ||
iframe: createRange(itemCount).map((id) => `Iframe: ${id}`), | ||
} | ||
); | ||
const snapshot = useRef(cloneDeep(items)); | ||
|
||
const [bodyClassName, setBodyClassName] = useState(''); | ||
|
||
useEffect(() => { | ||
const body = document.querySelector('body'); | ||
|
||
if (!body) return; | ||
|
||
if (body.classList.contains('dark')) { | ||
setBodyClassName('dark'); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<DragDropProvider | ||
plugins={debug ? [...defaultPreset.plugins, Debug] : undefined} | ||
onDragStart={() => { | ||
snapshot.current = cloneDeep(items); | ||
}} | ||
onDragOver={(event) => { | ||
setItems((items) => move(items, event)); | ||
}} | ||
onDragEnd={(event) => { | ||
if (event.canceled) { | ||
setItems(snapshot.current); | ||
return; | ||
} | ||
}} | ||
> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
gap: 20, | ||
}} | ||
> | ||
<Column id="host" scrollable={scrollable} style={columnStyle}> | ||
{items.host.map((id, index) => ( | ||
<SortableItem key={id} id={id} column={'host'} index={index} /> | ||
))} | ||
</Column> | ||
|
||
<AutoFrame | ||
style={{ | ||
border: 'none', | ||
transform: transform ? 'scale(0.8)' : undefined, | ||
}} | ||
> | ||
<style | ||
dangerouslySetInnerHTML={{ | ||
__html: 'body { background: transparent; margin: 0 !important; }', | ||
}} | ||
/> | ||
<div className={bodyClassName}> | ||
<Column id="iframe" scrollable={scrollable} style={columnStyle}> | ||
{items.iframe.map((id, index) => ( | ||
<SortableItem key={id} id={id} column="iframe" index={index} /> | ||
))} | ||
</Column> | ||
</div> | ||
</AutoFrame> | ||
</div> | ||
</DragDropProvider> | ||
); | ||
} | ||
|
||
interface SortableItemProps { | ||
id: string; | ||
column: string; | ||
index: number; | ||
style?: React.CSSProperties; | ||
} | ||
|
||
const COLORS: Record<string, string> = { | ||
Host: '#7193f1', | ||
Iframe: '#FF851B', | ||
}; | ||
|
||
function SortableItem({ | ||
id, | ||
column, | ||
index, | ||
style, | ||
}: PropsWithChildren<SortableItemProps>) { | ||
const group = column; | ||
const {ref, isDragSource} = useSortable({ | ||
id, | ||
group, | ||
accept: 'item', | ||
type: 'item', | ||
feedback: 'clone', | ||
index, | ||
data: {group}, | ||
}); | ||
|
||
return ( | ||
<Item | ||
ref={ref} | ||
accentColor={COLORS[column]} | ||
shadow={isDragSource} | ||
style={style} | ||
transitionId={`sortable-${column}-${id}`} | ||
> | ||
{id} | ||
</Item> | ||
); | ||
} | ||
|
||
interface ColumnProps { | ||
id: string; | ||
scrollable?: boolean; | ||
style?: React.CSSProperties; | ||
} | ||
|
||
function Column({ | ||
children, | ||
id, | ||
scrollable, | ||
style, | ||
}: PropsWithChildren<ColumnProps>) { | ||
return ( | ||
<Container | ||
label={id.charAt(0).toUpperCase() + id.slice(1)} | ||
scrollable={scrollable} | ||
transitionId={`sortable-column-${id}`} | ||
style={style} | ||
> | ||
{children} | ||
</Container> | ||
); | ||
} |
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
Oops, something went wrong.