|
1 | 1 | --- |
2 | 2 | title: "EditorFocusScope" |
3 | 3 | sidebarTitle: "EditorFocusScope" |
4 | | -description: "Register portaled UI as part of the editor's focus scope." |
5 | | -icon: "crosshair" |
| 4 | +description: "Track editor focus across portaled UI with EditorFocusScope and EditorFocusScopeProvider." |
| 5 | +icon: "crosshairs" |
6 | 6 | --- |
7 | 7 |
|
8 | 8 | Everything is accessed through a single import: |
9 | 9 |
|
10 | 10 | ```tsx |
11 | | -import { EditorFocusScope } from '@react-email/editor/ui'; |
| 11 | +import { |
| 12 | + EditorFocusScope, |
| 13 | + EditorFocusScopeProvider, |
| 14 | +} from '@react-email/editor/ui'; |
12 | 15 | ``` |
13 | 16 |
|
| 17 | +Use `EditorFocusScopeProvider` to keep the editor's focus and blur behavior in |
| 18 | +sync across registered surfaces, then wrap each portaled surface that should |
| 19 | +still count as being inside the editor UI with `EditorFocusScope`. |
| 20 | + |
| 21 | +<Tip> |
| 22 | + `Inspector.Root` already renders `EditorFocusScopeProvider` and wraps itself |
| 23 | + in `EditorFocusScope` by default, so the Inspector handles the editor's focus |
| 24 | + idiomatically and reliably. Inside a custom inspector, you usually only add |
| 25 | + `EditorFocusScope` around extra portaled content. |
| 26 | +</Tip> |
| 27 | + |
| 28 | +<h2 id="editor-focus-scope-provider">EditorFocusScopeProvider</h2> |
| 29 | + |
| 30 | +Wrap your editor UI with `EditorFocusScopeProvider` when you need focus-aware |
| 31 | +portals outside the built-in Inspector. The provider must be rendered inside |
| 32 | +the current editor context so it can access the active editor instance. |
| 33 | + |
| 34 | +### Example |
| 35 | + |
| 36 | +```tsx |
| 37 | +import { |
| 38 | + EditorFocusScope, |
| 39 | + EditorFocusScopeProvider, |
| 40 | +} from '@react-email/editor/ui'; |
| 41 | +import { EditorContent, EditorContext, useEditor } from '@tiptap/react'; |
| 42 | +import * as Popover from '@radix-ui/react-popover'; |
| 43 | + |
| 44 | +export function MyEditor() { |
| 45 | + const editor = useEditor({ |
| 46 | + extensions, |
| 47 | + content, |
| 48 | + }); |
| 49 | + |
| 50 | + return ( |
| 51 | + <EditorContext.Provider value={{ editor }}> |
| 52 | + <EditorFocusScopeProvider clearSelectionOnBlur={false}> |
| 53 | + <EditorContent editor={editor} /> |
| 54 | + |
| 55 | + <Popover.Root> |
| 56 | + <Popover.Trigger type="button">Theme presets</Popover.Trigger> |
| 57 | + |
| 58 | + <Popover.Portal> |
| 59 | + <EditorFocusScope> |
| 60 | + <Popover.Content sideOffset={8}> |
| 61 | + <button type="button">Apply preset</button> |
| 62 | + </Popover.Content> |
| 63 | + </EditorFocusScope> |
| 64 | + </Popover.Portal> |
| 65 | + </Popover.Root> |
| 66 | + </EditorFocusScopeProvider> |
| 67 | + </EditorContext.Provider> |
| 68 | + ); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Use `EditorFocusScope` for each portaled surface that should still count as |
| 73 | +being inside the editor UI. |
| 74 | + |
| 75 | +### Props |
| 76 | + |
| 77 | +<ResponseField name="children" type="ReactNode"> |
| 78 | + The editor UI subtree that should share focus tracking. |
| 79 | +</ResponseField> |
| 80 | + |
| 81 | +<ResponseField name="clearSelectionOnBlur" type="boolean" default="true"> |
| 82 | + When focus leaves every registered scope, clear the current selection as part |
| 83 | + of blur handling. Set this to `false` if you want to preserve the selection |
| 84 | + when the editor truly loses focus. |
| 85 | +</ResponseField> |
| 86 | + |
| 87 | +## EditorFocusScope |
| 88 | + |
14 | 89 | Use `EditorFocusScope` around portaled UI like Radix `Select.Content`, |
15 | 90 | `Popover.Content`, or dialog content so moving focus into that portal is still |
16 | 91 | treated as staying inside the editor UI. |
17 | 92 |
|
18 | 93 | `EditorFocusScope` must be used inside |
19 | | -[EditorFocusScopeProvider](/editor/api-reference/ui/editor-focus-scope-provider), |
| 94 | +[`EditorFocusScopeProvider`](/editor/api-reference/ui/editor-focus-scope#editor-focus-scope-provider), |
20 | 95 | which tracks registered focus scopes and updates the editor's focus state for |
21 | 96 | them. |
22 | 97 |
|
23 | | -<Tip> |
24 | | - `Inspector.Root` already renders |
25 | | - [EditorFocusScopeProvider](/editor/api-reference/ui/editor-focus-scope-provider) |
26 | | - and wraps itself in `EditorFocusScope` by default, so the Inspector handles |
27 | | - the editor's focus idiomatically and reliably. Inside a custom inspector, you |
28 | | - usually only add `EditorFocusScope` around extra portaled content. |
29 | | -</Tip> |
30 | | - |
31 | | -## Example |
| 98 | +### Example |
32 | 99 |
|
33 | 100 | ```tsx |
34 | 101 | import { EditorFocusScope, Inspector } from '@react-email/editor/ui'; |
@@ -68,10 +135,10 @@ import * as Select from '@radix-ui/react-select'; |
68 | 135 |
|
69 | 136 | If you are building a focus-aware editor shell outside `Inspector.Root`, wrap |
70 | 137 | the editor with |
71 | | -[EditorFocusScopeProvider](/editor/api-reference/ui/editor-focus-scope-provider) |
| 138 | +[`EditorFocusScopeProvider`](/editor/api-reference/ui/editor-focus-scope#editor-focus-scope-provider) |
72 | 139 | and then use `EditorFocusScope` for each portaled surface. |
73 | 140 |
|
74 | | -## Props |
| 141 | +### Props |
75 | 142 |
|
76 | 143 | <ResponseField name="children" type="ReactNode"> |
77 | 144 | The element or subtree to register as an additional editor focus scope. Wrap |
|
0 commit comments