Skip to content

Commit

Permalink
Hide header in chat view
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Griffiths committed Aug 25, 2024
1 parent 5cbc2e9 commit 8f15fbe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
47 changes: 34 additions & 13 deletions src/components/webview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,55 @@ import { useRef, useEffect, useState, LegacyRef } from "react";

import { Loader } from "./loader";

export type IWebviewRef = LegacyRef<HTMLWebViewElement> & {
injectJavaScript: (script: string) => void;
export type IWebviewRef = HTMLWebViewElement & {
insertCSS: (css: string) => void;
};

export interface IWebviewProps {
src: string;
injectedCSS?: string;
}

export const Webview = ({ src }: IWebviewProps) => {
const webviewRef = useRef<HTMLWebViewElement>(null);
export const Webview = ({ src, injectedCSS }: IWebviewProps) => {
const webviewRef = useRef<IWebviewRef>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);

/**
* Show loading spinner when webview is initially loading
*/
useEffect(() => {
const webview = webviewRef.current;

if (webview) {
const handleLoadStop = () => {
setIsLoading(false);
};
if (!webview) return;

webview.addEventListener("did-stop-loading", handleLoadStop);
const handleLoadStop = () => {
setIsLoading(false);
};

return () => {
webview.removeEventListener("did-stop-loading", handleLoadStop);
};
}
webview.addEventListener("did-stop-loading", handleLoadStop);

return () => {
webview.removeEventListener("did-stop-loading", handleLoadStop);
};
}, []);

/**
* Inject CSS into webview
*/
useEffect(() => {
const webview = webviewRef.current;
if (!webview || !injectedCSS) return;

const handleDomReady = () => {
webview.insertCSS(injectedCSS);
};
webview.addEventListener("dom-ready", handleDomReady);

return () => {
webview.removeEventListener("dom-ready", handleDomReady);
};
}, [injectedCSS]);

return (
<StyledWebview>
{isLoading && <Loader />}
Expand Down
7 changes: 6 additions & 1 deletion src/pages/chat.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Webview } from "../components/webview";

export function ChatPage() {
return <Webview src="https://steamcommunity.com/chat" />;
return (
<Webview
src="https://steamcommunity.com/chat"
injectedCSS="[class^='main_SteamPageHeader_']{ display: none !important;}"
/>
);
}

0 comments on commit 8f15fbe

Please sign in to comment.