Skip to content

Commit

Permalink
Switch product card details trigger to render prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Youakeem committed Sep 19, 2024
1 parent 01f4821 commit 4ae8304
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export const Default: Story = {

<ProductCardDetails.Root>
<ProductCardDetails.Trigger>
<ProductCardDetails.CollapsedLabel>Show details</ProductCardDetails.CollapsedLabel>
<ProductCardDetails.ExpandedLabel>Hide details</ProductCardDetails.ExpandedLabel>
{(isOpen) => (isOpen ? 'Hide details' : 'Show details')}
</ProductCardDetails.Trigger>

<ProductCardDetails.Content className={sprinkles({ paddingBlock: 'md' })}>
Expand Down
32 changes: 6 additions & 26 deletions apps/store/src/components/ProductCard/ProductCardDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ComponentProps, createContext, type PropsWithChildren, useContext, useState } from 'react'
import { type ComponentProps, createContext, type ReactNode, useContext, useState } from 'react'
import { Button } from 'ui'
import Collapsible from '@/components/Collapsible/Collapsible'

Expand Down Expand Up @@ -34,14 +34,16 @@ const Root = ({ children, ...props }: RootProps) => {
)
}

type TriggerProps = ComponentProps<typeof Collapsible.Trigger>
type TriggerProps = Omit<ComponentProps<typeof Collapsible.Trigger>, 'children'> & {
children?: ((isOpen: boolean) => ReactNode) | ReactNode
}
const Trigger = ({ children, ...props }: TriggerProps) => {
const { toggle } = useProductDetails()
const { isOpen, toggle } = useProductDetails()

return (
<Collapsible.Trigger asChild {...props}>
<Button variant="outline" size="medium" onClick={toggle} fullWidth>
{children}
{typeof children === 'function' ? children(isOpen) : children}
</Button>
</Collapsible.Trigger>
)
Expand All @@ -57,30 +59,8 @@ const Content = ({ children, ...props }: ContentProps) => {
)
}

const ExpandedLabel = ({ children }: PropsWithChildren) => {
const { isOpen } = useProductDetails()

if (!isOpen) {
return null
}

return children
}

const CollapsedLabel = ({ children }: PropsWithChildren) => {
const { isOpen } = useProductDetails()

if (isOpen) {
return null
}

return children
}

export const ProductCardDetails = {
Root,
Trigger,
Content,
ExpandedLabel,
CollapsedLabel,
}

0 comments on commit 4ae8304

Please sign in to comment.