Skip to content
Open
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
136 changes: 39 additions & 97 deletions docs/src/app/(main)/pricing/_components/campaign-countdown.tsx
Original file line number Diff line number Diff line change
@@ -1,115 +1,57 @@
'use client';

import { cn } from '@/lib/utils';
import { useEffect, useState } from 'react';
import { type PricingInfo } from './pricing';

export function CampaignCountdown(props: {
campaign: NonNullable<PricingInfo['campaign']>;
className?: string;
}) {
const { campaign } = props;
const [timeLeft, setTimeLeft] = useState(() =>
getTimeDifference(new Date(), campaign.endDate),
);
const { campaign, className } = props;
const endTime = new Date(campaign.endDate).getTime();
const [now, setNow] = useState(() => Date.now());

useEffect(() => {
const intervalId = setInterval(() => {
setTimeLeft(getTimeDifference(new Date(), campaign.endDate));
}, 1000);

return () => {
clearInterval(intervalId);
};
}, [campaign.endDate]);
const intervalId = setInterval(() => setNow(Date.now()), 1000);
return () => clearInterval(intervalId);
}, []);

if (timeLeft.diff < 0) {
const secondsLeft = Math.floor((endTime - now) / 1000);
if (secondsLeft < 0) {
return null;
}

const timeLeft = [
[Math.floor(secondsLeft / 86400), 'd'],
[Math.floor(secondsLeft / 3600) % 24, 'h'],
[Math.floor(secondsLeft / 60) % 60, 'm'],
[secondsLeft % 60, 's'],
] as const;

return (
<div className="mx-auto w-full rounded-lg border bg-background p-8 sm:w-min sm:min-w-96">
<div className="space-y-4">
<div className="text-center">
<h2 className="text-xl font-bold sm:text-3xl sm:text-nowrap">
{campaign.name} Ends Soon
</h2>
<p className="text-sm text-muted-foreground sm:text-base">
{campaign.description}
</p>
</div>
<div className="mx-auto grid max-w-md grid-cols-4 gap-2 text-center sm:gap-4">
<div className="space-y-1">
<div
className="text-4xl font-bold text-primary sm:text-5xl"
suppressHydrationWarning
>
{timeLeft.days}
</div>
<div className="text-sm text-muted-foreground sm:text-base">
Days
</div>
</div>
<div className="space-y-1">
<div
className="text-4xl font-bold text-primary sm:text-5xl"
suppressHydrationWarning
>
{timeLeft.hours}
</div>
<div
className="text-sm text-muted-foreground sm:text-base"
suppressHydrationWarning
>
Hours
</div>
</div>
<div className="space-y-1">
<div
className="text-4xl font-bold text-primary sm:text-5xl"
suppressHydrationWarning
>
{timeLeft.minutes}
</div>
<div className="text-sm text-muted-foreground sm:text-base">
Minutes
</div>
</div>
<div className="space-y-1">
<div
className="text-4xl font-bold text-primary sm:text-5xl"
suppressHydrationWarning
>
{timeLeft.seconds}
</div>
<div className="text-sm text-muted-foreground sm:text-base">
Seconds
</div>
</div>
</div>
</div>
<div
className={cn(
'flex items-center justify-between gap-6 rounded-xl border border-site-accent/35 bg-[color-mix(in_srgb,var(--site-accent)_8%,var(--site-bg))] px-6 py-4 max-[601px]:flex-col max-[601px]:items-start max-[601px]:gap-2 max-[601px]:px-5',
className,
)}
>
<p className="text-[15px] leading-[1.6]">
<span className="font-semibold">{campaign.name}.</span>{' '}
<span className="text-site-muted">{campaign.description}</span>
</p>
<p
className="shrink-0 text-[15px] font-[550] text-site-accent tabular-nums"
suppressHydrationWarning
>
Ends in{' '}
{timeLeft.map(([value, unit]) => (
<span key={unit} className="ml-1.5">
{value}
{unit}
</span>
))}
</p>
</div>
);
}

function getTimeDifference(date1: Date, date2: Date) {
const diff = (date2.getTime() - date1.getTime()) / 1000;
let delta = Math.abs(diff);

const days = Math.floor(delta / 86400);
delta -= days * 86400;

const hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;

const minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;

const seconds = Math.floor(delta % 60);

return {
diff,
days,
hours,
minutes,
seconds,
};
}
74 changes: 74 additions & 0 deletions docs/src/app/(main)/pricing/_components/plan-tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use client';

import { cn } from '@/lib/utils';
import { Children, useId, useRef, useState, type ReactNode } from 'react';

export function PlanTabs(props: {
tabs: { name: string; price: string }[];
defaultIndex: number;
children: ReactNode;
}) {
const { tabs, children } = props;
const [active, setActive] = useState(props.defaultIndex);
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
const id = useId();

const select = (index: number) => {
const next = (index + tabs.length) % tabs.length;
setActive(next);
buttons.current[next]?.focus();
};

return (
<div>
<div className="sticky top-(--site-header-height) z-10 -mx-8 bg-site-bg/90 px-8 py-3 backdrop-blur-lg max-[601px]:-mx-5 max-[601px]:px-5">
<div
role="tablist"
aria-label="Plans"
className="grid grid-cols-3 gap-1 rounded-xl border border-site-line bg-site-panel p-1"
onKeyDown={(event) => {
if (event.key === 'ArrowRight') select(active + 1);
if (event.key === 'ArrowLeft') select(active - 1);
}}
>
{tabs.map((tab, index) => (
<button
key={tab.name}
ref={(node) => {
buttons.current[index] = node;
}}
type="button"
role="tab"
id={`${id}-tab-${index}`}
aria-selected={index === active}
aria-controls={`${id}-panel-${index}`}
tabIndex={index === active ? 0 : -1}
onClick={() => setActive(index)}
className={cn(
'flex flex-col items-center rounded-[9px] p-2 leading-[1.35]',
index === active
? 'bg-site-bg text-site-text shadow-[0_0_0_1px_var(--site-line),0_1px_3px_#0000000f]'
: 'text-site-muted hover:text-site-text',
)}
>
<span className="text-sm font-semibold">{tab.name}</span>
<span className="text-xs tabular-nums">{tab.price}/mo</span>
</button>
))}
</div>
</div>
{Children.toArray(children).map((panel, index) => (
<div
key={index}
role="tabpanel"
id={`${id}-panel-${index}`}
aria-labelledby={`${id}-tab-${index}`}
hidden={index !== active}
className="mt-1"
>
{panel}
</div>
))}
</div>
);
}
Loading
Loading