Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-chairs-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/astro": patch
---

Add an Astro component and a React UI Component for Google One Tap.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
import type { GoogleOneTapProps } from "@clerk/types";
type Props = GoogleOneTapProps

import InternalUIComponentRenderer from './InternalUIComponentRenderer.astro'
---

<InternalUIComponentRenderer {...Astro.props} component="google-one-tap" />
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
interface Props {
[key: string]: unknown
component: 'sign-in' | 'sign-up' | 'organization-list' | 'organization-profile' | 'organization-switcher' | 'user-button' | 'user-profile'
component: 'sign-in' | 'sign-up' | 'organization-list' | 'organization-profile' | 'organization-switcher' | 'user-button' | 'user-profile' | 'google-one-tap'
Comment thread
wobsoriano marked this conversation as resolved.
}

import { customAlphabet, urlAlphabet } from "nanoid";
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/astro-components/interactive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export { default as OrganizationProfile } from './OrganizationProfile.astro';
export { default as OrganizationSwitcher } from './OrganizationSwitcher.astro';
// @ts-ignore
export { default as OrganizationList } from './OrganizationList.astro';
// @ts-ignore
export { default as GoogleOneTap } from './GoogleOneTap.astro';
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mountAllClerkAstroJSComponents = () => {
'user-profile': 'mountUserProfile',
'sign-in': 'mountSignIn',
'sign-up': 'mountSignUp',
'google-one-tap': 'openGoogleOneTap',
} as const;

Object.entries(mountFns).forEach(([category, mountFn]) => {
Expand Down
58 changes: 50 additions & 8 deletions packages/astro/src/client/react/uiComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
GoogleOneTapProps,
OrganizationListProps,
OrganizationProfileProps,
OrganizationSwitcherProps,
Expand All @@ -7,22 +8,40 @@ import type {
UserButtonProps,
UserProfileProps,
} from '@clerk/types';
import React, { createElement } from 'react';
import React from 'react';

import { withClerk, type WithClerkProp } from './utils';

export interface OpenProps {
open: ((props: any) => void) | undefined;
close: (() => void) | undefined;
props?: any;
}

export interface MountProps {
mount?: (node: HTMLDivElement, props: any) => void;
unmount?: (node: HTMLDivElement) => void;
mount: ((node: HTMLDivElement, props: any) => void) | undefined;
unmount: ((node: HTMLDivElement) => void) | undefined;
updateProps?: (props: any) => void;
props?: any;
customPagesPortals?: any[];
// TODO: Support custom pages
// customPagesPortals?: any[];
}

class Portal extends React.PureComponent<MountProps> {
const isMountProps = (props: any): props is MountProps => {
return 'mount' in props;
};

const isOpenProps = (props: any): props is OpenProps => {
return 'open' in props;
};

class Portal extends React.PureComponent<MountProps | OpenProps> {
private portalRef = React.createRef<HTMLDivElement>();

componentDidUpdate(prevProps: Readonly<MountProps>) {
if (!isMountProps(prevProps) || !isMountProps(this.props)) {
return;
}
if (
prevProps.props.appearance !== this.props.props.appearance ||
prevProps.props?.customPages?.length !== this.props.props?.customPages?.length
Expand All @@ -36,21 +55,34 @@ class Portal extends React.PureComponent<MountProps> {

componentDidMount() {
if (this.portalRef.current) {
this.props.mount?.(this.portalRef.current, this.props.props);
if (isMountProps(this.props)) {
this.props.mount?.(this.portalRef.current, this.props.props);
}

if (isOpenProps(this.props)) {
this.props.open?.(this.props.props);
}
}
}

componentWillUnmount() {
if (this.portalRef.current) {
this.props.unmount?.(this.portalRef.current);
if (isMountProps(this.props)) {
this.props.unmount?.(this.portalRef.current);
}
if (isOpenProps(this.props)) {
this.props.close?.();
}
}
}

render() {
return (
<>
<div ref={this.portalRef} />
{this.props?.customPagesPortals?.map((portal, index) => createElement(portal, { key: index }))}
{/*TODO: Support custom pages*/}
{/*{isMountProps(this.props) &&*/}
{/* this.props?.customPagesPortals?.map((portal, index) => createElement(portal, { key: index }))}*/}
</>
);
}
Expand Down Expand Up @@ -132,3 +164,13 @@ export const OrganizationList = withClerk(({ clerk, ...props }: WithClerkProp<Or
/>
);
}, 'OrganizationList');

export const GoogleOneTap = withClerk(({ clerk, ...props }: WithClerkProp<GoogleOneTapProps>) => {
return (
<Portal
open={clerk?.openGoogleOneTap}
close={clerk?.closeGoogleOneTap}
props={props}
/>
);
}, 'GoogleOneTap');