'use client'; import React, { useMemo, useCallback } from 'react'; import { VariantOption, VariantType } from '@akinon/next/types'; import { usePathname, useSearchParams } from 'next/navigation'; import clsx from 'clsx'; import { useRouter } from '@akinon/next/hooks'; type VariantProps = { className?: string; onChange?: (option: VariantOption) => void; preventDefaultClick?: boolean; } & VariantType; export const Variant = ({ attribute_key, attribute_name, options, preventDefaultClick, onChange, className }: VariantProps) => { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); // This is a workaround for the fact that we can't use the useSearchParams set method because of this is not implemented in next.js yet. So we have to use the URLSearchParams's set method. const params = useMemo( () => new URLSearchParams(searchParams.toString()), [searchParams] ); const handleClick = useCallback( (option: VariantOption) => { if (onChange) { onChange(option); } if (preventDefaultClick) return; params.set(attribute_key, option.value); router.push(`${pathname}?${params.toString()}`); }, [onChange, preventDefaultClick, params, attribute_key, pathname, router] ); // Base button classes that don't change with theme editor const baseButtonClasses = 'transition-colors duration-200 tracking-[0.39px]'; return (

{attribute_name}

{options.map((option, i) => ( ))}
); }; export default Variant;