Skip to content

Commit 55d3e82

Browse files
committed
bench
1 parent 5e6a9fd commit 55d3e82

8 files changed

Lines changed: 76 additions & 43 deletions

File tree

.oxlintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"jsx-no-new-object-as-prop": "off",
3333
"jsx-no-new-array-as-prop": "off",
3434
"jsx-no-jsx-as-prop": "off",
35-
"jsx-no-new-function-as-prop": "off",
35+
"jsx-no-new-function-as-prop": "off"
3636
}
3737
}
3838
]

demo/components/SpeechSynthesis.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type {PortableTextMarkComponent} from '@portabletext/react'
22

3-
43
interface SpeechSynthesisMark {
54
_type: 'speech'
65
pitch?: number
@@ -15,14 +14,16 @@ export const SpeechSynthesis: PortableTextMarkComponent<SpeechSynthesisMark> = (
1514
}) => {
1615
const pitch = value?.pitch || 1
1716

18-
1917
return (
20-
<button type="button" onClick={() => {
21-
const msg = new SpeechSynthesisUtterance()
22-
msg.text = text
23-
msg.pitch = pitch
24-
window.speechSynthesis.speak(msg)
25-
}}>
18+
<button
19+
type="button"
20+
onClick={() => {
21+
const msg = new SpeechSynthesisUtterance()
22+
msg.text = text
23+
msg.pitch = pitch
24+
window.speechSynthesis.speak(msg)
25+
}}
26+
>
2627
{children}
2728
</button>
2829
)

demo/components/TermDefinition.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export const TermDefinition: PortableTextMarkComponent<DefinitionMark> = ({value
2323
</Text>
2424
}
2525
>
26-
<span style={{textDecoration: 'underline'}} onMouseOver={() => setOpen(true)} onMouseOut={() => setOpen(false)}>
26+
<span
27+
style={{textDecoration: 'underline'}}
28+
onMouseOver={() => setOpen(true)}
29+
onMouseOut={() => setOpen(false)}
30+
>
2731
{children}
2832
</span>
2933
</Popover>

demo/demo.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {type PortableTextComponents, PortableText} from '@portabletext/react'
22
import {studioTheme, ThemeProvider} from '@sanity/ui'
3-
import {StrictMode} from 'react'
3+
import {StrictMode, useEffect, useState} from 'react'
44
import {createRoot} from 'react-dom/client'
55

66
import {AnnotatedMap} from './components/AnnotatedMap'
@@ -49,7 +49,15 @@ const ptComponents: PortableTextComponents = {
4949
}
5050

5151
function Demo() {
52-
return <PortableText value={blocks} components={ptComponents} />
52+
const [count, setCount] = useState(0)
53+
const showingEverything = count >= blocks.length
54+
const blocksToShow = showingEverything ? blocks : blocks.slice(0, count)
55+
useEffect(() => {
56+
if (!showingEverything) {
57+
setCount(count + 1)
58+
}
59+
}, [showingEverything, count])
60+
return <PortableText value={blocksToShow} components={ptComponents} />
5361
}
5462

5563
const root = createRoot(document.getElementById('demo-root')!)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"refractor": "^5.0.0",
7878
"rimraf": "^5.0.1",
7979
"rollup-plugin-visualizer": "^6.0.5",
80+
"@portabletext/react-latest": "npm:@portabletext/react@latest",
8081
"styled-components": "npm:@sanity/css-in-js@6.1.26",
8182
"tsdown": "^0.17.2",
8283
"typescript": "5.9.3",

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/defaults.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,29 @@ import {
1313
DefaultUnknownType,
1414
} from './unknown'
1515

16-
export const DefaultHardBreak = (): JSX.Element => <br />
16+
const DefaultHardBreak = (): JSX.Element => <br />
1717

18-
export const defaultBlockStyles: Record<
18+
const DefaultParagraph: PortableTextBlockComponent = ({children}) => <p>{children}</p>
19+
const DefaultBlockquote: PortableTextBlockComponent = ({children}) => <blockquote>{children}</blockquote>
20+
const DefaultH1: PortableTextBlockComponent = ({children}) => <h1>{children}</h1>
21+
const DefaultH2: PortableTextBlockComponent = ({children}) => <h2>{children}</h2>
22+
const DefaultH3: PortableTextBlockComponent = ({children}) => <h3>{children}</h3>
23+
const DefaultH4: PortableTextBlockComponent = ({children}) => <h4>{children}</h4>
24+
const DefaultH5: PortableTextBlockComponent = ({children}) => <h5>{children}</h5>
25+
const DefaultH6: PortableTextBlockComponent = ({children}) => <h6>{children}</h6>
26+
27+
const defaultBlockStyles: Record<
1928
PortableTextBlockStyle,
2029
PortableTextBlockComponent | undefined
2130
> = {
22-
normal: ({children}) => <p>{children}</p>,
23-
blockquote: ({children}) => <blockquote>{children}</blockquote>,
24-
h1: ({children}) => <h1>{children}</h1>,
25-
h2: ({children}) => <h2>{children}</h2>,
26-
h3: ({children}) => <h3>{children}</h3>,
27-
h4: ({children}) => <h4>{children}</h4>,
28-
h5: ({children}) => <h5>{children}</h5>,
29-
h6: ({children}) => <h6>{children}</h6>,
31+
normal: DefaultParagraph,
32+
blockquote: DefaultBlockquote,
33+
h1: DefaultH1,
34+
h2: DefaultH2,
35+
h3: DefaultH3,
36+
h4: DefaultH4,
37+
h5: DefaultH5,
38+
h6: DefaultH6,
3039
}
3140

3241
export const defaultComponents: PortableTextReactComponents = {

src/react-portable-text.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ export function PortableText<B extends TypedObject = PortableTextBlock>({
4242
onMissingComponent: missingComponentHandler = printWarning,
4343
}: PortableTextProps<B>): ReactNode {
4444
const handleMissingComponent = missingComponentHandler || noop
45-
const blocks = useMemo(() => Array.isArray(input) ? input : [input], [input])
46-
const nested = useMemo(() => nestLists(blocks, listNestingMode || LIST_NEST_MODE_HTML), [blocks, listNestingMode])
45+
const blocks = useMemo(() => (Array.isArray(input) ? input : [input]), [input])
46+
const nested = useMemo(
47+
() => nestLists(blocks, listNestingMode || LIST_NEST_MODE_HTML),
48+
[blocks, listNestingMode],
49+
)
4750

4851
const components = useMemo(() => {
4952
return componentOverrides
@@ -300,15 +303,10 @@ function RenderCustomBlock({
300303
index: number
301304
isInline: boolean
302305
}) {
303-
const nodeOptions = {
304-
value: node,
305-
isInline,
306-
index,
307-
renderNode,
308-
}
309-
310306
const Node = components.types[node._type]
311-
return Node ? <Node {...nodeOptions} /> : null
307+
return Node ? (
308+
<Node value={node} isInline={isInline} index={index} renderNode={renderNode} />
309+
) : null
312310
}
313311

314312
function RenderBlock({
@@ -326,13 +324,13 @@ function RenderBlock({
326324
index: number
327325
isInline: boolean
328326
}) {
329-
const {_key, ...props} = serializeBlock({
327+
const block = serializeBlock({
330328
node,
331329
index,
332330
isInline,
333331
renderNode,
334332
})
335-
const style = props.node.style || 'normal'
333+
const style = block.node.style || 'normal'
336334
const handler =
337335
typeof components.block === 'function' ? components.block : components.block[style]
338336
const Block = handler || components.unknownBlockStyle
@@ -344,7 +342,11 @@ function RenderBlock({
344342
})
345343
}
346344

347-
return <Block {...props} value={props.node} renderNode={renderNode} />
345+
return (
346+
<Block index={block.index} isInline={block.isInline} value={block.node} renderNode={renderNode}>
347+
{block.children}
348+
</Block>
349+
)
348350
}
349351

350352
function RenderText({
@@ -377,20 +379,13 @@ function RenderUnknownType({
377379
index: number
378380
isInline: boolean
379381
}) {
380-
const nodeOptions = {
381-
value: node,
382-
isInline,
383-
index,
384-
renderNode,
385-
}
386-
387382
handleMissingComponent(unknownTypeWarning(node._type), {
388383
nodeType: 'block',
389384
type: node._type,
390385
})
391386

392387
const UnknownType = components.unknownType
393-
return <UnknownType {...nodeOptions} />
388+
return <UnknownType value={node} isInline={isInline} index={index} renderNode={renderNode} />
394389
}
395390

396391
function serializeBlock(options: Serializable<PortableTextBlock>): SerializedBlock {

0 commit comments

Comments
 (0)