Skip to content

Commit

Permalink
fix(clerk-js): Refactor defaultOpen prop
Browse files Browse the repository at this point in the history
  • Loading branch information
raptisj committed Oct 3, 2022
1 parent 044860f commit 1d7b0a9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
11 changes: 5 additions & 6 deletions packages/clerk-js/src/ui/UserButton/UserButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@ import { UserButtonPopover } from './UserButtonPopover';
import { UserButtonTrigger } from './UserButtonTrigger';

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

return (
<Flow.Root flow='userButton'>
<Flex
elementDescriptor={descriptors.userButtonBox}
isOpen={isOpenProp || isOpen}
isOpen={isOpen}
align='center'
gap={2}
>
<UserButtonTopLevelIdentifier />
<UserButtonTrigger
ref={reference}
onClick={toggle}
isOpen={isOpenProp || isOpen}
isOpen={isOpen}
/>
<Portal>
<UserButtonPopover
isOpen={isOpenProp || isOpen}
isOpen={isOpen}
close={toggle}
ref={floating}
style={{ ...styles }}
Expand Down
11 changes: 5 additions & 6 deletions packages/clerk-js/src/ui/hooks/usePopover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ import {
useFloating,
UseFloatingProps,
} from '@floating-ui/react-dom-interactions';
import React, { useLayoutEffect } from 'react';
import React, { useEffect } 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 { reference, floating, strategy, x, y, context } = useFloating({
const { update, 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()],
});

useLayoutEffect(() => {
if (props.isOpenProp) {
toggle();
useEffect(() => {
if (props.defaultOpen) {
update();
}
}, []);

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/uiComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Portal extends React.PureComponent<MountProps, {}> {
private portalRef = React.createRef<HTMLDivElement>();

componentDidUpdate(prevProps: Readonly<MountProps>) {
if (prevProps.props.appearance !== this.props.props.appearance || prevProps.props !== this.props.props) {
if (prevProps.props.appearance !== this.props.props.appearance) {
this.props.updateProps({ node: this.portalRef.current, props: this.props.props });
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ export type UserButtonProps = {
*/
showName?: boolean;
/**
Controls programmatically if the UserButton component is open or not
Controls the default state of the UserButton
*/
isOpen?: boolean;
defaultOpen?: boolean;
/**
* Full URL or path to navigate after sign out is complete
*/
Expand Down

0 comments on commit 1d7b0a9

Please sign in to comment.