-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathwithClerk.tsx
More file actions
40 lines (33 loc) · 1.39 KB
/
withClerk.tsx
File metadata and controls
40 lines (33 loc) · 1.39 KB
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
37
38
39
40
import { usePortalRoot } from '@clerk/shared/react';
import type { LoadedClerk, Without } from '@clerk/shared/types';
import React from 'react';
import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
import { useAssertWrappedByClerkProvider } from '../hooks/useAssertWrappedByClerkProvider';
export const withClerk = <P extends { clerk: LoadedClerk; component?: string }>(
Component: React.ComponentType<P>,
displayNameOrOptions?: string | { component: string; renderWhileLoading?: boolean },
) => {
const passedDisplayedName =
typeof displayNameOrOptions === 'string' ? displayNameOrOptions : displayNameOrOptions?.component;
const displayName = passedDisplayedName || Component.displayName || Component.name || 'Component';
Component.displayName = displayName;
const options = typeof displayNameOrOptions === 'string' ? undefined : displayNameOrOptions;
const HOC = (props: Without<P, 'clerk'>) => {
useAssertWrappedByClerkProvider(displayName || 'withClerk');
const clerk = useIsomorphicClerkContext();
const getContainer = usePortalRoot();
if (!clerk.loaded && !options?.renderWhileLoading) {
return null;
}
return (
<Component
getContainer={getContainer}
{...(props as P)}
component={displayName}
clerk={clerk}
/>
);
};
HOC.displayName = `withClerk(${displayName})`;
return HOC;
};