Skip to content

Commit e649b6a

Browse files
authored
Add experimental hook-based examples (#1256)
* refactor: rename the hook-example tags in the index * docs: add a docsite queryarg to switch into experimental mode This will allow us to test out the hooks-api without having to change what users normally see. * docs: add headerto chessboard hooks example * docs: move dustbin box to hooks * docs: working on hook examples * docs: get hooks examples compiling * refactor: make the begin() function optional * refactor: hooks api cleanup * docs: add headers to hook-based examples * fix: use drop return value in useDropTargetHandler * docs: get dustbin examples working * docs: update dragLayer example * docs: fix custom drag layer example * docs: get hooks working with more examples * feat: allow the hooks dragPreview to be a promise * fix: remove text fixtures from hooks examples
1 parent 35352b2 commit e649b6a

File tree

64 files changed

+1154
-1599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1154
-1599
lines changed

packages/documentation/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"react": "link:../react-dnd/node_modules/react",
3232
"react-dnd": "^7.2.1",
3333
"react-dnd-documentation-examples": "^7.2.1",
34+
"react-dnd-documentation-examples-hooks": "^7.2.1",
3435
"react-dnd-html5-backend": "^7.2.0",
3536
"react-dnd-test-backend": "^7.2.0",
3637
"react-dom": "link:../react-dnd/node_modules/react-dom",

packages/documentation/src/components/header.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,32 @@ import NavBar from './navbar'
44

55
export interface HeaderProps {
66
debugMode?: boolean
7+
experimentalMode?: boolean
78
}
89

9-
const DebugModeFlag = () => (
10-
<a className="github-fork-ribbon" data-ribbon="Debug Mode" title="Debug Mode">
11-
Debug Mode
12-
</a>
13-
)
10+
const DebugModeFlag = ({ debugMode, experimentalMode }: any) => {
11+
if (!debugMode && !experimentalMode) {
12+
return null
13+
}
14+
15+
let text = ''
16+
if (debugMode && experimentalMode) {
17+
text = 'Dbg Experimental.'
18+
} else if (debugMode) {
19+
text = 'Debug'
20+
} else if (experimentalMode) {
21+
text = 'Experimental'
22+
}
23+
return (
24+
<a className="github-fork-ribbon" data-ribbon={text} title={text}>
25+
{text}
26+
</a>
27+
)
28+
}
1429

15-
const Header: React.FC<HeaderProps> = ({ debugMode }) => (
30+
const Header: React.FC<HeaderProps> = ({ debugMode, experimentalMode }) => (
1631
<Container>
17-
{debugMode ? <DebugModeFlag /> : null}
32+
<DebugModeFlag debugMode={debugMode} experimentalMode={experimentalMode} />
1833
<NavBar />
1934
</Container>
2035
)

packages/documentation/src/components/layout.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { PageGroup } from '../constants'
1414
import { APIPages, ExamplePages } from '../constants'
1515
import Header from './header'
1616
import './layout.css'
17+
import { isExperimentalApiMode } from '../util/renderHtmlAst'
1718
require('prismjs/themes/prism.css')
1819
const favicon = require('../favicon.png')
1920

@@ -31,6 +32,7 @@ const Layout: React.FC<LayoutProps> = props => {
3132
const sidebarItems: PageGroup[] = isExampleUrl ? ExamplePages : APIPages
3233
const hideSidebar = props.hideSidebar || sitepath === '/about'
3334
const debugMode = isDebugMode()
35+
const experimentalMode = isExperimentalApiMode()
3436
return (
3537
<>
3638
<Helmet
@@ -47,7 +49,7 @@ const Layout: React.FC<LayoutProps> = props => {
4749
href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.2/gh-fork-ribbon.min.css"
4850
/>
4951
</Helmet>
50-
<Header debugMode={debugMode} />
52+
<Header debugMode={debugMode} experimentalMode={experimentalMode} />
5153
<DragDropContextProvider backend={HTML5Backend} debugMode={debugMode}>
5254
<ContentContainer>
5355
<PageBody hasSidebar={sitepath !== '/about'}>

packages/documentation/src/util/renderHtmlAst.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
declare var require: any
22
import { createElement } from 'react'
33
import { componentIndex } from 'react-dnd-documentation-examples/lib/esm/index'
4+
import { componentIndex as hookComponentIndex } from 'react-dnd-documentation-examples-hooks/lib/esm/index'
5+
import { parse } from 'query-string'
46
import processImages from './processImagesInMarkdownAst'
57
const log = require('debug')('site:renderHtmlAst')
68
const rehypeReact = require('rehype-react')
79

10+
export function isExperimentalApiMode() {
11+
if (typeof window !== 'undefined') {
12+
const queryObject = parse(window.location.search)
13+
return queryObject.experimental !== undefined
14+
} else {
15+
return false
16+
}
17+
}
18+
819
// Registers the examples as custom components
920
const renderAst = new rehypeReact({
1021
createElement,
11-
components: componentIndex,
22+
components: {
23+
...(isExperimentalApiMode() ? hookComponentIndex : componentIndex),
24+
},
1225
}).Compiler
1326

1427
export default function renderHtmlAst(node: any) {

packages/examples-hooks/src/00 Chessboard/Board.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import * as React from 'react'
22
import { BoardSquare } from './BoardSquare'
33
import { Knight } from './Knight'
44

packages/examples-hooks/src/00 Chessboard/BoardSquare.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef } from 'react'
1+
import * as React from 'react'
22
import { __EXPERIMENTAL_DND_HOOKS_THAT_MAY_CHANGE_AND_BREAK_MY_BUILD__ } from 'react-dnd'
33
import { Square } from './Square'
44
import { canMoveKnight, moveKnight } from './Game'
@@ -17,7 +17,7 @@ export interface BoardSquareProps {
1717
export const BoardSquare: React.FC<BoardSquareProps> = (
1818
props: BoardSquareProps,
1919
) => {
20-
const ref = useRef(null)
20+
const ref = React.useRef(null)
2121
const { isOver, canDrop } = useDrop({
2222
ref,
2323
type: ItemTypes.KNIGHT,

packages/examples-hooks/src/00 Chessboard/Knight.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, useMemo } from 'react'
1+
import * as React from 'react'
22
import { __EXPERIMENTAL_DND_HOOKS_THAT_MAY_CHANGE_AND_BREAK_MY_BUILD__ } from 'react-dnd'
33
import ItemTypes from './ItemTypes'
44
import knightImage from './knightImage'
@@ -23,13 +23,12 @@ function createKnightImage() {
2323
}
2424

2525
export const Knight: React.FC = () => {
26-
const ref = useRef(null)
27-
const dragPreview = useMemo(createKnightImage, [])
26+
const ref = React.useRef(null)
27+
const dragPreview = React.useMemo(createKnightImage, [])
2828
const { isDragging } = useDrag({
2929
ref,
3030
type: ItemTypes.KNIGHT,
31-
begin: () => ({}),
32-
dragPreview,
31+
preview: dragPreview,
3332
collect: mon => ({
3433
isDragging: !!mon.isDragging(),
3534
}),

packages/examples-hooks/src/00 Chessboard/Overlay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

33
export interface OverlayProps {
44
color: string

packages/examples-hooks/src/00 Chessboard/Square.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

33
export interface SquareProps {
44
black: boolean

packages/examples-hooks/src/00 Chessboard/index.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react'
1+
import * as React from 'react'
22
import Board from './Board'
33
import { observe } from './Game'
44

@@ -16,13 +16,18 @@ const containerStyle: React.CSSProperties = {
1616
* The Chessboard Tutorial Application
1717
*/
1818
const ChessboardTutorialApp: React.FC = () => {
19-
const [knightPos, setKnightPos] = useState<[number, number]>([1, 7])
19+
const [knightPos, setKnightPos] = React.useState<[number, number]>([1, 7])
2020

2121
// the observe function will return an unsubscribe callback
22-
useEffect(() => observe((newPos: [number, number]) => setKnightPos(newPos)))
22+
React.useEffect(() =>
23+
observe((newPos: [number, number]) => setKnightPos(newPos)),
24+
)
2325
return (
24-
<div style={containerStyle}>
25-
<Board knightPosition={knightPos} />
26+
<div>
27+
<h1>EXPERIMENTAL API</h1>
28+
<div style={containerStyle}>
29+
<Board knightPosition={knightPos} />
30+
</div>
2631
</div>
2732
)
2833
}

0 commit comments

Comments
 (0)