UI: fix browser compatibility

This commit is contained in:
Fuma Nama
2024-11-20 21:00:27 +08:00
parent 8e54a402fb
commit 6fd480f10f
4 changed files with 30 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-ui': patch
---
Fix old browser compatibility
@@ -0,0 +1,18 @@
'use client';
import { useEffect, useState } from 'react';
export function UrlBar() {
const [url, setUrl] = useState('');
useEffect(() => {
const timer = setInterval(() => {
setUrl(window.location.pathname + window.location.hash);
}, 100);
return () => {
clearInterval(timer);
};
}, []);
return <pre className="rounded-lg border bg-card p-2 text-sm">{url}</pre>;
}
@@ -6,6 +6,8 @@ description:
preview: tabs
---
import { UrlBar } from './tabs.client';
## Usage
Import it in your MDX documents.
@@ -97,6 +99,8 @@ to automatically update the URL hash whenever time a new tab is selected:
</Tabs>
```
<UrlBar />
<Tabs items={['Hello', 'World']} updateAnchor>
<Tab id="tab-hello" value="Hello">
Hello!
+3 -3
View File
@@ -98,9 +98,9 @@ export function Tabs({
const hash = window.location.hash.slice(1);
if (!hash) return;
const entry = valueToIdMapRef.current
.entries()
.find(([_, id]) => id === hash);
const entry = Array.from(valueToIdMapRef.current.entries()).find(
([_, id]) => id === hash,
);
if (entry) setValue(entry[0]);
}, []);