Skip to content

Commit

Permalink
Changes variants to use router replace (vercel#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotny authored and bc-alexsaiannyi committed Dec 7, 2023
1 parent 01da47f commit 72d28dc
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions components/product/variant-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
VercelProductVariant as ProductVariant
} from 'lib/bigcommerce/types';
import { createUrl } from 'lib/utils';
import Link from 'next/link';
import { usePathname, useSearchParams } from 'next/navigation';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';

type Combination = {
id: string;
Expand All @@ -22,6 +21,7 @@ export function VariantSelector({
options: ProductOption[];
variants: ProductVariant[];
}) {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
const hasNoOptionsOrJustOneOption =
Expand Down Expand Up @@ -79,17 +79,14 @@ export function VariantSelector({
// The option is active if it's in the url params.
const isActive = searchParams.get(optionNameLowerCase) === value;

// You can't disable a link, so we need to render something that isn't clickable.
const DynamicTag = isAvailableForSale ? Link : 'p';
const dynamicProps = {
...(isAvailableForSale && { scroll: false })
};

return (
<DynamicTag
<button
key={value}
aria-disabled={!isAvailableForSale}
href={optionUrl}
disabled={!isAvailableForSale}
onClick={() => {
router.replace(optionUrl, { scroll: false });
}}
title={`${option.name} ${value}${!isAvailableForSale ? ' (Out of Stock)' : ''}`}
className={clsx(
'flex min-w-[48px] items-center justify-center rounded-full border bg-neutral-100 px-2 py-1 text-sm dark:border-neutral-800 dark:bg-neutral-900',
Expand All @@ -101,10 +98,9 @@ export function VariantSelector({
!isAvailableForSale
}
)}
{...dynamicProps}
>
{value}
</DynamicTag>
</button>
);
})}
</dd>
Expand Down

0 comments on commit 72d28dc

Please sign in to comment.