Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@jaredreisinger/react-crossword

JaredReisinger4.3kMIT5.2.0TypeScript support: included

A flexible, responsive, and easy-to-use crossword component for React apps

react, react-crossword, crossword, puzzle, library

readme

react-crossword: A flexible, responsive, and easy-to-use crossword component for React apps.

npm version build status code coverage
known vulnerabilities dependencies
license more badges commitizen friendly semantic-release contributors

Please see the complete docs for in-depth details.


Install

npm install --save @jaredreisinger/react-crossword
  # or #
yarn add @jaredreisinger/react-crossword

Usage

import React from 'react';

import Crossword from '@jaredreisinger/react-crossword';

const data = {
  /* ... puzzle data (see below) ... */
};

export default function MyPage() {
  return <Crossword data={data} />;
}

Clue/data format

To make crosswords as easy to create as possible, with the least amount of extraneous and boilerplate typing, the clue/answer format is structured as a set of nested objects:

{
  across: {
    1: {
      clue: 'one plus one',
      answer: 'TWO',
      row: 0,
      col: 0,
    },
  },
  down: {
    2: {
      clue: 'three minus two',
      answer: 'ONE',
      row: 0,
      col: 2,
    },
  },
}

At the top level, the across and down properties group together the clues/answers for their respective directions. Each of those objects is a map, keyed by the answer number rather than an array. (This is done so that the creator has control over the numbering/labelling of the clues/answers.) Each item contains a clue and answer property, as well as row and col for the starting position.

The Crossword component calculates the needed grid size from the data itself, so you don't need to pass an overall size to the component.

Styling

One other major difference (and advantage) to this crossword component is that it is very "stylable"... as many of the styling properties as possible are exposed so that you can create any look you want for the crossword. The Crossword component makes use of styled-components' ThemeProvider and offers the following properties to control colors and layout:

theme property default description
columnBreakpoint '768px' browser-width at which the clues go from showing beneath the grid to showing beside the grid.
gridBackground 'rgb(0,0,0)' overall background color (fill) for the crossword grid. Can be 'transparent' to show through a page background image.
cellBackground 'rgb(255,255,255)' background for an answer cell
cellBorder 'rgb(0,0,0)' border for an answer cell
textColor 'rgb(0,0,0)' color for answer text (entered by the player)
numberColor 'rgba(0,0,0, 0.25)' color for the across/down numbers in the grid
focusBackground 'rgb(255,255,0)' background color for the cell with focus, the one that the player is typing into
highlightBackground 'rgb(255,255,204)' background color for the cells in the answer the player is working on, helps indicate in which direction focus will be moving; also used as a background on the active clue

