-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAnchorIdParts.tsx
33 lines (28 loc) · 1.22 KB
/
AnchorIdParts.tsx
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
import { FernNavigation } from "@fern-api/fdr-sdk";
import { useDeepCompareMemoize } from "@fern-ui/react-commons";
import { PropsWithChildren, ReactElement, createContext, useContext } from "react";
import { getAnchorId } from "../../util/anchor";
const SlugContext = createContext<FernNavigation.Slug>(FernNavigation.Slug(""));
const AnchorIdPartsContext = createContext<string[]>([]);
export function SlugProvider({ slug, children }: PropsWithChildren<{ slug: FernNavigation.Slug }>): ReactElement {
return <SlugContext.Provider value={slug}>{children}</SlugContext.Provider>;
}
export function AnchorProvider({
children,
parts,
}: PropsWithChildren<{ parts: string | readonly string[] }>): ReactElement {
const parentParts = useContext(AnchorIdPartsContext);
const childParts = Array.isArray(parts) ? parts : [parts];
return (
<AnchorIdPartsContext.Provider value={useDeepCompareMemoize([...parentParts, ...childParts])}>
{children}
</AnchorIdPartsContext.Provider>
);
}
export function useSlug(): FernNavigation.Slug {
return useContext(SlugContext);
}
export function useAnchorId(): string {
const parts = useContext(AnchorIdPartsContext);
return getAnchorId(parts);
}