Skip to content

8342705: Add dark mode for docs #26185

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ protected void generateOtherFiles(ClassTree classTree)
copyResource(DocPaths.RIGHT_SVG, DocPaths.RESOURCE_FILES.resolve(DocPaths.RIGHT_SVG), true);
copyResource(DocPaths.CLIPBOARD_SVG, DocPaths.RESOURCE_FILES.resolve(DocPaths.CLIPBOARD_SVG), true);
copyResource(DocPaths.LINK_SVG, DocPaths.RESOURCE_FILES.resolve(DocPaths.LINK_SVG), true);
copyResource(DocPaths.MOON_SVG, DocPaths.RESOURCE_FILES.resolve(DocPaths.MOON_SVG), true);
copyResource(DocPaths.SUN_SVG, DocPaths.RESOURCE_FILES.resolve(DocPaths.SUN_SVG), true);

if (options.createIndex()) {
copyResource(DocPaths.SEARCH_JS_TEMPLATE, DocPaths.SCRIPT_FILES.resolve(DocPaths.SEARCH_JS), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public class HtmlIds {
static final HtmlId SEARCH_INPUT = HtmlId.of("search-input");
static final HtmlId SERVICES = HtmlId.of("services-summary");
static final HtmlId SKIP_NAVBAR_TOP = HtmlId.of("skip-navbar-top");
static final HtmlId THEME_BUTTON = HtmlId.of("theme-button");
static final HtmlId THEME_DARK = HtmlId.of("theme-dark");
static final HtmlId THEME_LIGHT = HtmlId.of("theme-light");
static final HtmlId THEME_OS = HtmlId.of("theme-os");
static final HtmlId THEME_PANEL = HtmlId.of("theme-panel");
static final HtmlId UNNAMED_PACKAGE_ANCHOR = HtmlId.of("unnamed-package");
private static final String FIELDS_INHERITANCE = "fields-inherited-from-class-";
private static final String METHODS_INHERITANCE = "methods-inherited-from-class-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case MODULE:
addOverviewLink(target);
Expand All @@ -148,6 +149,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case PACKAGE:
addOverviewLink(target);
Expand All @@ -166,6 +168,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case CLASS:
addOverviewLink(target);
Expand All @@ -184,6 +187,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case USE:
addOverviewLink(target);
Expand All @@ -208,6 +212,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case TREE:
addOverviewLink(target);
Expand All @@ -226,6 +231,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case DEPRECATED:
case INDEX:
Expand Down Expand Up @@ -268,6 +274,7 @@ private void addMainNavLinks(Content target) {
} else {
addHelpLink(target);
}
addThemeSwitcher(target);
break;
case ALL_CLASSES:
case ALL_PACKAGES:
Expand All @@ -285,6 +292,7 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
case DOC_FILE:
addOverviewLink(target);
Expand All @@ -311,16 +319,13 @@ private void addMainNavLinks(Content target) {
addIndexLink(target);
addSearchLink(target);
addHelpLink(target);
addThemeSwitcher(target);
break;
default:
break;
}
}

private void addContentToList(List<Content> listContents, Content source) {
listContents.add(HtmlTree.LI(source));
}

private void addItemToList(Content list, Content item) {
list.add(HtmlTree.LI(item));
}
Expand Down Expand Up @@ -492,6 +497,17 @@ private void addHelpLink(Content target) {
}
}

private void addThemeSwitcher(Content target) {
var selectTheme = contents.getContent("doclet.theme.select_theme");
target.add(HtmlTree.LI(HtmlTree.BUTTON(HtmlIds.THEME_BUTTON)
.add(HtmlTree.IMG(pathToRoot.resolve(DocPaths.RESOURCE_FILES).resolve(DocPaths.SUN_SVG),
selectTheme.toString()).addStyle(HtmlIds.THEME_LIGHT.name()))
.add(HtmlTree.IMG(pathToRoot.resolve(DocPaths.RESOURCE_FILES).resolve(DocPaths.MOON_SVG),
selectTheme.toString()).addStyle(HtmlIds.THEME_DARK.name()))
.put(HtmlAttr.ARIA_LABEL, selectTheme.toString())
.put(HtmlAttr.TITLE, selectTheme.toString())));
}

private void addSearch(Content target) {
var resources = configuration.getDocResources();
var inputText = HtmlTree.INPUT(HtmlAttr.InputType.TEXT, HtmlIds.SEARCH_INPUT)
Expand Down Expand Up @@ -545,6 +561,7 @@ public Content getContent() {
navContent.add(aboutDiv);
navigationBar.add(HtmlTree.DIV(HtmlStyles.topNav, navContent).setId(HtmlIds.NAVBAR_TOP));


var subNavContent = HtmlTree.DIV(HtmlStyles.navContent);
List<Content> subNavLinks = new ArrayList<>();
switch (documentedPage) {
Expand All @@ -557,6 +574,22 @@ public Content getContent() {
breadcrumbNav.addAll(subNavLinks, HtmlTree::LI);
subNavContent.addUnchecked(breadcrumbNav);

var selectTheme = contents.getContent("doclet.theme.select_theme");
subNavContent.add(HtmlTree.DIV(HtmlIds.THEME_PANEL)
.add(HtmlTree.DIV(selectTheme))
.add(HtmlTree.DIV(HtmlTree.LABEL(HtmlIds.THEME_LIGHT.name(), Text.EMPTY)
.add(HtmlTree.INPUT(HtmlAttr.InputType.RADIO, HtmlIds.THEME_LIGHT)
.put(HtmlAttr.NAME, "theme").put(HtmlAttr.VALUE, HtmlIds.THEME_LIGHT.name()))
.add(HtmlTree.SPAN(contents.getContent("doclet.theme.light"))))
.add(HtmlTree.LABEL(HtmlIds.THEME_DARK.name(), Text.EMPTY)
.add(HtmlTree.INPUT(HtmlAttr.InputType.RADIO, HtmlIds.THEME_DARK)
.put(HtmlAttr.NAME, "theme").put(HtmlAttr.VALUE, HtmlIds.THEME_DARK.name()))
.add(HtmlTree.SPAN(contents.getContent("doclet.theme.dark"))))
.add(HtmlTree.LABEL(HtmlIds.THEME_OS.name(), Text.EMPTY)
.add(HtmlTree.INPUT(HtmlAttr.InputType.RADIO, HtmlIds.THEME_OS)
.put(HtmlAttr.NAME, "theme").put(HtmlAttr.VALUE, HtmlIds.THEME_OS.name()))
.add(HtmlTree.SPAN(contents.getContent("doclet.theme.system"))))));

if (options.createIndex() && documentedPage != PageMode.SEARCH) {
addSearch(subNavContent);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,188 @@
/* ignored */
}

body.theme-dark {
.hljs-title.function_,
.hljs-template-variable {
color: #66bcce;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you can nest rules here, the browser may skip/ignore them.

Copy link
Member Author

Choose a reason for hiding this comment

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

It works for me, I've tried it on Firefox, Safari and Chrome.

Documentation of CSS nesting: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_nesting/Using_CSS_nesting
It's implemented in all major browsers: https://caniuse.com/?search=css%20nesting

}
.hljs-code,
.hljs-comment,
.hljs-quote {
color:#9d9d9d;
font-style: italic;
}
.hljs-meta {
color: #836F00;
}
.hljs-symbol,
.hljs-template-tag,
.hljs-keyword,
.hljs-literal,
.hljs-name,
.hljs-built_in,
.hljs-char.escape_ {
color: #88aece;
}
.hljs-variable,
.hljs-property,
.hljs-attr,
.hljs-section {
color: #c59bc1;
}
.hljs-attribute {
color: #c59bc1;
}
.hljs-regexp,
.hljs-number {
color: #cfe374;
}
.hljs-link {
color: #b5bd68;
}
.hljs-string {
color: #b5bd68;
}
.hljs-doctag {
text-decoration: underline;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
.hljs-subst,
.hljs-title,
.hljs-params,
.hljs-bullet,
.hljs-formula,
.hljs-tag,
.hljs-type {
/* ignored */
}
}

@media (prefers-color-scheme: dark) {
.hljs-title.function_,
.hljs-template-variable {
color: #66bcce;
}
.hljs-code,
.hljs-comment,
.hljs-quote {
color:#9d9d9d;
font-style: italic;
}
.hljs-meta {
color: #836F00;
}
.hljs-symbol,
.hljs-template-tag,
.hljs-keyword,
.hljs-literal,
.hljs-name,
.hljs-built_in,
.hljs-char.escape_ {
color: #88aece;
}
.hljs-variable,
.hljs-property,
.hljs-attr,
.hljs-section {
color: #c59bc1;
}
.hljs-attribute {
color: #c59bc1;
}
.hljs-regexp,
.hljs-number {
color: #cfe374;
}
.hljs-link {
color: #b5bd68;
}
.hljs-string {
color: #b5bd68;
}
.hljs-doctag {
text-decoration: underline;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
.hljs-subst,
.hljs-title,
.hljs-params,
.hljs-bullet,
.hljs-formula,
.hljs-tag,
.hljs-type {
/* ignored */
}

body.theme-light {
.hljs-title.function_,
.hljs-template-variable {
color: #00738F;
}
.hljs-code,
.hljs-comment,
.hljs-quote {
color: #6e6e71;
font-style: italic;
}
.hljs-meta {
color: #836F00;
}
.hljs-symbol,
.hljs-template-tag,
.hljs-keyword,
.hljs-literal,
.hljs-name,
.hljs-built_in,
.hljs-char.escape_ {
color: #0C40C2;
}
.hljs-variable,
.hljs-property,
.hljs-attr,
.hljs-section {
color: #841191;
}
.hljs-attribute {
color: #164ad9;
}
.hljs-regexp,
.hljs-number {
color: #104BEB;
}
.hljs-link {
color: #47688a;
}
.hljs-string {
color: #008313;
}
.hljs-doctag {
text-decoration: underline;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
.hljs-subst,
.hljs-title,
.hljs-params,
.hljs-bullet,
.hljs-formula,
.hljs-tag,
.hljs-type {
/* ignored */
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading