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

Try CSS variable again #545

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
00f72e0
Add styles based on the documentation tool
humitos Dec 16, 2024
24d7869
Use a reactive property to add specific classes
humitos Dec 17, 2024
f3477b7
Use `tool=sphinx` `tool-theme=furo` to add CSS rules
humitos Dec 18, 2024
053d931
Remove unused code
humitos Dec 18, 2024
8c337ea
Update furo font size
humitos Dec 18, 2024
9d08961
Update MkDocs Material font size
humitos Dec 18, 2024
12057bf
Useful for Jupyter Book and others
humitos Dec 18, 2024
28bb43c
Style for Antora
humitos Dec 18, 2024
434f187
Update snapshots to match `tool=` and `tool-theme=` attrs
humitos Dec 18, 2024
718b52a
Better comment
humitos Dec 18, 2024
531c7e2
Rename attribute with a `data-` prefix
humitos Jan 8, 2025
7f099cc
Adjust style a little more
humitos Jan 8, 2025
fe1319f
Show example of setting attributes and variables at parent DOM
agjohnson Jan 8, 2025
d6e86c6
Show example of setting attributes and variables at parent DOM (#484)
agjohnson Jan 9, 2025
1446007
Small tweaks to font sizes
humitos Jan 9, 2025
ed87f06
Fix some tests
humitos Jan 9, 2025
4d2a219
Handle error when checking mdBook
humitos Jan 9, 2025
6b5db39
More fixes
agjohnson Jan 9, 2025
12f1b3f
Update font sizes
agjohnson Jan 9, 2025
5d2a33f
Working example of CSS layer defaults
agjohnson Jan 13, 2025
1b2ce13
Merge branch 'main' into agj/css-variable-root-defaults
agjohnson Feb 19, 2025
a348a9e
Hack not needed because this is a native CSSStyleSheet
agjohnson Feb 19, 2025
d6c139b
Bad merge?
agjohnson Feb 19, 2025
047c286
Drop unused doctools.css
agjohnson Feb 19, 2025
f847c0e
Tune var usage and defaults
agjohnson Feb 19, 2025
69dfc0a
Lint
agjohnson Feb 19, 2025
5844acf
Relint
agjohnson Feb 19, 2025
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
2 changes: 1 addition & 1 deletion public/_/readthedocs-addons.json
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is for illustration only.

Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"load_when_embedded": false
},
"ethicalads": {
"ad_free": false,
"ad_free": true,
"enabled": true,
"publisher": "readthedocs",
"campaign_types": ["community", "house", "paid"],
Expand Down
5 changes: 5 additions & 0 deletions public/global.css
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also for illustration only

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Theme defaults? */
:root {
--readthedocs-font-size: 10px;
--readthedocs-flyout-font-size: 30px;
}
1 change: 1 addition & 0 deletions public/index.html
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And also not needed

Copy link
Member

Choose a reason for hiding this comment

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

We can probably create another HTML file to keep in the repository and show this as example 👍🏼

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
);
</script>
<meta name="readthedocs-resolver-filename" content="/index.html" />
<link rel="stylesheet" href="global.css" />
</head>
<body>
<h1 id="documentation-addons">Documentation Addons</h1>
Expand Down
8 changes: 4 additions & 4 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as customscript from "./customscript";
import * as application from "./application";
import { default as objectPath } from "object-path";

import doctoolsStyleSheet from "./doctools.css";
import { defaultStyleSheet } from "./defaults.js";

