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
givensare 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
<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.
<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.
<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
| Key | Element |
|---|---|
root | Root wrapper |
bar | Bar holding the draggable elements |
barItem | An element in the bar |
count | Counter badge on a bar element |
grid | The n×n grid |
cell | A grid cell |
givenCell | A locked (given) cell, added on top of cell |
checkButton | Check button |
resetButton | Reset button |
result | Results text |
CSS Variables
Scoped under .tbx-latinsquare:
| Variable | Purpose |
|---|---|
--latinsquare-min-cell-size | Minimum cell width/height (set by minCellSize) |
--latinsquare-bar-bg, --latinsquare-bar-border, --latinsquare-bar-drop | Bar background, border, and background while dropping on it |
--latinsquare-cell-bg, --latinsquare-cell-border | Cell background and border |
--latinsquare-cell-drop | Cell background while an element is dragged over it. Uses color-mix with --exercise-accent-color. |
--latinsquare-given-bg | Background of locked cells |
--latinsquare-item-bg, --latinsquare-item-border | Bar element background and border |
--latinsquare-item-dragging | Background of the element being dragged |
--latinsquare-count-bg, --latinsquare-count-color | Counter badge colors (defaults to --exercise-accent-color / white) |
--latinsquare-action-bg, --latinsquare-action-border | Check/Reset button background and border |
--latinsquare-result-neutral, --latinsquare-result-correct, --latinsquare-result-wrong | Results text colors |
--latinsquare-correct-bg, --latinsquare-correct-border | Correct cell highlight |
--latinsquare-wrong-bg, --latinsquare-wrong-border | Wrong (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 });