TBX Docs
ExercisesCatalogue

LatinSquare

Fill an n×n grid so each element appears once per row and column

LatinSquare is a generalised sudoku. You pass n elements (text or images) and the learner fills an n × n grid by dragging copies of them from a bar. Each element in the bar has a counter showing how many copies are left. The exercise is correct when every cell is filled and no element repeats in any row or column. It uses @dnd-kit/react for drag and drop.

  • Drag an element from the bar onto a cell to place a copy. The counter goes down.
  • Drop onto a filled cell to replace it. The replaced element goes back to the bar.
  • Drag a placed element to another cell to move it.
  • Drag a placed element back to the bar, or anywhere outside the grid, to remove it. The counter goes up.
  • Cells set with givens are locked: they can't be moved, replaced or removed.

The grid size comes from the number of elements: 4 elements give a 4×4 grid, 3 elements a 3×3 grid, and so on.

Import

import { LatinSquare } from '@bside-tech/tbx-ui/components';

Example

Latin Square
Fill the grid so each element appears once in every row and column.
Elements
🍎4🍌4🍇4🍒4
<LatinSquare elements={['🍎', '🍌', '🍇', '🍒']} />

With givens

givens pre-fills and locks some cells. It is an n × n array: each entry is an index into elements, or null for a cell the learner fills. Locked copies count against an element's counter.

Complete the square
Fill the grid so each element appears once in every row and column.
Elements
A0B4C4D2
A
D
A
A
D
A
<LatinSquare
  title="Complete the square"
  elements={['A', 'B', 'C', 'D']}
  givens={[
    [0, null, null, 3],
    [null, 0, null, null],
    [null, null, 0, null],
    [3, null, null, 0],
  ]}
/>

With images and minCellSize

Elements can be any React node, such as an <img> or an inline <svg>. Use minCellSize (any CSS length) to keep cells, and the images inside them, from shrinking below a given size. If the grid no longer fits, it scrolls horizontally instead.

Place each shape once per row and column
Fill the grid so each element appears once in every row and column.
Elements
333
<LatinSquare
  title="Place each shape once per row and column"
  minCellSize="4.5rem"
  elements={[
    <svg key="circle" viewBox="0 0 40 40" width={40} height={40} aria-label="Circle">
      <circle cx="20" cy="20" r="16" fill="#3B82F6" />
    </svg>,
    <svg key="square" viewBox="0 0 40 40" width={40} height={40} aria-label="Square">
      <rect x="5" y="5" width="30" height="30" rx="3" fill="#22C55E" />
    </svg>,
    <svg key="triangle" viewBox="0 0 40 40" width={40} height={40} aria-label="Triangle">
      <polygon points="20,4 37,35 3,35" fill="#F59E0B" />
    </svg>,
  ]}
/>

// With book images:
<LatinSquare
  minCellSize="96px"
  elements={[
    <img key="cat" src={catImg} alt="Cat" />,
    <img key="dog" src={dogImg} alt="Dog" />,
    <img key="fish" src={fishImg} alt="Fish" />,
    <img key="bird" src={birdImg} alt="Bird" />,
  ]}
/>

Props

Prop

Type

Validation

After Check:

  • Cells whose element repeats in the same row or column are marked wrong.
  • Other filled cells are marked correct.
  • Empty cells get a dashed red border.
  • The result counts correct cells out of the cells the learner had to fill (givens aren't counted).

Style

Tailwind v4 is available for customisations. Pass classes via the classNames prop, or override CSS variables for fine-grained control.

CSS Classes

KeyElement
rootRoot wrapper
barBar holding the draggable elements
barItemAn element in the bar
countCounter badge on a bar element
gridThe n×n grid
cellA grid cell
givenCellA locked (given) cell, added on top of cell
checkButtonCheck button
resetButtonReset button
resultResults text

CSS Variables

Scoped under .tbx-latinsquare:

VariablePurpose
--latinsquare-min-cell-sizeMinimum cell width/height (set by minCellSize)
--latinsquare-bar-bg, --latinsquare-bar-border, --latinsquare-bar-dropBar background, border, and background while dropping on it
--latinsquare-cell-bg, --latinsquare-cell-borderCell background and border
--latinsquare-cell-dropCell background while an element is dragged over it. Uses color-mix with --exercise-accent-color.
--latinsquare-given-bgBackground of locked cells
--latinsquare-item-bg, --latinsquare-item-borderBar element background and border
--latinsquare-item-draggingBackground of the element being dragged
--latinsquare-count-bg, --latinsquare-count-colorCounter badge colors (defaults to --exercise-accent-color / white)
--latinsquare-action-bg, --latinsquare-action-borderCheck/Reset button background and border
--latinsquare-result-neutral, --latinsquare-result-correct, --latinsquare-result-wrongResults text colors
--latinsquare-correct-bg, --latinsquare-correct-borderCorrect cell highlight
--latinsquare-wrong-bg, --latinsquare-wrong-borderWrong (repeated) cell highlight; the border is also used for empty cells

These feed into --exercise-action-bg, --exercise-action-border and --exercise-result-* used by ExerciseLayout.

Text elements use --exercise-layout-content-font-size.

Labels

<LatinSquare
  elements={['A', 'B', 'C']}
  labels={{
    items: 'Letters',
    check: 'Verify',
    reset: 'Try again',
    results: 'right',
  }}
/>

Hook

Use useLatinSquare to build a custom Latin-square interface.

import { useLatinSquare } from '@bside-tech/tbx-ui/hooks';

const { cells, remaining, conflicts, place, move, clear, isCorrect, check, reset } =
  useLatinSquare({ size: 4 });

On this page