diff --git a/assets/css/base/_globals.css b/assets/css/base/_globals.css index fb6af15..6bd3c3b 100644 --- a/assets/css/base/_globals.css +++ b/assets/css/base/_globals.css @@ -16,6 +16,10 @@ } button, a { -webkit-tap-highlight-color: transparent; } button { cursor: pointer; } + + /* A consistent focus ring for links, buttons and native controls. Components + that provide their own ring (form inputs) override this through utilities. */ + :focus-visible { outline: 2px solid var(--color-coral-text); outline-offset: 2px; } } main { overflow: hidden; flex: 1 0 auto; } diff --git a/assets/css/base/_theme.css b/assets/css/base/_theme.css index 332554a..d132086 100644 --- a/assets/css/base/_theme.css +++ b/assets/css/base/_theme.css @@ -10,8 +10,11 @@ --color-coral: var(--coral); --color-coral-control: var(--coral); --color-coral-control-hover: var(--coral-dark); - --color-coral-text: var(--coral-dark); - --color-success-text: var(--mint-deep); + --color-coral-text: #b8432c; + --color-success-text: #3f6d5a; + --color-warning-text: #8a5a00; + --color-primary-text: #b8432c; + --color-error-text: #b8432c; --color-mint-chip: var(--mint); --color-on-accent-muted: #5f2c23; @@ -71,6 +74,9 @@ html[data-theme="dark"] { --color-coral-control-hover: #b8432c; --color-coral-text: #ff9c85; --color-success-text: #8fd3b5; + --color-warning-text: #f0b35a; + --color-primary-text: #f2816b; + --color-error-text: #f2816b; --color-mint-chip: #24403a; --color-on-accent-muted: rgba(255, 255, 255, 0.97); --color-promotion-surface: #1b2822; diff --git a/assets/css/components/_board.css b/assets/css/components/_board.css index fb94a80..9fb30e0 100644 --- a/assets/css/components/_board.css +++ b/assets/css/components/_board.css @@ -38,4 +38,11 @@ /* Admin Chessboard */ .bg-boardwhite { background-color: #f2f5f3; /* light square */ } -.bg-boardblack { background-color: #71828f; /* dark square */ } \ No newline at end of file +.bg-boardblack { background-color: #71828f; /* dark square */ } + +/* Play board */ +#endgame-board { position: relative; } + +/* Chessground's wrapper has no intrinsic height; let it fill the sized + aspect-ratio container so the board scales with the viewport. */ +#endgame-board .cg-wrap { width: 100%; height: 100%; } \ No newline at end of file diff --git a/assets/js/hooks/chessBoard.js b/assets/js/hooks/chessBoard.js index 6c6f030..6896ad0 100644 --- a/assets/js/hooks/chessBoard.js +++ b/assets/js/hooks/chessBoard.js @@ -22,7 +22,20 @@ export const ChessBoard = { this.applyBestLineMove({ from, to, promotion, san }); }); + this.handleEvent("apply_move", ({ from, to, promotion }) => { + if (this.reviewing) return; + this.executeMove(from, to, promotion); + }); + this.handleKeyDown = (event) => { + if (this.pendingPromotion) { + if (event.key === "Escape") { + event.preventDefault(); + this.cancelPromotion(); + } + return; + } + if (!this.reviewing) return; if (event.key === "ArrowLeft") { @@ -31,6 +44,12 @@ export const ChessBoard = { } else if (event.key === "ArrowRight") { event.preventDefault(); this.pushEvent("review_next", {}); + } else if (event.key === "Home") { + event.preventDefault(); + this.pushEvent("review_start", {}); + } else if (event.key === "End") { + event.preventDefault(); + this.pushEvent("review_end", {}); } }; @@ -185,9 +204,13 @@ export const ChessBoard = { showPromotionDialog(orig, dest) { this.pendingPromotion = { orig, dest }; + this.previousFocus = document.activeElement; const color = this.chess.turn() === "w" ? "white" : "black"; const overlay = document.createElement("div"); overlay.className = "promotion-overlay"; + overlay.setAttribute("role", "dialog"); + overlay.setAttribute("aria-modal", "true"); + overlay.setAttribute("aria-label", "Choose a promotion piece"); const picker = document.createElement("div"); picker.className = "promotion-picker"; const pieces = [ @@ -198,22 +221,64 @@ export const ChessBoard = { ]; pieces.forEach(({ type, code }) => { const btn = document.createElement("button"); + btn.type = "button"; btn.className = `promotion-piece ${type} ${color}`; + btn.setAttribute("aria-label", `Promote to ${type}`); btn.addEventListener("click", () => this.completePromotion(code)); picker.appendChild(btn); }); + overlay.addEventListener("keydown", (event) => { + if (event.key === "Escape") { + event.preventDefault(); + this.cancelPromotion(); + return; + } + + if (event.key !== "Tab") return; + + const buttons = Array.from(picker.querySelectorAll("button")); + const first = buttons[0]; + const last = buttons[buttons.length - 1]; + + if (event.shiftKey && document.activeElement === first) { + event.preventDefault(); + last.focus(); + } else if (!event.shiftKey && document.activeElement === last) { + event.preventDefault(); + first.focus(); + } + }); overlay.appendChild(picker); - const boardContainer = this.el.closest(".shadow-inner"); - boardContainer.style.position = "relative"; - boardContainer.appendChild(overlay); + const container = this.el.parentElement; + container.style.position = "relative"; + container.appendChild(overlay); + picker.querySelector("button")?.focus(); + }, + + cancelPromotion() { + this.pendingPromotion = null; + const overlay = this.el.parentElement?.querySelector(".promotion-overlay"); + if (overlay) overlay.remove(); + this.restoreFocus(); + }, + + restoreFocus() { + if (this.previousFocus instanceof HTMLElement) { + this.previousFocus.focus(); + } else { + document.getElementById("move-input")?.focus(); + } + + this.previousFocus = null; }, completePromotion(pieceType) { const { orig, dest } = this.pendingPromotion; this.pendingPromotion = null; - const picker = this.el.closest(".shadow-inner")?.querySelector(".promotion-overlay"); + const picker = this.el.parentElement?.querySelector(".promotion-overlay"); if (picker) picker.remove(); this.executeMove(orig, dest, pieceType); + document.getElementById("move-input")?.focus(); }, executeMove(orig, dest, promotion) { @@ -234,6 +299,7 @@ export const ChessBoard = { san: move.san, promotion: promotion || null, draw: this.chess.isDraw(), + check: this.chess.isCheck(), checkmate: this.chess.isCheckmate(), }; @@ -252,8 +318,14 @@ export const ChessBoard = { ? { color: null, dests: new Map() } : { color: nextTurnColor, dests: this.getValidDests() }, }); + + return true; } else { this.ground.set({ fen: this.chess.fen() }); + this.pushEvent("move_input_error", { + message: "That move is not legal in this position.", + }); + return false; } }, @@ -279,6 +351,7 @@ export const ChessBoard = { fen: this.chess.fen(), san: move.san, draw: this.chess.isDraw(), + check: this.chess.isCheck(), checkmate: this.chess.isCheckmate(), }); }, diff --git a/lib/chesstrainer_web/components/board/board_component.ex b/lib/chesstrainer_web/components/board/board_component.ex index cb37619..f406bb7 100644 --- a/lib/chesstrainer_web/components/board/board_component.ex +++ b/lib/chesstrainer_web/components/board/board_component.ex @@ -6,6 +6,8 @@ defmodule ChesstrainerWeb.Board.BoardComponent do use Phoenix.Component import ChesstrainerWeb.Board.SquareComponent + alias ChesstrainerWeb.Board.PositionText + attr :game, :map, required: true attr :myself, :any, default: nil @@ -41,7 +43,11 @@ defmodule ChesstrainerWeb.Board.BoardComponent do ~H"""
-
+