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

Adapt syntax highlighting on dark theme #2769

Merged
merged 1 commit into from
Feb 2, 2022

Conversation

dbelokon
Copy link
Contributor

Issue This PR Addresses

Fixes #2377.

Type of Change

  • Bugfix: Change which fixes an issue
  • New Feature: Change which adds functionality
  • Documentation Update: Change which improves documentation
  • UI: Change which improves UI

Description

All code blocks that the Telescope back-end sends us are annotated with highlight.js classes, so the front-end has to provide the stylesheet that defines these.

Since the stylesheet is global, we have to link both stylesheets for light and dark theme, and disable either depending on the current theme.

Checklist

  • Quality: This PR builds and passes our npm test and works locally

  • Tests: This PR does not includes tests as it does not add new functionality or change it.

  • Screenshots: This PR includes screenshots or GIFs of the changes made:
    image
    image

  • Documentation: This PR does not include documentation as it does not add user exposed functionality. However, a couple of comments were provided in the code as well in the commit message to explain certain changes.

@gitpod-io
Copy link

gitpod-io bot commented Jan 28, 2022

Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

I'm thrilled to see us fixing this, thank you!

A couple of things to consider.


// Enable the stylesheet for the syntax highlighting depending on the theme
if (preferredTheme === 'dark') {
document.querySelector('#light-stylesheet')?.setAttribute('disabled', 'disabled');
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Question: should this happen within a useEffect @DukeManh?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it should be useEffect of hooks/use-preferred-theme

const toggleTheme = () => {
setPreferredTheme(preferredTheme === 'dark' ? 'light' : 'dark');
if (preferredTheme === 'dark') {
document.querySelector('#light-stylesheet')?.removeAttribute('disabled');
Copy link
Contributor

Choose a reason for hiding this comment

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

We're repeating code here, let's consider moving this enable/disable logic to function(s), and maybe even put it in hooks/use-preferred-theme so it happens when we get/set the theme.

Copy link
Contributor

@DukeManh DukeManh left a comment

Choose a reason for hiding this comment

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

Why haven't we done this earlier. Thank @dbelokon.

One thing, let's use the minified css files to save loading time. https://highlightjs.org/download/

@TueeNguyen
Copy link
Contributor

I like these inline format more than the old one
image

@@ -0,0 +1,10 @@
pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}/*!
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible for us to get these via node_modules/ vs. embedding directly in our code? Then we can get updates.

Copy link
Contributor Author

@dbelokon dbelokon Feb 1, 2022

Choose a reason for hiding this comment

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

So, as I understand how next.js deals with static files and CSS style sheets in particular, it might not be possible with the tools we have available...

If your style sheets were in src/web/src/styles, you can @import a style sheet found in node_modules, but next.js modifies the original file so that the @import rule is not there anymore. So, we cannot access the github.css and github-dark.css files from the @import rules like in CSSImportRule. And, even if next.js kept the @import rule, we wouldn't be able to find the original style sheet with an id, because the style element generated by next.js does not have an id.

If you place the style sheets in src/web/public, you cannot @import a style sheet found in node_modules.

So, as of right now, it might not be possible. If someone knows a lot about next.js, please let us know if you can think of a way to make this possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the worst case, we would have to use a dependency that copies certain files in node_modules during next.js compilation time for this specific case.

@aserputov aserputov self-requested a review January 31, 2022 21:20
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

I'm surprised there's no way to do this with next.js, but if it can't be done, we can go this route. One question.

src/web/src/pages/_app.tsx Outdated Show resolved Hide resolved
@dbelokon
Copy link
Contributor Author

dbelokon commented Feb 1, 2022

I'm surprised there's no way to do this with next.js, but if it can't be done, we can go this route.

I am not very experienced with next.js, so I do not know the limits of it and I cannot research more because Google is giving me the same pages I already checked. I could try to ask the next.js developers about something like this, if we do not have someone else that might have an idea

@humphd
Copy link
Contributor

humphd commented Feb 1, 2022

I'm surprised there's no way to do this with next.js, but if it can't be done, we can go this route.

I am not very experienced with next.js, so I do not know the limits of it and I cannot research more because Google is giving me the same pages I already checked. I could try to ask the next.js developers about something like this, if we do not have someone else that might have an idea

cc @DukeManh, @Andrewnt219 for thoughts

@aserputov
Copy link
Contributor

@dbelokon I can help you. Let's ask people in the chat, otherwise we will think something.

@DukeManh
Copy link
Contributor

DukeManh commented Feb 1, 2022

@dbelokon, I think you misunderstood the import CSS part. In React/Next, CSS files can be imported like a module in JS like so: import 'highlight.js/styles/github.css. What you are talking about is importing a CSS file into another, correct?

@humphd, I don't think importing CSS files from node_modules would work for us because we want to give them ids so we can select and disable them later. I don't know how to give ids to imported CSS files.

@humphd
Copy link
Contributor

humphd commented Feb 1, 2022

What if you do dynamic imports and only pull in the one you need, when you need it?

@DukeManh
Copy link
Contributor

DukeManh commented Feb 1, 2022

Would it work for CSS file?
Why don't you give it a try @dbelokon, https://nextjs.org/docs/advanced-features/dynamic-import

@dbelokon
Copy link
Contributor Author

dbelokon commented Feb 1, 2022

So, it seems that dynamic import might not work either :(

As I understand, it seems next.js treats the import 'path/to/style.css; statements as an exception more than a rule. That means, import() will not work.

The main problem is that the CSS style sheet has to be a global one, because otherwise the elements that are annotated with the class names won't get styled at all. So, if you have two global sheets that apply to the same class names, you need to disable one of them for the other to work. If the styling was bundled in a React component, this would be much easier, but of course, we wouldn't have the problem of the style sheet being global anyway.

@sirinoks sirinoks added the area: design Issues needing design or assets label Feb 1, 2022
humphd
humphd previously approved these changes Feb 1, 2022
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

@dbelokon I feel like we're holding up "good" work in an effort to have "perfect," and this is the wrong approach: we should take what you've done, which is awesome, and iterate on it later.

Thanks for entertaining these discussions as we look for a proper way to solve this. It was worth researching.

aserputov
aserputov previously approved these changes Feb 1, 2022
@aserputov
Copy link
Contributor

@dbelokon could you please rebase this, and we can merge it.

@dbelokon
Copy link
Contributor Author

dbelokon commented Feb 1, 2022

Hahaha thank you @humphd!!

Going to rebase it shortly

@dbelokon
Copy link
Contributor Author

dbelokon commented Feb 1, 2022

Rebased and squashed without conflicts!

Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

Why is the light background gray now vs. white?

Screen Shot 2022-02-01 at 4 40 01 PM

@dbelokon
Copy link
Contributor Author

dbelokon commented Feb 1, 2022

Why is the light background gray now vs. white?

I did it because it looked a little bit better, especially with GitHub's syntax highlighting. This is my opinion, though 😅

I can change it back to white background, if you don't mind

@dbelokon
Copy link
Contributor Author

dbelokon commented Feb 1, 2022

It looks a bit more "separated" from the other content when the code has grey background color vs. white.

Dark theme:

image

White theme:

image

@humphd
Copy link
Contributor

humphd commented Feb 1, 2022

For a "light" theme, I think white is the way to go. It helps with text contrast with accessibility.

aserputov
aserputov previously approved these changes Feb 1, 2022
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

This is only partially fixed: inline <code> is still gray:

Screen Shot 2022-02-01 at 5 58 28 PM

All code blocks that the Telescope back-end sends us are annotated with
highlight.js classes, so the front-end has to provide the stylesheet
that defines these.

Since the stylesheet is global, we have to link both stylesheets for
light and dark theme, and disable either depending on the current
theme.
@aserputov aserputov merged commit f30ac73 into Seneca-CDOT:master Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: css styling Anything related to CSS styling area: design Issues needing design or assets area: front-end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve dark mode code colours
6 participants