Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP2universalStack: React
reacteventsdomperformance
Detect outside clicks with a document listener and a ref, not an extra wrapper element
To close on outside click, attach a listener to document and check whether event.target is inside the ref, instead of adding a wrapping div whose only purpose is catching clicks.
PR: hegnar-zephr-components · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetsx
| 1 | // Extra wrapper div added solely to capture outside clicks |
| 2 | return ( |
| 3 | <div onClick={handleOutsideClick} role="presentation"> |
| 4 | <section ref={modalRef}>{children}</section> |
| 5 | </div> |
| 6 | ); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | useEffect(() => { |
| 2 | const controller = new AbortController(); |
| 3 | document.addEventListener('mousedown', event => { |
| 4 | if (modalRef.current && event.target instanceof Node && !modalRef.current.contains(event.target)) { |
| 5 | onCloseRef.current(); |
| 6 | } |
| 7 | }, { signal: controller.signal }); |
| 8 | return () => controller.abort(); |
| 9 | }, []); |
Explanation (EN)
Objašnjenje (HR)