-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdemo.tsx
63 lines (56 loc) · 2.1 KB
/
demo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {studioTheme, ThemeProvider} from '@sanity/ui'
import {StrictMode} from 'react'
import {createRoot} from 'react-dom/client'
import {PortableTextComponents} from '../src'
import {PortableText} from '../src/react-portable-text'
import {AnnotatedMap} from './components/AnnotatedMap'
import {CharacterReference} from './components/CharacterReference'
import {Code} from './components/Code'
import {CurrencyAmount} from './components/CurrencyAmount'
import {Link} from './components/Link'
import {LinkableHeader} from './components/LinkableHeader'
import {SchnauzerList} from './components/SchnauzerList'
import {hasSpeechApi, SpeechSynthesis} from './components/SpeechSynthesis'
import {TermDefinition} from './components/TermDefinition'
import {blocks} from './fixture'
/**
* Note that these are statically defined (outside the scope of a function),
* which ensures that unnecessary rerenders does not happen because of a new
* components object being generated on every render. The alternative is to
* `useMemo()`, but if you can get away with this approach it is _better_.
**/
const ptComponents: PortableTextComponents = {
// Components for totally custom types outside the scope of Portable Text
types: {
code: Code,
currencyAmount: CurrencyAmount,
annotatedMap: AnnotatedMap,
},
// Overrides for specific block styles - in this case just the `h2` style
block: {
h2: LinkableHeader,
},
// Implements a custom component to handle the `schnauzer` list item type
list: {
schnauzer: SchnauzerList,
},
// Custom components for marks - note that `link` overrides the default component,
// while the others define components for totally custom types.
marks: {
link: Link,
characterReference: CharacterReference,
speech: hasSpeechApi ? SpeechSynthesis : undefined,
definition: TermDefinition,
},
}
function Demo() {
return <PortableText value={blocks} components={ptComponents} />
}
const root = createRoot(document.getElementById('demo-root')!)
root.render(
<StrictMode>
<ThemeProvider theme={studioTheme}>
<Demo />
</ThemeProvider>
</StrictMode>,
)