@react-chess-tools/react-chess-puzzle
Version:
A lightweight, customizable React component library for rendering and interacting with chess puzzles.
117 lines (103 loc) • 3.8 kB
TypeScript
import * as React from 'react';
import React__default from 'react';
import { DeepPartial, ChessGameTheme, ChessGame } from '@react-chess-tools/react-chess-game';
/**
* Puzzle-specific state colors (RGBA color strings)
*/
interface PuzzleStateTheme {
/** Background color for successful moves */
success: string;
/** Background color for failed moves */
failure: string;
/** Background color for hint squares */
hint: string;
}
/**
* Complete theme configuration for ChessPuzzle component.
* Extends ChessGameTheme with puzzle-specific colors.
*/
interface ChessPuzzleTheme extends ChessGameTheme {
puzzle: PuzzleStateTheme;
}
/**
* Partial theme for puzzle customization - allows overriding only specific properties
*/
type PartialChessPuzzleTheme = DeepPartial<ChessPuzzleTheme>;
type Status = "not-started" | "in-progress" | "solved" | "failed";
type Hint = "none" | "piece" | "move";
type Puzzle = {
fen: string;
moves: string[];
makeFirstMove?: boolean;
};
interface HintProps {
asChild?: boolean;
showOn?: Status[];
}
interface ResetProps {
asChild?: boolean;
puzzle?: Puzzle;
onReset?: (puzzleContext: ChessPuzzleContextType) => void;
showOn?: Status[];
}
interface PuzzleBoardProps extends React__default.ComponentProps<typeof ChessGame.Board> {
}
type ChessPuzzleContextType = {
status: Status;
changePuzzle: (puzzle: Puzzle) => void;
resetPuzzle: () => void;
puzzle: Puzzle;
hint: Hint;
nextMove?: string | null;
isPlayerTurn: boolean;
onHint: () => void;
puzzleState: Status;
movesPlayed: number;
totalMoves: number;
};
interface RootProps {
puzzle: Puzzle;
onSolve?: (puzzleContext: ChessPuzzleContextType) => void;
onFail?: (puzzleContext: ChessPuzzleContextType) => void;
/** Optional theme configuration. Supports partial themes - only override the colors you need. */
theme?: PartialChessPuzzleTheme;
}
declare const ChessPuzzle: {
Root: React.FC<React.PropsWithChildren<RootProps>>;
Board: React.FC<PuzzleBoardProps>;
Reset: React.FC<React.PropsWithChildren<ResetProps>>;
Hint: React.FC<React.PropsWithChildren<HintProps>>;
};
declare const useChessPuzzleContext: () => ChessPuzzleContextType;
/**
* Default theme for ChessPuzzle component.
* Extends the default game theme with puzzle-specific colors.
* These values match the original hardcoded colors for backward compatibility.
*/
declare const defaultPuzzleTheme: ChessPuzzleTheme;
/**
* Deep merges a partial puzzle theme with the default puzzle theme.
* Allows users to override only specific theme properties while keeping defaults for the rest.
*
* @param partialTheme - Partial theme with only the properties to override
* @returns Complete puzzle theme with overridden properties merged with defaults
*
* @example
* ```typescript
* const customTheme = mergePuzzleTheme({
* puzzle: { hint: "rgba(0, 255, 255, 0.5)" }
* });
* // Returns full puzzle theme with only hint color changed
* ```
*/
declare const mergePuzzleTheme: (partialTheme?: PartialChessPuzzleTheme) => ChessPuzzleTheme;
/**
* Context for ChessPuzzle theme
*/
declare const ChessPuzzleThemeContext: React__default.Context<ChessPuzzleTheme>;
/**
* Hook to access the current ChessPuzzle theme.
* Returns the default puzzle theme if no ThemeProvider is present.
*/
declare const useChessPuzzleTheme: () => ChessPuzzleTheme;
export { ChessPuzzle, type ChessPuzzleContextType, type ChessPuzzleTheme, ChessPuzzleThemeContext, type Hint, type HintProps, type PartialChessPuzzleTheme, type Puzzle, type PuzzleBoardProps, type PuzzleStateTheme, type ResetProps, type RootProps, type Status, defaultPuzzleTheme, mergePuzzleTheme, useChessPuzzleContext, useChessPuzzleTheme };