Skip to content

Commit 2d35edf

Browse files
authored
feat(theme): add ability to inject data attributes from query-string - possibility to create an iframe/embed variant of a page (#9028)
1 parent 444c157 commit 2d35edf

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

packages/docusaurus-theme-classic/src/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const ContextReplacementPlugin = requireFromDocusaurusCore(
2626
// Need to be inlined to prevent dark mode FOUC
2727
// Make sure the key is the same as the one in `/theme/hooks/useTheme.js`
2828
const ThemeStorageKey = 'theme';
29+
// Support for ?docusaurus-theme=dark
2930
const ThemeQueryStringKey = 'docusaurus-theme';
31+
// Support for ?docusaurus-data-mode=embed&docusaurus-data-myAttr=42
32+
const DataQueryStringPrefixKey = 'docusaurus-data-';
3033

3134
const noFlashColorMode = ({
3235
defaultMode,
@@ -42,19 +45,15 @@ const noFlashColorMode = ({
4245
}
4346
4447
function getQueryStringTheme() {
45-
var theme = null;
4648
try {
47-
theme = new URLSearchParams(window.location.search).get('${ThemeQueryStringKey}')
49+
return new URLSearchParams(window.location.search).get('${ThemeQueryStringKey}')
4850
} catch(e) {}
49-
return theme;
5051
}
5152
5253
function getStoredTheme() {
53-
var theme = null;
5454
try {
55-
theme = localStorage.getItem('${ThemeStorageKey}');
55+
return localStorage.getItem('${ThemeStorageKey}');
5656
} catch (err) {}
57-
return theme;
5857
}
5958
6059
var initialTheme = getQueryStringTheme() || getStoredTheme();
@@ -77,6 +76,21 @@ const noFlashColorMode = ({
7776
}
7877
})();`;
7978

79+
/* language=js */
80+
const DataAttributeQueryStringInlineJavaScript = `
81+
(function() {
82+
try {
83+
const entries = new URLSearchParams(window.location.search).entries();
84+
for (var [searchKey, value] of entries) {
85+
if (searchKey.startsWith('${DataQueryStringPrefixKey}')) {
86+
var key = searchKey.replace('${DataQueryStringPrefixKey}',"data-")
87+
document.documentElement.setAttribute(key, value);
88+
}
89+
}
90+
} catch(e) {}
91+
})();
92+
`;
93+
8094
// Duplicated constant. Unfortunately we can't import it from theme-common, as
8195
// we need to support older nodejs versions without ESM support
8296
// TODO: import from theme-common once we only support Node.js with ESM support
@@ -205,6 +219,7 @@ export default function themeClassic(
205219
tagName: 'script',
206220
innerHTML: `
207221
${noFlashColorMode(colorMode)}
222+
${DataAttributeQueryStringInlineJavaScript}
208223
${announcementBar ? AnnouncementBarInlineJavaScript : ''}
209224
`,
210225
},

website/docs/styling-layout.mdx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ description: A Docusaurus site is a pre-rendered single-page React application.
33
---
44

55
import ColorGenerator from '@site/src/components/ColorGenerator';
6+
import IframeWindow from '@site/src/components/BrowserWindow/IframeWindow';
67

78
# Styling and Layout
89

@@ -133,8 +134,33 @@ It is possible to initialize the Docusaurus theme directly from a `docusaurus-th
133134

134135
Examples:
135136

136-
- [`https://docusaurus.io/?docusaurus-theme=dark`](https://docusaurus.io/?docusaurus-theme=dark)
137-
- [`https://docusaurus.io/docs/configuration?docusaurus-theme=light`](https://docusaurus.io/docs/configuration?docusaurus-theme=light)
137+
<IframeWindow url="/docs/?docusaurus-theme=dark" />
138+
139+
<IframeWindow url="/docs/configuration?docusaurus-theme=light" />
140+
141+
:::
142+
143+
### Data Attributes {#data-attributes}
144+
145+
It is possible to inject `<html>` data attributes with query string parameters following the `docusaurus-data-<key>` pattern. This gives you the flexibility to style a page differently based on the query string.
146+
147+
For example, let's render one of our pages with a red border and no navbar:
148+
149+
```css title="/src/css/custom.css"
150+
html[data-navbar='false'] .navbar {
151+
display: none;
152+
}
153+
154+
html[data-red-border] div#__docusaurus {
155+
border: red solid thick;
156+
}
157+
```
158+
159+
<IframeWindow url="/docs/?docusaurus-data-navbar=false&docusaurus-data-red-border" />
160+
161+
:::tip Iframe Mode
162+
163+
If you plan to embed some Docusaurus pages on another site though an iframe, it can be useful to create an alternative display mode and use iframe urls such as `https://mysite.com/docs/myDoc?docusaurus-data-mode=iframe`. It is your responsibility to provide the additional styles and decide which UI elements you want to keep or hide.
138164

139165
:::
140166

website/src/css/custom.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,11 @@ div[class^='announcementBar_'] {
230230
[data-theme='dark'] [data-rmiz-modal-overlay='visible'] {
231231
background-color: rgba(0 0 0 / 95%);
232232
}
233+
234+
html[data-navbar='false'] .navbar {
235+
display: none;
236+
}
237+
238+
html[data-red-border] div#__docusaurus {
239+
border: red solid thick;
240+
}

0 commit comments

Comments
 (0)