File tree Expand file tree Collapse file tree 3 files changed +52
-2
lines changed
Expand file tree Collapse file tree 3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change @@ -24,16 +24,31 @@ let uuid = 0;
2424const cacheStyle = new Map < Element , React . CSSProperties > ( ) ;
2525
2626export default class ScrollLocker {
27- lockTarget : typeof uuid ;
27+ private lockTarget : typeof uuid ;
2828
29- options : scrollLockOptions ;
29+ private options : scrollLockOptions ;
3030
3131 constructor ( options ?: scrollLockOptions ) {
3232 // eslint-disable-next-line no-plusplus
3333 this . lockTarget = uuid ++ ;
3434 this . options = options ;
3535 }
3636
37+ getContainer = ( ) : HTMLElement | undefined => {
38+ return this . options ?. container ;
39+ } ;
40+
41+ // if options change...
42+ reLock = ( options ?: scrollLockOptions ) => {
43+ const findLock = locks . find ( ( { target } ) => target === this . lockTarget ) ;
44+ findLock && this . unLock ( ) ;
45+
46+ this . options = options ;
47+ findLock && ( findLock . options = options ) ;
48+
49+ findLock && this . lock ( ) ;
50+ } ;
51+
3752 lock = ( ) => {
3853 // If lockTarget exist return
3954 if ( locks . some ( ( { target } ) => target === this . lockTarget ) ) {
Original file line number Diff line number Diff line change @@ -90,11 +90,28 @@ class PortalWrapper extends React.Component<PortalWrapperProps> {
9090
9191 componentDidUpdate ( prevProps : PortalWrapperProps ) {
9292 this . updateOpenCount ( prevProps ) ;
93+ this . updateScrollLocker ( prevProps ) ;
9394
9495 this . setWrapperClassName ( ) ;
9596 this . attachToParent ( ) ;
9697 }
9798
99+ updateScrollLocker = ( prevProps ?: Partial < PortalWrapperProps > ) => {
100+ const { visible : prevVisible } = prevProps || { } ;
101+ const { getContainer, visible } = this . props ;
102+
103+ if (
104+ visible &&
105+ visible !== prevVisible &&
106+ supportDom &&
107+ getParent ( getContainer ) !== this . scrollLocker . getContainer ( )
108+ ) {
109+ this . scrollLocker . reLock ( {
110+ container : getParent ( getContainer ) as HTMLElement ,
111+ } ) ;
112+ }
113+ } ;
114+
98115 updateOpenCount = ( prevProps ?: Partial < PortalWrapperProps > ) => {
99116 const { visible : prevVisible , getContainer : prevGetContainer } =
100117 prevProps || { } ;
Original file line number Diff line number Diff line change @@ -124,4 +124,22 @@ describe('ScrollLocker', () => {
124124 expect ( testContainer . className ) . toBe ( '' ) ;
125125 expect ( testContainer . getAttribute ( 'style' ) ) . toBe ( initialStyle ) ;
126126 } ) ;
127+
128+ it ( 'reLock' , ( ) => {
129+ scrollLocker . lock ( ) ;
130+ const testContainer = document . createElement ( 'div' ) ;
131+
132+ expect ( document . body . className ) . toBe ( effectClassname ) ;
133+ expect ( document . body . getAttribute ( 'style' ) ) . toBe ( effectStyle ) ;
134+
135+ scrollLocker . reLock ( {
136+ container : testContainer ,
137+ } ) ;
138+
139+ expect ( document . body . className ) . toBe ( '' ) ;
140+ expect ( document . body . getAttribute ( 'style' ) ) . toBe ( initialStyle ) ;
141+
142+ expect ( testContainer . className ) . toBe ( effectClassname ) ;
143+ expect ( testContainer . getAttribute ( 'style' ) ) . toBe ( effectStyle ) ;
144+ } ) ;
127145} ) ;
You can’t perform that action at this time.
0 commit comments