-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.tsx
40 lines (34 loc) · 1.12 KB
/
index.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
import { useEntityProp } from '@wordpress/core-data';
import { RichText, store as blockEditorStore } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { usePost } from '../../hooks';
interface PostTitleProps
extends Omit<React.ComponentPropsWithoutRef<typeof RichText>, 'value' | 'onChange'> {}
export const PostTitle = ({ tagName: TagName = 'h1', ...rest }: PostTitleProps) => {
const { postId, postType, isEditable } = usePost();
const [rawTitle = '', setTitle, fullTitle] = useEntityProp(
'postType',
postType,
'title',
postId as undefined | string,
);
const titlePlaceholder = useSelect(
// @ts-ignore-next-line - The type definitions for the block-editor store are incomplete.
(select) => select(blockEditorStore).getSettings().titlePlaceholder,
[],
);
if (!isEditable) {
// @ts-ignore-next-line
return <TagName {...rest} dangerouslySetInnerHTML={{ __html: fullTitle?.rendered }} />;
}
return (
<RichText
tagName={TagName}
placeholder={titlePlaceholder}
value={rawTitle}
onChange={(value: string) => setTitle(value)}
allowedFormats={[]}
{...rest}
/>
);
};