Skip to content

Commit

Permalink
Updates Image component for latest Hydrogen versions, fixes add to ca…
Browse files Browse the repository at this point in the history
…rt button text shift, adds tel url support for markdown (#48)

- Removes the customization within `Image` component. After recent version updates to `Image` from `@shopify/hydrogen-react`, the custom `srcSetOptions` passed in and the `isStatic` logic is no longer needed. This will address any lower res images becoming visibly blurry after upgrading to latest versions
- Fixes the "Added To Cart" misalignment in the `AddToCart` button
- Adds a url transform for `tel:` links for support in `Markdown`
  • Loading branch information
jeremyagabriel authored Sep 27, 2024
1 parent 9a48af7 commit c4c1abe
Show file tree
Hide file tree
Showing 16 changed files with 20 additions and 58 deletions.
1 change: 0 additions & 1 deletion app/components/Account/Order/OrderItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export function OrderItem({item}: {item: OrderLineItem}) {
: PRODUCT_IMAGE_ASPECT_RATIO
}
width="48"
isStatic
/>

<div className="flex flex-1 flex-col items-start gap-2">
Expand Down
4 changes: 2 additions & 2 deletions app/components/AddToCart/AddToCart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function AddToCart({
{buttonText}
</span>

{isAdding && !isAdded && (
{isAdding && (
<LoadingDots
status="Adding to cart"
withAbsolutePosition
Expand All @@ -77,7 +77,7 @@ export function AddToCart({
)}

{isAdded && (
<span aria-live="assertive" role="status">
<span aria-live="assertive" className="absolute-center" role="status">
Added To Cart
</span>
)}
Expand Down
1 change: 0 additions & 1 deletion app/components/Cart/CartLine/CartLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export function CartLine({closeCart, line}: CartLineProps) {
: PRODUCT_IMAGE_ASPECT_RATIO
}
width="88"
isStatic
className="bg-offWhite"
/>
</Link>
Expand Down
1 change: 0 additions & 1 deletion app/components/Cart/CartUpsell/CartUpsellItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function CartUpsellItem({
: PRODUCT_IMAGE_ASPECT_RATIO
}
width="40"
isStatic
/>
</Link>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export function CollectionFilterOption({
}}
aspectRatio="1/1"
width="24"
isStatic
className="media-fill"
/>
)}
Expand Down
1 change: 0 additions & 1 deletion app/components/Header/Menu/DesktopMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export function DesktopMenu({
}}
aspectRatio="16/9"
width="400"
isStatic
/>

<p className="mt-3 text-sm">{caption}</p>
Expand Down
41 changes: 2 additions & 39 deletions app/components/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,35 @@ import type {AspectRatio} from '~/lib/types';

type ImageProps = React.ComponentProps<typeof HydrogenImage> & {
aspectRatio?: AspectRatio | undefined;
isStatic?: boolean;
withLoadingAnimation?: boolean;
};

const getPxWidthNum = (width: string | number | undefined) => {
if (!width) return undefined;
if (typeof width === 'number') return width;
if (width.endsWith('rem')) return Number(width.replace('rem', '')) * 16;
if (width.endsWith('em')) return Number(width.replace('em', '')) * 16;
return Number(width.replace('px', ''));
};

export const Image = forwardRef(
(
{
aspectRatio,
className,
data,
width: passedWidth,
isStatic, // sets only 1 srcSet option that is 3x scale
width,
withLoadingAnimation = true, // adds a loading shimmer animation if data.url is undefined
...props
}: ImageProps,
ref: React.Ref<HTMLImageElement>,
) => {
let width = passedWidth;
const isRelativeWidth =
typeof width === 'string' &&
(width.endsWith('%') || width.endsWith('vw'));
if (!isRelativeWidth) {
width = getPxWidthNum(passedWidth);
}
const isPxWidth = typeof width === 'number';

return data?.url ? (
<HydrogenImage
ref={ref}
data={data}
aspectRatio={aspectRatio}
width={width}
className={`bg-offWhite object-cover ${className}`}
srcSetOptions={
isStatic && isPxWidth
? {
intervals: 1,
startingWidth: Number(width) * 3,
incrementSize: Number(width) * 3,
placeholderWidth: Number(width) * 3,
}
: {
intervals: 12,
startingWidth: 200,
incrementSize: 250,
placeholderWidth: 100,
}
}
{...props}
/>
) : (
<div
ref={ref}
className={`relative overflow-hidden bg-offWhite ${className}`}
style={{
aspectRatio,
width: isPxWidth ? `${width}px` : width || '100%',
}}
style={{aspectRatio, width}}
>
{withLoadingAnimation && <div className="loading-shimmer opacity-60" />}
</div>
Expand Down
13 changes: 11 additions & 2 deletions app/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {forwardRef} from 'react';
import ReactMarkdown from 'react-markdown';
import type {Components} from 'react-markdown';
import ReactMarkdown, {defaultUrlTransform} from 'react-markdown';
import remarkGfm from 'remark-gfm';
import remarkBreaks from 'remark-breaks';
import type {Components} from 'react-markdown';

interface MarkdownProps {
centerAllText?: boolean;
Expand All @@ -19,13 +19,22 @@ export const Markdown = forwardRef(
const hTextAlign = centerAllText
? '[&>h2]:text-center [&>h3]:text-center [&>h4]:text-center [&>h5]:text-center [&>h6]:text-center'
: '';

const urlTransform = (url: string) => {
if (url.startsWith('tel:')) {
return url;
}
return defaultUrlTransform(url);
};

return (
<div
ref={ref}
className={`[&>:first-child]:mt-0 [&>:last-child]:mb-0 [&_a]:underline [&_h1]:mb-6 [&_h1]:text-center [&_h2]:mb-5 [&_h2]:mt-8 [&_h3]:mb-4 [&_h3]:mt-6 [&_h4]:my-4 [&_h5]:mb-4 [&_h5]:mt-2 [&_h6]:mb-4 [&_li>p]:mb-0 [&_li]:mb-2 [&_ol>li]:list-decimal [&_ol]:mb-4 [&_ol]:pl-8 [&_p]:mb-4 [&_table]:relative [&_table]:mb-4 [&_table]:w-full [&_table]:table-fixed [&_table]:border-collapse [&_table]:overflow-x-auto [&_table]:border [&_table]:border-border [&_td]:border [&_td]:border-border [&_td]:p-3 [&_td]:text-center [&_td]:align-top [&_th]:border [&_th]:border-border [&_th]:px-2 [&_th]:py-1.5 [&_thead]:bg-offWhite [&_ul>li]:list-disc [&_ul]:mb-4 [&_ul]:pl-8 ${pTextAlign} ${hTextAlign}`}
>
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkBreaks]}
urlTransform={urlTransform}
components={components}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function ProductMediaThumbnail({
aspectRatio="1/1"
width="80"
loading={index < 6 ? 'eager' : 'lazy'}
isStatic
/>

{mediaContentType === 'VIDEO' && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function InnerColorOptionValue({
aspectRatio="1/1"
width="32"
className="media-fill"
isStatic
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function ColorVariantOption({
width="24"
aspectRatio="1/1"
className="media-fill"
isStatic
/>
)}

Expand Down
4 changes: 2 additions & 2 deletions app/sections/BuildYourOwnBundle/BYOBAddToCart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function BYOBAddToCart({
{`Add To Cart${total ? ` - ${total}` : ''}`}
</span>

{isAdding && !isAdded && (
{isAdding && (
<LoadingDots
status="Adding to cart"
withAbsolutePosition
Expand All @@ -97,7 +97,7 @@ export function BYOBAddToCart({
)}

{isAdded && (
<span aria-live="assertive" role="status">
<span aria-live="assertive" className="absolute-center" role="status">
Added To Cart
</span>
)}
Expand Down
1 change: 0 additions & 1 deletion app/sections/IconRow/IconRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export function IconRow({cms}: {cms: IconRowCms}) {
className="bg-transparent"
aspectRatio="1/1"
width="48"
isStatic
/>
)}

Expand Down
1 change: 0 additions & 1 deletion app/sections/PressSlider/PressSliderThumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export function PressSliderThumb({
className={`bg-transparent transition ${
isActive ? 'opacity-100' : 'opacity-30'
}`}
isStatic
/>
)}
</button>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hydrogen-starter",
"private": true,
"sideEffects": false,
"version": "1.9.0",
"version": "1.9.1",
"scripts": {
"dev": "shopify hydrogen dev --port 8080",
"build": "shopify hydrogen build",
Expand Down

0 comments on commit c4c1abe

Please sign in to comment.