Repository navigation
[IRN-6872][BpkCollapsible] Add asChild support to BpkCollapsibleRoot - #4925
Conversation
Mirrors the discriminated-union pattern introduced for BpkCollapsibleRootProvider in #4897. Passes asChild through to Collapsible.Root so consumers can project their own element as the root node without an injected wrapper div.
|
Visit https://backpack.github.io/storybook-prs/4925 to see this build running in a browser. |
…d in README Fix SimpleCollapsible prop type: Partial<BpkCollapsibleRootProps> collapses the discriminated union under React 19's stricter JSX checking. Use Omit<..., 'children' | 'asChild'> instead, which resolves to the flat base type that all callers actually use. Extend the README 'asChild' section to cover both Root and RootProvider, matching the documentation precedent set in #4897 for RootProvider.
| import type { BpkCollapsibleRootProps } from './BpkCollapsibleRoot'; | ||
|
|
||
| const SimpleCollapsible = (props: Partial<BpkCollapsibleRootProps> = {}) => ( | ||
| const SimpleCollapsible = (props: Omit<BpkCollapsibleRootProps, 'children' | 'asChild'> = {}) => ( |
There was a problem hiding this comment.
This simplified wrapper notices the new support for asChild so keep TS happy with typing
| const list = container.querySelector('ul') as HTMLElement; | ||
| // With asChild the <li> is the direct child of <ul>; without asChild | ||
| // Ark injects a <div> between them. | ||
| expect(list.firstElementChild?.tagName).toBe('LI'); |
There was a problem hiding this comment.
test attach to child element
| variant?: BpkCollapsibleVariant; | ||
| }; | ||
| } & ( | ||
| | { asChild: true; children: ReactElement } |
There was a problem hiding this comment.
Only single element child can be passed when using asChild - consistent with RootProvider
| ### Preventing a wrapper element with `asChild` | ||
|
|
||
| By default `RootProvider` renders a `<div>` to host its ARIA attributes and class names. Pass `asChild` to merge those props onto your single child element instead — useful when the provider wraps an existing item element and an extra DOM node would break layout or produce invalid HTML: | ||
| Both `Root` and `RootProvider` accept an `asChild` prop. By default they render a `<div>` to host ARIA attributes and class names. Pass `asChild` to merge those props onto your single child element instead — useful when an extra DOM node would break layout or produce invalid HTML. |
There was a problem hiding this comment.
Mention this one alongside existing note in docs for RootProvider
|
Visit https://backpack.github.io/storybook-prs/4925 to see this build running in a browser. |
Mirrors the existing AsChildOnRootProvider story — side-by-side without/with asChild to make the wrapper injection visible in the DOM.
|
|
||
| // Demonstrates asChild on Root: the Root merges its props onto the child | ||
| // element instead of injecting a wrapper <div>. No hook required. | ||
| const AsChildRoot = () => ( |
There was a problem hiding this comment.
Add demo of the new prop (based on the RootProvider one)
|
Visit https://backpack.github.io/storybook-prs/4925 to see this build running in a browser. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Adds asChild support to BpkCollapsibleRoot so consumers can project their own root element (avoiding an injected wrapper <div>), matching the existing RootProvider API.
Changes:
- Extend
BpkCollapsibleRootPropswith anasChilddiscriminated union and passasChildthrough toCollapsible.Root. - Add Storybook example demonstrating
Rootwith and withoutasChild. - Add unit tests for
Root asChildbehavior and update README docs to coverRootandRootProvider.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| packages/backpack-web/src/bpk-component-collapsible/src/BpkCollapsibleRoot.tsx | Adds asChild prop typing and forwards it to Ark Collapsible.Root. |
| packages/backpack-web/src/bpk-component-collapsible/src/BpkCollapsible.stories.tsx | Adds a new Storybook example demonstrating Root asChild usage. |
| packages/backpack-web/src/bpk-component-collapsible/src/BpkCollapsible-test.tsx | Adds tests ensuring asChild removes wrappers and merges attributes. |
| packages/backpack-web/src/bpk-component-collapsible/README.md | Documents asChild for both Root and RootProvider, with examples. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| In both cases the `<li>` becomes the collapsible root — no wrapper `<div>` is inserted between it and the `<ul>`. | ||
|
|
||
| > **Note:** `asChild` requires exactly one React element child. Passing a fragment, a string, or multiple siblings will throw a runtime error from Ark's slot merging. |
There was a problem hiding this comment.
Adding additional note to clarify this detail 👍
| <div> | ||
| <BpkCollapsible.Root defaultOpen> | ||
| <div> | ||
| <BpkCollapsible.Trigger> | ||
| <BpkText textStyle={TEXT_STYLES.heading5}>Without asChild</BpkText> | ||
| <BpkCollapsible.Indicator> | ||
| <ChevronIcon /> | ||
| </BpkCollapsible.Indicator> | ||
| </BpkCollapsible.Trigger> |
There was a problem hiding this comment.
Consistent with other example so this seems fine as-is - you can easily inspect the dom to properly see what elements are present
| const list = container.querySelector('ul') as HTMLElement; | ||
| // With asChild the <li> is the direct child of <ul>; without asChild | ||
| // Ark injects a <div> between them. | ||
| expect(list.firstElementChild?.tagName).toBe('LI'); |
There was a problem hiding this comment.
This was following pattern of existing tests but if we adjust the demo markup we can use alternative selectors to avoid the cast - fixing...
| const wrapper = container.querySelector('[data-testid="wrapper"]') as HTMLElement; | ||
| expect(wrapper.firstElementChild?.tagName).toBe('DIV'); |
There was a problem hiding this comment.
As above - fixing
…orwardRef doc note
- Replace container.querySelector(...) as HTMLElement with screen.getByRole
('list')/getByTestId('wrapper') in the Root and RootProvider asChild tests,
so a missing element fails with an actionable message and drops the cast.
- Document that custom-component asChild children must forward props and ref
(e.g. via forwardRef) or the merged attributes will not be applied.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| ); | ||
| }; | ||
| const { container } = render(<Harness />); | ||
| const list = container.querySelector('ul') as HTMLElement; |
There was a problem hiding this comment.
Remove the existing HTMLElement cast since co-pilot flagged us following that pattern
|
Visit https://backpack.github.io/storybook-prs/4925 to see this build running in a browser. |
Adds
asChildsupport toBpkCollapsibleRoot, letting consumers project their own element as the collapsible root node without an injected wrapperdiv. This completes parity withBpkCollapsibleRootProvider, which already supportsasChild(shipped in #4897). The implementation follows the exact same discriminated-union pattern —asChild: trueenforces a singleReactElementchild (required by Ark's slot merging) while the default keepsReactNode— and simply passesasChildthrough to the underlyingCollapsible.Root, which already extendsPolymorphicPropsnatively.Changes:
BpkCollapsibleRoot.tsx: add theasChilddiscriminated union toBpkCollapsibleRootProps, importReactElement, destructure and pass throughasChild.BpkCollapsible-test.tsx: adddescribe('Root asChild', ...)block mirroring the existingdescribe('RootProvider asChild', ...)tests.