Skip to content

Commit 399137d

Browse files
committed
reactify the approach
1 parent 31115ef commit 399137d

File tree

5 files changed

+14
-41
lines changed

5 files changed

+14
-41
lines changed

packages/clerk-js/src/ui/components/PricingTable/PricingTable.tsx

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,11 @@ import type { CommercePlanResource, CommerceSubscriptionPlanPeriod, PricingTable
33
import { useEffect, useMemo, useState } from 'react';
44

55
import { Flow } from '@/ui/customizables/Flow';
6-
import { useFlowMetadata } from '@/ui/elements/contexts';
76

87
import { usePaymentMethods, usePlans, usePlansContext, usePricingTableContext, useSubscription } from '../../contexts';
98
import { PricingTableDefault } from './PricingTableDefault';
109
import { PricingTableMatrix } from './PricingTableMatrix';
1110

12-
function SyncRootElement() {
13-
const clerk = useClerk();
14-
const { data: subscription, isLoading: isSubscriptionLoading } = useSubscription();
15-
const { data: plans, isLoading: isPlansLoading } = usePlans();
16-
const { rootElement } = useFlowMetadata();
17-
18-
useEffect(() => {
19-
if (isSubscriptionLoading || isPlansLoading) {
20-
return;
21-
}
22-
const isReady = clerk.isSignedIn ? !!subscription : plans.length > 0;
23-
24-
if (isReady) {
25-
rootElement?.setAttribute('data-ready', 'true');
26-
}
27-
}, [subscription, plans]);
28-
29-
return null;
30-
}
31-
3211
const PricingTableRoot = (props: PricingTableProps) => {
3312
const clerk = useClerk();
3413
const { mode = 'mounted', signInMode = 'redirect' } = usePricingTableContext();
@@ -95,11 +74,11 @@ const PricingTableRoot = (props: PricingTableProps) => {
9574
return (
9675
<Flow.Root
9776
flow='pricingTable'
77+
isFlowReady={clerk.isSignedIn ? !!subscription : plans.length > 0}
9878
sx={{
9979
width: '100%',
10080
}}
10181
>
102-
<SyncRootElement />
10382
{mode !== 'modal' && (props as any).layout === 'matrix' ? (
10483
<PricingTableMatrix
10584
plans={plansToRender}

packages/clerk-js/src/ui/customizables/Flow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { descriptors } from './index';
1010

1111
type FlowRootProps = React.PropsWithChildren & FlowMetadata & { sx?: ThemableCssProp };
1212

13-
const Root = (props: FlowRootProps) => {
13+
const Root = (props: FlowRootProps & { isFlowReady?: boolean }) => {
1414
return (
1515
<FlowMetadataProvider flow={props.flow}>
1616
<InternalThemeProvider>

packages/clerk-js/src/ui/elements/InvisibleRootBox.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React from 'react';
22

33
import { makeCustomizable } from '../customizables/makeCustomizable';
4-
import { useFlowMetadata } from './contexts';
54

65
type RootBoxProps = React.PropsWithChildren<{ className: string }>;
76

8-
const _InvisibleRootBox = React.memo((props: RootBoxProps) => {
7+
const _InvisibleRootBox = React.memo((props: RootBoxProps & { isFlowReady?: boolean }) => {
98
const [showSpan, setShowSpan] = React.useState(true);
10-
const { setRootElement } = useFlowMetadata();
119
const parentRef = React.useRef<HTMLElement | null>(null);
1210

1311
React.useEffect(() => {
@@ -18,8 +16,10 @@ const _InvisibleRootBox = React.memo((props: RootBoxProps) => {
1816
if (showSpan) {
1917
setShowSpan(false);
2018
}
21-
parent.className = props.className;
22-
}, [props.className]);
19+
20+
parent.setAttribute('class', props.className);
21+
parent.setAttribute('data-component-status', props.isFlowReady ? 'ready' : 'awaiting-data');
22+
}, [props.className, props.isFlowReady]);
2323

2424
return (
2525
<>
@@ -28,7 +28,6 @@ const _InvisibleRootBox = React.memo((props: RootBoxProps) => {
2828
<span
2929
ref={el => {
3030
parentRef.current = el ? el.parentElement : parentRef.current;
31-
setRootElement(parentRef.current);
3231
}}
3332
aria-hidden
3433
style={{ display: 'none' }}

packages/clerk-js/src/ui/elements/contexts/index.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,11 @@ export type FlowMetadata = {
125125
| 'accountSwitcher';
126126
};
127127

128-
const [FlowMetadataCtx, useFlowMetadata] = createContextAndHook<
129-
FlowMetadata & {
130-
rootElement: HTMLElement | null;
131-
setRootElement: React.Dispatch<React.SetStateAction<HTMLElement | null>>;
132-
}
133-
>('FlowMetadata');
128+
const [FlowMetadataCtx, useFlowMetadata] = createContextAndHook<FlowMetadata>('FlowMetadata');
134129

135130
export const FlowMetadataProvider = (props: React.PropsWithChildren<FlowMetadata>) => {
136131
const { flow, part } = props;
137-
const [rootElement, setRootElement] = React.useState<HTMLElement | null>(null);
138-
const value = React.useMemo(
139-
() => ({ value: { ...props, rootElement, setRootElement } }),
140-
[flow, part, rootElement, setRootElement],
141-
);
132+
const value = React.useMemo(() => ({ value: { ...props } }), [flow, part]);
142133
return <FlowMetadataCtx.Provider value={value}>{props.children}</FlowMetadataCtx.Provider>;
143134
};
144135

packages/react/src/components/uiComponents.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,11 @@ export const Waitlist = withClerk(
577577

578578
export const PricingTable = withClerk(
579579
({ clerk, component, fallback, ...props }: WithClerkProp<PricingTableProps & FallbackProp>) => {
580-
const mountingStatus = useWaitForComponentMount(component, { selector: '[data-ready="true"]' });
580+
const mountingStatus = useWaitForComponentMount(component, {
581+
// This attribute is added to the PricingTable root element after we've successfully fetched the plans asynchronously.
582+
selector: '[data-component-status="ready"]',
583+
});
584+
console.log('mountingStatus', mountingStatus);
581585
const shouldShowFallback = mountingStatus === 'rendering' || !clerk.loaded;
582586

583587
const rendererRootProps = {

0 commit comments

Comments
 (0)