Skip to content

Commit

Permalink
chore: Removes next/image dependency from ui/component SKUMatrixSideb…
Browse files Browse the repository at this point in the history
…ar (#2639)

## What's the purpose of this pull request?

We should not use next/image dependency in our components package.

note: We agreed that this component does not fit within our components
library, we should move it to core.

## How it works?

Adds `ImageComponent` that can use to render the image. Then we can use
the next/image in core.

## How to test it?

1. run the application locally or use this
[preview](https://starter-371nj2xzn-vtex.vercel.app/headphone-default-99988229/p)
2. go to Headphone Default plp -> check if the SkuMatrixSidebar image is
rendered as expected using next/image

<img width="500" alt="image"
src="https://github.com/user-attachments/assets/af6cc6e2-3879-4d64-bef6-f9ae2556ee15"
/>

<img width="500" alt="image"
src="https://github.com/user-attachments/assets/c84d5d3d-7fff-4b00-954b-5eb5fcc5d28a"
/>

### Starters Deploy Preview

[WIP]

## References


[SFS-2025](https://vtex-dev.atlassian.net/browse/SFS-2025?atlOrigin=eyJpIjoiYmMxNzE0YzAzZDFmNDNkMDg5ZjY1ZmU5MDZjOTE3MzEiLCJwIjoiaiJ9)

[SFS-2025]:
https://vtex-dev.atlassian.net/browse/SFS-2025?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
hellofanny authored Jan 29, 2025
1 parent 6fea4f1 commit 9d5231c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
35 changes: 25 additions & 10 deletions packages/components/src/organisms/SKUMatrix/SKUMatrixSidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Image from 'next/image'
/* eslint-disable @next/next/no-img-element */
import type { FunctionComponent } from 'react'
import React, { useMemo } from 'react'
import { Badge, Button, QuantitySelector, Skeleton } from '../..'
import Price, { PriceFormatter } from '../../atoms/Price'
import Icon from '../../atoms/Icon'
import Price, { PriceFormatter } from '../../atoms/Price'
import { useFadeEffect, useSKUMatrix, useUI } from '../../hooks'
import {
Table,
Expand All @@ -12,7 +13,6 @@ import {
TableRow,
} from '../../molecules/Table'
import SlideOver, { SlideOverHeader, SlideOverProps } from '../SlideOver'

interface VariationProductColumn {
name: string
additionalColumns: Array<{ label: string; value: string }>
Expand All @@ -24,8 +24,15 @@ interface VariationProductColumn {
quantitySelector: number
}

const ImageComponentFallback: SKUMatrixSidebarProps['ImageComponent'] = ({
src,
alt,
...otherProps
}) => <img src={src} alt={alt} {...otherProps} />

// TODO: Moves this component to the `@faststore/core` package. We decided that it doesn't make sense to have this component in the library since it's vey specific to the SKU Matrix feature.
export interface SKUMatrixSidebarProps
extends Omit<SlideOverProps, 'isOpen' | 'setIsOpen' | "fade"> {
extends Omit<SlideOverProps, 'isOpen' | 'setIsOpen' | 'fade'> {
/**
* Title for the SKUMatrixSidebar component.
*/
Expand All @@ -51,18 +58,28 @@ export interface SKUMatrixSidebarProps
* Check if some result is still loading before render the result.
*/
loading?: boolean
/**
* Function that returns a React component that will be used to render images.
*/
ImageComponent: FunctionComponent<{
src: string
alt: string
width?: number
height?: number
}>
}

function SKUMatrixSidebar({
direction = 'rightSide',
title,
overlayProps,
direction = 'rightSide',
size = 'partial',
children,
columns,
buyProps: { onClick: buyButtonOnClick, ...buyProps },
loading,
formatter,
ImageComponent = ImageComponentFallback,
buyProps: { onClick: buyButtonOnClick, ...buyProps },
overlayProps,
...otherProps
}: SKUMatrixSidebarProps) {
const {
Expand Down Expand Up @@ -177,11 +194,9 @@ function SKUMatrixSidebar({
{allVariantProducts.map((variantProduct) => (
<TableRow key={`${variantProduct.name}-${variantProduct.id}`}>
<TableCell data-fs-sku-matrix-sidebar-cell-image align="left">
<Image
<ImageComponent
src={variantProduct.image.url}
alt={variantProduct.image.alternateName}
width={48}
height={48}
/>
{variantProduct.name}
</TableCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import { gql } from '@generated/gql'
import { useBuyButton } from 'src/sdk/cart/useBuyButton'
import { usePDP } from 'src/sdk/overrides/PageProvider'
import { useAllVariantProducts } from 'src/sdk/product/useAllVariantProducts'
import { Image } from '../Image'

interface SKUMatrixProps extends UISKUMatrixSidebarProps {}

const ImageComponent: UISKUMatrixSidebarProps['ImageComponent'] = ({
src,
alt,
...otherProps
}) => <Image src={src} alt={alt} width={48} height={48} {...otherProps} />

function SKUMatrixSidebar(props: SKUMatrixProps) {
const {
data: { product },
Expand Down Expand Up @@ -76,6 +83,7 @@ function SKUMatrixSidebar(props: SKUMatrixProps) {
buyProps={buyProps}
title={product.isVariantOf.name ?? ''}
loading={isValidating}
ImageComponent={ImageComponent}
{...props}
/>
)
Expand Down
55 changes: 27 additions & 28 deletions packages/ui/src/components/organisms/SKUMatrix/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
// --------------------------------------------------------

// Default properties

// Background
--fs-sku-matrix-sidebar-bkg-color : var(--fs-color-body-bkg);
--fs-sku-matrix-sidebar-bkg-color : var(--fs-color-body-bkg);

// Title
--fs-sku-matrix-sidebar-title-size : var(--fs-text-size-6);
--fs-sku-matrix-sidebar-title-text-weight : var(--fs-text-weight-semibold);

--fs-sku-matrix-sidebar-title-size : var(--fs-text-size-6);
--fs-sku-matrix-sidebar-title-text-weight : var(--fs-text-weight-semibold);
// Cell
--fs-sku-matrix-sidebar-table-cell-font-size : var(--fs-text-size-tiny);
--fs-sku-matrix-sidebar-table-cell-text-weight : var(--fs-text-weight-medium);
--fs-sku-matrix-sidebar-table-cell-font-size : var(--fs-text-size-tiny);
--fs-sku-matrix-sidebar-table-cell-text-weight : var(--fs-text-weight-medium);

// Image
--fs-sku-matrix-sidebar-table-cell-image-width : var(--fs-spacing-7); // 48px
--fs-sku-matrix-sidebar-table-cell-image-border-radius : var(--fs-border-radius);

// Partial
--fs-sku-matrix-slide-over-partial-gap : calc(2 * var(--fs-grid-padding));
--fs-sku-matrix-slide-over-partial-width-mobile : calc(100vw - var(--fs-sku-matrix-slide-over-partial-gap));
--fs-sku-matrix-slide-over-partial-gap : calc(2 * var(--fs-grid-padding));
--fs-sku-matrix-slide-over-partial-width-mobile : calc(100vw - var(--fs-sku-matrix-slide-over-partial-gap));

// --------------------------------------------------------
// Structural Styles
Expand All @@ -34,8 +35,8 @@
flex-shrink: 0;
padding: var(--fs-spacing-3) var(--fs-spacing-8);
padding-bottom: 0;
@include media("<tablet") {

@include media('<tablet') {
padding: var(--fs-spacing-3);
padding-bottom: 0;
}
Expand All @@ -48,16 +49,16 @@
padding: var(--fs-spacing-8);
padding-bottom: 0;

@include media("<tablet") {
@include media('<tablet') {
padding: var(--fs-spacing-3);
padding-bottom: 0;
}
}

&[data-fs-slide-over-size="partial"] {
&[data-fs-slide-over-size='partial'] {
width: var(--fs-sku-matrix-slide-over-partial-width-mobile);

@include media(">=notebook") {
@include media('>=notebook') {
max-width: var(--fs-sku-matrix-slide-over-partial-width-mobile);
}
}
Expand Down Expand Up @@ -91,7 +92,7 @@
}

[data-fs-table-cell],
[data-fs-price-variant="spot"] {
[data-fs-price-variant='spot'] {
font-weight: var(--fs-text-weight-medium);
}

Expand All @@ -115,11 +116,12 @@
align-items: center;
gap: var(--fs-spacing-2);

> div {
img {
object-fit: cover;
object-position: center;
}
img {
object-fit: cover;
object-position: center;
width: var(--fs-sku-matrix-sidebar-table-cell-image-width);
height: var(--fs-sku-matrix-sidebar-table-cell-image-width);
border-radius: var(--fs-sku-matrix-sidebar-table-cell-image-border-radius);
}
}

Expand All @@ -131,19 +133,16 @@

[data-fs-sku-matrix-sidebar-footer] {
display: flex;
width: 100%;
padding: var(--fs-spacing-4) var(--fs-spacing-8);
justify-content: space-between;

position: sticky;
bottom: 0;
left: 0;
right: 0;
margin-top: auto;

background-color: var(--fs-sku-matrix-sidebar-bkg-color);

padding: var(--fs-spacing-4) var(--fs-spacing-8);
border-top: var(--fs-border-width) solid var(--fs-border-color-light);
width: 100%;
background-color: var(--fs-sku-matrix-sidebar-bkg-color);

> div {
display: flex;
Expand All @@ -156,7 +155,7 @@
}
}

@include media("<tablet") {
@include media('<tablet') {
padding: var(--fs-spacing-3);
}
}
Expand Down

0 comments on commit 9d5231c

Please sign in to comment.