josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
816 stars 108 forks source link

feat: use SCSS variables internally #344

Closed SamMousa closed 6 months ago

SamMousa commented 7 months ago

This reworks the default theme to use SCSS variables.

Some things that are still todo:

Since these todos only require a very small subset of variables one solution is to define them in the relevant component as internal CSS variables.

Todo from feedback below: Using the default theme:

the selection bar between two rows:

Using theme "dark":

In the nested editor modal (same as with the default theme I think):

Using theme "big"

SamMousa commented 7 months ago

@josdejong rudely pinging you!

This PR solves the styling issue:

I hope you like it, otherwise I wasted a few hours for nothing xD

josdejong commented 7 months ago

Thanks a lot Sam, it looks neat at first sight! I'll do an in-depth review+testing coming Wednesday I expect.

One first thought: I think the file jse-theme-dark.css should not contain SCSS variables and be plain CSS, so I think $text-color should remain var(--jse-text-color) like it was before?

SamMousa commented 7 months ago

Yes, that might be a global search and replace mistake. Thought I caught all of them..

josdejong commented 7 months ago

I see there where some tricky things involved, like getting the colors work in text mode, and you did something nice with the calculation of the left margin of nested items. Great job!

This is a lot more work than the original 1 line PR πŸ˜… , but I really love how this works out. It is indeed easy to maintain like this.

I've gone through all details of the editor, and almost everything looks alright! I came across a number of (I think) small issues, listed below. Can you have a look into those? Please let me know if you need help.

I've mostly tested with the development page http://localhost:5173/development:

Using the default theme:

  1. the keys in text mode are red instead of black
  2. the light gray border of the editor is gone
  3. in transform modal: border of the preview editors is missing
  4. the selection bar between two rows: 4.1. is missing the left-margin 4.2. when the editor does not have focus, it is not rendered correctly (shoud be rendered in light gray)
  5. in the nested editor modal (right click an object, click "Edit object" in the context menu): 5.1 editor menu is missing the background color 5.2 the Apply button bottom right is missing a background color

Using theme "dark":

  1. the keys in text mode are light green instead of light blue (same issue as default theme?)
  2. the select boxes do not have a dark theme in the Transform Modal and Sort modal
  3. In the nested editor modal (same as with the default theme I think): 8.1. the menu doesn't have the right background color and text color 8.2. the Apply button is missing a background color
  4. the validation warnings a the bottom do not have a dark text color

Using theme "big"

  1. the select boxes in Transform and Sort modal do not respect the font-size
SamMousa commented 7 months ago

I've fixed some points, others remain open; I'd love some help to pull this over the finish line.

Regarding the buttons in modals I think the way this is currently implemented should actually be configured a bug:

  1. Variable --jse-theme-color sets the primary theme color at the top level
  2. Variable --jse-modal-theme-color sets the primary theme color for use inside modals
  3. Variable --jse-button-primary-background is defined as var(--jse-theme-color

Due to the way CSS variables work the variable defined in 3 has the value from 1, even if it is shown inside a modal. This seems to be a reasoning error. Inside a model you have redefined the primary theme color so the button should obey that. This screenshot from jsoneditoronline.org shows the inconsistency with respect to the menu bar, which also by default has the theme color.

image

josdejong commented 7 months ago

Thanks for all the fixes. I'll look into the issue with the modals, no problem.

SamMousa commented 7 months ago

Big tip: if you see $variable in developer tools it's an issue with SCSS escaping. On the right hand side of CSS variable declaration SCSS requires explicit interpolation: --jse-abc: #{$abc} instead of the default implicit interpolation: background-color: $abc;

josdejong commented 7 months ago

Regarding the buttons in modals I think the way this is currently implemented should actually be configured a bug

Your explanation is correct. However my intention was a bit different: I wanted to be able to just give the menu of the editor inside a modal a different color because it was looking ugly to me. So I think the name --jse-modal-theme-color is misleading and should be something like --jse-modal-editor-menu-background-color. Renaming would be a breaking change, but we can keep it backward compatible I think. Does that make sense?

SamMousa commented 7 months ago

I wouldn't worry about BC at this level of detail to be honest. I mean anything can be considered a BC break if you're critical enough. This PR already breaks BC because we no longer support overriding some specific svelte-select settings via the original CSS variables, you have to use the --jse-svelte-select prefixed version for some of them.

josdejong commented 7 months ago

Good point, it's already a very breaking change πŸ˜‚ . I'll publish it as a major release and explain the changes.

SamMousa commented 7 months ago

While you're at it you could also document the BC promise. For example you could say that users customizing svelte-select should ALSO use the prefixed variables. This allows you to customize those properties in the future without it being another BC break.

josdejong commented 7 months ago

Just for clarity: do you want me to finish the last open issues?

SamMousa commented 7 months ago

Just for clarity: do you want me to finish the last open issues?

yes please!

josdejong commented 7 months ago

okido

josdejong commented 7 months ago

I created https://github.com/josdejong/svelte-jsoneditor/pull/350, fixing the last open issues. I want to do one more testing round later this week, I expect on Wednesday.

@SamMousa one last question: do you know whether the new way to handle indentation with a CSS variable --level has any performance implications? I'm a bit in doubt whether CSS variables are suitable to be used like this, with possibly thousands of re-definitions with every re-render.

SamMousa commented 7 months ago

I can't imagine it is faster to change styles directly, since you're doing 1000s of updates in such a case as well... But this is a guess

josdejong commented 7 months ago

I just did a small test by hand, it actually feels a bit faster 😁. I didn't do a real benchmark but it is as fast as before or a bit faster.

SamMousa commented 7 months ago

That's what I expected, in theory it is more optimizable:

josdejong commented 7 months ago

It makes sense indeed. This is a nice side-effect of this refactor πŸ˜ŽπŸ‘

SamMousa commented 7 months ago

Btw, for future reference: you can directly push to a PR branch as well, as long as a contributor has checked the checkbox during pr creation. Thanks for your efforts in finishing it!

josdejong commented 6 months ago

Ahh, I didn't know that. That's very handy indeed, also for example for PR's that are about finished but just have a linting error or something like that.

josdejong commented 6 months ago

Btw, for future reference: you can directly push to a PR branch as well, as long as a contributor has checked the checkbox during pr creation.

Yay, that indeed works 😎

I've done another testing round, found a few small things and cleaned up some left over TODO's.

Time to merge this PR.

josdejong commented 6 months ago

The refactoring is now published in v0.20.0. Thanks a lot for all your effort and for thinking along Sam!