Skip to content
Palette.tsx 1.83 KiB
Newer Older
Grant's avatar
Grant committed
import { useEffect, useState } from "react";
Grant's avatar
Grant committed
import { useAppContext } from "../../contexts/AppContext";
import { Canvas } from "../../lib/canvas";
Grant's avatar
Grant committed
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faXmark } from "@fortawesome/free-solid-svg-icons";
Grant's avatar
Grant committed
import { IPalleteContext } from "@sc07-canvas/lib/src/net";
Grant's avatar
Grant committed

Grant's avatar
Grant committed
export const Palette = () => {
Grant's avatar
Grant committed
  const { config, user } = useAppContext();
  const [pallete, setPallete] = useState<IPalleteContext>({});

  useEffect(() => {
    if (!Canvas.instance) return;

Grant's avatar
Grant committed
    Canvas.instance.updatePallete(pallete);
Grant's avatar
Grant committed
  }, [pallete]);

  return (
    <div id="pallete">
      <div className="pallete-colors">
        <button
          aria-label="Deselect Color"
          className="pallete-color--deselect"
          title="Deselect Color"
          onClick={() => {
            setPallete(({ color, ...pallete }) => {
              return pallete;
            });
          }}
        >
          <FontAwesomeIcon icon={faXmark} />
        </button>
        {config.pallete.colors.map((color) => (
          <button
            key={color.id}
            aria-label={color.name}
            className={["pallete-color", color.id === pallete.color && "active"]
              .filter((a) => a)
              .join(" ")}
            style={{
              backgroundColor: "#" + color.hex,
            }}
            title={color.name}
            onClick={() => {
              setPallete((pallete) => {
                return {
                  ...pallete,
                  color: color.id,
                };
              });
            }}
          ></button>
        ))}
      </div>

      {!user && (
        <div className="pallete-user-overlay">
          You are not logged in
          <a href="/api/login" className="user-login">
            Login
          </a>
        </div>
      )}
    </div>
  );
};