Skip to content

Commit

Permalink
Merge pull request #2237 from adevinta/feat-collapsible-ids
Browse files Browse the repository at this point in the history
feat(collapsible): add "ids" props, useful for composition
  • Loading branch information
acd02 authored Jun 15, 2024
2 parents 55efa48 + 10c3ade commit 135d2ef
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/components/collapsible/src/Collapsible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface CollapsibleProps extends ComponentPropsWithoutRef<'div'> {
* The controlled open state of the collapsible. Must be used in conjunction with `onOpenChange`.
*/
open?: boolean
/**
* The ids of the elements in the collapsible. Useful for composition
*/
ids?: collapsible.Context['ids']
}

const CollapsibleContext = createContext<collapsible.Api<PropTypes> | null>(null)
Expand All @@ -37,6 +41,7 @@ export const Collapsible = forwardRef<HTMLDivElement, CollapsibleProps>(
disabled = false,
onOpenChange,
open,
ids,
...props
},
ref
Expand All @@ -46,13 +51,14 @@ export const Collapsible = forwardRef<HTMLDivElement, CollapsibleProps>(
open: defaultOpen || open,
disabled,
id: useId(),
onOpenChange(details) {
onOpenChange?.(details.open)
},
ids,
}

const context: collapsible.Context = {
...initialContext,
onOpenChange(details) {
onOpenChange?.(details.open)
},
open,
disabled,
}
Expand All @@ -63,13 +69,11 @@ export const Collapsible = forwardRef<HTMLDivElement, CollapsibleProps>(

const Component = asChild ? Slot : 'div'

const mergedProps = mergeProps(api.getRootProps(), props)

return (
<CollapsibleContext.Provider value={api}>
<Component
data-spark-component="collapsible"
ref={ref}
{...mergeProps(api.getRootProps(), props)}
>
<Component data-spark-component="collapsible" ref={ref} {...mergedProps}>
{children}
</Component>
</CollapsibleContext.Provider>
Expand Down

0 comments on commit 135d2ef

Please sign in to comment.