Skip to content

Commit

Permalink
app: Remove detected default OBS styles
Browse files Browse the repository at this point in the history
This avoids having to explicitly tell people to remove these default
styles, since the cause issues with the alignment of the overlays.
  • Loading branch information
evanpurkhiser committed Dec 21, 2020
1 parent 35fcc3e commit 39a37e7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/overlay/Router.tsx
Expand Up @@ -7,6 +7,7 @@ import globalCss from 'src/shared/globalCss';
import store from 'src/shared/store';

import NotFound from './components/NotFound';
import {ensureNoOBSDefaultStyles} from './utils/obsFixes';
import {registeredOverlays} from '.';

type Props = RouteComponentProps<{overlayKey: string}>;
Expand All @@ -27,11 +28,15 @@ const MapOverlay: React.FC<Props> = observer(props => {
return <descriptor.component config={instance.config} />;
});

const Router = () => (
<BrowserRouter>
<Global styles={globalCss} />
<Route path="/:overlayKey" component={MapOverlay} />
</BrowserRouter>
);
const Router = () => {
React.useEffect(ensureNoOBSDefaultStyles, []);

return (
<BrowserRouter>
<Global styles={globalCss} />
<Route path="/:overlayKey" component={MapOverlay} />
</BrowserRouter>
);
};

export default Router;
33 changes: 33 additions & 0 deletions src/overlay/utils/obsFixes.tsx
@@ -0,0 +1,33 @@
/**
* The default browser source styles that OBS injects for a new browser source.
*
* We should remove these default since the margin auto will break alignment
* for our overlays
*/
const DEFAULT_STYLES =
'body { background-color: rgba(0, 0, 0, 0); margin: 0px auto; overflow: hidden; }';

/**
* Locate the style element containing the default OBS browser source styles
*/
export const findDefaultOBSStlyeNode = () =>
Array.from(document.querySelectorAll('head style:not([data-emotion])')).find(
el => el.innerHTML === DEFAULT_STYLES
);

export const ensureNoOBSDefaultStyles = () => {
findDefaultOBSStlyeNode()?.remove();

const head = document.querySelector('head');

if (head === null) {
return () => null;
}
const observer = new MutationObserver(() => {
findDefaultOBSStlyeNode()?.remove();
});

observer.observe(head, {childList: true, attributes: false});

return () => observer.disconnect();
};

1 comment on commit 39a37e7

@vercel
Copy link

@vercel vercel bot commented on 39a37e7 Dec 21, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.