/* eslint-disable react-hooks/rules-of-hooks */
import { useCallback, useState } from "react";
import { motion } from "framer-motion";
import { commonProps } from "../../utils/storybook";
import { styled } from "../../theme";
import { pxToRem, useBreakpoints } from "../../utils";
import { Button } from "../button";
import { Box } from "../box";
import { Flex } from "../flex";
import { Text } from "../text";
import { Carousel } from "./";
import type { StoryObj, Meta } from "@storybook/react";
function clamp(value: number, min: number, max: number): number {
return Math.floor(Math.min(Math.max(value, min), max));
}
const StyledMotionLink = styled(motion.a, {
display: "flex",
p: "$1",
flexDirection: "column",
gap: "$1",
borderRadius: "$s",
border: "2px solid transparent",
"&:focus": {
boxShadow: "0px 0px 0px 2px $off-white-72",
scale: "0.9",
},
});
const Placeholder: React.FC<{ i: number; width: number; height: number }> = ({
i,
width,
height,
}) => {
return (
{`${width}x${height}`}
{`Chromia ${i}`}
);
};
const ResponsivePlaceholder = styled("div", {
width: 150,
height: 150,
backgroundColor: "$off-white-72",
m: "$1",
borderRadius: "$s",
border: "2px solid transparent",
"@s": {
width: 100,
height: 100,
},
});
const defaultItems = [...Array(10).keys()].map((i) => (
));
const meta: Meta = {
title: "Carousel",
component: Carousel,
tags: ["autodocs"],
argTypes: {
gap: {
table: {
category: "Grid Props",
},
},
span: {
table: {
category: "Grid Props",
},
},
gapX: {
table: {
category: "Grid Props",
},
},
gapY: {
table: {
category: "Grid Props",
},
},
disableDrag: { table: { defaultValue: { summary: "false" } } },
...commonProps,
},
args: {
children: defaultItems,
rows: 2,
columns: 4,
css: { rowGap: "20px" },
},
decorators: [
(Story) => (
),
],
};
export default meta;
type Story = StoryObj;
/**
* The internal navigation buttons are enabled by default
*/
export const Uncontrolled: Story = {
render: (args) => ,
};
/**
* The carousel can be fully controlled
*/
export const Controlled: Story = {
render: (args) => {
const [step, setStep] = useState(0);
const handlePreviousClick = useCallback(() => {
setStep((prev) => clamp(prev - 1, 0, defaultItems.length / 2 - 4));
}, []);
const handleNExtClick = useCallback(() => {
setStep((prev) => clamp(prev + 1, 0, defaultItems.length / 2 - 4));
}, []);
const handleChange = useCallback((currentStep: number) => {
setStep(currentStep);
}, []);
return (
);
},
};
/**
* Add an optional link title/url
*/
export const WithHeaderLink: Story = {
render: (args) => ,
};
/**
* The Grid will adapt if children change (either by re-render or style change)
*/
export const WithDynamicChildren: Story = {
render: (args) => {
const [toggleChildren, setToggleChildre] = useState(false);
return (
<>
{toggleChildren
? defaultItems
: [...Array(10).keys()].map((i) => (
responsive item #{i}
))}