Skip to content

Commit

Permalink
Merge pull request #29 from toririm/feature/header
Browse files Browse the repository at this point in the history
ヘッダーを追加&接続ステータスを表示
  • Loading branch information
toririm authored Aug 27, 2024
2 parents 12b77b2 + 903a63e commit cdb88f8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/components/online-status.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useSyncExternalStore } from "react";

const subscribe = (callback: () => void) => {
window.addEventListener("online", callback);
window.addEventListener("offline", callback);
return () => {
window.removeEventListener("online", callback);
window.removeEventListener("offline", callback);
};
};

export const useOnlineStatus = () => {
return useSyncExternalStore(
subscribe,
() => navigator.onLine,
() => true,
);
};

export const OnlineStatus = () => {
const online = useOnlineStatus();
return online ? <p>✅オンライン</p> : <p>⛔オフライン</p>;
};
File renamed without changes.
18 changes: 18 additions & 0 deletions app/routes/_header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Outlet } from "@remix-run/react";
import { OnlineStatus } from "~/components/online-status";

export default function BaseHeader() {
return (
<div>
<header className="bg-gray-800 p-4 text-white">
<h1 className="text-3xl">かふぇおれPOS 2024</h1>
<OnlineStatus />
</header>
<Outlet />
</div>
);
}

// cacher.tsx -> _header.cacher.tsx と変更することで
// cacher.tsx ページに対して上記ヘッダーを付与することができる
// 子となったcacher.tsxの中身が <Outlet /> に入るイメージ

0 comments on commit cdb88f8

Please sign in to comment.