Skip to content

Commit 1275884

Browse files
authored
Make wco-docs-page component and move nav HTML generation into it (#1412)
- Make a wco-docs-page component - Move nav HTML generation into wco-nav-page component - Fix hydration of docs page Also: - Rename hydrate.js to hydrate-support.js - Move hydrate-support directly into base template
1 parent f3199dd commit 1275884

File tree

11 files changed

+113
-78
lines changed

11 files changed

+113
-78
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import {customElement} from 'lit/decorators.js';
8+
import {WCONavPage} from './wco-nav-page.js';
9+
10+
export type {NavEntry} from './wco-nav-page.js';
11+
12+
@customElement('wco-docs-page')
13+
export class WCODocsPage extends WCONavPage {}
14+
15+
declare global {
16+
interface HTMLElementTagNameMap {
17+
'wco-docs-page': WCODocsPage;
18+
}
19+
}

packages/site-client/src/components/wco-nav-page.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {html, css} from 'lit';
8-
import {customElement} from 'lit/decorators.js';
7+
import {html, css, TemplateResult} from 'lit';
8+
import {customElement, property} from 'lit/decorators.js';
9+
import {classMap} from 'lit/directives/class-map.js';
910
import {WCOPage} from './wco-page.js';
1011
import './wco-top-bar.js';
1112

13+
export interface NavEntry {
14+
key: string;
15+
url: string;
16+
children?: NavEntry[];
17+
}
18+
1219
/**
1320
* A page with left and right side navigation areas.
1421
*/
@@ -36,16 +43,45 @@ export class WCONavPage extends WCOPage {
3643
align-items: center;
3744
border: solid 1px red;
3845
}
46+
47+
nav li.active {
48+
font-weight: bold;
49+
}
3950
`,
4051
];
4152

53+
@property({attribute: false})
54+
navEntries?: NavEntry[];
55+
4256
protected override renderMain() {
4357
return html`
44-
<nav id="main-outline"><slot name="outline"></slot></nav>
58+
<nav id="main-outline">${this._renderNav(this.navEntries)}</nav>
4559
<article>${this.renderContent()}</article>
4660
<nav id="right-nav">[Page ToC]</nav>
4761
`;
4862
}
63+
64+
private _renderNav(
65+
entries: NavEntry[] | undefined
66+
): TemplateResult | undefined {
67+
if (entries === undefined || entries.length === 0) {
68+
return;
69+
}
70+
return html`<ul>
71+
${entries.map(
72+
(entry) => html`<li
73+
class="${classMap({
74+
active: entry.url === globalThis.location.pathname,
75+
})}"
76+
>
77+
${entry.url
78+
? html`<a href="${entry.url}">${entry.key}</a>`
79+
: entry.key}
80+
${this._renderNav(entry.children)}
81+
</li>`
82+
)}
83+
</ul>`;
84+
}
4985
}
5086

5187
declare global {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import {NavEntry} from '../components/wco-nav-page.js';
8+
9+
const data = (globalThis as unknown as {__ssrData: [NavEntry[]]}).__ssrData;
10+
11+
const navEntries = data[0];
12+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
13+
const el = document.querySelector('wco-docs-page')!;
14+
el.navEntries = navEntries;
15+
el.removeAttribute('defer-hydration');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import '../components/wco-docs-page.js';
8+
import type {NavEntry} from '../components/wco-docs-page.js';
9+
import {html} from 'lit';
10+
import {unsafeHTML} from 'lit/directives/unsafe-html.js';
11+
12+
export const renderDocsPage = (content: string, navEntries: NavEntry[]) =>
13+
html`<wco-docs-page .navEntries=${navEntries}
14+
>${unsafeHTML(content)}</wco-docs-page
15+
>`;

packages/site-content/site/_includes/layouts/docs.11ty.cjs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
const {navToHTML} = require('./nav.cjs');
8-
97
module.exports = {
108
async render(data) {
11-
const {renderPage, html, unsafeHTML} = await import(
12-
'../../../templates/lib/base.js'
13-
);
9+
const {renderPage} = await import('../../../templates/lib/base.js');
1410
await import(
1511
'@webcomponents/internal-site-client/lib/components/wco-nav-page.js'
1612
);
13+
const {renderDocsPage} = await import(
14+
'@webcomponents/internal-site-client/lib/entrypoints/docs.js'
15+
);
1716

1817
// Set location because wco-nav-bar reads pathname from it. URL isn't
1918
// exactly a Location, but it's close enough for read-only uses
20-
globalThis.location = new URL('http://localhost:5450/docs/');
21-
22-
const navigationEntries = this.eleventyNavigation(data.collections.all);
23-
24-
const navigationHTML = navToHTML(
25-
navigationEntries,
26-
{slot: 'outline'},
27-
data.page
19+
globalThis.location = new URL(
20+
`http://localhost:5450${data.page.url || '/'}`
2821
);
2922

23+
const navEntries = this.eleventyNavigation(data.collections.all);
24+
3025
return [
31-
...renderPage({
32-
...data,
33-
content: html`<wco-nav-page>
34-
${unsafeHTML(navigationHTML)} ${unsafeHTML(data.content)}
35-
</wco-nav-page>`,
36-
}),
26+
...renderPage(
27+
{
28+
...data,
29+
content: renderDocsPage(data.content, navEntries),
30+
initialData: [navEntries],
31+
initScript: '/js/docs-hydrate.js',
32+
},
33+
{
34+
// We need to defer elements from hydrating so that we can
35+
// manually provide data to the element in docs-hydrate.js
36+
deferHydration: true,
37+
}
38+
),
3739
].join('');
3840
},
3941
};

packages/site-content/site/_includes/layouts/nav.cjs

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
22
"layout": "layouts/docs.11ty.cjs",
3-
"scripts": [
4-
"../js/homepage.js"
5-
]
3+
"scripts": ["/js/docs.js"]
64
}

packages/site-content/templates/src/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function* renderPage(
3737
href="https://fonts.googleapis.com/css?family=Material+Icons&display=block"
3838
rel="stylesheet"
3939
/>
40+
<script type="module" src="/js/hydrate-support.js"></script>
4041
<!-- TODO (justinfagnani): only add this in dev. In prod we should have
4142
compiled any process access out. DO_NOT_LAUNCH -->
4243
<script>

packages/site-server/src/lib/catalog/routes/catalog/catalog-route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const handleCatalogRoute = async (
2727
context.body = Readable.from(
2828
renderPage({
2929
title: `Web Components Catalog`,
30-
scripts: ['/js/hydrate.js', '/js/catalog.js'],
30+
scripts: ['/js/catalog.js'],
3131
content: html`<wco-catalog-page></wco-catalog-page>`,
3232
})
3333
);

0 commit comments

Comments
 (0)