lichess-org / chessground

Mobile/Web chess UI for lichess.org
https://lichess.org
GNU General Public License v3.0
1.04k stars 261 forks source link

Allow custom classes applied to custom squares #274

Closed melgrove closed 1 year ago

melgrove commented 1 year ago

Problem

It would be useful to be able to style squares with custom classes dynamically with the .set() method. The motivation is to be able to do programmatic custom highlighting, but you could also do whatever else css allows like add a border. There is an api for custom drawing, but there isn't a way to do custom highlighting unless I'm missing something.

Proposed solution

Add a new optional property to the state state.highlight.custom which takes the SquareClasses = Map<Key, string> type. When this is set, computeSquareClasses will add a square to the board with the desired class for every square-class pair in the set. For multiple classes just pass a space delimited string like normal html. Below is an example of what it looks like after cg.set({highlight: {custom: new Map([["a1", "red"]])}}) and the class .red {background-color: red; border: 10px solid black'}. !important can be passed to override the selected or other styling:

Screen Shot 2023-07-13 at 4 52 37 PM

Having this under highlight makes sense practically, but I can see if it is wanted to be put elsewhere since it does something more general than highlighting a square.

niklasf commented 1 year ago

Via #275