Skip to content

Commit cc210e0

Browse files
committed
Expose editor preparation without scheduling
1 parent 83f3ba4 commit cc210e0

20 files changed

Lines changed: 167 additions & 384 deletions

‎docs/app/docs-infra/components/code-highlighter/page.mdx‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -531,15 +531,3 @@ export const metadata =
531531
/** @type {import('@mui/internal-docs-infra/createSitemap/types').NextMetadata} */ ({
532532
keywords: ['syntax highlighting', 'code block', 'demo', 'precompute', 'variants', 'fallback'],
533533
});
534-
535-
## Preparing editors during idle
536-
537-
Set `editActivation="idle"` on an editable code block to initialize its editor before the reader clicks it.
538-
Visible blocks wait until the page has loaded and input has stopped for 1.5 seconds, then initialize during browser idle time.
539-
If idle callbacks are starved, initialization runs after another five seconds without input.
540-
Initialization does not focus the editor.
541-
Hidden and offscreen blocks stay deferred.
542-
543-
Clicking before initialization still loads the editor on demand.
544-
Without `IntersectionObserver`, this mode falls back to interaction activation.
545-
Use `editActivation="interaction"` to keep all editing work deferred until interaction.

‎docs/app/docs-infra/components/code-highlighter/types.md‎

Lines changed: 57 additions & 77 deletions
Large diffs are not rendered by default.

‎docs/app/docs-infra/components/page.mdx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ It supports both static code blocks and interactive demos with component preview
5252
- Types
5353
- CodeHighlighter
5454
- CodeHighlighter Types
55-
- Preparing editors during idle
5655
- Exports:
5756
- CodeHighlighter
5857
- Props: Content, ContentFrame, ContentLoading, children, code, collapseToEmpty, components, contentProps, controlled, defaultVariant, deferParsing, editActivation, enhanceAfter, fallbackCollapsed, fallbackUsesAllVariants, fallbackUsesExtraFiles, fileName, forceClient, globalsCode, highlightAfter, initialDisabled, initialExpanded, initialVariant, language, loadCodeMeta, loadPrecompute, loadSource, loadVariantMeta, name, precompute, slug, sourceEnhancers, sourceParser, sourceTransformers, url, urlPrefix, variant, variantType, variants

‎docs/app/docs-infra/hooks/page.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The `useCode` hook provides programmatic access to code display, editing, and tr
3636
- Editing behavior
3737
- Collapsed and expanded editing
3838
- Editor interaction
39+
- Preparing an editor
3940
- Selected file metadata
4041
- Source actions
4142
- Controlled transforms

‎docs/app/docs-infra/hooks/use-code/page.mdx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ Starting to edit a live demo switches it to the live preview immediately, even b
4545
first change. It stays on the live preview until the demo unmounts; resetting the source
4646
does not switch back to the bundled component.
4747

48+
### Preparing an editor
49+
50+
With `editActivation="interaction"`, call `prepareEditor()` from `useCode` or `useDemo`
51+
to initialize the selected editor and its editing runtime before a click:
52+
53+
```tsx
54+
const code = useCode(props);
55+
return <button onClick={code.prepareEditor}>Prepare editor</button>;
56+
```
57+
58+
Preparation loads asynchronously without moving focus or changing source.
59+
Repeated calls are safe. Calls while editing is disabled do nothing.
60+
The host owns visibility checks, idle timing, and cancellation.
61+
`preloadCodeEditor()` only fetches the editor module; it does not initialize an editor.
62+
4863
### Selected file metadata
4964

5065
The hook exposes details about the selected file so controls do not need to inspect the

