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

[docs] Support live demo editing #32107

Closed
wants to merge 32 commits into from
Closed

Conversation

nihgwu
Copy link
Contributor

@nihgwu nihgwu commented Apr 2, 2022

Preview: https://deploy-preview-32107--material-ui.netlify.app/components/buttons/

a POC to add live editing for components docs, using react-runner

Code highlighting keeps the same, and with that we don't need a loader to load the demos anymore, just run the code

Fix #26476.

@mui-bot
Copy link

mui-bot commented Apr 2, 2022

No bundle size changes

Generated by 🚫 dangerJS against 66da51b

@nihgwu nihgwu force-pushed the neo/react-runner branch 4 times, most recently from eb13bc5 to 8024083 Compare April 3, 2022 00:22
@michaldudak
Copy link
Member

michaldudak commented Apr 4, 2022

There is an ongoing effort to make the demos editable: #30873 and #29885.

@michaldudak michaldudak closed this Apr 4, 2022
@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 4, 2022

@michaldudak Sorry I didn't know that, but you can see my change is much smaller and simpler than that PR, and that PR doesn't support SSR

@michaldudak
Copy link
Member

@bharatkashyap could you take a look at this and coordinate your efforts?

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 4, 2022

@michaldudak can you reopen it and I'll fix the failing builds, and then you can compare, at least it works well locally for Button/Autocomplete, builds fail probably because I missed some imports

@michaldudak michaldudak reopened this Apr 4, 2022
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 4, 2022
@danilo-leal danilo-leal changed the title PoC: support live editing [docs] Support live demo editing Apr 4, 2022
@danilo-leal danilo-leal added docs Improvements or additions to the documentation proof of concept Studying and/or experimenting with a to be validated approach labels Apr 4, 2022
@nihgwu nihgwu force-pushed the neo/react-runner branch 2 times, most recently from 3aed61a to 7b0e4b4 Compare April 4, 2022 22:10
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 4, 2022
@nihgwu nihgwu force-pushed the neo/react-runner branch 3 times, most recently from c91a87e to 858dd84 Compare April 5, 2022 08:16
@mbrookes
Copy link
Member

mbrookes commented Apr 7, 2022

Thanks for working on it. I was able to try it out locally. A few minor issues (there may well be others):

  • The preview isn't editable.
  • The JS code is replaced with TS code.
  • The editor has a keyboard focus indicator when selected by mouse.
  • A bunch of symbols have an wiggly underline as if they're incorrect.
  • It has a memory leak:
error

<--- Last few GCs --->

[95270:0x1049e0000] 86058467 ms: Mark-sweep (reduce) 1984.0 (2036.8) -> 1977.3 (2030.3) MB, 1116.6 / 0.0 ms (average mu = 0.269, current mu = 0.139) allocation failure GC in old space requested
[95270:0x1049e0000] 86059705 ms: Mark-sweep (reduce) 1977.3 (2029.3) -> 1977.2 (2030.0) MB, 1237.9 / 0.0 ms (average mu = 0.164, current mu = 0.000) allocation failure GC in old space requested

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0x10130c5e5 node::Abort() (.cold.1) [/usr/local/bin/node]
2: 0x1000b2289 node::Abort() [/usr/local/bin/node]
3: 0x1000b23ef node::OnFatalError(char const*, char const*) [/usr/local/bin/node]
4: 0x1001f68c7 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
5: 0x1001f6863 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/bin/node]
6: 0x1003a47e5 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/usr/local/bin/node]
7: 0x1003a628a v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/usr/local/bin/node]
8: 0x1003a19b5 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/bin/node]
9: 0x10039f2e0 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/bin/node]
10: 0x10039fc92 v8::internal::Heap::CollectAllAvailableGarbage(v8::internal::GarbageCollectionReason) [/usr/local/bin/node]
11: 0x1003adaae v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/usr/local/bin/node]
12: 0x100374fc7 v8::internal::FactoryBasev8::internal::Factory::NewFixedArrayWithFiller(v8::internal::Handlev8::internal::Map, int, v8::internal::Handlev8::internal::Oddball, v8::internal::AllocationType) [/usr/local/bin/node]
13: 0x1005f5a50 v8::internal::OrderedHashTable<v8::internal::OrderedHashMap, 2>::Rehash(v8::internal::Isolate*, v8::internal::Handlev8::internal::OrderedHashMap, int) [/usr/local/bin/node]
14: 0x1006e18cf v8::internal::Runtime_MapGrow(int, unsigned long*, v8::internal::Isolate*) [/usr/local/bin/node]
15: 0x100a81a79 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/usr/local/bin/node]
error Command failed with signal "SIGABRT".

I presume these are all fixable. Other than that it appears to work as expected, so the main considerations vs the alternative is bundle size. react-runner is using sucrase, so that's a good sign.

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 7, 2022