Note that these values can be provided either via ThemeProvider, or directly as a theme property on the Crossword component itself. (And further, if you're not using styled-components, but want to make use of ThemeProvider, this library re-exports ThemeProvider so you can pull it from here.)

Also, several class names are applied to elements in the crossword, in case you want to apply styles that way:

element class name
entire crossword component; encompassing grid and clues crossword
entire crossword is correct (on same element as crossword) correct
answer grid grid
all of the clues clues
header and clues for one direction direction
direction header ('across' or 'down') header
an individual clue clue
an individual clue with a correct answer (on same element as clue) correct

(No class names are currently applied within the grid, as the SVG layout is very layout-sensitive.)

Player progress events

In addition to providing properties for styling, there are some properties to help your application "understand" the player's progress:

property description
onCorrect callback function that fires when a player answers a clue correctly; called with (direction, number, answer) arguments, where direction is 'across' or 'down', number is the clue number as text (like '1'), and answer is the answer itself
onLoadedCorrect callback function that’s called when a crossword is loaded, to batch up correct answers loaded from storage; passed an array of the same values that onCorrect would recieve
onCrosswordCorrect callback function that’s called when the overall crossword is completely correct (or not)
onCellChange callback function called when a cell changes (e.g. when the user types a letter); passed the row and column and the character typed
onClueSelected callback function called when a clue is selected; passed the direction and the “number”

Imperative methods

The following imperative methods can be called on a "ref" handle to the component:

method name parameters description
focus() (none) Sets focus to the crossword component.
reset() (none) Resets the entire crossword; clearing all answers in the grid and also any persisted data.
fillAllAnswers() (none) Fills all the answers in the grid and calls the onLoadedCorrect callback with every answer.
isCrosswordCorrect() (none) Returns whether the crossword is entirely correct or not.

Background

Initially written as a replacement for @guardian/react-crossword, to make custom styling and puzzle-definition easier.

There are several things about the Crossword component from @guardian/react-crossword that are less than ideal, in my opinion:

  • the styles/formatting are baked in
  • semi-unrelated functionality like the "anagram helper" is baked in
  • the data format for clues/answers is horrendous

This is an attempt to create a less-opinionated component that's much easier to drop in to an arbitrary React page.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Pushpendra Singh
Pushpendra Singh

🤔
Amit Bansal
Amit Bansal

🐛
embeddedt
embeddedt

💻
Albranco
Albranco

🤔
Rob McCollough
Rob McCollough

🤔
lorddaedra
lorddaedra

🤔
Ilya Lukyanov
Ilya Lukyanov

🤔
Richard Hoffmann
Richard Hoffmann

🤔
gitname
gitname

🐛
Craig
Craig

🤔

This project follows the all-contributors specification. Contributions of any kind welcome!

changelog

5.2.0 (2022-12-16)

Features

5.1.0 (2022-12-13)

Bug Fixes

  • check answer correctness when loading/reloading guesses (40b9cd5), closes #271

Features

  • allow non-square rendering (74a608e), closes #17

5.0.0 (2022-12-03)

Bug Fixes

  • depedencies: update all dependencies (57da3b5)
  • dependencies: update dependencies (some time ago!) (90cb96b)
  • example: fix up example to use updated crossword component (5b08cab)
  • fix one lingering typescript error (1a2af83)
  • missed saving one file! (9430ea3)

BREAKING CHANGES

  • depedencies: Now requires React >=18.2.0 and Node >= 18.7.0

4.3.2 (2022-03-10)

Bug Fixes

  • dependencies: update all dependencies (c48d609)

4.3.1 (2022-03-09)

Bug Fixes

  • dependencies: update all dependencies (de7e5fd)

4.3.0 (2022-02-26)

Features

  • add per-answer "complete" and "incomplete" callbacks, and tweak crossword-level (d761a75), closes #131

4.2.0 (2022-02-25)

Features

  • add props for across/down clue labels (c41c64d), closes #132
  • storage: add storageKey for custom lovalStorage key (d3d9334), closes #142

4.1.2 (2022-02-25)

Bug Fixes

  • dependencies: npm audit fix (ccadd2e)
  • dependencies: update all dependencies (1bb4f16)

4.1.1 (2022-01-04)

Bug Fixes

  • apply dev branch updates to main (a8add9a), closes #164

4.1.0 (2022-01-03)

Features

4.0.2 (2022-01-03)

Bug Fixes

  • update homepage link in package.json to point to new docs location (f284c63)

4.0.1 (2022-01-03)

Bug Fixes

  • update dev dependencies, force release to update README (f59241e)

4.0.0 (2022-01-03)

Documentation

  • readme: fix README, force breaking change (08d758e)

BREAKING CHANGES

  • readme: The refactoring (a previous commit) was intended to reflect a breaking change... doing that now.

3.1.0 (2022-01-03)

Bug Fixes

  • components: changed DirectionClues to use CrosswordContext (a3a83ef)
  • tests: increase test coverage, fix components as needed (5baf1c4)

Features

  • components: begin factoring of logic/rendering (20589d4), closes #140

3.0.0 (2021-12-31)

Features

  • add classes to Cell for checking per-letter correctness (df4f2b9)
  • dependencies: massive dependency update! (5cf5083)
  • typescript: convert to typescript (255c54a)

BREAKING CHANGES

  • typescript: Converted library to Typescript and to ESM module (reverted to CJS for publishing sanity)
  • dependencies: all dependencies updated, also updated node version minimum

2.3.1 (2020-10-05)

Bug Fixes

  • upgrade @testing-library/user-event from 12.1.5 to 12.1.7 (f929bf4)
  • upgrade eslint from 7.8.1 to 7.10.0 (380b452)
  • upgrade eslint-plugin-react from 7.20.6 to 7.21.3 (ec95fd5)
  • upgrade rollup from 2.27.1 to 2.28.2 (f88b4e7)
  • upgrade snyk from 1.398.1 to 1.406.0 (c49e8a1)
  • upgrade yargs-parser from 20.0.0 to 20.2.1 (178c081)

2.3.0 (2020-09-29)

Bug Fixes

  • upgrade @testing-library/user-event from 12.1.3 to 12.1.5 (3e88e81)
  • upgrade eslint-plugin-react-hooks from 4.1.0 to 4.1.2 (1dea653)
  • upgrade lint-staged from 10.2.13 to 10.4.0 (8600467)
  • upgrade multiple dependencies with Snyk (4c19f26)
  • upgrade prettier from 2.1.0 to 2.1.2 (334f30f)
  • upgrade react-styleguidist from 11.0.10 to 11.1.0 (effaaf5)
  • upgrade rollup from 2.26.10 to 2.27.1 (e7ce16e)
  • upgrade snyk from 1.389.0 to 1.393.0 (cce01e8)
  • upgrade snyk from 1.393.0 to 1.398.1 (1d674b7)
  • upgrade stylelint from 13.6.1 to 13.7.1 (bbf0298)

Features

  • upgrade yargs-parser from 19.0.1 to 20.0.0 (1155112)

2.2.12 (2020-09-08)

Bug Fixes

  • upgrade eslint from 7.7.0 to 7.8.1 (3f347ad)
  • upgrade react-styleguidist from 11.0.8 to 11.0.10 (ff2cbac)
  • upgrade rollup from 2.26.5 to 2.26.10 (3d4068f)
  • upgrade rollup-plugin-terser from 7.0.0 to 7.0.2 (1338c86)
  • upgrade snyk from 1.384.0 to 1.389.0 (6f35027)
  • upgrade styled-components from 5.1.1 to 5.2.0 (9470709)

2.2.11 (2020-09-04)

Bug Fixes

  • upgrade @testing-library/user-event from 12.1.2 to 12.1.3 (d41cb11)
  • upgrade commitizen from 4.1.5 to 4.2.1 (cff2a2f)
  • upgrade immer from 7.0.7 to 7.0.8 (43715a9)
  • upgrade lint-staged from 10.2.11 to 10.2.13 (fe9f2da)
  • upgrade prettier from 2.0.5 to 2.1.0 (221da66)
  • upgrade snyk from 1.381.1 to 1.384.0 (0350352)

2.2.10 (2020-09-04)

Bug Fixes

  • upgrade @testing-library/jest-dom from 5.11.3 to 5.11.4 (995f1da)
  • upgrade @testing-library/react from 10.4.8 to 10.4.9 (836d825)
  • upgrade @testing-library/user-event from 12.1.1 to 12.1.2 (bf0d283)
  • upgrade jest from 26.4.1 to 26.4.2 (9159da8)
  • upgrade rollup from 2.26.4 to 2.26.5 (e2ecf6b)

2.2.9 (2020-08-24)

Bug Fixes

  • upgrade @babel/core from 7.11.1 to 7.11.4 (b13c307)
  • upgrade commitizen from 4.1.2 to 4.1.5 (4e7ef03)
  • upgrade jest from 26.4.0 to 26.4.1 (b885f21)
  • upgrade pretty-quick from 2.0.1 to 2.0.2 (2e82f27)
  • upgrade rollup from 2.26.3 to 2.26.4 (62a8020)
  • upgrade snyk from 1.377.1 to 1.380.0 (52ab171)
  • upgrade snyk from 1.380.0 to 1.381.1 (a60f850)

2.2.8 (2020-08-20)

Bug Fixes

  • upgrade eslint-plugin-react-hooks from 4.0.8 to 4.1.0 (b616b87)
  • upgrade rollup from 2.24.0 to 2.26.3 (bda2920)
  • upgrade snyk from 1.374.0 to 1.377.1 (f0a6cfc)

2.2.7 (2020-08-17)

Bug Fixes

  • example/package.json & example/package-lock.json to reduce vulnerabilities (eb2d4db)

2.2.6 (2020-08-17)

Bug Fixes

  • package.json & package-lock.json to reduce vulnerabilities (be52987)
  • upgrade eslint from 7.6.0 to 7.7.0 (17ff219)
  • upgrade rollup from 2.23.1 to 2.24.0 (aa2f1a9)
  • upgrade snyk from 1.373.1 to 1.374.0 (da6744e)

2.2.5 (2020-08-14)

Bug Fixes

  • upgrade eslint-plugin-react from 7.20.5 to 7.20.6 (b7fdb92)
  • upgrade jest from 26.3.0 to 26.4.0 (18b1e94)

2.2.4 (2020-08-12)

Bug Fixes

  • dependencies: upgrade snyk (and kick off new build/release) (fb86ecd), closes #33

2.2.3 (2020-08-11)

Bug Fixes

  • package.json, package-lock.json & .snyk to reduce vulnerabilities (a6d2a37)

2.2.2 (2020-08-11)

Bug Fixes

  • dependencies: move run-time dependencies into regular dependencies (53f684a), closes #31
  • dependencies: update all dependencies (9cdbe1e)

2.2.1 (2020-06-02)

Bug Fixes

  • fix 'correct' class on correct crossword (794f3b1)

2.2.0 (2020-06-02)

Features

2.1.0 (2020-05-27)

Features

  • add onCellChange callback and 'correct' classname on clues (6fd2539), closes #20 #21 #21

2.0.0 (2020-05-21)

Bug Fixes

  • avoid calling onLoadedCorrect if there are no correct answers (a3e89f5)
  • upgrade react-scripts from 3.4.0 to 3.4.1 (10fb7cf)

Code Refactoring

  • hooks: re-implement using React hooks (e123d25)

improvement

  • theming: switch to use ThemeProvider for styling (75cb12c)

BREAKING CHANGES

  • theming: Styling properties moved into theme property (or consumed from ThemeProvider).
  • hooks: Switched to React hooks, and thus using React.forwardRef() on Crossword to expose imperative commands (reset(), focus(), fillAllAnswers()).

1.0.5 (2020-03-17)

Bug Fixes

  • vulnerabilities: update dependencies (5c08901)

1.0.4 (2020-03-09)

Bug Fixes

1.0.3 (2020-03-03)

Bug Fixes

  • improve doc commenting, force release with improved testing (ff6957d)

1.0.2 (2020-02-28)

Bug Fixes

  • set minimum node version to 10.18 (22f7167)

1.0.1 (2020-02-21)

Bug Fixes

  • package: update react-scripts to version 3.4.0 (aa0744b), closes #6

1.0.0 (2020-01-20)

Bug Fixes

  • proptypes: fix proptypes definition of across/down data (ae8b19a)

Features

  • tweaks and adjustment for preliminary release (0c3e4a7)
  • initial: initial copy of sources from proof-of-concept (31b73ec)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.