Merge pull request #5399 from Amr-Shams/main
feat(examples): sync dropdown with current example on refresh
This commit is contained in:
@@ -6,13 +6,17 @@ import routes from './routes';
|
|||||||
export default function Header() {
|
export default function Header() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [currentPath, setCurrentPath] = useState(location.pathname);
|
|
||||||
|
const pathParts = location.pathname.split('/').filter(Boolean);
|
||||||
|
const initialExample = pathParts.length > 1 ? pathParts[1] : 'basic';
|
||||||
|
|
||||||
|
const [currentPath, setCurrentPath] = useState(initialExample);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const name = routes.find((route) => route.path === currentPath)?.name;
|
const name = routes.find((route) => route.path === currentPath)?.name;
|
||||||
document.title = `React Flow Examples${name ? ' - ' + name : ''}`;
|
document.title = `React Flow Examples${name ? ' - ' + name : ''}`;
|
||||||
navigate(currentPath);
|
navigate(currentPath);
|
||||||
}, [currentPath]);
|
}, [currentPath, navigate]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -20,7 +24,11 @@ export default function Header() {
|
|||||||
<a className="logo" href="https://github.com/xyflow/xyflow">
|
<a className="logo" href="https://github.com/xyflow/xyflow">
|
||||||
React Flow Dev
|
React Flow Dev
|
||||||
</a>
|
</a>
|
||||||
<select value={currentPath} onChange={(event) => setCurrentPath(event.target.value)}>
|
<select
|
||||||
|
value={currentPath}
|
||||||
|
onChange={(event) => setCurrentPath(event.target.value)}
|
||||||
|
aria-label="select an example"
|
||||||
|
>
|
||||||
{routes.map((route) => (
|
{routes.map((route) => (
|
||||||
<option value={route.path} key={route.path}>
|
<option value={route.path} key={route.path}>
|
||||||
{route.name}
|
{route.name}
|
||||||
|
|||||||
Reference in New Issue
Block a user