Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="board"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const { Component } = React
const { DragDropContext, DragSource, DropTarget } = ReactDnD
const HTML5Backend = ReactDnDHTML5Backend
const { func, bool, number, arrayOf, oneOf } = PropTypes

let knightPosition = [0, 0]
let observer = null

const emitChange = () => observer(knightPosition)

const observe = o => {
  if (observer) {
    throw new Error('Multiple observers not implemented.')
  }
  
  observer = o
  emitChange()
}

const canMoveKnight = (toX, toY) => {
  const [x, y] = knightPosition
  
  const dx = toX - x
  const dy = toY - y

  return (Math.abs(dx) === 2 && Math.abs(dy) === 1) ||
         (Math.abs(dx) === 1 && Math.abs(dy) === 2)
}

const moveKnight = (toX, toY) => {
  knightPosition = [toX, toY]
  emitChange()
}

const ItemTypes = {
  KNIGHT: 'knight'
}

const knightSource = {
  beginDrag(props) {
    return {}
  }
}

const knightCollect = (connect, monitor) => {
  return {
    connectDragSource: connect.dragSource(),
    isDragging: monitor.isDragging(),
  }
}

const squareTarget = {
  canDrop(props) {
    return canMoveKnight(props.x, props.y);
  },
  drop(props, monitor) {
    moveKnight(props.x, props.y)
  }
}

const squareCollect = (connect, monitor) => {
  return {
    connectDropTarget: connect.dropTarget(),
    isOver: monitor.isOver(),
    canDrop: monitor.canDrop(),
  }
}

class Board extends Component {
  static propTypes = {
    knightPosition: arrayOf(number).isRequired,
  };

  renderSquare(i) {
    const x = i % 8
    const y = Math.floor(i / 8)

    return (
      <div
        key={i}
        style={{
          width: '12.5%',
          height: '12.5%',
        }}
      >
        <DropTargetBoardSquare x={x} y={y}>
          {this.renderPiece(x, y)}
        </DropTargetBoardSquare>
      </div>
    )
  }

  renderPiece(x, y) {
    const [knightX, knightY] = this.props.knightPosition
    
    if (knightX === x && knightY === y) {
      return <DragSourceKnight black />
    }
  }
  
  render() {
    const squares = []
    for (let i = 0; i < 64; i++) {
      squares.push(this.renderSquare(i))
    }
    
    return (
      <div style={{
        display: 'flex',
        flexWrap: 'wrap',
        width: '640px',
        height: '640px',
      }}>
        {squares}
      </div>
    )
  }
}

const DragDropContextBoard = DragDropContext(HTML5Backend)(Board)

class BoardSquare extends Component {
  static propTypes = {
    connectDropTarget: func.isRequired,
    isOver: bool.isRequired,
    x: number.isRequired,
    y: number.isRequired,
  };

  renderOverlay(color) {
    return (
      <div style={{
        position: 'absolute',
        top: 0,
        left: 0,
        height: '100%',
        width: '100%',
        zIndex: 1,
        opacity: 0.5,
        backgroundColor: color,
      }} />
    )
  }

  render() {
    const {connectDropTarget, isOver, canDrop, x, y, children} = this.props
    const black = (x + y) % 2 === 1
    
    return connectDropTarget(
      <div style={{
        position: 'relative',
        width: '100%',
        height: '100%',
      }}>
        <Square black={black}>
          {children}
        </Square>
        {isOver && !canDrop && this.renderOverlay('red')}
        {!isOver && canDrop && this.renderOverlay('yellow')}
        {isOver && canDrop && this.renderOverlay('green')}
      </div>
    )
  }
}

const DropTargetBoardSquare = DropTarget(ItemTypes.KNIGHT, squareTarget, squareCollect)(BoardSquare)

class Square extends Component {
  static propTypes = {
    black: bool,
  };

  render() {
    const { black, children } = this.props
    const background = black ? 'black' : 'white'
    const color = black ? 'white' : 'black'
    
    return (
      <div style={{
        width: '100%',
        height: '100%',
        background,
        color
      }}>
        {children}
      </div>
    )
  }
}

class Knight extends Component {
  static propTypes = {
    connectDragSource: func.isRequired,
    isDragging: bool.isRequired,
    black: bool,
  };

  render() {
    const { connectDragSource, isDragging, black } = this.props
    const fill = black ? 'black' : 'white'
    const stroke = black ? 'gold' : 'silver'
    
    return connectDragSource(
      <div style={{
        opacity: isDragging ? 0.5 : 1,
        position: 'relative',
        width: '100%',
        height: '100%',
        fontSize: 50,
        cursor: 'move',
      }}>
        <span style={{
          position: 'absolute',
          top: '50%',
          left: '50%',
          transform: 'translate(-50%, -50%)',
          color: fill,
        }}>♞</span>
        <span style={{
          position: 'absolute',
          top: '50%',
          left: '50%',
          transform: 'translate(-50%, -50%)',
          color: stroke,
        }}>♘</span>
      </div>
    )
  }
}

const DragSourceKnight = DragSource(ItemTypes.KNIGHT, knightSource, knightCollect)(Knight)

observe(knightPosition => {
  ReactDOM.render(
    <DragDropContextBoard knightPosition={knightPosition} />,
    board
  )
})

              
            
!
999px

Console