Skip to content

Commit

Permalink
feat: devtools to shadow dom to avoid css influence
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Sep 3, 2022
1 parent 80769a4 commit 7097b4a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/KeyContextMenu/KeyContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import MenuItem from '@mui/material/MenuItem';
import { styled } from '@mui/material/styles';
import type { TranslationService } from '@tolgee/core/lib/services/TranslationService';

import { ThemeProvider } from '../ThemeProvider';
import { DEVTOOLS_ID, DEVTOOLS_Z_INDEX } from '../constants';
import { ThemeProvider } from '../ThemeProvider';

const ScMenuItem = styled(MenuItem)`
display: flex;
Expand Down
20 changes: 17 additions & 3 deletions packages/ui/src/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react';
import React, { useRef } from 'react';
import {
createTheme,
ThemeProvider as MuiThemeProvider,
} from '@mui/material/styles';
import { DEVTOOLS_Z_INDEX } from './constants';
import { DEVTOOLS_ID, DEVTOOLS_Z_INDEX } from './constants';
import createCache from '@emotion/cache';
import { CacheProvider } from '@emotion/react';

let theme = createTheme({
typography: {
Expand Down Expand Up @@ -53,5 +55,17 @@ theme = createTheme(theme, {
});

export const ThemeProvider: React.FC = ({ children }) => {
return <MuiThemeProvider theme={theme}>{children}</MuiThemeProvider>;
const cache = useRef(
createCache({
key: 'css',
prepend: true,
container: document.getElementById(DEVTOOLS_ID).shadowRoot,
})
);

return (
<CacheProvider value={cache.current!}>
<MuiThemeProvider theme={theme}>{children}</MuiThemeProvider>
</CacheProvider>
);
};
5 changes: 4 additions & 1 deletion packages/ui/src/common/BodyEnd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export class BodyEnd extends React.PureComponent<{ document?: Document }> {
}

get devTools() {
return this.document.getElementById(DEVTOOLS_ID) || this.document.body;
return (
this.document.getElementById(DEVTOOLS_ID)?.shadowRoot ||
this.document.body
);
}

componentDidMount() {
Expand Down
13 changes: 8 additions & 5 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ export class UI {
private keyContextMenu: KeyContextMenu;

constructor(private dependencies: ComponentDependencies) {
const devTools = document.createElement('div');
devTools.id = DEVTOOLS_ID;
document.body.append(devTools);
const rootElement = document.createElement('div');
rootElement.id = DEVTOOLS_ID;
rootElement.attachShadow({ mode: 'open' });
const devTools = rootElement.shadowRoot;

document.body.appendChild(rootElement);

const tolgeeModalContainer = document.createElement('div');
devTools.append(tolgeeModalContainer);
devTools.appendChild(tolgeeModalContainer);

const contextMenuContainer = document.createElement('div');
devTools.append(contextMenuContainer);
devTools.appendChild(contextMenuContainer);

const viewerElement = createElement(KeyDialog, {
dependencies: this.dependencies,
Expand Down

0 comments on commit 7097b4a

Please sign in to comment.