Skip to content

Commit

Permalink
Support permalinking examples (#365)
Browse files Browse the repository at this point in the history
closes #361
  • Loading branch information
zpao authored Aug 27, 2024
1 parent 1c7da3c commit 38e030a
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions examples/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, StrictMode} from 'react';
import React, {useState, StrictMode, useCallback, useEffect} from 'react';
import {createRoot} from 'react-dom/client';
import {version} from '../package.json';
import {FullDemo} from './full';
Expand Down Expand Up @@ -30,8 +30,35 @@ const DEMOS = {

type DemoComponentKeys = keyof typeof DEMOS;

function getInitialDemo(): DemoComponentKeys {
const urlParams = new URLSearchParams(window.location.search);
const demo = urlParams.get('demo');
return demo && demo in DEMOS ? (demo as DemoComponentKeys) : 'full';
}

function Demo() {
const [demo, setDemo] = useState<DemoComponentKeys>('full');
const [demo, setDemo] = useState<DemoComponentKeys>(getInitialDemo());

const handleDemoChange = useCallback((nextDemo: DemoComponentKeys) => {
setDemo(nextDemo);
history.pushState({demo: nextDemo}, '', `?demo=${nextDemo}`);
}, []);

// handle back/forward navigation
useEffect(() => {
function handlePopState(e: PopStateEvent) {
setDemo(e.state?.demo || 'full');
}
window.addEventListener('popstate', handlePopState);
return () => {
window.removeEventListener('popstate', handlePopState);
};
});

// update document title
useEffect(() => {
document.title = `QRCode.react Demo - ${DEMOS[demo].label}`;
}, [demo]);

const demoData = DEMOS[demo];

Expand All @@ -49,7 +76,9 @@ function Demo() {
<div className="container">
<label>
<select
onChange={(e) => setDemo(e.target.value as DemoComponentKeys)}
onChange={(e) =>
handleDemoChange(e.target.value as DemoComponentKeys)
}
value={demo}>
{Object.keys(DEMOS).map((key) => {
const data = DEMOS[key as DemoComponentKeys];
Expand Down

0 comments on commit 38e030a

Please # to comment.