Tools.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from "react";
  2. import { saveAsPng } from "save-html-as-image";
  3. import {
  4. AiOutlineFullscreen,
  5. AiOutlineMinus,
  6. AiOutlinePlus,
  7. } from "react-icons/ai";
  8. import { FiDownload } from "react-icons/fi";
  9. import { HiOutlineSun, HiOutlineMoon } from "react-icons/hi";
  10. import { MdCenterFocusWeak } from "react-icons/md";
  11. import { SearchInput } from "src/containers/SearchInput";
  12. import styled from "styled-components";
  13. import useConfig from "src/hooks/store/useConfig";
  14. import shallow from "zustand/shallow";
  15. export const StyledTools = styled.div`
  16. display: flex;
  17. align-items: center;
  18. gap: 4px;
  19. flex-direction: row-reverse;
  20. height: 28px;
  21. padding: 4px 16px;
  22. background: ${({ theme }) => theme.BACKGROUND_PRIMARY};
  23. color: ${({ theme }) => theme.SILVER};
  24. box-shadow: 0 1px 0px ${({ theme }) => theme.BACKGROUND_TERTIARY};
  25. `;
  26. const StyledToolElement = styled.button`
  27. display: grid;
  28. place-content: center;
  29. font-size: 20px;
  30. background: none;
  31. color: ${({ theme }) => theme.INTERACTIVE_NORMAL};
  32. &:hover {
  33. color: ${({ theme }) => theme.INTERACTIVE_HOVER};
  34. opacity: 1;
  35. box-shadow: none;
  36. }
  37. `;
  38. export const Tools: React.FC = () => {
  39. const [lightmode, performance, hideEditor] = useConfig(
  40. (state) => [
  41. state.settings.lightmode,
  42. state.settings.performance,
  43. state.settings.hideEditor,
  44. ],
  45. shallow
  46. );
  47. const updateSetting = useConfig((state) => state.updateSetting);
  48. const zoomIn = useConfig((state) => state.zoomIn);
  49. const zoomOut = useConfig((state) => state.zoomOut);
  50. const centerView = useConfig((state) => state.centerView);
  51. const toggleEditor = () => updateSetting("hideEditor", !hideEditor);
  52. const toggleTheme = () => updateSetting("lightmode", !lightmode);
  53. const exportAsImage = () => {
  54. saveAsPng(document.querySelector("svg[id*='ref']"), {
  55. filename: "jsonvisio.com",
  56. printDate: true,
  57. });
  58. };
  59. return (
  60. <StyledTools>
  61. <StyledToolElement aria-label="fullscreen" onClick={toggleEditor}>
  62. <AiOutlineFullscreen />
  63. </StyledToolElement>
  64. <StyledToolElement aria-label="switch theme" onClick={toggleTheme}>
  65. {lightmode ? <HiOutlineMoon /> : <HiOutlineSun />}
  66. </StyledToolElement>
  67. {!performance && <SearchInput />}
  68. {!performance && (
  69. <StyledToolElement aria-label="save" onClick={exportAsImage}>
  70. <FiDownload />
  71. </StyledToolElement>
  72. )}
  73. <StyledToolElement aria-label="center canvas" onClick={centerView}>
  74. <MdCenterFocusWeak />
  75. </StyledToolElement>
  76. <StyledToolElement aria-label="zoom out" onClick={zoomOut}>
  77. <AiOutlineMinus />
  78. </StyledToolElement>
  79. <StyledToolElement aria-label="zoom in" onClick={zoomIn}>
  80. <AiOutlinePlus />
  81. </StyledToolElement>
  82. </StyledTools>
  83. );
  84. };