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

Example to show settings all varialbes at :root explicitly #492

Closed
wants to merge 20 commits into from
Closed
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
2 changes: 1 addition & 1 deletion public/_/readthedocs-addons.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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
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
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
7 changes: 7 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ export const ASCIIDOCTOR = "asciidoctor";
export const JEKYLL = "jekyll";
export const DOCSIFY = "docsify";
export const ANTORA = "antora";
export const MDBOOK = "mdbook";
export const FALLBACK_DOCTOOL = "fallback";

// Known documentation tools themes
export const SPHINX_ALABASTER = "alabaster";
export const SPHINX_FURO = "furo";
export const SPHINX_READTHEDOCS = "readthedocs";
export const SPHINX_IMMATERIAL = "immaterial";
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(`
@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);
}
}
}
77 changes: 36 additions & 41 deletions src/flyout.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
/* New */
/* Flyout styles */

@layer defaults {
:root {
--readthedocs-flyout-font-family: var(--readthedocs-font-family);
--readthedocs-flyout-font-size: var(--readthedocs-font-size);
--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 {
position: fixed;
Expand All @@ -7,6 +27,7 @@
height: auto;
max-height: calc(100% - 100px);
overflow-y: auto;
line-height: var(--readthedocs-flyout-line-height);
}

.container.bottom-right {
Expand All @@ -29,25 +50,11 @@
top: 50px;
}

:host {
--addons-flyout-font-size: var(
--readthedocs-flyout-font-size,
var(--readthedocs-font-size, 0.8rem)
);
}

:host > div {
font-family: var(
--readthedocs-flyout-font-family,
"Lato",
"proxima-nova",
"Helvetica Neue",
"Arial",
"sans-serif"
);
font-size: var(--addons-flyout-font-size);
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);
z-index: 3000;
padding: 0 10px;
overflow-y: auto;
Expand All @@ -59,27 +66,21 @@ header {
justify-content: space-between;
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: 0;
padding: 10px 0;
}

header > span {
color: var(--readthedocs-flyout-current-version-color, #27ae60);
font-size: var(
--readthedocs-flyout-header-font-size,
calc(var(--addons-flyout-font-size) * 1.125)
);
color: var(--readthedocs-flyout-current-version-color);
font-size: var(--readthedocs-flyout-header-font-size);
margin-left: 10px;
}

header > span svg.icon {
color: rgb(128, 128, 128);
height: var(
--readthedocs-flyout-header-font-size,
calc(var(--addons-flyout-font-size) * 1.125)
);
height: var(--readthedocs-flyout-header-font-size);
padding-right: 5px;
vertical-align: middle;
}
Expand Down Expand Up @@ -107,25 +108,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)
);
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 @@ -148,10 +143,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);
}
9 changes: 5 additions & 4 deletions src/flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { default as objectPath } from "object-path";

import styleSheet from "./flyout.css";
import { AddonBase, addUtmParameters, getLinkWithFilename } from "./utils";
import { SPHINX, MKDOCS_MATERIAL } from "./constants";
import {
EVENT_READTHEDOCS_SEARCH_SHOW,
EVENT_READTHEDOCS_FLYOUT_HIDE,
Expand All @@ -31,9 +32,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 Expand Up @@ -310,11 +314,8 @@ export class FlyoutElement extends LitElement {
return nothing;
}

const classes = { floating: this.floating, container: true };
classes[this.position] = true;

return html`
<div class=${classMap(classes)}>
<div class=${classMap(this.classes)}>
${this.renderHeader()}
<main class=${classMap({ closed: !this.opened })}>
${this.renderLanguages()} ${this.renderVersions()}
Expand Down
25 changes: 25 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import * as filetreediff from "./filetreediff";
import * as customscript from "./customscript";
import { default as objectPath } from "object-path";
import {
docTool,
domReady,
isEmbedded,
IS_PRODUCTION,
setupLogging,
getMetadataValue,
} from "./utils";

import { defaultStyleSheet } from "./defaults.js";

export function setup() {
const addons = [
flyout.FlyoutAddon,
Expand Down Expand Up @@ -50,6 +53,28 @@ export function setup() {
}
}

// Apply fixes to variables for individual documentation tools
const elementHtml = document.querySelector("html");
if (elementHtml) {
// Inject styles at the parent DOM to set variables at :root
document.adoptedStyleSheets = [defaultStyleSheet];

// If we detect a documentation tool, set attributes on :root to allow
// for CSS selectors to utilize these values.
if (docTool.documentationTool) {
elementHtml.setAttribute(
"data-readthedocs-tool",
docTool.documentationTool,
);
}
if (docTool.documentationTheme) {
elementHtml.setAttribute(
"data-readthedocs-tool-theme",
docTool.documentationTheme,
);
}
}

return getReadTheDocsConfig(sendUrlParam);
})
.then((config) => {
Expand Down
Loading