Skip to content

Commit

Permalink
feat(AppRoot): add disableParentTransformForPositionFixedElements prop
Browse files Browse the repository at this point in the history
Перенёс `translate3d(0, 0, 0)` из CSS в компонент.
  • Loading branch information
inomdzhon committed Aug 24, 2023
1 parent b83e532 commit 31a9613
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/vkui/src/components/AppRoot/AppRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ describe('AppRoot', () => {
render(<AppRoot mode="embedded" />).unmount();
expect(document.body).toContainElement(portalRoot1 as HTMLElement);
});
it('should disable CSS transform on parent for mode="embedded"', () => {
const { rerender, container } = render(<AppRoot mode="embedded" />);
expect(container.style.transform).toBe('translate3d(0, 0, 0)');
rerender(<AppRoot mode="embedded" disableParentTransformForPositionFixedElements />);
expect(container.style.transform).toBe('');
});
it('Accepts custom portal root', () => {
const customPortalRoot = document.createElement('div');
let portalRoot: HTMLElement | undefined | null;
Expand Down
28 changes: 24 additions & 4 deletions packages/vkui/src/components/AppRoot/AppRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ export interface AppRootProps extends React.HTMLAttributes<HTMLDivElement> {
* Кастомный root-элемент портала
*/
portalRoot?: HTMLElement | React.RefObject<HTMLElement> | null;
/** Disable portal for components */
/**
* Отключает рендер всплывающих компонентов в отдельном контейнере
*/
disablePortal?: boolean;
/**
* По умолчанию, mode="embedded" переносит систему координат элементов с `position: fixed` на
* свой контейнер через `transform: translate3d(0, 0, 0)`.
*
* Это поведение можно отключить с помощью этого параметра.
*/
disableParentTransformForPositionFixedElements?: boolean;
}

/**
Expand All @@ -56,6 +65,7 @@ export const AppRoot = ({
scroll = 'global',
portalRoot: portalRootProp = null,
disablePortal,
disableParentTransformForPositionFixedElements,
className,
safeAreaInsets,
...props
Expand Down Expand Up @@ -107,12 +117,22 @@ export const AppRoot = ({

const parent = rootRef.current?.parentElement;
const classes = ['vkui__root'].concat(mode === 'embedded' ? 'vkui__root--embedded' : []);
parent?.classList.add(...classes);
if (parent) {
if (!disableParentTransformForPositionFixedElements) {
parent.style.transform = 'translate3d(0, 0, 0)';
}
parent.classList.add(...classes);
}

return () => {
parent?.classList.remove(...classes);
if (parent) {
if (!disableParentTransformForPositionFixedElements) {
parent.style.transform = '';
}
parent.classList.remove(...classes);
}
};
}, []);
}, [disableParentTransformForPositionFixedElements]);

useIsomorphicLayoutEffect(() => {
if (mode === 'full') {
Expand Down
1 change: 0 additions & 1 deletion packages/vkui/src/styles/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
}

.vkui__root--embedded {
transform: translate3d(0, 0, 0);
overflow-x: hidden;
}

Expand Down

0 comments on commit 31a9613

Please sign in to comment.