-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'carbon-v11' into 2917-add-hooks-docs-v11
- Loading branch information
Showing
15 changed files
with
129 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/cloud-cognitive/src/global/js/hooks/useResizeObserver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { useRef, useState, useLayoutEffect } from 'react'; | ||
|
||
export const useResizeObserver = ( | ||
ref, | ||
options = { callback: null, throttleInterval: 0 } | ||
) => { | ||
const { callback, throttleInterval } = options; | ||
const [width, setWidth] = useState(0); | ||
const [height, setHeight] = useState(0); | ||
const throttleTimeout = useRef(null); | ||
const entriesToHandle = useRef(null); | ||
|
||
useLayoutEffect(() => { | ||
if (!ref?.current) { | ||
return; | ||
} | ||
|
||
const doCallbacks = () => { | ||
if (!ref?.current || !Array.isArray(entriesToHandle?.current)) { | ||
return; | ||
} | ||
|
||
const entry = entriesToHandle.current[0]; | ||
|
||
setWidth(entry.contentRect.width); | ||
setHeight(entry.contentRect.height); | ||
|
||
throttleTimeout.current = null; | ||
|
||
callback && callback(entry.contentRect); | ||
}; | ||
|
||
let observer = new ResizeObserver((entries) => { | ||
// always update entriesToHandle | ||
entriesToHandle.current = entries; | ||
|
||
if (throttleInterval) { | ||
// if a throttleInterval check for running timeout | ||
if (throttleTimeout.current === null) { | ||
// no live timeout set entries to handle and move on | ||
|
||
// set up throttle | ||
throttleTimeout.current = setTimeout(() => { | ||
// do callbacks | ||
doCallbacks(); | ||
// reset throttle | ||
throttleTimeout.current = null; | ||
}, throttleInterval); | ||
} | ||
} else { | ||
// no throttle do callbacks every time | ||
doCallbacks(); | ||
} | ||
}); | ||
|
||
// observe all refs passed | ||
observer.observe(ref.current); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
observer = null; | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ref, options]); | ||
|
||
return { width, height }; | ||
}; |
Oops, something went wrong.