export class AddonsApplication {
constructor() {
Expand Down Expand Up @@ -125,9 +125,9 @@ export class AddonsApplication {
const elementHtml = document.querySelector("html");
if (elementHtml) {
// Inject styles at the parent DOM to set variables at :root
let styleSheet = doctoolsStyleSheet;
if (doctoolsStyleSheet instanceof CSSResult) {
styleSheet = doctoolsStyleSheet.styleSheet;
let styleSheet = defaultStyleSheet;
if (defaultStyleSheet instanceof CSSResult) {
styleSheet = defaultStyleSheet.styleSheet;
}
document.adoptedStyleSheets = [styleSheet];

Expand Down
55 changes: 55 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import styleSheetFlyout from "./flyout.css";

import { CSSResult } from "lit";

// We use a native construct here as Lit's CSSResult is largely read only.
export const defaultStyleSheet = new CSSStyleSheet();

/*
* Specific styles based on documentation tools and themes
*
* Usage of `@layer` at-rule pushes this rules down a step in
* precedence/priority. This allows a user `:root` rule to override these
* values.
**/
defaultStyleSheet.replaceSync(`
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 like too much that we are writing all these CSS in a JS file. Can't we just have a defaults.css file and import it here instead?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I don't really understand why you migrated from doctools.css to default.js. You had to change the code in application.js as well -- why not just keep the doctools.css (or defaults.css if you want) but keeping the CSS extension and importing the CSS from application.js as we were doing?

@layer defaults {
:root[data-readthedocs-tool="mkdocs-material"] {
--readthedocs-font-size: 0.58rem;
--readthedocs-flyout-font-size: 0.58rem;
}

:root[data-readthedocs-tool="antora"] {
--readthedocs-font-size: 0.7rem;
--readthedocs-flyout-font-size: 0.7rem;
}

:root[data-readthedocs-tool="mdbook"] {
--readthedocs-font-size: 1.3rem;
--readthedocs-flyout-font-size: 1.3rem;
}

:root[data-readthedocs-tool="sphinx"][data-readthedocs-tool-theme="furo"] {
--readthedocs-font-size: 0.75rem;
--readthedocs-flyout-font-size: 0.75rem;
}

:root[data-readthedocs-tool="sphinx"][data-readthedocs-tool-theme="immaterial"] {
--readthedocs-font-size: 0.58rem;
--readthedocs-flyout-font-size: 0.58rem;
}
}
`);

const styleSheets = [styleSheetFlyout];

for (let styleSheet of styleSheets) {
if (styleSheet instanceof CSSResult) {
styleSheet = styleSheet.styleSheet;
}
for (const rule of styleSheet.cssRules) {
if (rule instanceof CSSLayerBlockRule && rule.name == "defaults") {
defaultStyleSheet.insertRule(rule.cssText);
}
}
}
71 changes: 35 additions & 36 deletions src/flyout.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
/* Flyout styles */

:host {
/* These variables are used more than once, use a local variable so we can set
* a default in this addon */
--addons-flyout-font-size: var(
--readthedocs-flyout-font-size,
var(--readthedocs-font-size, 0.8rem)
);
--addons-flyout-line-height: var(--readthedocs-flyout-line-height, 1.25em);
@layer defaults {
:root {
--readthedocs-flyout-font-family: var(--readthedocs-font-family);
--readthedocs-flyout-font-size: var(--readthedocs-font-size);
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

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

It seems we need to define these in defaults.js, right? I mean, --readthedocs-font-*. I didn't find where they are defined.

--readthedocs-flyout-line-height: 1.25em;
--readthedocs-flyout-header-font-size: 1.125em;
--readthedocs-flyout-dt-font-size: 1.125em;
--readthedocs-flyout-dd-font-size: 1.125em;
--readthedocs-flyout-line-height: 1.25em;

--readthedocs-flyout-color: rgb(128, 128, 128);
--readthedocs-flyout-background-color: rgb(39, 39, 37);
--readthedocs-flyout-current-version-color: #27ae60;
--readthedocs-flyout-section-heading-color: rgb(128, 128, 128);
--readthedocs-flyout-item-link-color: rgb(252, 252, 252);
--readthedocs-flyout-divider-color: #413d3d;
--readthedocs-flyout-link-color: rgb(42, 128, 185);
}
}

.container {
Expand All @@ -17,7 +27,7 @@
height: auto;
max-height: calc(100% - 100px);
overflow-y: auto;
line-height: var(--addons-flyout-line-height);
line-height: var(--readthedocs-flyout-line-height);
}

.container.bottom-right {
Expand All @@ -41,18 +51,11 @@
}

:host > div {
font-family: var(
--readthedocs-flyout-font-family,
"Lato",
"proxima-nova",
"Helvetica Neue",
"Arial",
"sans-serif"
);
font-size: var(--addons-flyout-font-size);
line-height: 1.2em;
color: var(--readthedocs-flyout-color, rgb(128, 128, 128));
background-color: var(--readthedocs-flyout-background-color, rgb(39, 39, 37));
font-family: var(--readthedocs-flyout-font-family);
font-size: var(--readthedocs-flyout-font-size);
color: var(--readthedocs-flyout-color);
background-color: var(--readthedocs-flyout-background-color);
line-height: var(--readthedocs-flyout-line-height, 1.2em);
z-index: 3000;
padding: 0 10px;
overflow-y: auto;
Expand All @@ -64,7 +67,7 @@ header {
gap: 1em;
align-items: center;
cursor: pointer;
background-color: var(--readthedocs-flyout-background-color, rgb(39, 39, 37));
background-color: var(--readthedocs-flyout-background-color);
position: sticky;
top: 0px;
font-size: var(--readthedocs-flyout-header-font-size, 1.25em);
Expand All @@ -80,7 +83,9 @@ header > img.logo {
}

header > span {
color: var(--readthedocs-flyout-current-version-color, #27ae60);
color: var(--readthedocs-flyout-current-version-color);
font-size: var(--readthedocs-flyout-header-font-size);
margin-left: 10px;
Copy link
Member

Choose a reason for hiding this comment

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

This property is new. Is this correct?

/* Total height of 1 + 0.75 + 0.75 = 2.5em */
padding: 0.75em 0em;
line-height: 1em;
Expand All @@ -105,7 +110,7 @@ header > span:first-of-type {

header > span svg.icon {
color: rgb(128, 128, 128);
height: 1em;
height: var(--readthedocs-flyout-header-font-size);
padding-right: 0.25em;
vertical-align: middle;
}
Expand All @@ -126,25 +131,19 @@ dl {
}

dl > dt {
font-size: var(
--readthedocs-flyout-dt-font-size,
calc(var(--addons-flyout-font-size) * 1.125)
);
color: var(--readthedocs-flyout-section-heading-color, rgb(128, 128, 128));
font-size: var(--readthedocs-flyout-dt-font-size);
color: var(--readthedocs-flyout-section-heading-color);
}

dl > dd {
display: inline-block;
margin: 0;
font-size: var(
--readthedocs-flyout-dd-font-size,
calc(var(--addons-flyout-font-size) * 1.125)
);
Comment on lines -139 to -142
Copy link
Member

Choose a reason for hiding this comment

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

We are missing the calculated value here.

font-size: var(--readthedocs-flyout-dd-font-size);
}

dd a {
text-decoration: none;
color: var(--readthedocs-flyout-item-link-color, rgb(252, 252, 252));
color: var(--readthedocs-flyout-item-link-color);
padding: 6px;
display: inline-block;
}
Expand All @@ -167,10 +166,10 @@ hr {
padding: 0;
border-top-width: 1px;
border-top-style: solid;
border-top-color: var(--readthedocs-flyout-divider-color, #413d3d);
border-top-color: var(--readthedocs-flyout-divider-color);
}

small a {
text-decoration: none;
color: var(--readthedocs-flyout-link-color, rgb(42, 128, 185));
color: var(--readthedocs-flyout-link-color);
}
3 changes: 3 additions & 0 deletions src/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ export class FlyoutElement extends LitElement {
super();

this.config = null;
this.classes = {};
this.opened = false;
this.floating = true;
this.position = "bottom-right";
this.classes = { floating: this.floating, container: true };
this.classes[this.position] = true;
this.readthedocsLogo = READTHEDOCS_LOGO;
}

Expand Down