Skip to content

Commit b8577ba

Browse files
committed
Implemented scroll-lock
1 parent 313592c commit b8577ba

File tree

1 file changed

+4
-77
lines changed

1 file changed

+4
-77
lines changed

src/ui/Dialog/index.js

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import { nanoid } from 'nanoid'
55
import { ReactComponent as CloseIcon } from './icon-close.svg'
66

77
import './styles.scss'
8+
import ScrollLock from 'react-scrolllock'
89

910
export function Dialog ({ isOpen, title, className, persistent, width, height, showCloseButton, onDismiss, onCloseBtnClick, onClickOutside, onPressEscape, style, ...props }) {
1011
// Set an unique ID for the dialog
1112
const [id] = useState(`${nanoid()}`)
12-
// Parents that have overflow set
13-
const [overflowParents, setoverflowParents] = useState();
1413

1514
const parsedStyles = useMemo(() => {
1615
const _style = { ...style }
@@ -46,79 +45,6 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
4645
}
4746
})
4847

49-
useEffect(() => {
50-
const dialogs = document.getElementsByClassName('dialog')
51-
const thisDialog = document.getElementById(`dialog-${id}`);
52-
53-
/**
54-
* Retreives all parents of an element
55-
* @param {HTMLElement} element
56-
* @returns {HTMLElement[] | undefined}
57-
*/
58-
function getParents(element) {
59-
if(!element) return;
60-
if(!element.parentNode) return;
61-
const parents = [];
62-
let currentElement = element;
63-
while(currentElement.parentNode && currentElement.parentNode.style) {
64-
currentElement = currentElement.parentNode;
65-
console.log('Current name', currentElement.nodeName)
66-
parents.push(currentElement);
67-
}
68-
return parents;
69-
}
70-
71-
function enableScrolling() {
72-
if(dialogs.length > 0) return;
73-
74-
// Reset all overflow settings
75-
if(overflowParents && overflowParents.length > 0) {
76-
for(const parent of overflowParents) {
77-
parent.element.style = {
78-
...parent.element.style,
79-
...parent.style
80-
}
81-
}
82-
setoverflowParents([]);
83-
}
84-
}
85-
86-
if(isOpen) {
87-
// Get all parents
88-
let parents = getParents(thisDialog);
89-
90-
if(parents && parents.length > 0) {
91-
// Filter to only the parents where overflow is specified
92-
console.log('Found parents', parents)
93-
// Parent state
94-
let parentState = [];
95-
for(const parent of parents) {
96-
// Retreive the parents overflow state
97-
parentState.push({
98-
element: parent,
99-
style: {
100-
overflow: parent.style.overflow,
101-
overflowX: parent.style.overflowX,
102-
overflowY: parent.style.overflowY
103-
}
104-
})
105-
// Set the overflow to hidden
106-
parent.style.overflow = 'hidden';
107-
parent.style.overflowX = 'hidden';
108-
parent.style.overflowY = 'hidden';
109-
}
110-
setoverflowParents(parentState);
111-
}
112-
} else {
113-
enableScrolling();
114-
}
115-
116-
return () => {
117-
enableScrolling();
118-
}
119-
}, [isOpen])
120-
121-
12248
// Plays the shaking animation-effect when the dialog is persistent
12349
function shakeDialogBox () {
12450
const dialog = document.getElementById(`dialog-${id}`)
@@ -149,7 +75,7 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
14975
// Render the component
15076
return (
15177
isOpen === true &&
152-
<>
78+
<ScrollLock isActive={isOpen}>
15379
<div id={`dialog-backdrop-${id}`} className={`dialog-backdrop ${className}`} onClick={(e) => handleBackdropClick(e)}>
15480
<div
15581
id={`dialog-${id}`}
@@ -158,6 +84,7 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
15884
aria-modal='true'
15985
role='dialog'
16086
style={parsedStyles}
87+
onClick={(e) => { e.preventDefault(); e.stopPropagation(); }}
16188
>
16289
{!persistent && showCloseButton &&
16390
<button className='dialog-close-btn' onClick={(e) => { handleCloseBtnClick(); e.preventDefault() }} aria-label='Lukk'>
@@ -166,7 +93,7 @@ export function Dialog ({ isOpen, title, className, persistent, width, height, s
16693
{props.children}
16794
</div>
16895
</div>
169-
</>
96+
</ScrollLock>
17097
)
17198
}
17299

0 commit comments

Comments
 (0)