diff --git a/examples/demo.tsx b/examples/demo.tsx index ed855ee..83b71be 100644 --- a/examples/demo.tsx +++ b/examples/demo.tsx @@ -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'; @@ -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('full'); + const [demo, setDemo] = useState(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]; @@ -49,7 +76,9 @@ function Demo() {