All files / webview-app/src/components FrameConfigModal.tsx

82.85% Statements 29/35
72.22% Branches 13/18
77.77% Functions 7/9
82.85% Lines 29/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                2x                                                               18x 18x 18x 18x 18x       18x 6x 2x     4x 4x 4x 4x 4x     18x 6x 2x     4x                 4x 4x 4x       18x 3x     15x 4x           4x     15x                                                                               2x                 1x                                                    
import { useEffect, useState } from "react";
 
import { ColorField } from "@/components/PropertiesPanel/ColorField";
import { FormField } from "@/components/PropertiesPanel/FormField";
import { NumberField } from "@/components/PropertiesPanel/NumberField";
import { Button } from "@/components/ui/button";
import type { NumericInputOptions } from "@/schemas/parsers";
 
const FRAME_SIZE_RANGE: NumericInputOptions = {
  min: 1,
  max: 5_000,
  integer: true,
};
 
export interface FrameConfigurationValues {
  width: number;
  height: number;
  title: string;
  backgroundColor?: string;
}
 
interface FrameConfigModalProps {
  isOpen: boolean;
  initialWidth: number;
  initialHeight: number;
  initialTitle: string;
  initialBackgroundColor?: string;
  onApply: (values: FrameConfigurationValues) => void;
  onClose: () => void;
}
 
export function FrameConfigModal({
  isOpen,
  initialWidth,
  initialHeight,
  initialTitle,
  initialBackgroundColor,
  onApply,
  onClose,
}: FrameConfigModalProps) {
  const [frameWidth, setFrameWidth] = useState(initialWidth);
  const [frameHeight, setFrameHeight] = useState(initialHeight);
  const [frameTitle, setFrameTitle] = useState(initialTitle);
  const [useCustomBackground, setUseCustomBackground] = useState(initialBackgroundColor !== undefined);
  const [frameBackgroundColor, setFrameBackgroundColor] = useState(
    initialBackgroundColor ?? "#ffffff",
  );
 
  useEffect(() => {
    if (!isOpen) {
      return;
    }
 
    setFrameWidth(initialWidth);
    setFrameHeight(initialHeight);
    setFrameTitle(initialTitle);
    setUseCustomBackground(initialBackgroundColor !== undefined);
    setFrameBackgroundColor(initialBackgroundColor ?? "#ffffff");
  }, [initialBackgroundColor, initialHeight, initialTitle, initialWidth, isOpen]);
 
  useEffect(() => {
    if (!isOpen) {
      return;
    }
 
    const handleKeyDown = (event: KeyboardEvent) => {
      if (event.key !== "Escape") {
        return;
      }
 
      event.preventDefault();
      onClose();
    };
 
    window.addEventListener("keydown", handleKeyDown);
    return () => {
      window.removeEventListener("keydown", handleKeyDown);
    };
  }, [isOpen, onClose]);
 
  if (!isOpen) {
    return null;
  }
 
  const handleApply = () => {
    onApply({
      width: frameWidth,
      height: frameHeight,
      title: frameTitle,
      backgroundColor: useCustomBackground ? frameBackgroundColor : undefined,
    });
    onClose();
  };
 
  return (
    <div
      className="fixed inset-0 z-50 flex items-center justify-center bg-black/55 px-6 py-8"
      role="dialog"
      aria-modal="true"
      aria-labelledby="jframe-config-title"
      onMouseDown={(event) => {
        if (event.target === event.currentTarget) {
          onClose();
        }
      }}
    >
      <section className="w-full max-w-md rounded-lg border border-vscode-panel-border bg-vscode-panel-background shadow-2xl">
        <header className="border-b border-vscode-panel-border px-4 py-3">
          <h2 id="jframe-config-title" className="text-sm font-semibold">
            JFrame Configuration
          </h2>
        </header>
 
        <div className="space-y-3 p-4">
          <div className="grid grid-cols-2 gap-2">
            <NumberField
              label="Width"
              value={frameWidth}
              range={FRAME_SIZE_RANGE}
              onChange={setFrameWidth}
            />
            <NumberField
              label="Height"
              value={frameHeight}
              range={FRAME_SIZE_RANGE}
              onChange={setFrameHeight}
            />
          </div>
 
          <FormField label="Title">
            <input
              type="text"
              className="flex h-8 w-full rounded border border-vscode-panel-border bg-vscode-input-background px-2 py-1 text-sm text-vscode-input-foreground outline-none transition-colors placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
              value={frameTitle}
              onChange={(event) => setFrameTitle(event.target.value)}
            />
          </FormField>
 
          <label className="flex items-center gap-2 text-xs text-muted-foreground">
            <input
              type="checkbox"
              className="h-4 w-4 accent-vscode-focusBorder"
              checked={useCustomBackground}
              onChange={(event) => setUseCustomBackground(event.target.checked)}
            />
            Use custom background color
          </label>
 
          {useCustomBackground ? (
            <ColorField
              label="Background"
              value={frameBackgroundColor}
              onChange={setFrameBackgroundColor}
            />
          ) : null}
        </div>
 
        <footer className="flex justify-end gap-2 border-t border-vscode-panel-border px-4 py-3">
          <Button type="button" variant="outline" size="sm" onClick={onClose}>
            Cancel
          </Button>
          <Button type="button" size="sm" onClick={handleApply}>
            Apply
          </Button>
        </footer>
      </section>
    </div>
  );
}