Code Monkey home page Code Monkey logo

react-recoil-klondike's Introduction

react-recoil-klondike

image

Description

react-recoil-klondike is an implementation of the classic Klondike solitaire game using React + Recoil.

It's a sufficiently complex but still self-contained example of a React application that uses Recoil for state management. See Architectural decisions for more details.

The game is available at https://react-recoil-klondike.netlify.app/.

Usage

In the project directory, you can run:

npm start

Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.

The page will reload if you make edits.
You will also see any lint errors in the console.

npm run build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

Architectural decisions

Some of the goals/conclusions I've had while working on this project:

Minimal dependencies

Other than React and Recoil, the game uses only clsx for juggling CSS class names, and @material-design-icons/svg for the HUD's icons.

No images (well, almost)

Other than the icons on the HUD's buttons, the game is implemented using only CSS and HTML. No images are used. See src/styles for more details.

Still, there are some visual goodies, including a backface pattern for the cards, nice fonts, subtle shadows and gradients, and a playful animation when the game is won.

The pattern is © Lea Verou.
Fonts are © Google Fonts.

No react-dnd, no HTML5 drag-and-drop

HTML5 drag-and-drop (and transitively, react-dnd) was deemed both needlessly complex and performance-wise suboptimal for this use case. Instead, the game uses a custom drag-and-drop implementation based on pointer events and CSS transforms. See src/hooks/drag-and-drop.ts for more details.

For the sake of performance, the game avoids react re-renders during drag-and-drop operations. Details of the in-flight drag are communicated via custom events, data attributes and React refs.

Our custom DnD logic allows us to implement some nice features, such as the ability to drop a card onto a stack even if the pointer is not over the stack's bounding box. This allows for a more natural drag-and-drop experience.

Explicit positioning

While the game relies on the browser's layout engine to position the stacks using CSS grid, the cards are not rendered inside the stacks. Instead, they are positioned explicitly using a custom algorithm. See src/hooks/cards.ts for more details.

This is done to avoid detaching and re-attaching the cards to the DOM during drag-and-drop operations. This would cause the browser to re-render both stacks and cards, which would lead to janky animations.

Undo/redo

The game supports undo/redo operations via Recoil's useRecoilTransactionObserver hook. See src/hooks/history.ts and src/util/history.ts for more details.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.