Skip to content

Replace internalContent prop sync effects with $derived #693

Closed

Description

@malibio

Overview

Replace $effect blocks that sync internalContent from content prop with $derived pattern.

Problem Statement

Four node components use the anti-pattern of syncing a prop to internal state via $effect:

let internalContent = $state(content);
$effect(() => {
  internalContent = content;
});

This is unnecessary state duplication. The internalContent can simply be derived from content.

Files to Modify

  1. calendar.svelte (line 39) - sync external value to internal
  2. code-block-node.svelte (line 76) - sync internalContent
  3. ordered-list-node.svelte (line 54) - sync internalContent
  4. quote-block-node.svelte (line 55) - sync internalContent

Proposed Solution

Replace with $derived:

// BEFORE
let internalContent = $state(content);
$effect(() => {
  internalContent = content;
});

// AFTER
const internalContent = $derived(content);

Or if mutation is needed, use bind:content in parent and remove internal state entirely.

Acceptance Criteria

  • calendar.svelte $effect replaced with $derived
  • code-block-node.svelte $effect replaced with $derived
  • ordered-list-node.svelte $effect replaced with $derived
  • quote-block-node.svelte $effect replaced with $derived
  • 4 $effect blocks eliminated
  • All node types render correctly
  • Edit mode works correctly
  • Tests pass (bun run test:all)
  • Quality checks pass (bun run quality:fix)

Related Issues

Activity

  1. self-assigned this
    on Nov 30, 2025
  2. malibio commented on Nov 30, 2025

    @malibio
    CollaboratorAuthor

    Test baseline: Frontend 1469 passed (74 files), Rust 531 passed (16 ignored)

  3. added a commit that references this issue on Nov 30, 2025
    62c4440
  4. malibio commented on Nov 30, 2025

    @malibio
    CollaboratorAuthor

    Implementation Notes (from PR #696 review)

    The actual implementation differs from the original proposal, for valid technical reasons:

    Original Proposal:

    const internalContent = $derived(content);

    Actual Implementation:

    let internalContent = $state(content);
    // $effect removed entirely, not replaced with $derived

    Why this approach is correct:

    1. Mutable state required: These components modify internalContent within handleContentChange handlers (injecting language codes, adding prefixes). Using $derived(content) would make it read-only, breaking the mutation logic.

    2. The $effect was truly redundant: The sync $effect(() => { internalContent = content; }) served no purpose because:

      • Initial sync: Already handled by $state(content) initialization
      • User edits: Flow through event handlers, not prop changes
      • External changes: Handled by component remount
    3. calendar.svelte excluded: Its $effect is a necessary workaround for bits-ui's Svelte 5 binding limitations (two-way binding bridge pattern) - a fundamentally different use case.

    Final count: 3 $effect blocks removed (not 4) since calendar.svelte was correctly preserved.

  5. added a commit that references this issue on Nov 30, 2025
    8dd23f3
  6. added 2 commits that reference this issue on Feb 4, 2026
    9b71270
    8b0f13e
  7. added a commit that references this issue on Feb 26, 2026
    d4f09ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions