Conditions | 11 |
Total Lines | 109 |
Code Lines | 81 |
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, { |
||
25 | |||
26 | export default function Modal({ |
||
27 | id, |
||
28 | parentElement, |
||
29 | visible, |
||
30 | children, |
||
31 | onModalConfirm, |
||
32 | onModalMiddle, |
||
33 | onModalCancel, |
||
34 | }: ModalProps): React.ReactPortal | null { |
||
35 | // Set up div ref to measure modal height |
||
36 | const modalRef = useRef<HTMLDivElement>(null); |
||
37 | |||
38 | const handleTabKey = (e: KeyboardEvent): void => { |
||
39 | if (modalRef && modalRef.current) { |
||
40 | const focusableModalElements = modalRef.current.querySelectorAll( |
||
41 | 'a[href], button, textarea, input[type="text"], input[type="email"], input[type="radio"], select', |
||
42 | ); |
||
43 | const firstElement = focusableModalElements[0] as HTMLElement; |
||
44 | const lastElement = focusableModalElements[ |
||
45 | focusableModalElements.length - 1 |
||
46 | ] as HTMLElement; |
||
47 | |||
48 | const focusableModalElementsArray = Array.from(focusableModalElements); |
||
49 | |||
50 | if ( |
||
51 | document.activeElement && |
||
52 | !focusableModalElementsArray.includes(document.activeElement) |
||
53 | ) { |
||
54 | firstElement.focus(); |
||
55 | e.preventDefault(); |
||
56 | } |
||
57 | |||
58 | if (!e.shiftKey && document.activeElement === lastElement) { |
||
59 | firstElement.focus(); |
||
60 | e.preventDefault(); |
||
61 | } |
||
62 | |||
63 | if (e.shiftKey && document.activeElement === firstElement) { |
||
64 | lastElement.focus(); |
||
65 | e.preventDefault(); |
||
66 | } |
||
67 | } |
||
68 | }; |
||
69 | |||
70 | // Collection of key codes and event listeners |
||
71 | const keyListenersMap = new Map([[27, onModalCancel], [9, handleTabKey]]); |
||
72 | |||
73 | // Runs every time visible changes to set the overflow on the modal and update the body overflow |
||
74 | useEffect((): (() => void) => { |
||
75 | function setBodyStyle(): void { |
||
76 | document.body.style.overflow = visible ? "hidden" : "visible"; |
||
77 | } |
||
78 | setBodyStyle(); |
||
79 | // Runs on component unmount |
||
80 | return (): void => { |
||
81 | setBodyStyle(); |
||
82 | }; |
||
83 | }, [visible]); |
||
84 | |||
85 | // Adds various key commands to the modal |
||
86 | useEffect((): (() => void) => { |
||
87 | let keyListener; |
||
88 | if (visible) { |
||
89 | keyListener = (e: KeyboardEvent): void => { |
||
90 | const listener = keyListenersMap.get(e.keyCode); |
||
91 | return listener && listener(e); |
||
92 | }; |
||
93 | document.addEventListener("keydown", keyListener); |
||
94 | } |
||
95 | |||
96 | return (): void => { |
||
97 | if (keyListener !== undefined) { |
||
98 | document.removeEventListener("keydown", keyListener); |
||
99 | } |
||
100 | }; |
||
101 | }, [keyListenersMap, visible]); |
||
102 | |||
103 | if (parentElement !== null) { |
||
104 | return createPortal( |
||
105 | <div |
||
106 | aria-describedby={`${id}-description`} |
||
107 | aria-hidden={!visible} |
||
108 | aria-labelledby={`${id}-title`} |
||
109 | data-c-dialog={visible ? "active--overflowing" : ""} |
||
110 | data-c-padding="top(double) bottom(double)" |
||
111 | role="dialog" |
||
112 | ref={modalRef} |
||
113 | > |
||
114 | <div data-c-background="white(100)" data-c-radius="rounded"> |
||
115 | <modalContext.Provider |
||
116 | value={{ |
||
117 | id, |
||
118 | parentElement, |
||
119 | visible, |
||
120 | onModalConfirm, |
||
121 | onModalMiddle, |
||
122 | onModalCancel, |
||
123 | }} |
||
124 | > |
||
125 | {children} |
||
126 | </modalContext.Provider> |
||
127 | </div> |
||
128 | </div>, |
||
129 | parentElement, |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | return null; |
||
134 | } |
||
216 |