forked from ascorbic/impala
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhead-context.tsx
More file actions
36 lines (29 loc) · 807 Bytes
/
head-context.tsx
File metadata and controls
36 lines (29 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { createContext, ReactElement, ReactNode } from "react";
class HeadProvider {
private head: React.ReactElement[] = [];
private removeTag(tag: string) {
this.head = this.head.filter((item) => item.type !== tag);
}
public addHead(head?: ReactElement | ReactElement[]) {
if (!head) {
return;
}
if (Array.isArray(head)) {
// Can't have more than one title tag
if (head.some((item) => item.type === "title")) {
this.removeTag("title");
}
this.head.push(...head);
return;
}
if (head.type === "title") {
this.removeTag("title");
}
this.head.push(head);
}
public getHead() {
return this.head;
}
}
const headProvider = new HeadProvider();
export const HeadContext = createContext(headProvider);