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

refactor: remove default carousel padding #4

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/carousel-static/carousel-static.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export const CarouselStatic = forwardRef(function CarouselStatic(

if (products.length === 0) {
return (
<div className={cn(className, 'w-full bg-gray-100 p-6 text-center')}>
<div className={cn(className, 'w-full bg-gray-100 text-center')}>
No products have been added
</div>
);
}

return (
<Carousel aria-labelledby="Carousel" className={cn(className, 'pb-0')} ref={ref}>
<Carousel aria-labelledby="Carousel" className={cn(className)} ref={ref}>
<div className="flex items-center justify-between">
<h2 className="text-3xl font-black lg:text-4xl" id="title">
{title}
Expand Down
2 changes: 1 addition & 1 deletion components/product-card-carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const ProductCardCarousel = ({
}, []);

return (
<Carousel aria-labelledby="title" className="mb-14">
<Carousel aria-labelledby="title">
<div className="flex items-center justify-between">
<h2 className="text-3xl font-black lg:text-4xl" id="title">
{title}
Expand Down
3 changes: 2 additions & 1 deletion components/product-carousel/product-carousel.makeswift.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Combobox, TextInput } from '@makeswift/runtime/controls';
import { Combobox, Style, TextInput } from '@makeswift/runtime/controls';
import { forwardNextDynamicRef } from '@makeswift/runtime/next';
import dynamic from 'next/dynamic';

Expand All @@ -21,6 +21,7 @@ const fetchCategories = async (): Promise<Category[]> => {

export const props = {
title: TextInput({ label: 'Title', defaultValue: 'Products', selectAll: true }),
className: Style(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use this guy:

className: Style({ properties: [Style.Margin, Style.Padding, Style.Width] }),

By default Style comes with a bunch of things like border radius that aren't relavent to this component.

categoryId: Combobox({
label: 'Category',
async getOptions(query) {
Expand Down
137 changes: 70 additions & 67 deletions components/product-carousel/product-carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Product, ProductCard } from './product-card';
interface Props {
title: string;
categoryId?: number;
className?: string;
}

type QuickSearchResults = Awaited<ReturnType<typeof getQuickSearchResults>>;
Expand All @@ -41,82 +42,84 @@ export const fetchQuickSearch = async ({ categoryId }: { categoryId?: number })
}));
};

export const ProductCardCarousel = forwardRef<HTMLElement, Props>(({ title, categoryId }, ref) => {
const id = useId();
const deferredQuery = useDeferredValue({ categoryId });
export const ProductCardCarousel = forwardRef<HTMLElement, Props>(
({ title, categoryId, className }, ref) => {
const id = useId();
const deferredQuery = useDeferredValue({ categoryId });

const { data: products = [], isLoading } = useQuery({
queryKey: ['quick-search-product-carousel', deferredQuery],
queryFn: () => fetchQuickSearch(deferredQuery),
enabled: !!categoryId,
});

if (products.length === 0) {
return (
<Carousel aria-labelledby="title" className={className} ref={ref}>
<div className="flex items-center justify-between">
<h2 className="text-3xl font-black lg:text-4xl" id="title">
{title}
</h2>
<span className="no-wrap flex">
<CarouselPreviousIndicator className="opacity-20" disabled />
<CarouselNextIndicator className="opacity-20" disabled />
</span>
</div>
<div className="mt-5 flex min-h-48 w-full items-center justify-center gap-4 lg:mt-6">
{isLoading ? (
<>
<DummyCard />
<DummyCard />
<DummyCard />
<DummyCard />
</>
) : (
<span className="text-lg opacity-20">Category has no products</span>
)}
</div>
</Carousel>
);
}

const groupedProducts = products.reduce<Array<Array<Partial<Product>>>>((batches, _, index) => {
if (index % 4 === 0) {
batches.push([]);
}

const product = products[index];

if (batches[batches.length - 1] && product) {
batches[batches.length - 1]?.push(product);
}

const { data: products = [], isLoading } = useQuery({
queryKey: ['quick-search-product-carousel', deferredQuery],
queryFn: () => fetchQuickSearch(deferredQuery),
enabled: !!categoryId,
});
return batches;
}, []);

if (products.length === 0) {
return (
<Carousel aria-labelledby="title" className="mb-14 w-full" ref={ref}>
<Carousel aria-labelledby="title" className="mb-14" ref={ref}>
<div className="flex items-center justify-between">
<h2 className="text-3xl font-black lg:text-4xl" id="title">
{title}
</h2>
<span className="no-wrap flex">
<CarouselPreviousIndicator className="opacity-20" disabled />
<CarouselNextIndicator className="opacity-20" disabled />
<CarouselPreviousIndicator />
<CarouselNextIndicator />
</span>
</div>
<div className="mt-5 flex min-h-48 w-full items-center justify-center gap-4 lg:mt-6">
{isLoading ? (
<>
<DummyCard />
<DummyCard />
<DummyCard />
<DummyCard />
</>
) : (
<span className="text-lg opacity-20">Category has no products</span>
)}
</div>
<CarouselContent>
{products.map((product, index) => (
<CarouselSlide
aria-label={`${index + 1} of ${groupedProducts.length}`}
id={`${id}-slide-${index + 1}`}
index={index}
key={index}
>
<ProductCard imageSize="tall" key={product.entityId} product={product} />
</CarouselSlide>
))}
</CarouselContent>
<Pagination groupedProducts={groupedProducts} id={id} />
</Carousel>
);
}

const groupedProducts = products.reduce<Array<Array<Partial<Product>>>>((batches, _, index) => {
if (index % 4 === 0) {
batches.push([]);
}

const product = products[index];

if (batches[batches.length - 1] && product) {
batches[batches.length - 1]?.push(product);
}

return batches;
}, []);

return (
<Carousel aria-labelledby="title" className="mb-14" ref={ref}>
<div className="flex items-center justify-between">
<h2 className="text-3xl font-black lg:text-4xl" id="title">
{title}
</h2>
<span className="no-wrap flex">
<CarouselPreviousIndicator />
<CarouselNextIndicator />
</span>
</div>
<CarouselContent>
{products.map((product, index) => (
<CarouselSlide
aria-label={`${index + 1} of ${groupedProducts.length}`}
id={`${id}-slide-${index + 1}`}
index={index}
key={index}
>
<ProductCard imageSize="tall" key={product.entityId} product={product} />
</CarouselSlide>
))}
</CarouselContent>
<Pagination groupedProducts={groupedProducts} id={id} />
</Carousel>
);
});
},
);
2 changes: 1 addition & 1 deletion components/ui/carousel/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Carousel = forwardRef<ElementRef<'section'>, ComponentPropsWithRef<'sectio
<CarouselContext.Provider value={[emblaRef, emblaApi]}>
<section
aria-roledescription="carousel"
className={cn('relative overflow-x-hidden pb-16', className)}
className={cn('relative overflow-x-hidden', className)}
ref={ref}
{...props}
>
Expand Down