Skip to content

Commit 182eaa3

Browse files
authored
feat(richtext-lexical): ability to override default placeholder (#10278)
![CleanShot 2024-12-30 at 23 28 01@2x](https://github.com/user-attachments/assets/0bab3a2a-bded-440f-9bfe-cb8bf5b74e2a) ![CleanShot 2024-12-30 at 23 28 08@2x](https://github.com/user-attachments/assets/589b7be0-f662-4c4a-b66b-66f9b88ee7c6)
1 parent 943798a commit 182eaa3

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

docs/fields/rich-text.mdx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ _* An asterisk denotes that a property is required._
4545

4646
## Admin Options
4747

48-
The customize the appearance and behavior of the Rich Text Field in the [Admin Panel](../admin/overview), you can use the `admin` option:
48+
The customize the appearance and behavior of the Rich Text Field in the [Admin Panel](../admin/overview), you can use the `admin` option. The Rich Text Field inherits all of the default options from the base [Field Admin Config](../admin/fields#admin-options)
4949

5050
```ts
5151
import type { Field } from 'payload'
@@ -58,13 +58,7 @@ export const MyRichTextField: Field = {
5858
}
5959
```
6060

61-
The Rich Text Field inherits all of the default options from the base [Field Admin Config](../admin/fields#admin-options), plus the following additional options:
62-
63-
| Property | Description |
64-
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
65-
| **`placeholder`** | Set this property to define a placeholder string for the field. |
66-
| **`hideGutter`** | Set this property to `true` to hide this field's gutter within the Admin Panel. |
67-
| **`rtl`** | Override the default text direction of the Admin Panel for this field. Set to `true` to force right-to-left text direction. |
61+
Further customization can be done with editor-specific options.
6862

6963
## Editor-specific Options
7064

docs/rich-text/overview.mdx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ keywords: lexical, rich text, editor, headless cms
88

99
<Banner type="warning">
1010

11-
The Payload editor is based on Lexical, Meta's rich text editor. The previous default editor was
12-
based on Slate and is still supported. You can read [its documentation](/docs/rich-text/slate),
11+
The Payload editor is based on Lexical, Meta's rich text editor. The previous default editor was
12+
based on Slate and is still supported. You can read [its documentation](/docs/rich-text/slate),
1313
or the optional [migration guide](/docs/rich-text/migration) to migrate from Slate to Lexical (recommended).
1414

1515
</Banner>
@@ -298,3 +298,43 @@ Make sure to only use types exported from `@payloadcms/richtext-lexical`, not fr
298298
### Automatic type generation
299299

300300
Lexical does not generate the accurate type definitions for your richText fields for you yet - this will be improved in the future. Currently, it only outputs the rough shape of the editor JSON which you can enhance using type assertions.
301+
302+
## Admin customization
303+
304+
The Rich Text Field editor configuration has an `admin` property with the following options:
305+
306+
| Property | Description |
307+
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
308+
| **`placeholder`** | Set this property to define a placeholder string for the field. |
309+
| **`hideGutter`** | Set this property to `true` to hide this field's gutter within the Admin Panel. |
310+
311+
### Disable the gutter
312+
313+
You can disable the gutter (the vertical line padding between the editor and the left edge of the screen) by setting the `hideGutter` prop to `true`:
314+
315+
```ts
316+
{
317+
name: 'richText',
318+
type: 'richText',
319+
editor: lexicalEditor({
320+
admin: {
321+
hideGutter: true
322+
},
323+
}),
324+
}
325+
```
326+
327+
### Customize the placeholder
328+
329+
You can customize the placeholder (the text that appears in the editor when it's empty) by setting the `placeholder` prop:
330+
331+
```ts
332+
{
333+
name: 'richText',
334+
type: 'richText',
335+
editor: lexicalEditor({
336+
admin: {
337+
placeholder: 'Type your content here...'
338+
},
339+
}),
340+
}

packages/richtext-lexical/src/lexical/LexicalEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const LexicalEditor: React.FC<
115115
contentEditable={
116116
<div className="editor-scroller">
117117
<div className="editor" ref={onRef} tabIndex={-1}>
118-
<LexicalContentEditable />
118+
<LexicalContentEditable editorConfig={editorConfig} />
119119
</div>
120120
</div>
121121
}

packages/richtext-lexical/src/lexical/ui/ContentEditable.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ import { useTranslation } from '@payloadcms/ui'
66
import * as React from 'react'
77

88
import './ContentEditable.scss'
9+
import type { SanitizedClientEditorConfig } from '../config/types.js'
910

10-
export function LexicalContentEditable({ className }: { className?: string }): JSX.Element {
11+
export function LexicalContentEditable({
12+
className,
13+
editorConfig,
14+
}: {
15+
className?: string
16+
editorConfig: SanitizedClientEditorConfig
17+
}): JSX.Element {
1118
const { t } = useTranslation<{}, string>()
1219

1320
return (
1421
<ContentEditable
1522
aria-placeholder={t('lexical:general:placeholder')}
1623
className={className ?? 'ContentEditable__root'}
17-
placeholder={<p className="editor-placeholder">{t('lexical:general:placeholder')}</p>}
24+
placeholder={
25+
<p className="editor-placeholder">
26+
{editorConfig?.admin?.placeholder ?? t('lexical:general:placeholder')}
27+
</p>
28+
}
1829
/>
1930
)
2031
}

packages/richtext-lexical/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export type LexicalFieldAdminProps = {
2222
* Controls if the gutter (padding to the left & gray vertical line) should be hidden. @default false
2323
*/
2424
hideGutter?: boolean
25+
/**
26+
* Changes the placeholder text in the editor if no content is present.
27+
*/
28+
placeholder?: string
2529
}
2630

2731
export type LexicalEditorProps = {

0 commit comments

Comments
 (0)