diff --git a/packages/docs/.storybook/preview.tsx b/packages/docs/.storybook/preview.tsx index e6a35a86..4d66d5c6 100644 --- a/packages/docs/.storybook/preview.tsx +++ b/packages/docs/.storybook/preview.tsx @@ -36,7 +36,7 @@ const parameters: Parameters = { 'Orders', ['OrderStorage', 'OrderContainer'], 'Cart', - ['AddToCartButton', 'HostedCart', 'CartLink'] + ['AddToCartButton', 'HostedCart', 'CartLink', 'MiniCart'] ], 'Examples', ['Listing Page', 'Cart Page'] diff --git a/packages/docs/stories/cart/CartLink.stories.tsx b/packages/docs/stories/cart/CartLink.stories.tsx index 209b5bfb..a53aa640 100644 --- a/packages/docs/stories/cart/CartLink.stories.tsx +++ b/packages/docs/stories/cart/CartLink.stories.tsx @@ -2,8 +2,6 @@ import type { Meta, StoryFn } from '@storybook/react' import CommerceLayer from '../_internals/CommerceLayer' import OrderContainer from '#components/orders/OrderContainer' import CartLink from '#components/orders/CartLink' -import { HostedCart } from '#components/orders/HostedCart' -import { OrderStorage } from '../_internals/OrderStorage' const setup: Meta = { title: 'Components/Cart/CartLink', @@ -29,48 +27,3 @@ Default.args = { onClick: () => {}, className: 'underline hover:text-blue-500' } - -/** - * - * You can use this component to open the mini cart, by setting the `type` prop to `mini`. - * It requires the `` component to be on the same page. - * - */ -export const MiniCart: StoryFn = (arg) => { - return ( - - - - - ) -} - -MiniCart.decorators = [ - (Story) => { - return ( - - -
- {/* we need more space on the canvas to open the mini cart */} - -
-
-
- ) - } -] diff --git a/packages/docs/stories/cart/MiniCart.mdx b/packages/docs/stories/cart/MiniCart.mdx new file mode 100644 index 00000000..96d5d1ba --- /dev/null +++ b/packages/docs/stories/cart/MiniCart.mdx @@ -0,0 +1,56 @@ +import { Meta, Source, Canvas } from '@storybook/addon-docs'; +import * as Stories from './MiniCart.stories.tsx' + + + + +# Mini Cart +We can combine the two components just described (`` and ``) to create a mini cart +that will open as a modal on top of the page. + +To do so, we need to set the prop `type='mini'` to both components. Try to click the link below to open the mini cart. + + + +### Advanced usage +In the previous example we used as `label` prop a simple string, but it can accept any React component (`ReactNode`). + +This means we can be creative and mix other components to create a more complex label, like a shopping bag icon +that previews the number of items in the cart. + +This icon component can be built using `` and `` and a custom svg icon. + + ( +
+ + + {/* static icon */} + + + + + {/* total number of cart items */} + + + +
+) +`} language='jsx' /> + +Now we can use our custom component as `label` prop of `` to create a mini cart that can be +placed in the top navigation of your site. + + +This works because `LineItemsContainer` and `HostedCart` are both using the same order context: +they are both inside the same `` component. + + +Click on the icon to show the side cart modal. + diff --git a/packages/docs/stories/cart/MiniCart.stories.tsx b/packages/docs/stories/cart/MiniCart.stories.tsx new file mode 100644 index 00000000..1c3ebedf --- /dev/null +++ b/packages/docs/stories/cart/MiniCart.stories.tsx @@ -0,0 +1,138 @@ +import type { Meta, StoryFn, Decorator } from '@storybook/react' +import CommerceLayer from '../_internals/CommerceLayer' +import OrderContainer from '#components/orders/OrderContainer' +import CartLink from '#components/orders/CartLink' +import { HostedCart } from '#components/orders/HostedCart' +import { OrderStorage } from '../_internals/OrderStorage' +import LineItemsContainer from '#components/line_items/LineItemsContainer' +import LineItemsCount from '#components/line_items/LineItemsCount' + +const setup: Meta = { + title: 'Components/Cart/Mini Cart' +} + +export default setup + +const CartDecorator: Decorator = (Story) => { + return ( + + +
+ {/* we need more space on the canvas to open the mini cart */} + +
+
+
+ ) +} + +export const Basic: StoryFn = (arg) => { + return ( + + + + + ) +} + +Basic.decorators = [CartDecorator] + +export const CartIcon: StoryFn = (args) => { + return ( +
+ + + + + + +
+ ) +} +CartIcon.parameters = {} +CartIcon.decorators = [ + (Story) => ( + + + + + + + + ) +] + +export const WithCartIcon: StoryFn = (args) => { + const MyCartIcon = (): JSX.Element => ( +
+ + + + + + +
+ ) + + return ( + + + } + type='mini' + className='underline hover:text-blue-500' + /> + + ) +} + +WithCartIcon.decorators = [CartDecorator] +WithCartIcon.parameters = { + docs: { + canvas: { + sourceState: 'shown' + } + } +}