123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- import React from "react";
- import { Popover } from "./Popover";
- import "./ColorPicker.scss";
- import { KEYS } from "../keys";
- import { t, getLanguage } from "../i18n";
- import { isWritableElement } from "../utils";
- import colors from "../colors";
- const standardizeColor = (
- value: string,
- canvasContext: CanvasRenderingContext2D,
- ): string | null => {
- const defaultHexColor = "#000000";
- canvasContext.fillStyle = value;
- const hexColor = canvasContext.fillStyle;
- canvasContext.fillStyle = defaultHexColor;
- return hexColor !== defaultHexColor || value === "black" ? hexColor : null;
- };
- // This is a narrow reimplementation of the awesome react-color Twitter component
- // https://github.com/casesandberg/react-color/blob/master/src/components/twitter/Twitter.js
- // Unfortunately, we can't detect keyboard layout in the browser. So this will
- // only work well for QWERTY but not AZERTY or others...
- const keyBindings = [
- ["1", "2", "3", "4", "5"],
- ["q", "w", "e", "r", "t"],
- ["a", "s", "d", "f", "g"],
- ].flat();
- const Picker = function ({
- colors,
- color,
- onChange,
- onClose,
- label,
- showInput = true,
- }: {
- colors: string[];
- color: string | null;
- onChange: (color: string) => void;
- onClose: () => void;
- label: string;
- showInput: boolean;
- }) {
- const firstItem = React.useRef<HTMLButtonElement>();
- const activeItem = React.useRef<HTMLButtonElement>();
- const gallery = React.useRef<HTMLDivElement>();
- const colorInput = React.useRef<HTMLInputElement>();
- React.useEffect(() => {
- // After the component is first mounted
- // focus on first input
- if (activeItem.current) {
- activeItem.current.focus();
- } else if (colorInput.current) {
- colorInput.current.focus();
- }
- }, []);
- const handleKeyDown = (event: React.KeyboardEvent) => {
- if (event.key === KEYS.TAB) {
- const { activeElement } = document;
- if (event.shiftKey) {
- if (activeElement === firstItem.current) {
- colorInput.current?.focus();
- event.preventDefault();
- }
- } else {
- if (activeElement === colorInput.current) {
- firstItem.current?.focus();
- event.preventDefault();
- }
- }
- } else if (
- event.key === KEYS.ARROW_RIGHT ||
- event.key === KEYS.ARROW_LEFT ||
- event.key === KEYS.ARROW_UP ||
- event.key === KEYS.ARROW_DOWN
- ) {
- const { activeElement } = document;
- const isRTL = getLanguage().rtl;
- const index = Array.prototype.indexOf.call(
- gallery!.current!.children,
- activeElement,
- );
- if (index !== -1) {
- const length = gallery!.current!.children.length - (showInput ? 1 : 0);
- const nextIndex =
- event.key === (isRTL ? KEYS.ARROW_LEFT : KEYS.ARROW_RIGHT)
- ? (index + 1) % length
- : event.key === (isRTL ? KEYS.ARROW_RIGHT : KEYS.ARROW_LEFT)
- ? (length + index - 1) % length
- : event.key === KEYS.ARROW_DOWN
- ? (index + 5) % length
- : event.key === KEYS.ARROW_UP
- ? (length + index - 5) % length
- : index;
- (gallery!.current!.children![nextIndex] as any).focus();
- }
- event.preventDefault();
- } else if (
- keyBindings.includes(event.key.toLowerCase()) &&
- !isWritableElement(event.target)
- ) {
- const index = keyBindings.indexOf(event.key.toLowerCase());
- (gallery!.current!.children![index] as any).focus();
- event.preventDefault();
- } else if (event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) {
- event.preventDefault();
- onClose();
- }
- event.nativeEvent.stopImmediatePropagation();
- };
- return (
- <div
- className="color-picker"
- role="dialog"
- aria-modal="true"
- aria-label={t("labels.colorPicker")}
- onKeyDown={handleKeyDown}
- >
- <div className="color-picker-triangle color-picker-triangle-shadow"></div>
- <div className="color-picker-triangle"></div>
- <div
- className="color-picker-content"
- ref={(el) => {
- if (el) {
- gallery.current = el;
- }
- }}
- >
- {colors.map((_color, i) => (
- <button
- className="color-picker-swatch"
- onClick={(event) => {
- (event.currentTarget as HTMLButtonElement).focus();
- onChange(_color);
- }}
- title={`${_color} — ${keyBindings[i].toUpperCase()}`}
- aria-label={_color}
- aria-keyshortcuts={keyBindings[i]}
- style={{ color: _color }}
- key={_color}
- ref={(el) => {
- if (el && i === 0) {
- firstItem.current = el;
- }
- if (el && _color === color) {
- activeItem.current = el;
- }
- }}
- onFocus={() => {
- onChange(_color);
- }}
- >
- {_color === "transparent" ? (
- <div className="color-picker-transparent"></div>
- ) : undefined}
- <span className="color-picker-keybinding">{keyBindings[i]}</span>
- </button>
- ))}
- {showInput && (
- <ColorInput
- color={color}
- label={label}
- onChange={(color) => {
- onChange(color);
- }}
- ref={colorInput}
- />
- )}
- </div>
- </div>
- );
- };
- const ColorInput = React.forwardRef(
- (
- {
- color,
- onChange,
- label,
- }: {
- color: string | null;
- onChange: (color: string) => void;
- label: string;
- },
- ref,
- ) => {
- const colorRegex = /^([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8}|transparent)$/;
- const [innerValue, setInnerValue] = React.useState(color);
- const inputRef = React.useRef(null);
- const canvasContext = React.useRef<CanvasRenderingContext2D>(
- document.createElement("canvas").getContext("2d"),
- );
- React.useEffect(() => {
- setInnerValue(color);
- }, [color]);
- React.useImperativeHandle(ref, () => inputRef.current);
- return (
- <label className="color-input-container">
- <div className="color-picker-hash">#</div>
- <input
- spellCheck={false}
- className="color-picker-input"
- aria-label={label}
- onChange={(event) => {
- const value = event.target.value.toLowerCase();
- if (value.match(colorRegex)) {
- onChange(value === "transparent" ? "transparent" : `#${value}`);
- } else if (canvasContext.current) {
- const hexColor = standardizeColor(value, canvasContext.current);
- if (hexColor) {
- onChange(hexColor);
- }
- }
- setInnerValue(value);
- }}
- value={(innerValue || "").replace(/^#/, "")}
- onPaste={(event) => onChange(event.clipboardData.getData("text"))}
- onBlur={() => setInnerValue(color)}
- ref={inputRef}
- />
- </label>
- );
- },
- );
- export function ColorPicker({
- type,
- color,
- onChange,
- label,
- }: {
- type: "canvasBackground" | "elementBackground" | "elementStroke";
- color: string | null;
- onChange: (color: string) => void;
- label: string;
- }) {
- const [isActive, setActive] = React.useState(false);
- const pickerButton = React.useRef<HTMLButtonElement>(null);
- return (
- <div>
- <div className="color-picker-control-container">
- <button
- className="color-picker-label-swatch"
- aria-label={label}
- style={
- color
- ? ({ "--swatch-color": color } as React.CSSProperties)
- : undefined
- }
- onClick={() => setActive(!isActive)}
- ref={pickerButton}
- />
- <ColorInput
- color={color}
- label={label}
- onChange={(color) => {
- onChange(color);
- }}
- />
- </div>
- <React.Suspense fallback="">
- {isActive ? (
- <Popover
- onCloseRequest={(event) =>
- event.target !== pickerButton.current && setActive(false)
- }
- >
- <Picker
- colors={colors[type]}
- color={color || null}
- onChange={(changedColor) => {
- onChange(changedColor);
- }}
- onClose={() => {
- setActive(false);
- pickerButton.current?.focus();
- }}
- label={label}
- showInput={false}
- />
- </Popover>
- ) : null}
- </React.Suspense>
- </div>
- );
- }
|