import { isValidElement, cloneElement, Children, useEffect, useState, useCallback, type FC, type ReactNode, type CSSProperties, } from 'react'; import { Dock } from 'react-dock'; import parseKey from 'parse-key'; import type { RecoilState } from 'recoil'; import { POSITIONS } from './constants'; const STORAGE_KEY = 'recoil-devtools-dock-state'; interface DockState { isVisible?: boolean; position?: Position; size?: number; } const loadState = (): DockState | null => { try { const stored = localStorage.getItem(STORAGE_KEY); return stored ? JSON.parse(stored) : null; } catch { return null; } }; const saveState = (state: DockState) => { try { localStorage.setItem(STORAGE_KEY, JSON.stringify(state)); } catch { // Ignore storage errors } }; interface KeyObject { name: string; ctrl: boolean; meta: boolean; shift: boolean; alt: boolean; sequence: string; } type Position = (typeof POSITIONS)[number]; export interface DockMonitorProps { values?: RecoilState[]; defaultPosition?: Position; defaultIsVisible?: boolean; defaultSize?: number; toggleVisibilityKey?: string; changePositionKey?: string; changeMonitorKey?: string; fluid?: boolean; persistState?: boolean; showShortcutButton?: boolean; children?: ReactNode; } const DockMonitor: FC = (props) => { const { toggleVisibilityKey = 'ctrl-h', changePositionKey = 'ctrl-q', changeMonitorKey = 'ctrl-m', children, defaultIsVisible = true, defaultPosition = 'right', defaultSize = 0.3, fluid = true, persistState = true, showShortcutButton = true, } = props; const [isVisible, setIsVisible] = useState(() => { if (!persistState) return defaultIsVisible; const stored = loadState(); return stored?.isVisible ?? defaultIsVisible; }); const [position, setPosition] = useState(() => { if (!persistState) return defaultPosition; const stored = loadState(); return stored?.position ?? defaultPosition; }); const [size, setSize] = useState(() => { if (!persistState) return defaultSize; const stored = loadState(); return stored?.size ?? defaultSize; }); const [childMonitorIndex, setChildMonitorIndex] = useState(0); const [showShortcuts, setShowShortcuts] = useState(false); // Format shortcut key for display const formatShortcut = (key: string) => { return key .replace('ctrl', 'Ctrl') .replace('meta', '⌘') .replace('shift', '⇧') .replace('alt', '⌥') .split('-') .join(' + '); }; const childrenCount = Children.count(children); if (childrenCount === 0) { console.error( ' requires at least one monitor inside. ' + 'Why dont you try ? You can get it at ' + 'https://github.com/ulises-jeremias/recoil-devtools/tree/main/packages/recoil-devtools-log-monitor.' ); } else if (childrenCount > 1 && !changeMonitorKey) { console.error( 'You specified multiple monitors inside ' + 'but did not provide `changeMonitorKey` prop to change them. ' + 'Try specifying ' + 'and then press Ctrl-M.' ); } const matchesKey = (key: KeyObject | undefined, event: KeyboardEvent) => { if (!key) { return false; } // Use the modern `KeyboardEvent.key` value instead of the deprecated // `keyCode`/`which` properties, which are incorrect for non-alphabetic // keys. `parse-key` names (e.g. "space") are lowercase words while // `event.key` holds the produced character (e.g. " "), so normalize // the space character before comparing case-insensitively. const pressedKey = event.key === ' ' ? 'space' : event.key; return ( key.name.toLowerCase() === pressedKey.toLowerCase() && key.alt === event.altKey && key.ctrl === event.ctrlKey && key.meta === event.metaKey && key.shift === event.shiftKey ); }; const handleKeyDown = useCallback( (e: KeyboardEvent) => { const target = e.target as HTMLElement | null; const isInput = target?.tagName === 'INPUT' || target?.tagName === 'SELECT' || target?.tagName === 'TEXTAREA' || target?.isContentEditable; // Ignore regular keys when focused on a field and no modifiers are active. if (!e.ctrlKey && !e.metaKey && !e.altKey && isInput) { return; } const visibilityKey = parseKey(toggleVisibilityKey); const positionKey = parseKey(changePositionKey); let monitorKey; if (changeMonitorKey) { monitorKey = parseKey(changeMonitorKey); } if (matchesKey(visibilityKey, e)) { e.preventDefault(); setIsVisible((prevIsVisible) => !prevIsVisible); } else if (matchesKey(positionKey, e)) { e.preventDefault(); setPosition((prevPos) => { const currentIdx = POSITIONS.indexOf(prevPos); return POSITIONS[(currentIdx + 1) % POSITIONS.length] ?? 'right'; }); } else if (matchesKey(monitorKey, e)) { e.preventDefault(); setChildMonitorIndex( (prevIdx) => (prevIdx + 1) % Children.count(children) ); } }, [toggleVisibilityKey, changePositionKey, changeMonitorKey, children] ); const handleSizeChange = (requestedSize: number) => { setSize(requestedSize); }; const renderChild = ( child: ReactNode, index: number, otherProps: Omit ) => { if (index !== childMonitorIndex) { return null; } if (!isValidElement(child)) { return child; } return cloneElement(child, { ...otherProps, ...(child.props as object) }); }; useEffect(() => { window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [handleKeyDown]); // Persist state changes to localStorage useEffect(() => { if (persistState) { saveState({ isVisible, position, size }); } }, [isVisible, position, size, persistState]); // Shortcut button styles const shortcutButtonStyle: CSSProperties = { position: 'absolute', top: 4, right: 4, padding: '4px 8px', fontSize: '10px', backgroundColor: 'rgba(0, 0, 0, 0.3)', color: 'white', border: 'none', borderRadius: '3px', cursor: 'pointer', zIndex: 1000, }; // Shortcuts modal styles const shortcutsModalStyle: CSSProperties = { position: 'absolute', top: 30, right: 4, padding: '12px', backgroundColor: 'rgba(0, 0, 0, 0.9)', color: 'white', borderRadius: '4px', fontSize: '12px', zIndex: 1000, minWidth: '180px', }; // Build shortcuts list const shortcuts = [ { label: 'Toggle visibility', key: formatShortcut(toggleVisibilityKey) }, ...(changePositionKey ? [{ label: 'Change position', key: formatShortcut(changePositionKey) }] : []), ...(changeMonitorKey && childrenCount > 1 ? [{ label: 'Change monitor', key: formatShortcut(changeMonitorKey) }] : []), ]; return ( {showShortcutButton && ( )} {showShortcuts && (
Keyboard Shortcuts
{shortcuts.map((shortcut, idx) => (
{shortcut.label} {shortcut.key}
))}
)} {Children.map(children, (child: ReactNode, index) => renderChild(child, index, props) )}
); }; export default DockMonitor;