-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathSignOutButton.tsx
30 lines (24 loc) · 1.11 KB
/
SignOutButton.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
import type { SignOutOptions } from '@clerk/types';
import React from 'react';
import type { WithClerkProp } from './utils';
import { assertSingleChild, normalizeWithDefaultValue, safeExecute, withClerk } from './utils';
export type SignOutButtonProps = SignOutOptions & {
children?: React.ReactNode;
};
export const SignOutButton = withClerk(
({ clerk, children, ...props }: React.PropsWithChildren<WithClerkProp<SignOutButtonProps>>) => {
const { redirectUrl = '/', sessionId, ...rest } = props;
children = normalizeWithDefaultValue(children, 'Sign out');
const child = assertSingleChild(children)('SignOutButton');
const clickHandler = () => clerk?.signOut({ redirectUrl, sessionId });
const wrappedChildClickHandler: React.MouseEventHandler = async e => {
if (child && typeof child === 'object' && 'props' in child) {
await safeExecute(child.props.onClick)(e);
}
return clickHandler();
};
const childProps = { ...rest, onClick: wrappedChildClickHandler };
return React.cloneElement(child as React.ReactElement<unknown>, childProps);
},
'SignOutButton',
);