Skip to content

Commit 2442423

Browse files
committed
Port buildContext to TS; use it in dropdown-context
1 parent 5fae368 commit 2442423

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
3+
export default function buildContext<P, V>({
4+
defaultValue = {} as V,
5+
useContextValue
6+
}: {
7+
defaultValue?: V;
8+
useContextValue: (p: P) => V;
9+
}) {
10+
const Context = React.createContext(defaultValue);
11+
12+
function useContext() {
13+
return React.useContext(Context);
14+
}
15+
16+
function ContextProvider({
17+
children,
18+
contextValueParameters = undefined as P
19+
}: React.PropsWithChildren<{
20+
contextValueParameters: P;
21+
}>) {
22+
const value = useContextValue(contextValueParameters);
23+
24+
if (value === undefined) {
25+
return null;
26+
}
27+
28+
return <Context.Provider value={value}>{children}</Context.Provider>;
29+
}
30+
31+
return {useContext, ContextProvider};
32+
}

src/app/components/shell/header/menus/dropdown-context.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import buildContext from '~/components/jsx-helpers/build-context';
2+
import buildContext from '~/components/jsx-helpers/build-context-ts';
33

44
function useContextValue({prefix} = {prefix: 'menulabel'}) {
55
const [activeDropdown, setActiveDropdown] = React.useState({});

0 commit comments

Comments
 (0)