Skip to content

Commit abecafc

Browse files
Navbar Wrapper Component Added which can able to wrap all the component along with its script and css in one component
1 parent a4cc329 commit abecafc

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

quartz/components/NavbarWrapper.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { concatenateResources } from "../util/resources"
2+
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
3+
import style from "./styles/navbarwrapper.scss"
4+
interface Options {
5+
components: QuartzComponent[]
6+
}
7+
8+
export default ((opts: Options) => {
9+
const NavbarWrapper: QuartzComponent = (props: QuartzComponentProps) => {
10+
return (
11+
<div class="navbar-wrapper">
12+
{opts?.components.map((Component) => {
13+
return <Component {...props}></Component>
14+
})}
15+
</div>
16+
)
17+
}
18+
NavbarWrapper.afterDOMLoaded = concatenateResources(
19+
...opts.components.map((c) => c.afterDOMLoaded),
20+
)
21+
NavbarWrapper.beforeDOMLoaded = concatenateResources(
22+
...opts.components.map((c) => c.beforeDOMLoaded),
23+
)
24+
NavbarWrapper.css = concatenateResources(...opts.components.map((c) => c.css), style)
25+
return NavbarWrapper
26+
}) satisfies QuartzComponentConstructor<Options>

quartz/components/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Breadcrumbs from "./Breadcrumbs"
2222
import Comments from "./Comments"
2323
import NavigationLinks from "./NavigationLinks"
2424
import Drawer from "./Drawer"
25+
import NavbarWrapper from "./NavbarWrapper"
2526

2627
export {
2728
ArticleTitle,
@@ -39,6 +40,7 @@ export {
3940
Explorer,
4041
TagList,
4142
Graph,
43+
NavbarWrapper,
4244
Backlinks,
4345
Search,
4446
Footer,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.navbar-wrapper {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
gap: 10px;
6+
}

quartz/plugins/emitters/componentResources.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ function getComponentResources(ctx: BuildCtx): ComponentResources {
3636
afterDOMLoaded: new Set<string>(),
3737
}
3838

39+
function normalizeResource(resource: string | string[] | undefined): string[] {
40+
if (!resource) return []
41+
if (Array.isArray(resource)) return resource
42+
return [resource]
43+
}
44+
3945
for (const component of allComponents) {
4046
const { css, beforeDOMLoaded, afterDOMLoaded } = component
41-
if (css) {
42-
componentResources.css.add(css)
43-
}
44-
if (beforeDOMLoaded) {
45-
componentResources.beforeDOMLoaded.add(beforeDOMLoaded)
46-
}
47-
if (afterDOMLoaded) {
48-
componentResources.afterDOMLoaded.add(afterDOMLoaded)
49-
}
47+
const normalizedCss = normalizeResource(css)
48+
const normalizedBeforeDOMLoaded = normalizeResource(beforeDOMLoaded)
49+
const normalizedAfterDOMLoaded = normalizeResource(afterDOMLoaded)
50+
51+
normalizedCss.forEach((c) => componentResources.css.add(c))
52+
normalizedBeforeDOMLoaded.forEach((b) => componentResources.beforeDOMLoaded.add(b))
53+
normalizedAfterDOMLoaded.forEach((a) => componentResources.afterDOMLoaded.add(a))
5054
}
5155

5256
return {

quartz/util/resources.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ export interface StaticResources {
6363
css: CSSResource[]
6464
js: JSResource[]
6565
}
66+
67+
export type StringResource = string | string[] | undefined
68+
export function concatenateResources(...resources: StringResource[]): StringResource {
69+
return resources
70+
.filter((resource): resource is string | string[] => resource !== undefined)
71+
.flat()
72+
}

0 commit comments

Comments
 (0)