Skip to content

Commit

Permalink
add variant selector to the product grid and card
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkevingreen committed Sep 12, 2023
1 parent aae70cf commit 235125c
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 31 deletions.
38 changes: 29 additions & 9 deletions app/components/sharable/productGrid.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// import BlockContent from '@sanity/block-content-to-react'
// import Serializer from '~/serializers/richText'

import { useState } from 'react'
import cx from 'classnames'

import {
ProductForm
} from '~/components/product/ProductForm'
Expand All @@ -9,26 +12,43 @@ const ProductCard = ({
product,
shopify
}) => {
const firstVariant = shopify?.variants?.nodes[0]
console.log('first variant', firstVariant)
const [activeVariant, setActiveVariant] = useState(shopify?.variants?.nodes[0])
console.log('first variant', product.store.variants[0], activeVariant)
return (
<div className='col-span-1'>
<div className='aspect-square bg-primary-green/60' />
<h3 className='text-mono-48 my-2'>{product?.store.title}</h3>
TEST
<div className='my-2'>
{/* <div className='flex my-4'>
<span className='h-10 w-10 rounded-full block mr-2 bg-primary-green' />
<span className='h-10 w-10 rounded-full block mr-2 bg-primary-green' />
<span className='h-10 w-10 rounded-full block mr-2 bg-primary-green' />
</div> */}
<div className='flex my-4'>
{product?.store?.variants?.map((variant, i) => {
return (
<button
key={variant.store.id}
style={{
backgroundImage: variant.pattern?.colorType?.image ? `url(${variant.pattern?.colorType?.image?.url})` : 'none',
backgroundSize: 'cover',
backgroundColor: variant.pattern?.colorType?.color,
}}
onClick={() => setActiveVariant(shopify.variants.nodes[i])}
aria-label={`${variant.pattern.colorName}`}
className={cx('block w-10 h-10 mr-2 rounded-full border-black', {
'opacity-100': activeVariant.id === variant.store.gid,
'opacity-50': activeVariant.id !== variant.store.gid,
})}
>
<span className='absolute opacity-0'>{variant.pattern.colorName}</span>
</button>
)
})}
</div>
<div>

<ProductForm
product={product}
selectedVariant={firstVariant}
selectedVariant={activeVariant}
// productAnalytics={analytics}
variantId={firstVariant?.id} />
variantId={activeVariant?.id} />
{/* <button className='button primary small'>Add to Cart</button> */}
</div>
</div>
Expand Down
47 changes: 28 additions & 19 deletions app/queries/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ const richText = groq`
}
`

const pattern = groq`
{
_type,
_key,
...,
'colorType': colorType[0] {
_type,
_key,
_type == 'color' => {
'color': hex,
},
_type == 'module.image' => {
'image': image.asset-> {
...
}
}
}
}
`

export const QUERY_THEME = groq`
*[_type == 'settings'][0] {
footer {
Expand Down Expand Up @@ -125,7 +145,13 @@ const MODULE_PRODUCT_GRID = groq`
_key,
_type,
'product': product-> {
store,
store {
...,
variants[]-> {
...,
'pattern': pattern-> ${pattern}
}
},
},
'productVariant': productVariant-> {
store,
Expand Down Expand Up @@ -215,24 +241,7 @@ const productQuery = groq`
url,
_id,
},
'pattern': pattern-> {
_type,
_key,
...,
'colorType': colorType[0] {
_type,
_key,
_type == 'color' => {
'color': hex,
},
_type == 'module.image' => {
'image': image.asset-> {
...
}
}
}
}
'pattern': pattern-> ${pattern}
}
}
`
Expand Down
9 changes: 6 additions & 3 deletions app/styles/components/buttons.css
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@
position: relative;
top: 2px;
}
@media (color-index: 48) {

@media (color-index: 48) {
.dark\:prose-invert {
--tw-prose-body: var(--tw-prose-invert-body);
--tw-prose-headings: var(--tw-prose-invert-headings);
Expand All @@ -425,7 +426,8 @@
--tw-prose-td-borders: var(--tw-prose-invert-td-borders);
}
}
@media (color: 48842621) {

@media (color: 48842621) {
.dark\:prose-invert {
--tw-prose-body: var(--tw-prose-invert-body);
--tw-prose-headings: var(--tw-prose-invert-headings);
Expand All @@ -445,7 +447,8 @@
--tw-prose-td-borders: var(--tw-prose-invert-td-borders);
}
}
@media (prefers-color-scheme: dark) {

@media (prefers-color-scheme: dark) {
.dark\:prose-invert {
--tw-prose-body: var(--tw-prose-invert-body);
--tw-prose-headings: var(--tw-prose-invert-headings);
Expand Down
Loading

0 comments on commit 235125c

Please sign in to comment.