Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: several eyeDropper fixes (#7002)

David Luzarc1952fd6cc96
diff --git a/src/components/ColorPicker/ColorInput.tsx b/src/components/ColorPicker/ColorInput.tsx
index f179d415c5..968729cc70 100644
--- a/src/components/ColorPicker/ColorInput.tsx
+++ b/src/components/ColorPicker/ColorInput.tsx
@@ -15,9 +15,15 @@ interface ColorInputProps {
color: string;
onChange: (color: string) => void;
label: string;
+ eyeDropperType: "strokeColor" | "backgroundColor";
}
-export const ColorInput = ({ color, onChange, label }: ColorInputProps) => {
+export const ColorInput = ({
+ color,
+ onChange,
+ label,
+ eyeDropperType,
+}: ColorInputProps) => {
const device = useDevice();
const [innerValue, setInnerValue] = useState(color);
const [activeSection, setActiveColorPickerSection] = useAtom(
@@ -110,6 +116,7 @@ export const ColorInput = ({ color, onChange, label }: ColorInputProps) => {
: {
keepOpenOnAlt: false,
onSelect: (color) => onChange(color),
+ previewType: eyeDropperType,
},
)
}
diff --git a/src/components/ColorPicker/ColorPicker.tsx b/src/components/ColorPicker/ColorPicker.tsx
index d2f89c81eb..4a378950cc 100644
--- a/src/components/ColorPicker/ColorPicker.tsx
+++ b/src/components/ColorPicker/ColorPicker.tsx
@@ -82,7 +82,14 @@ const ColorPickerPopupContent = ({
const { container } = useExcalidrawContainer();
const { isMobile, isLandscape } = useDevice();
- const colorInputJSX = (
+ const eyeDropperType =
+ type === "canvasBackground"
+ ? undefined
+ : type === "elementBackground"
+ ? "backgroundColor"
+ : "strokeColor";
+
+ const colorInputJSX = eyeDropperType && (
<div>
<PickerHeading>{t("colorPicker.hexCode")}</PickerHeading>
<ColorInput
@@ -91,6 +98,7 @@ const ColorPickerPopupContent = ({
onChange={(color) => {
onChange(color);
}}
+ eyeDropperType={eyeDropperType}
/>
</div>
);
@@ -140,7 +148,7 @@ const ColorPickerPopupContent = ({
alignOffset={-16}
sideOffset={20}
style={{
- zIndex: 9999,
+ zIndex: "var(--zIndex-layerUI)",
backgroundColor: "var(--popup-bg-color)",
maxWidth: "208px",
maxHeight: window.innerHeight,
@@ -152,7 +160,7 @@ const ColorPickerPopupContent = ({
"0px 7px 14px rgba(0, 0, 0, 0.05), 0px 0px 3.12708px rgba(0, 0, 0, 0.0798), 0px 0px 0.931014px rgba(0, 0, 0, 0.1702)",
}}
>
- {palette ? (
+ {palette && eyeDropperType ? (
<Picker
palette={palette}
color={color}
@@ -165,6 +173,7 @@ const ColorPickerPopupContent = ({
state = state || {
keepOpenOnAlt: true,
onSelect: onChange,
+ previewType: eyeDropperType,
};
state.keepOpenOnAlt = true;
return state;
@@ -175,6 +184,7 @@ const ColorPickerPopupContent = ({
: {
keepOpenOnAlt: false,
onSelect: onChange,
+ previewType: eyeDropperType,
};
});
}}
diff --git a/src/components/EyeDropper.scss b/src/components/EyeDropper.scss
index 3273e4d770..da706b4160 100644
--- a/src/components/EyeDropper.scss
+++ b/src/components/EyeDropper.scss
@@ -4,7 +4,7 @@
position: absolute;
width: 100%;
height: 100%;
- z-index: 2;
+ z-index: var(--zIndex-eyeDropperBackdrop);
touch-action: none;
}
@@ -21,7 +21,7 @@
width: 3rem;
height: 3rem;
position: fixed;
- z-index: 999999;
+ z-index: var(--zIndex-eyeDropperPreview);
border-radius: 1rem;
border: 1px solid var(--default-border-color);
filter: var(--theme-filter);
diff --git a/src/components/EyeDropper.tsx b/src/components/EyeDropper.tsx
index 5278dfab60..7a4ce8ac40 100644
--- a/src/components/EyeDropper.tsx
+++ b/src/components/EyeDropper.tsx
@@ -1,7 +1,7 @@
import { atom } from "jotai";
import { useEffect, useRef } from "react";
import { createPortal } from "react-dom";
-import { COLOR_PALETTE, rgbToHex } from "../colors";
+import { rgbToHex } from "../colors";
import { EVENT } from "../constants";
import { useUIAppState } from "../context/ui-appState";
import { mutateElement } from "../element/mutateElement";
@@ -18,8 +18,8 @@ import "./EyeDropper.scss";
type EyeDropperProperties = {
keepOpenOnAlt: boolean;
swapPreviewOnAlt?: boolean;
- onSelect?: (color: string, event: PointerEvent) => void;
- previewType?: "strokeColor" | "backgroundColor";
+ onSelect: (color: string, event: PointerEvent) => void;
+ previewType: "strokeColor" | "backgroundColor";
};
export const activeEyeDropperAtom = atom<null | EyeDropperProperties>(null);
@@ -28,13 +28,8 @@ export const EyeDropper: React.FC<{
onCancel: () => void;
onSelect: Required<EyeDropperProperties>["onSelect"];
swapPreviewOnAlt?: EyeDropperProperties["swapPreviewOnAlt"];
- previewType?: EyeDropperProperties["previewType"];
-}> = ({
- onCancel,
- onSelect,
- swapPreviewOnAlt,
- previewType = "backgroundColor",
-}) => {
+ previewType: EyeDropperProperties["previewType"];
+}> = ({ onCancel, onSelect, swapPreviewOnAlt, previewType }) => {
const eyeDropperContainer = useCreatePortalContainer({
className: "excalidraw-eye-dropper-backdrop",
parentSelector: ".excalidraw-eye-dropper-container",
@@ -58,24 +53,17 @@ export const EyeDropper: React.FC<{
return;
}
- let currentColor: string = COLOR_PALETTE.black;
let isHoldingPointerDown = false;
const ctx = app.canvas.getContext("2d")!;
- const mouseMoveListener = ({
+ const getCurrentColor = ({
clientX,
clientY,
- altKey,
}: {
clientX: number;
clientY: number;
- altKey: boolean;
}) => {
- // FIXME swap offset when the preview gets outside viewport
- colorPreviewDiv.style.top = `${clientY + 20}px`;
- colorPreviewDiv.style.left = `${clientX + 20}px`;
-
const pixel = ctx.getImageData(
(clientX - appState.offsetLeft) * window.devicePixelRatio,
(clientY - appState.offsetTop) * window.devicePixelRatio,
@@ -83,7 +71,23 @@ export const EyeDropper: React.FC<{
1,
).data;
- currentColor = rgbToHex(pixel[0], pixel[1], pixel[2]);
+ return rgbToHex(pixel[0], pixel[1], pixel[2]);
+ };
+
+ const mouseMoveListener = ({
+ clientX,
+ clientY,
+ altKey,
+ }: {
+ clientX: number;
+ clientY: number;
+ altKey: boolean;
+ }) => {
+ // FIXME swap offset when the preview gets outside viewport
+ colorPreviewDiv.style.top = `${clientY + 20}px`;
+ colorPreviewDiv.style.left = `${clientX + 20}px`;
+
+ const currentColor = getCurrentColor({ clientX, clientY });
if (isHoldingPointerDown) {
for (const element of metaStuffRef.current.selectedElements) {
@@ -125,7 +129,7 @@ export const EyeDropper: React.FC<{
event.stopImmediatePropagation();
event.preventDefault();
- onSelect(currentColor, event);
+ onSelect(getCurrentColor(event), event);
};
const keyDownListener = (event: KeyboardEvent) => {
diff --git a/src/css/styles.scss b/src/css/styles.scss
index f1d359d5e4..d46ab1bc2a 100644
--- a/src/css/styles.scss
+++ b/src/css/styles.scss
@@ -6,6 +6,8 @@
--zIndex-interactiveCanvas: 2;
--zIndex-wysiwyg: 3;
--zIndex-layerUI: 4;
+ --zIndex-eyeDropperBackdrop: 5;
+ --zIndex-eyeDropperPreview: 6;
--zIndex-modal: 1000;
--zIndex-popup: 1001;
diff --git a/src/types.ts b/src/types.ts
index bc77708663..f07b134097 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -507,8 +507,9 @@ export type AppProps = Merge<
* in the app, eg Manager. Factored out into a separate type to keep DRY. */
export type AppClassProperties = {
props: AppProps;
- canvas: HTMLCanvasElement;
interactiveCanvas: HTMLCanvasElement | null;
+ /** static canvas */
+ canvas: HTMLCanvasElement;
focusContainer(): void;
library: Library;
imageCache: Map<