Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState

componentDidMount() {
document.addEventListener('keydown', this.handleGlobalKeys);
this.observer = getResizeObserver(this.ref.current, this.handleResize);
this.observer = getResizeObserver(this.ref.current, this.handleResize, true);
Copy link
Member

@dlabrecq dlabrecq Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since useRequestAnimationFrame is opt-in, thinking we should leave this off the examples. Don't feel strongly about it, but we don't need it for most cases.

this.handleResize();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/components/Nav/NavList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class NavList extends React.Component<NavListProps> {
};

componentDidMount() {
this.observer = getResizeObserver(this.navList.current, this.handleScrollButtons);
this.observer = getResizeObserver(this.navList.current, this.handleScrollButtons, true);
this.handleScrollButtons();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class TextInputBase extends React.Component<TextInputProps, TextInputStat
componentDidMount() {
if (this.props.isLeftTruncated) {
const inputRef = this.props.innerRef || this.inputRef;
this.observer = getResizeObserver(inputRef.current, this.handleResize);
this.observer = getResizeObserver(inputRef.current, this.handleResize, true);
this.handleResize();
}
}
Expand Down
23 changes: 13 additions & 10 deletions packages/react-core/src/demos/JumpLinks.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ ScrollspyH2 = () => {
const masthead = document.getElementsByClassName('pf-c-masthead')[0];
const offsetForPadding = 10;

getResizeObserver(masthead, () => {
if (isVertical) {
setOffsetHeight(masthead.offsetHeight + offsetForPadding);
} else {
// Append jump links nav height to the masthead height when value exists.
const jumpLinksHeaderHeight = document.getElementsByClassName('pf-m-sticky')[0].offsetHeight;
jumpLinksHeaderHeight && setOffsetHeight(masthead.offsetHeight + jumpLinksHeaderHeight + offsetForPadding);
}
});
getResizeObserver(
masthead,
() => {
if (isVertical) {
setOffsetHeight(masthead.offsetHeight + offsetForPadding);
} else {
// Append jump links nav height to the masthead height when value exists.
const jumpLinksHeaderHeight = document.getElementsByClassName('pf-m-sticky')[0].offsetHeight;
jumpLinksHeaderHeight && setOffsetHeight(masthead.offsetHeight + jumpLinksHeaderHeight + offsetForPadding);
}
},
true
);
}, [isVertical]);

return (
Expand Down Expand Up @@ -132,7 +136,6 @@ ScrollspyH2 = () => {
};
```


### With drawer

This demo shows how jump links can be used in combination with a drawer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ export const JumpLinksWithDrawer = () => {
const masthead = document.getElementsByClassName('pf-c-masthead')[0];
const drawerToggleSection = document.getElementById('drawer-toggle');

getResizeObserver(masthead, () => {
setOffsetHeight(masthead.offsetHeight + drawerToggleSection.offsetHeight);
});
getResizeObserver(
masthead,
() => {
setOffsetHeight(masthead.offsetHeight + drawerToggleSection.offsetHeight);
},
true
);
}, []);

const onCloseClick = () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/react-core/src/helpers/resizeObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { canUseDOM } from './util';
* private observer: any = () => {};
*
* public componentDidMount() {
* this.observer = getResizeObserver(this.containerRef.current, this.handleResize);
* this.observer = getResizeObserver(this.containerRef.current, this.handleResize, true);
* }
*
* public componentWillUnmount() {
Expand All @@ -37,7 +37,7 @@ import { canUseDOM } from './util';
* private observer: any = () => {};
*
* public componentDidMount() {
* this.observer = getResizeObserver(this.inputRef.current, this.handleResize);
* this.observer = getResizeObserver(this.inputRef.current, this.handleResize, true);
* }
*
* public componentWillUnmount() {
Expand All @@ -59,18 +59,18 @@ import { canUseDOM } from './util';
* Example 3 - With debounced method passed in:
*
* public componentDidMount() {
* this.observer = getResizeObserver(this.inputRef.current, debounce(this.handleResize, 250), false);
* this.observer = getResizeObserver(this.inputRef.current, debounce(this.handleResize, 250));
* }
*
* @param {Element} containerRefElement The container reference to observe
* @param {Function} handleResize The function to call for resize events
* @param {boolean} useRequestAnimationFrame Whether to pass the handleResize function as a callback to requestAnimationFrame. Pass in false when the function passed in is debounced. Defaults to true.
* @param {boolean} useRequestAnimationFrame Whether to pass the handleResize function as a callback to requestAnimationFrame. Pass in true when the function passed in is not debounced.
* @return {Function} The function used to unobserve resize events
*/
export const getResizeObserver = (
containerRefElement: Element,
handleResize: () => void,
useRequestAnimationFrame: boolean = true
useRequestAnimationFrame?: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IS it worth adding some comment here about when to set this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's blocked out in the files tab, but I did previously add this description for the parameter:

* @param {boolean} useRequestAnimationFrame Whether to pass the handleResize function as a callback to requestAnimationFrame. Pass in false when the function passed in is debounced. Defaults to true.

That said I just pushed another commit to update the description since it was slightly inaccurate after this update.

) => {
let unobserve: any;

Expand Down