Skip to content

Commit 3086a9d

Browse files
committed
fix(preact): add <HeadProvider />
1 parent fad6ec2 commit 3086a9d

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

packages/preact/head.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { Head } from "./dist/head";
1+
export { Head, HeadProvider } from "./dist/head";

packages/preact/src/head-context.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext, VNode } from "preact";
22

3-
class HeadProvider {
3+
export class HeadManager {
44
private head: VNode<any>[] = [];
55

66
private removeTag(tag: string) {
@@ -31,6 +31,6 @@ class HeadProvider {
3131
}
3232
}
3333

34-
const headProvider = new HeadProvider();
34+
const defaultHeadProvider = new HeadManager();
3535

36-
export const HeadContext = createContext(headProvider);
36+
export const HeadContext = createContext(defaultHeadProvider);

packages/preact/src/head.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useContext } from "preact/hooks";
1+
import { useContext, useRef } from "preact/hooks";
22
import type { FunctionComponent, VNode } from "preact";
3-
import { HeadContext } from "./head-context";
3+
import { HeadManager, HeadContext } from "./head-context";
44

55
interface HeadProps {
66
title?: string;
@@ -15,3 +15,17 @@ export const Head: FunctionComponent<HeadProps> = ({ children, title }) => {
1515
}
1616
return null;
1717
};
18+
19+
20+
export const HeadProvider: FunctionComponent = ({ children }) => {
21+
const headProvider = useRef<HeadManager>();
22+
if (headProvider.current == null) {
23+
headProvider.current = new HeadManager();
24+
}
25+
26+
return (
27+
<HeadContext.Provider value={headProvider.current}>
28+
{children}
29+
</HeadContext.Provider>
30+
)
31+
}

0 commit comments

Comments
 (0)