/** * 보조지표 설정 — 색상 + 투명도 (스와치 클릭 시 팔레트·고급 선택기) */ import React, { useState, useRef, useEffect, useCallback } from 'react'; import { createPortal } from 'react-dom'; import { parsePlotColor, formatPlotColor, plotColorCss } from '../utils/plotColorUtils'; import { getOpacityPercentSpec } from '../utils/indicatorParamSpec'; import NumericParamInput from './NumericParamInput'; import ColorPickerPanel from './ColorPickerPanel'; export interface ColorInputProps { value: string; onChange: (v: string) => void; } const ColorInput: React.FC = ({ value, onChange }) => { const { hex6, alpha } = parsePlotColor(value); const [open, setOpen] = useState(false); const anchorRef = useRef(null); const popoverRef = useRef(null); const [pos, setPos] = useState({ top: 0, left: 0 }); const updatePosition = useCallback(() => { const el = anchorRef.current; if (!el) return; const rect = el.getBoundingClientRect(); const popW = 280; let left = rect.left; if (left + popW > window.innerWidth - 12) { left = window.innerWidth - popW - 12; } setPos({ top: rect.bottom + 6, left: Math.max(8, left) }); }, []); useEffect(() => { if (!open) return; updatePosition(); const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); }; const onScroll = () => updatePosition(); window.addEventListener('keydown', onKey); window.addEventListener('resize', updatePosition); window.addEventListener('scroll', onScroll, true); return () => { window.removeEventListener('keydown', onKey); window.removeEventListener('resize', updatePosition); window.removeEventListener('scroll', onScroll, true); }; }, [open, updatePosition]); useEffect(() => { if (!open) return; const onDoc = (e: MouseEvent) => { const t = e.target as Node; if (anchorRef.current?.contains(t) || popoverRef.current?.contains(t)) return; setOpen(false); }; document.addEventListener('mousedown', onDoc); return () => document.removeEventListener('mousedown', onDoc); }, [open]); const patchHex = (h: string) => onChange(formatPlotColor(h, alpha)); const patchAlpha = (a: number) => onChange(formatPlotColor(hex6, a)); const popover = open && typeof document !== 'undefined' ? (
e.stopPropagation()} >
) : null; return (
patchHex(e.target.value)} /> % {popover ? createPortal(popover, document.body) : null}
); }; export default ColorInput;