@mbrookes Thanks for trying, I have no idea why the builds failed, I can build & export successfully locally, regarding the issues, right now it's just a POC, and ofc we can fix them, but for the first one I'd prefer keep JSX preview uneditable, live editing is just a complement not necessary, so it's fine to provide the very basic functionalities as before, if you want try by themselves, then expand it to full mode

As I mentioned in the description, probably we can reduce the build time a lot by removing the demos builds and use react-runner's JIT to run them, right now it's super slow, I didn't dive in deep, but we can make the markdown loader super simpler without black magic

@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Apr 11, 2022
@mbrookes
Copy link
Member

mbrookes commented Apr 15, 2022

but for the first one I'd prefer keep JSX preview uneditable

This is on the "must have" list, but ofc no need to add it until this approach is proven.

@bharatkashyap Could you have a look?

@Janpot
Copy link
Member

Janpot commented Apr 19, 2022

I've reran the netlify build with success this time: https://deploy-preview-32107--material-ui.netlify.app/
Of the top of my mind, some of things this PR is missing is that can explain the reduced complexity compared to the existing PRs:

  1. Preview editing is missing
  2. It's not loading the react-runner asynchronously
  3. It supports only four dependencies react, @mui/material, @mui/icons-material, @mui/lab. How do you plan to support all the dependencies all of the demo's are using while simultaneously avoiding to load them on every page?

@bharatkashyap
Copy link
Member

bharatkashyap commented Apr 19, 2022

@nihgwu This is a promising approach, based on the bundle size of react-runner versus jarle.

