Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variant option color on product page in demo store #1751

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -39,6 +39,8 @@ import {MEDIA_FRAGMENT, PRODUCT_CARD_FRAGMENT} from '~/data/fragments';

export const headers = routeHeaders;

const VARIANT_OPTIONS_LIST_NUMBER = 7;

export async function loader({params, request, context}: LoaderFunctionArgs) {
const {productHandle} = params;
invariant(productHandle, 'Missing productHandle param, check route filename');
Expand Down Expand Up @@ -251,9 +253,13 @@ export function ProductForm({
>
<Heading as="legend" size="lead" className="min-w-[4rem]">
{option.name}
{option.name === 'Color' &&
option.values.length <= VARIANT_OPTIONS_LIST_NUMBER && (
<span className="font-medium px-1">: {option.value}</span>
)}
</Heading>
<div className="flex flex-wrap items-baseline gap-4">
{option.values.length > 7 ? (
{option.values.length > VARIANT_OPTIONS_LIST_NUMBER ? (
<div className="relative w-full">
<Listbox>
{({open}) => (
Expand All @@ -267,7 +273,15 @@ export function ProductForm({
: 'rounded',
)}
>
<span>{option.value}</span>
<div className="flex items-center w-full">
{option.name === 'Color' && (
<span
className="mr-2 border border-primary/20 w-6 h-6"
style={{backgroundColor: option.value}}
/>
)}
<span>{option.value}</span>
</div>
<IconCaret direction={open ? 'up' : 'down'} />
</Listbox.Button>
<Listbox.Options
Expand Down Expand Up @@ -295,6 +309,12 @@ export function ProductForm({
closeRef.current.click();
}}
>
{option.name === 'Color' && (
<span
className="mr-2 border border-primary/20 w-6 h-6"
style={{backgroundColor: value}}
/>
)}
{value}
{isActive && (
<span className="ml-2">
Expand All @@ -311,22 +331,46 @@ export function ProductForm({
</Listbox>
</div>
) : (
option.values.map(({value, isAvailable, isActive, to}) => (
<Link
key={option.name + value}
to={to}
preventScrollReset
prefetch="intent"
replace
className={clsx(
'leading-none py-1 border-b-[1.5px] cursor-pointer transition-all duration-200',
isActive ? 'border-primary/50' : 'border-primary/0',
isAvailable ? 'opacity-100' : 'opacity-50',
)}
>
{value}
</Link>
))
option.values.map(({value, isAvailable, isActive, to}) => {
return option.name === 'Color' ? (
<Link
key={option.name + value}
to={to}
preventScrollReset
prefetch="intent"
replace
className={clsx(
'p-1 bg-secondary border-[1.5px] cursor-pointer transition-all duration-200',
isActive
? 'border-primary/60'
: 'border-primary/10',
)}
>
<div
className={clsx(
'border border-primary/20 w-[38px] h-[38px]',
isAvailable ? 'opacity-100' : 'strike-diagonal',
)}
style={{backgroundColor: value}}
/>
</Link>
) : (
<Link
key={option.name + value}
to={to}
preventScrollReset
prefetch="intent"
replace
className={clsx(
'leading-none py-1 border-b-[1.5px] cursor-pointer transition-all duration-200',
isActive ? 'border-primary/50' : 'border-primary/0',
isAvailable ? 'opacity-100' : 'opacity-50',
)}
>
{value}
</Link>
);
})
)}
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions templates/demo-store/app/styles/app.css
Expand Up @@ -169,6 +169,20 @@ shop-pay-button {
}
}

.strike-diagonal {
position: relative;
&:before {
position: absolute;
content: '';
left: 0;
top: 50%;
right: 0;
border-top: 1px solid rgb(var(--color-contrast) / 0.6);
border-bottom: 1px solid rgb(var(--color-primary) / 0.3);
transform: skewY(-45deg);
}
}

.card-image {
@apply relative flex items-center justify-center overflow-clip rounded;
&::before {
Expand Down