|
| 1 | +Static text is boring. In a modern React application, your content should be alive. |
| 2 | + |
| 3 | +Today I want to share a fun pattern I implemented in **Fezcodex**: triggering dynamic UI interactions directly from standard Markdown links. Specifically, clicking a link in a blog post to open a side panel with a live React component, rather than navigating to a new page. |
| 4 | + |
| 5 | +## The Idea |
| 6 | + |
| 7 | +I wanted to explain technical terms like **[Prop Drilling](/#/vocab/prop-drilling)** without forcing the reader to leave the article. A tooltip is too small; a new tab is too distracting. The solution? My global **Side Panel**. |
| 8 | + |
| 9 | +But how do you tell a static Markdown file to "render a React component in the side panel"? |
| 10 | + |
| 11 | +## The Solution |
| 12 | + |
| 13 | +The secret sauce lies in `react-markdown`'s ability to customize how HTML elements are rendered. We can intercept every `<a>` tag and check if it's a "special" link. |
| 14 | + |
| 15 | +### 1. The Interceptor (`MarkdownLink`) |
| 16 | + |
| 17 | +I created a custom component that replaces standard HTML anchors. It checks the `href` for a specific pattern (in my case, `/vocab/`). |
| 18 | + |
| 19 | +```javascript |
| 20 | +const MarkdownLink = ({ href, children }) => { |
| 21 | + const { openSidePanel } = useSidePanel(); |
| 22 | + |
| 23 | + // Check if this is a "vocabulary" link |
| 24 | + const isVocab = href && href.includes('/vocab/'); |
| 25 | + |
| 26 | + if (isVocab) { |
| 27 | + // 1. Extract the term ID (e.g., "prop-drilling") |
| 28 | + const term = href.split('/vocab/')[1]; |
| 29 | + |
| 30 | + // 2. Look up the definition/component |
| 31 | + const definition = vocabulary[term]; |
| 32 | + |
| 33 | + return ( |
| 34 | + <a |
| 35 | + href={href} |
| 36 | + onClick={(e) => { |
| 37 | + e.preventDefault(); // Stop navigation! |
| 38 | + if (definition) { |
| 39 | + // 3. Trigger the global UI |
| 40 | + openSidePanel(definition.title, definition.content); |
| 41 | + } |
| 42 | + }} |
| 43 | + className="text-amber-400 dashed-underline cursor-help" |
| 44 | + > |
| 45 | + {children} |
| 46 | + </a> |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + // Fallback for normal links |
| 51 | + return <a href={href}>{children}</a>; |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. The Data (`vocabulary.js`) |
| 56 | + |
| 57 | +I store the actual content in a simple lookup object. The beauty is that `content` can be *anything*--text, images, or fully interactive React components. |
| 58 | + |
| 59 | +```javascript |
| 60 | +export const vocabulary = { |
| 61 | + 'prop-drilling': { |
| 62 | + title: 'Prop Drilling', |
| 63 | + content: <PropDrillingDiagram /> // A real component! |
| 64 | + }, |
| 65 | + // ... |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +### 3. Handling "Deep Links" |
| 70 | + |
| 71 | +What if someone actually copies the URL `https://fezcodex.com/#/vocab/prop-drilling` and sends it to a friend? The `onClick` handler won't fire because they aren't clicking a link—they are loading the app. |
| 72 | + |
| 73 | +To handle this, I added a "phantom" route in my Router: |
| 74 | + |
| 75 | +```javascript |
| 76 | +// VocabRouteHandler.js |
| 77 | +const VocabRouteHandler = () => { |
| 78 | + const { term } = useParams(); |
| 79 | + const navigate = useNavigate(); |
| 80 | + const { openSidePanel } = useSidePanel(); |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + // 1. Open the panel immediately |
| 84 | + if (vocabulary[term]) { |
| 85 | + openSidePanel(vocabulary[term].title, vocabulary[term].content); |
| 86 | + } |
| 87 | + // 2. Redirect to home (so the background isn't blank) |
| 88 | + navigate('/', { replace: true }); |
| 89 | + }, [term]); |
| 90 | + |
| 91 | + return null; |
| 92 | +}; |
| 93 | +``` |
| 94 | + |
| 95 | +## Why this rocks |
| 96 | + |
| 97 | +This pattern effectively turns your static Markdown content into a control surface for your application. You can write: |
| 98 | + |
| 99 | +> "Check out this `[interactive demo](/#/demos/sorting-algo)`..." |
| 100 | + |
| 101 | +And have it launch a full-screen visualization, a game, or a configuration wizard, all without leaving the flow of your writing. It bridges the gap between "content" and "app". |
0 commit comments