aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docs/components/Tabs.tsx
diff options
context:
space:
mode:
authorHsiangNianian <admin@jyunko.cn>2023-04-22 19:52:26 +0800
committerHsiangNianian <admin@jyunko.cn>2023-04-22 19:52:26 +0800
commit4838df315931bb883f704ec3e1abe2685f296cdf (patch)
tree57a8550c4cd5338f1126364bb518c6cde8d96e7d /docs/components/Tabs.tsx
parentdb74ade0234a40c2120ad5f2a41bee50ce13de02 (diff)
downloadHydroRoll-4838df315931bb883f704ec3e1abe2685f296cdf.tar.gz
HydroRoll-4838df315931bb883f704ec3e1abe2685f296cdf.zip
😀
Diffstat (limited to 'docs/components/Tabs.tsx')
-rw-r--r--docs/components/Tabs.tsx37
1 files changed, 37 insertions, 0 deletions
diff --git a/docs/components/Tabs.tsx b/docs/components/Tabs.tsx
new file mode 100644
index 0000000..f75019a
--- /dev/null
+++ b/docs/components/Tabs.tsx
@@ -0,0 +1,37 @@
+import type { FC, ReactElement } from "react";
+
+import { Tabs as NextraTabs, Tab } from "nextra-theme-docs";
+import useSWR from "swr";
+
+export { Tab };
+
+export const Tabs: FC<{
+ storageKey?: string;
+ items: string[];
+ children: ReactElement;
+}> = function ({ storageKey = "tab-index", items, children = null, ...props }) {
+ // Use SWR so all tabs with the same key can sync their states.
+ const { data, mutate } = useSWR(storageKey, (key) => {
+ try {
+ return JSON.parse(localStorage.getItem(key));
+ } catch (e) {
+ return null;
+ }
+ });
+
+ const selectedIndex = items.indexOf(data);
+
+ return (
+ <NextraTabs
+ onChange={(index) => {
+ localStorage.setItem(storageKey, JSON.stringify(items[index]));
+ mutate(items[index], false);
+ }}
+ selectedIndex={selectedIndex === -1 ? undefined : selectedIndex}
+ items={items}
+ {...props}
+ >
+ {children}
+ </NextraTabs>
+ );
+};