-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose API via useImperativeHandle. #16
base: main
Are you sure you want to change the base?
Conversation
Would be nice to have api usage example in README.md |
src/index.tsx
Outdated
api?.set(config); | ||
}, [api, config]); | ||
useImperativeHandle(apiRef, () => { | ||
return publicApi; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about exposing the whole chessground api instead of creating custom object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can do that. Done now.
Also re-exported Config
, Api
and Key
from the chessground API to reduce the clutter of imports in code using this React wrapper. Does that sound sensible?
I've added the example demo-game component from this PR description to the README, and added |
@@ -40,3 +42,51 @@ import "chessground/assets/chessground.cburnett.css"; | |||
|
|||
ReactDOM.render(<Chessground />, document.getElementById("root")); | |||
``` | |||
|
|||
## Example: showing the moves of a game |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i really like this example!
src/index.tsx
Outdated
useEffect(() => { | ||
if (divRef.current && !api) { | ||
const chessgroundApi = ChessgroundApi(divRef.current, { | ||
animation: { enabled: true, duration: 200 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets remove it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
src/index.tsx
Outdated
...config, | ||
}); | ||
setApi(chessgroundApi); | ||
} else if (divRef.current && api) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it needed? It looks like it can only be fired after line 34 completes and after that the config is already provided
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gave it a quick test, and you're right, it doesn't look to be needed. Removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fantastic!
Following on from the suggestion in #14 , reimplemented my changes using a forwardRef and useImperativeHandle. Returns an instance of the Chessground API for the current board.
Sample code demonstrating it's use (added to the README too):