As @mbrookes mentioned, making the JSX preview editable is pretty much a must have (which is why #30873 loads the editor and runner on first load, whereas the earlier #29885 loads these components after user input and swaps out the original, static preview).

Also, #30873 has a way to not statically import all the dependencies at once like this one does at present (as @Janpot mentioned). I'm sure we can take this ahead if you take a shot at addressing these two issues

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 19, 2022

@Janpot @bharatkashyap aysnc loading is never a problem for react-runner 😉 , checkout this demo, probably I should work on #30873 but revert all the changes on the variable orders if you do need live editing of JSX preview, though I still don't like the idea, as it's not the real code it's running against

@nihgwu
Copy link
Contributor Author

nihgwu commented Apr 19, 2022

OK, the problem with async module resolution is that you are not allowed to import other components other than the components included in the initial code
image
And it doesn't support SSR
Initial loading of all packages would be a problem for MUI as it's really huge

The problem with live editing of JSX preview, apart from the above one, the implementation in #30873 is not good as it's simply replacing the JSX part from the raw code, which means the JSX code and row code are highly coupled, and you can't make any mistake when editing them, not even a whitespace, if you do need that, of cause I can inject those components into scope instead of imports, but I think we can postpone it for following PRs, @oliviertassinari WDYT?

@nihgwu

This comment was marked as resolved.

@mbrookes

This comment was marked as resolved.

@nihgwu

This comment was marked as resolved.

@oliviertassinari

This comment was marked as resolved.

@mbrookes
Copy link
Member

How the focus style is currently implemented in this PR seems identical to how the platform behaves

Not true:
image

(Although that example lacks a keyboard focus indicator, which we should fix.)

and MUI's components behave by default

Asked and answered - we don't use the native Material Design appearance for the docs. And also not true – the text field has a much more subtle appearance on hover.

By all means make it more subtle when hovered and not keyboard focused, but the current appearance is not aesthetically pleasing, or in keeping with the overall docs theme. cc @danilo-leal

@oliviertassinari

This comment was marked as resolved.

@oliviertassinari

This comment was marked as resolved.

@nihgwu
Copy link
Contributor Author

nihgwu commented Jul 17, 2022

Sorry spent another hour trying to find the correct colours but failed, as it's different from TextInput, and it's always dark, I think the current colours works well in both dark and light mode, feel free to change to your taste

@oliviertassinari oliviertassinari added new feature New feature or request and removed proof of concept Studying and/or experimenting with a to be validated approach labels Jul 18, 2022
@mui mui deleted a comment from nihgwu Jul 18, 2022
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding what's the next step on this PR. Here are all the opportunities to improve the feature that I'm able to identify:

  • 1. We could fix

Screenshot 2022-07-18 at 00 33 52

by removing the type in https://github.com/satya164/react-simple-code-editor/blob/6b2a716de074f8ae09e0cc9d00667050fa06de50/src/index.tsx#L591. Reproduction: https://validator.w3.org/nu/?doc=https%3A%2F%2Fdeploy-preview-32107--material-ui.netlify.app%2Fmaterial-ui%2Freact-alert%2F.

react-simple-code-editor/react-simple-code-editor#98

  • 2. We could fix the broken code font size on Safari

Screenshot 2022-07-18 at 00 37 38

The trick would be to apply https://github.com/necolas/normalize.css/blob/master/normalize.css#L102.

  • 3. We could fix the lack of support for border-radius with outlined on Safari by replacing it with box-shadow, e.g. 0 0 0px 3px #66b2ff.

Screenshot 2022-07-18 at 02 22 21

  • 4. We could fix the leaking scrollbar display by using overflow: hidden

Screenshot 2022-07-18 at 01 39 05

  • 5. We could fix the out of sync textarea height when pressing Enter to add a new line:

Screenshot 2022-07-18 at 01 41 52

Removing pre > br: { display: none } seems to do it. I would be curious to know why we have this style.

Screenshot 2022-07-18 at 02 23 17

  • 7. We could improve a bit the UX on the styles. I think that a hover style of 2px wide rather than 3px would feel better. It would be less distraction when only moving the cursor between demos and more feedback validation when actually focusing on the input.

I see nothing blocking, maybe 6, but it seems that it's solvable in a follow-up PR so 👍 on my end to merge 🎉

netlify.toml Show resolved Hide resolved
docs/src/modules/components/CodeEditor.js Outdated Show resolved Hide resolved
docs/src/modules/components/CodeEditor.js Outdated Show resolved Hide resolved
@mbrookes

This comment was marked as resolved.

@nihgwu

This comment was marked as resolved.

opacity: 1,
},
},
'&:focus-within': {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbrookes I'm moving the previous discussion to the code side, so we can automatically resolve it once solved. I don't think that we should keep commenting on the main comment space of the PR. I have resolved all the comments.

From what I understood, this is the relevant line for:

The editor has a keyboard focus indicator when selected by mouse.

from #32107 (comment).

But I don't understand the point that you are trying to make, the same goes for @nihgwu. If you could expand on the current vs. expected behavior and why it should behave differently, it would be great.


My best assumption is that you are saying this should be:

Suggested change
'&:focus-within': {
'&:focus-visible': {

Which would then miss the focus ring for mouse users, going against:

Screenshot 2022-07-18 at 12 16 14

that I think we should fix, e.g. Tailwind does it correct https://tailwindcss.com/blog.

Screenshot 2022-07-18 at 12 12 24


My second best assumption is that you are saying that keyboard focus and mouse focus should have a different focus ring style. To me it would feel a bit strange, I can't recall any design system making a difference for inputs (they often do for buttons though)

@nihgwu
Copy link
Contributor Author

nihgwu commented Jul 18, 2022

@oliviertassinari my answer to #32107 (review)

  1. we can't do much on that unless we switch to another editor, I saw you also raised similar questions in that repo
  2. It exits without this PR, don't have a quick clue to fix it
  3. Fixed in my latest commit
  4. I also noticed that, that's the current behaviour without this PR, and the fix will be large so I didn't do that
  5. We have new line in our raw code, so I want to remove via css, but I changed to another approach
  6. Don't quite understand the diff as for SSR we produce the same result and then hydration
  7. I updated slightly

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 18, 2022

@nihgwu

  1. we can't do much on that unless we switch to another editor, I saw you also raised similar questions in that repo

I have edit rights on the repository and on npm: https://www.npmjs.com/package/react-simple-code-editor. I could make simple changes, the only problem, I might not have the time, running MUI should be more important 😁. In any case, it's not a blocker, more for MUI's maintainer to look into.

  1. Don't quite understand the diff as for SSR we produce the same result and then hydration

I would assume that it's about the hydration that takes more time with the live editor. My suggestion would be to the first hydrate with a static version, deferring the live editor to a later point in time. Ideally, we could selectively delay hydration. Maybe it's something we could do in a follow-up.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 26, 2022
@siriwatknp
Copy link
Member

I would assume that it's about the hydration that takes more time with the live editor. My suggestion would be to the first hydrate with a static version, deferring the live editor to a later point in time. Ideally, we could selectively delay hydration. Maybe it's something we could do in a follow-up.

We should make sure of this before merging. If the time is significant, we should improve it in this PR. However, I don't have time to dedicate to this 🥲.

@bharatkashyap
Copy link
Member

Comparing results from production and the deploy preview, it seems like there is a significant decrease in the time to interactive:
pagespeed.web.dev for production
Screenshot 2022-09-24 at 12 16 26 PM
pagespeed.web.dev for deploy preview
Screenshot 2022-09-24 at 12 16 32 PM

@michaldudak
Copy link
Member

Closing as per #34454 (comment)

@nihgwu nihgwu deleted the neo/react-runner branch October 17, 2022 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation new feature New feature or request on hold There is a blocker, we need to wait PR: out-of-date The pull request has merge conflicts and can't be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[docs] Make source code snippet editable