‎docs/app/docs-infra/hooks/use-code/types.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ type UseCodeResult<T extends {} = {}> = {
180180
* reader's edits (use `reset` to discard them).
181181
*/
182182
setEditable?: (editable: boolean) => void;
183+
/**
184+
* Initialize the selected editor and editing runtime without moving focus.
185+
* Loading is asynchronous. Does nothing while editing is disabled.
186+
* The host decides when to call this, for example after visible source is idle.
187+
*/
188+
prepareEditor: () => void;
183189
/**
184190
* Replace the source of the currently selected file (or `fileName` when
185191
* provided) in the controlled code. Internal hooks may pass additional

‎docs/app/docs-infra/hooks/use-demo/types.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ type ReturnValue = void;
254254
| hasControlledEdits | `boolean` | Whether the surrounding controller currently owns edited source. |
255255
| editable | `boolean` | Whether edit mode is currently on. `true` by default; starts `false` when the block&#xA;opts in via `initialDisabled` — per demo through the `createDemo` `meta`, or across a&#xA;factory's demos through `createDemoFactory`. While `false` the block renders&#xA;read-only and the live-editing engine is not even warmed; flip it with&#xA;`setEditable`. Independent of `editActivation`, which governs _when_ the engine&#xA;loads once editing is on, and of `disabled`, which turns editing off permanently. |
256256
| setEditable | `((editable: boolean) => void) \| undefined` | Turns edit mode on or off. `undefined` when the block can't be edited at all — no&#xA;`CodeControllerContext` with `setCode` in scope, or editing is hard-`disabled` — so&#xA;a toggle button can render only when this is defined. Toggling off keeps the&#xA;reader's edits (use `reset` to discard them). |
257+
| prepareEditor | `(() => void)` | Initialize the selected editor and editing runtime without moving focus.&#xA;Loading is asynchronous. Does nothing while editing is disabled.&#xA;The host decides when to call this, for example after visible source is idle. |
257258
| setSource | `((source: string, fileName?: string) => void) \| undefined` | Replace the source of the currently selected file (or `fileName` when&#xA;provided) in the controlled code. Internal hooks may pass additional&#xA;arguments (caret position, pre-parsed HAST) that are not part of the&#xA;public contract. |
258259
| reset | `(() => void) \| undefined` | Clears the entire controlled code state back to `undefined`, discarding&#xA;user edits across **all variants and files** owned by the surrounding&#xA;`CodeControllerContext` (not just the currently selected file or&#xA;variant). Only available when a `CodeControllerContext` with `setCode`&#xA;is in scope and editing is not disabled. |
259260
| refresh | `(() => void) \| undefined` | Re-fetches the block's data on the client by re-running the full variant&#xA;loader, then swaps in the fresh result while keeping the current highlighted&#xA;output visible until the new tree lands (stale-while-revalidate). Invalidates&#xA;the pre-parsed HAST cache. `undefined` (or a no-op) for a block with no `url`&#xA;to re-fetch from, or with no `CodeProvider` in scope. |

‎packages/docs-infra/src/CodeHighlighter/CodeHighlighterClient.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ export function CodeHighlighterClient(props: CodeHighlighterClientProps) {
13011301
!!speculativeCode &&
13021302
(Boolean(props.forceClient) ||
13031303
!hasAllVariants(variants, speculativeCode, true) ||
1304-
((editActivation ?? 'eager') === 'eager' && Boolean(controlled?.setCode)));
1304+
((editActivation ?? 'eager') !== 'interaction' && Boolean(controlled?.setCode)));
13051305
useSpeculativeGrammarPreload({ scopes: speculativeGrammarScopes, enabled: willClientHighlight });
13061306

13071307
// ── Fallback hoisting ──

‎packages/docs-infra/src/CodeHighlighter/CodeHighlighterContext.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ export interface CodeHighlighterContextType {
7171
/**
7272
* Echo of the `editActivation` prop on the surrounding `CodeHighlighter` /
7373
* `CodeHighlighterClient`. `useCode` reads it from here and threads it down to
74-
* `Pre`, which handles interaction and visible-idle activation. Configure the
75-
* strategy at the `CodeHighlighter` / demo level.
74+
* `Pre` (which defers the textarea load when `'interaction'`), so the strategy can be configured at
75+
* the `CodeHighlighter` / demo level rather than inside the content subtree.
7676
*/
77-
editActivation?: 'eager' | 'interaction' | 'idle';
77+
editActivation?: 'eager' | 'interaction';
7878
/**
7979
* Callback `useCode` threads down to the editor's `onActivate`, fired once
8080
* when the block first engages for editing. `CodeHighlighterClient` supplies it

‎packages/docs-infra/src/CodeHighlighter/types.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -593,20 +593,15 @@ export interface CodeLoadingProps {
593593
* When the textarea editor loads for an editable block:
594594
* - `'eager'` (default): load it as soon as the block is editable, and let
595595
* `CodeHighlighter` speculatively preload it on first render.
596-
* - `'interaction'`: defer the load until the reader clicks the code or
597-
* presses Enter on its wrapper. Blocks the reader never engages do not
598-
* fetch the editor chunk.
599-
*
600-
* - `'idle'`: initialize visible editors after page load and 1.5 seconds
601-
* without input, during browser idle time. Interaction still activates
602-
* the editor immediately. Hidden and offscreen blocks stay deferred.
603-
* Starved idle callbacks run after another five seconds without input.
596+
* - `'interaction'`: defer the load until the reader hovers, focuses, or
597+
* clicks the code, and suppress the speculative preload — so a block the
598+
* reader never engages does not fetch the editor chunk at all.
604599
*
605600
* Only meaningful for editable blocks (a `CodeControllerContext` exposing
606601
* `setCode`); ignored otherwise.
607602
* @default 'eager'
608603
*/
609-
editActivation?: 'eager' | 'interaction' | 'idle';
604+
editActivation?: 'eager' | 'interaction';
610605
/** Raw code string for simple use cases */
611606
children?: string;
612607
/**

0 commit comments

Comments
 (0)