From 1ae8730d6829102df64af27e94e5c977f14106e9 Mon Sep 17 00:00:00 2001 From: Thomas Cristina de Carvalho Date: Fri, 8 Dec 2023 00:31:20 -0500 Subject: [PATCH] Fix ShopPayButton flickering --- .../blocks/AddToCartButtonBlock.tsx | 8 ++-- .../app/components/blocks/PriceBlock.tsx | 4 +- .../blocks/ShopifyDescriptionBlock.tsx | 8 ++-- .../components/blocks/ShopifyTitleBlock.tsx | 6 +-- .../app/components/product/ProductDetails.tsx | 41 ++++++++++++------- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/templates/hydrogen-theme/app/components/blocks/AddToCartButtonBlock.tsx b/templates/hydrogen-theme/app/components/blocks/AddToCartButtonBlock.tsx index 9d59162..cc360f8 100644 --- a/templates/hydrogen-theme/app/components/blocks/AddToCartButtonBlock.tsx +++ b/templates/hydrogen-theme/app/components/blocks/AddToCartButtonBlock.tsx @@ -9,9 +9,11 @@ import type {loader} from '~/routes/($locale).products.$productHandle'; import {ProductForm} from '../product/ProductForm'; -export function AddToCartButtonBlock( - props: InferType, -) { +export type AddToCartButtonBlockProps = InferType< + typeof ADD_TO_CART_BUTTON_BLOCK +>; + +export function AddToCartButtonBlock(props: AddToCartButtonBlockProps) { const loaderData = useLoaderData(); const variantsPromise = loaderData.variants; diff --git a/templates/hydrogen-theme/app/components/blocks/PriceBlock.tsx b/templates/hydrogen-theme/app/components/blocks/PriceBlock.tsx index 69c8c21..7d5e9be 100644 --- a/templates/hydrogen-theme/app/components/blocks/PriceBlock.tsx +++ b/templates/hydrogen-theme/app/components/blocks/PriceBlock.tsx @@ -9,7 +9,9 @@ import type {loader} from '~/routes/($locale).products.$productHandle'; import {VariantPrice} from '../product/VariantPrice'; -export function PriceBlock(props: InferType) { +export type PriceBlockProps = InferType; + +export function PriceBlock(props: PriceBlockProps) { const loaderData = useLoaderData(); const variantsPromise = loaderData.variants; diff --git a/templates/hydrogen-theme/app/components/blocks/ShopifyDescriptionBlock.tsx b/templates/hydrogen-theme/app/components/blocks/ShopifyDescriptionBlock.tsx index b5a851e..003f64f 100644 --- a/templates/hydrogen-theme/app/components/blocks/ShopifyDescriptionBlock.tsx +++ b/templates/hydrogen-theme/app/components/blocks/ShopifyDescriptionBlock.tsx @@ -5,9 +5,11 @@ import {useProduct} from '@shopify/hydrogen-react'; import type {SHOPIFY_DESCRIPTION_BLOCK} from '~/qroq/blocks'; -export function ShopifyDescriptionBlock( - props: InferType, -) { +export type ShopifyDescriptionBlockProps = InferType< + typeof SHOPIFY_DESCRIPTION_BLOCK +>; + +export function ShopifyDescriptionBlock(props: ShopifyDescriptionBlockProps) { const {product} = useProduct() as {product: ProductQuery['product']}; return product ? ( diff --git a/templates/hydrogen-theme/app/components/blocks/ShopifyTitleBlock.tsx b/templates/hydrogen-theme/app/components/blocks/ShopifyTitleBlock.tsx index 44a7025..4307c29 100644 --- a/templates/hydrogen-theme/app/components/blocks/ShopifyTitleBlock.tsx +++ b/templates/hydrogen-theme/app/components/blocks/ShopifyTitleBlock.tsx @@ -5,9 +5,9 @@ import {useProduct} from '@shopify/hydrogen-react'; import type {SHOPIFY_TITLE_BLOCK} from '~/qroq/blocks'; -export function ShopifyTitleBlock( - props: InferType, -) { +export type ShopifyTitleBlockProps = InferType; + +export function ShopifyTitleBlock(props: ShopifyTitleBlockProps) { const {product} = useProduct() as {product: ProductQuery['product']}; return

{product?.title}

; diff --git a/templates/hydrogen-theme/app/components/product/ProductDetails.tsx b/templates/hydrogen-theme/app/components/product/ProductDetails.tsx index 9a8bb2a..abda0c0 100644 --- a/templates/hydrogen-theme/app/components/product/ProductDetails.tsx +++ b/templates/hydrogen-theme/app/components/product/ProductDetails.tsx @@ -1,5 +1,10 @@ import {PortableText} from '@portabletext/react'; +import {useMemo} from 'react'; +import type {AddToCartButtonBlockProps} from '../blocks/AddToCartButtonBlock'; +import type {PriceBlockProps} from '../blocks/PriceBlock'; +import type {ShopifyDescriptionBlockProps} from '../blocks/ShopifyDescriptionBlock'; +import type {ShopifyTitleBlockProps} from '../blocks/ShopifyTitleBlock'; import type {ProductInformationSectionProps} from '../sections/ProductInformationSection'; import {AddToCartButtonBlock} from '../blocks/AddToCartButtonBlock'; @@ -8,24 +13,30 @@ import {ShopifyDescriptionBlock} from '../blocks/ShopifyDescriptionBlock'; import {ShopifyTitleBlock} from '../blocks/ShopifyTitleBlock'; export function ProductDetails(props: {data: ProductInformationSectionProps}) { + const components = useMemo( + () => ({ + types: { + addToCartButton: (props: {value: AddToCartButtonBlockProps}) => { + return ; + }, + price: (props: {value: PriceBlockProps}) => ( + + ), + shopifyDescription: (props: {value: ShopifyDescriptionBlockProps}) => ( + + ), + shopifyTitle: (props: {value: ShopifyTitleBlockProps}) => ( + + ), + }, + }), + [], + ); + return (
{props.data.richtext && ( - { - return ; - }, - price: ({value}) => , - shopifyDescription: ({value}) => ( - - ), - shopifyTitle: ({value}) => , - }, - }} - value={props.data.richtext} - /> + )}
);