Skip to content
English
Shortest path comes with Pro. Its preview shows once you unlock it.

How it works

A textbook figure of shortest paths on a grid. Obstacles are solid cells; S and G are the start and the goal. After every change the A* search runs again: the cells it examined appear in the order it reached them, then the shortest path is drawn through cell centres in the accent. With 4 directions the length is the number of steps; with 8, a diagonal step counts √2 and may not cut an obstacle's corner. The row under the grid reads the length, and three counts follow: straight steps, diagonal steps and cells examined. When G cannot be reached, its marker turns red and the readout says so.

Use it for

  • Teach graph search: how a shortest path bends around obstacles, how its length changes, and how many cells the search had to examine.
  • Compare 4- and 8-direction movement and see why a diagonal step costs √2.

Look elsewhere when

  • For routes on a real map, use a map with roads: this grid has uniform cells only.

Usage

import React, { useState } from 'react'
import { KeepoutWireRouter } from './KeepoutWireRouter.jsx'

const start = { start: [1, 5], goal: [18, 5], walls: [[9, 3], [9, 4], [9, 5], [9, 6]] }

export function PathLesson() {
  const [grid, setGrid] = useState(start)
  const [length, setLength] = useState(null)
  return (
    <KeepoutWireRouter
      value={grid}
      onChange={(next, details) => {
        setGrid(next)
        setLength(details.path?.length ?? null)
      }}
      defaultMoves={4}
    />
  )
}

Props

PropTypeDefaultWhat it does
value{ start: [x, y], goal: [x, y], walls: Array<[x, y]> }—Controlled grid: the start and goal cells and the obstacle cells, as zero-based column and row.
defaultValue{ start, goal, walls }two walls, S at [2, 4], G at [17, 3]Starting grid when uncontrolled.
onChange(value, { reason, path }) => void—Called after every edit. reason is 'paint', 'drag', 'key', 'clear' or 'cancel'; path is null when G cannot be reached, otherwise { cells, length, straight, diagonal, examined }.
moves4 | 8—Controlled move set: 4 directions, or 8 with diagonal steps of length √2 that may not cut an obstacle's corner.
defaultMoves4 | 88Starting move set when uncontrolled.
onMovesChange(moves) => void—Called when the move set is changed.
columnsnumber20Number of columns (4–40).
rowsnumber9Number of rows (3–24).
labelsobject—Replaces any built-in text: title, formula4, formula8, legendWall, legendExplored, legendPath, grid, help, keys, moves, moves4, moves8, clear, startMark, goalMark, readout, noPath, straight, diagonal, explored, cell ('{x}', '{y}'), wallOn, wallOff, startAt, goalAt ('{cell}'), result ('{length}', '{explored}') and blocked.
localestring'en'Language tag for the numbers.
classNamestring—Extra class on the card, for example to set its width (30rem by default, never wider than its container).

Accessibility

  • The grid is an application region with its instructions as its description. A cell cursor moves with the arrows (Home and End go to the row's ends); Space or Enter toggles an obstacle; S and G put the start or the goal on the cursor. A hint with these keys shows while the grid has keyboard focus.
  • A polite status announces each change with the new path length and the number of cells examined, or that there is no path.
  • The move set is a radio group: arrows switch between 4 and 8 directions. Clear is a button, marked unavailable when there are no obstacles.
  • Painting uses pointer capture and touch-action none on the grid only; a cancelled gesture restores the grid it started from.

Motion

After an edit the examined cells appear in search order within 420ms and the path fades in for --ds-duration-base; while painting, everything updates at once. With reduced motion there is no animation.

Add it to your project

Pro members copy a prompt and their coding agent installs the source, the same way as the free components.

KeepoutWireRouter.jsxkeepout-wire-router.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Form inputs
Availability grid comes with Pro. Its preview shows once you unlock it.
Boolean operations comes with Pro. Its preview shows once you unlock it.
Cascader comes with Pro. Its preview shows once you unlock it.