-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcontainer-text-block.tsx
More file actions
54 lines (49 loc) · 1.95 KB
/
Copy pathcontainer-text-block.tsx
File metadata and controls
54 lines (49 loc) · 1.95 KB
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
import type {TextBlockRenderProps} from '@portabletext/editor'
import {useListIndex} from '@portabletext/plugin-list-index'
import {createElement, type JSX} from 'react'
import {BlockDropIndicator} from './block-drop-indicator'
type StyleConfig = {tag: keyof JSX.IntrinsicElements; className: string}
/**
* The text-block render shared by every container (callout, fact-box, cell).
* Containers render through the `defineTextBlock` pipeline, which emits only
* `data-pt-*` attributes and no drop-indicator chrome, so each container would
* otherwise repeat the same three concerns: re-emit the `data-list-*`
* attributes the playground's counter CSS keys off (with the index from
* `useListIndex`, the one value `node` cannot supply), pick the element for the
* block style, and draw the {@link BlockDropIndicator}. They differ only in
* which styles they allow, so that is the single parameter.
*
* The block element is `relative` so the absolutely-positioned indicator
* aligns to it.
*/
export function ContainerTextBlock(props: {
attributes: TextBlockRenderProps['attributes']
children: TextBlockRenderProps['children']
node: TextBlockRenderProps['node']
path: TextBlockRenderProps['path']
styles: {normal: StyleConfig} & Record<string, StyleConfig>
}): JSX.Element {
const listIndex = useListIndex(props.path)
if (props.node.listItem !== undefined) {
return (
<div
{...props.attributes}
data-list-item={props.node.listItem}
data-level={props.node.level}
data-list-index={listIndex}
className="relative my-1"
>
{props.children}
<BlockDropIndicator path={props.path} />
</div>
)
}
const style =
props.styles[props.node.style ?? 'normal'] ?? props.styles.normal
return createElement(
style.tag,
{...props.attributes, className: `relative ${style.className}`.trim()},
props.children,
<BlockDropIndicator key="drop-indicator" path={props.path} />,
)
}