|
| 1 | +import React, { useState, ErrorInfo } from 'react' |
| 2 | +import Link from 'next/link' |
| 3 | +import { Icon } from '../examples/ts/components/index' |
| 4 | +import { NON_HIDDEN_EXAMPLES } from '../constants/examples' |
| 5 | + |
| 6 | +const Header = (props: React.HTMLAttributes<HTMLDivElement>) => ( |
| 7 | + <div {...props} className="example-header" /> |
| 8 | +) |
| 9 | + |
| 10 | +const Title = (props: React.HTMLAttributes<HTMLSpanElement>) => ( |
| 11 | + <span {...props} className="example-title" /> |
| 12 | +) |
| 13 | + |
| 14 | +const LinkList = (props: React.HTMLAttributes<HTMLDivElement>) => ( |
| 15 | + <div {...props} className="example-link-list" /> |
| 16 | +) |
| 17 | + |
| 18 | +const A = (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => ( |
| 19 | + <a {...props} className="example-link" /> |
| 20 | +) |
| 21 | + |
| 22 | +const Pill = (props: React.HTMLAttributes<HTMLSpanElement>) => ( |
| 23 | + <span {...props} className="example-pill" /> |
| 24 | +) |
| 25 | + |
| 26 | +const TabList = ({ |
| 27 | + isVisible, |
| 28 | + ...props |
| 29 | +}: React.HTMLAttributes<HTMLDivElement> & { isVisible?: boolean }) => ( |
| 30 | + <div |
| 31 | + role="menu" |
| 32 | + aria-label="Examples navigation" |
| 33 | + aria-hidden={!isVisible} |
| 34 | + {...props} |
| 35 | + className={`example-tab-list ${isVisible ? 'visible' : 'hidden'}`} |
| 36 | + /> |
| 37 | +) |
| 38 | + |
| 39 | +const TabListUnderlay = ({ |
| 40 | + isVisible, |
| 41 | + ...props |
| 42 | +}: React.HTMLAttributes<HTMLDivElement> & { isVisible?: boolean }) => ( |
| 43 | + <div |
| 44 | + {...props} |
| 45 | + className={`example-tab-list-underlay ${isVisible ? 'visible' : 'hidden'}`} |
| 46 | + /> |
| 47 | +) |
| 48 | + |
| 49 | +const TabButton = (props: React.HTMLAttributes<HTMLSpanElement>) => ( |
| 50 | + <button |
| 51 | + {...props} |
| 52 | + aria-label="Toggle examples menu" |
| 53 | + aria-haspopup="menu" |
| 54 | + className="example-tab-button" |
| 55 | + /> |
| 56 | +) |
| 57 | + |
| 58 | +const Tab = React.forwardRef( |
| 59 | + ( |
| 60 | + { |
| 61 | + active, |
| 62 | + href, |
| 63 | + ...props |
| 64 | + }: React.AnchorHTMLAttributes<HTMLAnchorElement> & { |
| 65 | + active: boolean |
| 66 | + }, |
| 67 | + ref: React.Ref<HTMLAnchorElement> |
| 68 | + ) => ( |
| 69 | + <a |
| 70 | + ref={ref} |
| 71 | + href={href} |
| 72 | + role="menuitem" |
| 73 | + aria-current={active ? 'page' : undefined} |
| 74 | + {...props} |
| 75 | + className={`example-tab ${active ? 'active' : ''}`} |
| 76 | + /> |
| 77 | + ) |
| 78 | +) |
| 79 | + |
| 80 | +const ExampleHeader = (props: React.HTMLAttributes<HTMLDivElement>) => ( |
| 81 | + <div {...props} className="example-page-header" /> |
| 82 | +) |
| 83 | + |
| 84 | +const ExampleTitle = (props: React.HTMLAttributes<HTMLSpanElement>) => ( |
| 85 | + <span {...props} className="example-page-title" /> |
| 86 | +) |
| 87 | + |
| 88 | +const ExampleContent = (props: React.HTMLAttributes<HTMLDivElement>) => ( |
| 89 | + <div {...props} className="example-content" /> |
| 90 | +) |
| 91 | + |
| 92 | +export const Warning = (props: React.HTMLAttributes<HTMLDivElement>) => ( |
| 93 | + <div {...props} className="example-warning" /> |
| 94 | +) |
| 95 | + |
| 96 | +interface ExampleLayoutProps { |
| 97 | + children: React.ReactNode |
| 98 | + exampleName?: string |
| 99 | + examplePath?: string |
| 100 | + error?: Error | null |
| 101 | + stackTrace?: ErrorInfo | null |
| 102 | +} |
| 103 | + |
| 104 | +export function ExampleLayout({ |
| 105 | + children, |
| 106 | + exampleName, |
| 107 | + examplePath, |
| 108 | + error, |
| 109 | + stackTrace, |
| 110 | +}: ExampleLayoutProps) { |
| 111 | + const [showTabs, setShowTabs] = useState<boolean>(false) |
| 112 | + |
| 113 | + return ( |
| 114 | + <div> |
| 115 | + <Header> |
| 116 | + <Title>Slate Examples</Title> |
| 117 | + <LinkList> |
| 118 | + <A href="https://github.com/ianstormtaylor/slate">GitHub</A> |
| 119 | + <A href="https://docs.slatejs.org/">Docs</A> |
| 120 | + </LinkList> |
| 121 | + </Header> |
| 122 | + |
| 123 | + {exampleName && examplePath && ( |
| 124 | + <ExampleHeader> |
| 125 | + <TabButton |
| 126 | + onClick={e => { |
| 127 | + e.stopPropagation() |
| 128 | + setShowTabs(!showTabs) |
| 129 | + }} |
| 130 | + onKeyDown={(e: React.KeyboardEvent) => { |
| 131 | + if (e.key === 'Escape') { |
| 132 | + setShowTabs(false) |
| 133 | + } |
| 134 | + }} |
| 135 | + aria-expanded={showTabs} |
| 136 | + > |
| 137 | + <Icon>menu</Icon> |
| 138 | + </TabButton> |
| 139 | + <ExampleTitle> |
| 140 | + {exampleName} |
| 141 | + <A |
| 142 | + href={`https://github.com/ianstormtaylor/slate/blob/main/site/examples/js/${examplePath}.jsx`} |
| 143 | + > |
| 144 | + <Pill>JS Code</Pill> |
| 145 | + </A> |
| 146 | + <A |
| 147 | + href={`https://github.com/ianstormtaylor/slate/blob/main/site/examples/ts/${examplePath}.tsx`} |
| 148 | + > |
| 149 | + <Pill>TS Code</Pill> |
| 150 | + </A> |
| 151 | + </ExampleTitle> |
| 152 | + </ExampleHeader> |
| 153 | + )} |
| 154 | + |
| 155 | + <TabList isVisible={showTabs}> |
| 156 | + {NON_HIDDEN_EXAMPLES.map(([n, p]) => ( |
| 157 | + <Link |
| 158 | + key={p as string} |
| 159 | + href="/examples/[example]" |
| 160 | + as={`/examples/${p}`} |
| 161 | + legacyBehavior |
| 162 | + passHref |
| 163 | + > |
| 164 | + <Tab |
| 165 | + onClick={() => setShowTabs(false)} |
| 166 | + active={p === examplePath} |
| 167 | + onKeyDown={(e: React.KeyboardEvent) => { |
| 168 | + if (e.key === 'Escape') { |
| 169 | + setShowTabs(false) |
| 170 | + } |
| 171 | + }} |
| 172 | + > |
| 173 | + {n} |
| 174 | + </Tab> |
| 175 | + </Link> |
| 176 | + ))} |
| 177 | + </TabList> |
| 178 | + |
| 179 | + {error && stackTrace ? ( |
| 180 | + <Warning> |
| 181 | + <p>An error was thrown by one of the example's React components!</p> |
| 182 | + <pre> |
| 183 | + <code> |
| 184 | + {error.stack} |
| 185 | + {'\n'} |
| 186 | + {stackTrace.componentStack} |
| 187 | + </code> |
| 188 | + </pre> |
| 189 | + </Warning> |
| 190 | + ) : ( |
| 191 | + <ExampleContent>{children}</ExampleContent> |
| 192 | + )} |
| 193 | + |
| 194 | + <TabListUnderlay |
| 195 | + isVisible={showTabs} |
| 196 | + onClick={() => setShowTabs(false)} |
| 197 | + onKeyDown={(e: React.KeyboardEvent) => { |
| 198 | + if (e.key === 'Escape') { |
| 199 | + setShowTabs(false) |
| 200 | + } |
| 201 | + }} |
| 202 | + /> |
| 203 | + </div> |
| 204 | + ) |
| 205 | +} |
0 commit comments