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
11 changes: 8 additions & 3 deletions packages/base/src/hooks/useIsRTL.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getRTL } from '@ui5/webcomponents-base/dist/config/RTL';
import { useIsomorphicLayoutEffect } from '@ui5/webcomponents-react-base/dist/hooks';
import { RefObject, useState } from 'react';
import { RefObject, useRef, useState } from 'react';

const GLOBAL_DIR_CSS_VAR = '--_ui5_dir';

Expand Down Expand Up @@ -32,13 +32,17 @@ const detectRTL = (elementRef: RefObject<HTMLElement>) => {

const useIsRTL = (elementRef: RefObject<HTMLElement>): boolean => {
const [isRTL, setRTL] = useState<boolean>(getRTL()); // use config RTL as best guess
const isMounted = useRef(false);
useIsomorphicLayoutEffect(() => {
isMounted.current = true;
setRTL(detectRTL(elementRef)); // update immediately while rendering
const targets = [document.documentElement, document.body, elementRef.current].filter(Boolean);
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.attributeName === 'dir') {
setRTL(detectRTL(elementRef));
if (isMounted.current) {
setRTL(detectRTL(elementRef));
}
}
});
});
Expand All @@ -48,9 +52,10 @@ const useIsRTL = (elementRef: RefObject<HTMLElement>): boolean => {
});

return () => {
isMounted.current = false;
observer.disconnect();
};
}, []);
}, [isMounted]);

return isRTL;
};
Expand Down
15 changes: 12 additions & 3 deletions packages/main/src/components/DynamicPageTitle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import React, {
Ref,
useCallback,
useEffect,
useRef,
useState
} from 'react';
import { createUseStyles } from 'react-jss';
Expand Down Expand Up @@ -106,6 +107,14 @@ const DynamicPageTitle: FC<DynamicPageTitleProps> = forwardRef((props: InternalP
const dynamicPageTitleRef = useConsolidatedRef<HTMLDivElement>(ref);
const [showNavigationInTopArea, setShowNavigationInTopArea] = useState(undefined);
const isRtl = useIsRTL(dynamicPageTitleRef);
const isMounted = useRef(false);

useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, [isMounted]);

if (isIE()) {
containerClasses.put(classes.iEClass);
Expand Down Expand Up @@ -134,9 +143,9 @@ const DynamicPageTitle: FC<DynamicPageTitleProps> = forwardRef((props: InternalP
: titleContainer.borderBoxSize;
// Safari doesn't implement `borderBoxSize`
const titleContainerWidth = borderBoxSize?.inlineSize ?? titleContainer.target.getBoundingClientRect().width;
if (titleContainerWidth < 1280 && !showNavigationInTopArea === false) {
if (titleContainerWidth < 1280 && !showNavigationInTopArea === false && isMounted.current) {
setShowNavigationInTopArea(false);
} else if (titleContainerWidth >= 1280 && !showNavigationInTopArea === true) {
} else if (titleContainerWidth >= 1280 && !showNavigationInTopArea === true && isMounted.current) {
setShowNavigationInTopArea(true);
}
}, 300)
Expand All @@ -147,7 +156,7 @@ const DynamicPageTitle: FC<DynamicPageTitleProps> = forwardRef((props: InternalP
return () => {
observer.disconnect();
};
}, [dynamicPageTitleRef.current, showNavigationInTopArea]);
}, [dynamicPageTitleRef.current, showNavigationInTopArea, isMounted]);

const paddingLeftRtl = isRtl ? 'paddingRight' : 'paddingLeft';

Expand Down
9 changes: 7 additions & 2 deletions packages/main/src/internal/useResponsiveContentPadding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@ const useStyles = createUseStyles(
export const useResponsiveContentPadding = (element) => {
const [currentRange, setCurrentRange] = useState(getCurrentRange().name);
const resizeTimeout = useRef(null);
const isMounted = useRef(false);
const classes = useStyles();

useEffect(() => {
isMounted.current = true;
const observer = new ResizeObserver(([el]) => {
if (resizeTimeout.current) {
clearTimeout(resizeTimeout.current);
}
resizeTimeout.current = setTimeout(() => {
setCurrentRange(() => getCurrentRange(el.contentRect.width)?.name);
if (isMounted.current) {
setCurrentRange(() => getCurrentRange(el.contentRect.width)?.name);
}
}, 150);
});
if (element) {
observer.observe(element);
}
return () => {
isMounted.current = false;
observer.disconnect();
};
}, [element]);
}, [element, isMounted]);

return classes[currentRange];
};