Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

theme-live-codeblock shows incorrect preview on theme change #9630

Closed
2 of 7 tasks
NickGerleman opened this issue Dec 14, 2023 · 1 comment · Fixed by #9631
Closed
2 of 7 tasks

theme-live-codeblock shows incorrect preview on theme change #9630

NickGerleman opened this issue Dec 14, 2023 · 1 comment · Fixed by #9631
Labels
bug An error in the Docusaurus core causing instability or issues with its execution

Comments

@NickGerleman
Copy link
Contributor

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

If react-live LivePreview is re-rendered, it will peform preview from the LiveProvider code prop instead of the current state of the content.

I fixed this in something similar using theLiveEditor onChange prop, to keep state of the current code being edited, and passing that state to the LiveProvider instead of the original code.

Reproducible demo

No response

Steps to reproduce

  1. Go to https://docusaurus.io/docs/markdown-features/code-blocks#interactive-code-editor
  2. Add some text after the period in the output
  3. Change the theme

Expected behavior

The newly edited code is used for the preview

Actual behavior

The original code is used for the preview until a subsequent edit

Your environment

No response

Self-service

  • I'd be willing to fix this bug myself.
@NickGerleman NickGerleman added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Dec 14, 2023
@slorber slorber removed the status: needs triage This issue has not been triaged by maintainers label Dec 14, 2023
@slorber
Copy link
Collaborator

slorber commented Dec 14, 2023

😄 good catch!

I can see how the editor/preview are not in sync after a theme change:

CleanShot 2023-12-14 at 12 27 42@2x


@carloskelly13 wonder if it can be considered as a react-live bug?

This happens because we pass an inline non-memoized transformCode callback to <LiveProvider>, and due to this effect:

  useEffect(() => {
    transpileAsync(code).catch(onError);
  }, [code, scope, noInline, transformCode]);

You retranspile the code in this case, (which makes sense because transpilation uses transformCode), but you retranspile it from the initial code instead of the current editor code.

Maybe the editor code state should be hoisted to the Live provider, or you should read the editor state with useImperativeHandle?

I'll just memoize the callback in Docusaurus, it's more optimized anyway. But I thought you'd be interested to know about this issue: passing an inline or stable function should probably lead to the same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
2 participants