CheckerBoard.jsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React, { memo } from "react";
  2. import styled, { css } from "styled-components";
  3. const StyledCheckerBoard = styled.div`
  4. ${({ width, height, color, alternateColor, colCount, rowCount }) => css`
  5. width: ${width}px;
  6. height: ${height}px;
  7. background-color: ${color};
  8. display: grid;
  9. grid-template-columns: repeat(${colCount}, 1fr);
  10. grid-template-rows: repeat(${rowCount}, 1fr);
  11. box-shadow: rgba(60, 64, 67, 0.3) 0px 1px 2px 0px,
  12. rgba(60, 64, 67, 0.15) 0px 1px 3px 1px;
  13. & .alternate {
  14. background-color: ${alternateColor};
  15. }
  16. `}
  17. `;
  18. const CheckerBoard = ({
  19. width = 50,
  20. height = width,
  21. color = "#CCC",
  22. alternateColor = "#888",
  23. colCount = 3,
  24. rowCount = 3,
  25. }) => {
  26. return (
  27. <StyledCheckerBoard
  28. width={width}
  29. height={height}
  30. rowCount={rowCount}
  31. colCount={colCount}
  32. color={color}
  33. alternateColor={alternateColor}
  34. >
  35. {Array.from({ length: rowCount }).map((_, indexRow) =>
  36. Array.from({ length: colCount }).map((_, indexCol) => (
  37. <div
  38. key={`${indexCol}__${indexRow}`}
  39. className={
  40. indexRow % 2
  41. ? indexCol % 2
  42. ? "alternate"
  43. : ""
  44. : !(indexCol % 2)
  45. ? "alternate"
  46. : ""
  47. }
  48. />
  49. ))
  50. )}
  51. </StyledCheckerBoard>
  52. );
  53. };
  54. export default memo(CheckerBoard);