Skip to content

Commit

Permalink
fix(clerk-js): Refactor isOpen prop
Browse files Browse the repository at this point in the history
fix(clerk-js): Refactor isOpen prop
  • Loading branch information
raptisj committed Sep 30, 2022
1 parent 6ae7f42 commit 044860f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
2 changes: 0 additions & 2 deletions packages/clerk-js/src/core/resources/DisplayConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export class DisplayConfig extends BaseResource implements DisplayConfigResource
afterSignOutUrl!: string;
afterSignUpUrl!: string;
afterSwitchSessionUrl!: string;
isOpen!: boolean;
applicationName!: string;
backendHost!: string;
branded!: boolean;
Expand Down Expand Up @@ -48,7 +47,6 @@ export class DisplayConfig extends BaseResource implements DisplayConfigResource
this.afterSignInUrl = data.after_sign_in_url;
this.afterSignUpUrl = data.after_sign_up_url;
this.afterSignOutUrl = data.after_sign_out_url;
this.isOpen = data.is_open;
this.afterSignOutOneUrl = data.after_sign_out_one_url;
this.afterSignOutAllUrl = data.after_sign_out_all_url;
this.afterSwitchSessionUrl = data.after_switch_session_url;
Expand Down
11 changes: 6 additions & 5 deletions packages/clerk-js/src/ui/UserButton/UserButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ import { UserButtonPopover } from './UserButtonPopover';
import { UserButtonTrigger } from './UserButtonTrigger';

const _UserButton = () => {
const { isOpen: isButtonOpenFromOutside } = useUserButtonContext();
const { isOpen: isOpenProp } = useUserButtonContext();
const { floating, reference, styles, toggle, isOpen } = usePopover({
defaultOpen: isButtonOpenFromOutside,
defaultOpen: false,
placement: 'bottom-end',
offset: 8,
isOpenProp,
});

return (
<Flow.Root flow='userButton'>
<Flex
elementDescriptor={descriptors.userButtonBox}
isOpen={isButtonOpenFromOutside || isOpen}
isOpen={isOpenProp || isOpen}
align='center'
gap={2}
>
<UserButtonTopLevelIdentifier />
<UserButtonTrigger
ref={reference}
onClick={toggle}
isOpen={isButtonOpenFromOutside || isOpen}
isOpen={isOpenProp || isOpen}
/>
<Portal>
<UserButtonPopover
isOpen={isButtonOpenFromOutside || isOpen}
isOpen={isOpenProp || isOpen}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
17 changes: 8 additions & 9 deletions packages/clerk-js/src/ui/hooks/usePopover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ import {
useFloating,
UseFloatingProps,
} from '@floating-ui/react-dom-interactions';
import React, { useEffect } from 'react';
import React, { useLayoutEffect } from 'react';

type UsePopoverProps = {
defaultOpen?: boolean;
placement?: UseFloatingProps['placement'];
offset?: Parameters<typeof offset>[0];
autoUpdate?: boolean;
isOpenProp?: boolean;
};

export const usePopover = (props: UsePopoverProps = {}) => {
const [isOpen, setIsOpen] = React.useState(props.defaultOpen || false);
const { update, reference, floating, strategy, x, y, context } = useFloating({
const { reference, floating, strategy, x, y, context } = useFloating({
open: isOpen,
onOpenChange: setIsOpen,
whileElementsMounted: props.autoUpdate === false ? undefined : autoUpdate,
placement: props.placement || 'bottom-start',
middleware: [offset(props.offset || 6), flip(), shift()],
});

useEffect(() => {
setIsOpen(props.defaultOpen || false);
}, [props.defaultOpen]);

useEffect(() => {
update();
}, []); // this is due to a positioning bug on render
useLayoutEffect(() => {
if (props.isOpenProp) {
toggle();
}
}, []);

useDismiss(context);

Expand Down
2 changes: 0 additions & 2 deletions packages/types/src/displayConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface DisplayConfigJSON {
after_sign_out_all_url: string;
after_sign_out_one_url: string;
after_sign_out_url: string;
is_open: boolean;
after_sign_up_url: string;
after_switch_session_url: string;
application_name: string;
Expand Down Expand Up @@ -38,7 +37,6 @@ export interface DisplayConfigResource extends ClerkResource {
afterSignOutUrl: string;
afterSignUpUrl: string;
afterSwitchSessionUrl: string;
isOpen?: boolean;
applicationName: string;
backendHost: string;
branded: boolean;
Expand Down

0 comments on commit 044860f

Please sign in to comment.