SidebarHeader.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import clsx from "clsx";
  2. import { useContext } from "react";
  3. import { t } from "../../i18n";
  4. import { useDevice } from "../App";
  5. import { SidebarPropsContext } from "./common";
  6. import { CloseIcon, PinIcon } from "../icons";
  7. import { withUpstreamOverride } from "../hoc/withUpstreamOverride";
  8. import { Tooltip } from "../Tooltip";
  9. export const SidebarDockButton = (props: {
  10. checked: boolean;
  11. onChange?(): void;
  12. }) => {
  13. return (
  14. <div className="layer-ui__sidebar-dock-button" data-testid="sidebar-dock">
  15. <Tooltip label={t("labels.sidebarLock")}>
  16. <label
  17. className={clsx(
  18. "ToolIcon ToolIcon__lock ToolIcon_type_floating",
  19. `ToolIcon_size_medium`,
  20. )}
  21. >
  22. <input
  23. className="ToolIcon_type_checkbox"
  24. type="checkbox"
  25. onChange={props.onChange}
  26. checked={props.checked}
  27. aria-label={t("labels.sidebarLock")}
  28. />{" "}
  29. <div
  30. className={clsx("Sidebar__pin-btn", {
  31. "Sidebar__pin-btn--pinned": props.checked,
  32. })}
  33. tabIndex={0}
  34. >
  35. {PinIcon}
  36. </div>{" "}
  37. </label>{" "}
  38. </Tooltip>
  39. </div>
  40. );
  41. };
  42. const _SidebarHeader: React.FC<{
  43. children?: React.ReactNode;
  44. className?: string;
  45. }> = ({ children, className }) => {
  46. const device = useDevice();
  47. const props = useContext(SidebarPropsContext);
  48. const renderDockButton = !!(device.canDeviceFitSidebar && props.dockable);
  49. const renderCloseButton = !!props.onClose;
  50. return (
  51. <div
  52. className={clsx("layer-ui__sidebar__header", className)}
  53. data-testid="sidebar-header"
  54. >
  55. {children}
  56. {(renderDockButton || renderCloseButton) && (
  57. <div className="layer-ui__sidebar__header__buttons">
  58. {renderDockButton && (
  59. <SidebarDockButton
  60. checked={!!props.docked}
  61. onChange={() => {
  62. props.onDock?.(!props.docked);
  63. }}
  64. />
  65. )}
  66. {renderCloseButton && (
  67. <button
  68. data-testid="sidebar-close"
  69. className="Sidebar__close-btn"
  70. onClick={props.onClose}
  71. aria-label={t("buttons.close")}
  72. >
  73. {CloseIcon}
  74. </button>
  75. )}
  76. </div>
  77. )}
  78. </div>
  79. );
  80. };
  81. const [Context, Component] = withUpstreamOverride(_SidebarHeader);
  82. /** @private */
  83. export const SidebarHeaderComponents = { Context, Component };