| Conditions | 11 |
| Total Lines | 117 |
| Code Lines | 87 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Modal.tsx ➔ Modal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import React, { createContext, useContext, useEffect, useRef } from "react"; |
||
| 19 | |||
| 20 | export default function Modal({ |
||
| 21 | id, |
||
| 22 | parentElement, |
||
| 23 | visible, |
||
| 24 | children, |
||
| 25 | onModalConfirm, |
||
| 26 | onModalMiddle, |
||
| 27 | onModalCancel, |
||
| 28 | }: ModalProps): React.ReactPortal | null { |
||
| 29 | // Set up div ref to measure modal height |
||
| 30 | const modalRef = useRef<HTMLDivElement>(null); |
||
| 31 | |||
| 32 | const handleTabKey = (e: KeyboardEvent): void => { |
||
| 33 | if (modalRef && modalRef.current) { |
||
| 34 | const focusableModalElements = modalRef.current.querySelectorAll( |
||
| 35 | 'a[href], button, textarea, input[type="text"], input[type="email"], input[type="radio"], select', |
||
| 36 | ); |
||
| 37 | const firstElement = focusableModalElements[0] as HTMLElement; |
||
| 38 | const lastElement = focusableModalElements[ |
||
| 39 | focusableModalElements.length - 1 |
||
| 40 | ] as HTMLElement; |
||
| 41 | |||
| 42 | const focusableModalElementsArray = Array.from(focusableModalElements); |
||
| 43 | |||
| 44 | if ( |
||
| 45 | document.activeElement && |
||
| 46 | !focusableModalElementsArray.includes(document.activeElement) |
||
| 47 | ) { |
||
| 48 | firstElement.focus(); |
||
| 49 | e.preventDefault(); |
||
| 50 | } |
||
| 51 | |||
| 52 | if (!e.shiftKey && document.activeElement === lastElement) { |
||
| 53 | firstElement.focus(); |
||
| 54 | e.preventDefault(); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (e.shiftKey && document.activeElement === firstElement) { |
||
| 58 | lastElement.focus(); |
||
| 59 | e.preventDefault(); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | }; |
||
| 63 | |||
| 64 | // Collection of key codes and event listeners |
||
| 65 | const keyListenersMap = new Map([ |
||
| 66 | [27, onModalCancel], |
||
| 67 | [9, handleTabKey], |
||
| 68 | ]); |
||
| 69 | |||
| 70 | // Runs every time visible changes to set the overflow on the modal and update the body overflow |
||
| 71 | useEffect((): (() => void) => { |
||
| 72 | function setBodyStyle(): void { |
||
| 73 | document.body.style.overflow = visible ? "hidden" : "visible"; |
||
| 74 | } |
||
| 75 | setBodyStyle(); |
||
| 76 | // Runs on component unmount |
||
| 77 | return (): void => { |
||
| 78 | setBodyStyle(); |
||
| 79 | }; |
||
| 80 | }, [visible]); |
||
| 81 | |||
| 82 | // Adds various key commands to the modal |
||
| 83 | useEffect((): (() => void) => { |
||
| 84 | let keyListener; |
||
| 85 | if (visible) { |
||
| 86 | keyListener = (e: KeyboardEvent): void => { |
||
| 87 | const listener = keyListenersMap.get(e.keyCode); |
||
| 88 | return listener && listener(e); |
||
| 89 | }; |
||
| 90 | document.addEventListener("keydown", keyListener); |
||
| 91 | } |
||
| 92 | |||
| 93 | return (): void => { |
||
| 94 | if (keyListener !== undefined) { |
||
| 95 | document.removeEventListener("keydown", keyListener); |
||
| 96 | } |
||
| 97 | }; |
||
| 98 | }, [keyListenersMap, visible]); |
||
| 99 | |||
| 100 | if (parentElement !== null) { |
||
| 101 | return createPortal( |
||
| 102 | <div |
||
| 103 | aria-describedby={`${id}-description`} |
||
| 104 | aria-hidden={!visible} |
||
| 105 | aria-labelledby={`${id}-title`} |
||
| 106 | data-c-dialog={visible ? "active--overflowing" : ""} |
||
| 107 | data-c-padding="top(double) bottom(double)" |
||
| 108 | role="dialog" |
||
| 109 | ref={modalRef} |
||
| 110 | style={{ |
||
| 111 | display: "flex", |
||
| 112 | alignItems: "center", |
||
| 113 | justifyContent: "center", |
||
| 114 | }} |
||
| 115 | > |
||
| 116 | <div data-c-background="white(100)" data-c-radius="rounded"> |
||
| 117 | <modalContext.Provider |
||
| 118 | value={{ |
||
| 119 | id, |
||
| 120 | parentElement, |
||
| 121 | visible, |
||
| 122 | onModalConfirm, |
||
| 123 | onModalMiddle, |
||
| 124 | onModalCancel, |
||
| 125 | }} |
||
| 126 | > |
||
| 127 | {children} |
||
| 128 | </modalContext.Provider> |
||
| 129 | </div> |
||
| 130 | </div>, |
||
| 131 | parentElement, |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | return null; |
||
| 136 | } |
||
| 218 |