Skip to content
This repository was archived by the owner on Aug 5, 2026. It is now read-only.

[IRN-6872][BpkCollapsible] Add asChild support to BpkCollapsibleRoot - #4925

Merged
Iris Li (wpe1arhy) merged 4 commits into
mainfrom
irn-6855-bpk-collapsible-aschild-root
Jul 22, 2026
Merged

Iris Li (wpe1arhy) merged 4 commits into
mainfrom
irn-6855-bpk-collapsible-aschild-root

Conversation

@steviehailey-skyscanner

Copy link
Copy Markdown
Contributor

Adds asChild support to BpkCollapsibleRoot, letting consumers project their own element as the collapsible root node without an injected wrapper div. This completes parity with BpkCollapsibleRootProvider, which already supports asChild (shipped in #4897). The implementation follows the exact same discriminated-union pattern — asChild: true enforces a single ReactElement child (required by Ark's slot merging) while the default keeps ReactNode — and simply passes asChild through to the underlying Collapsible.Root, which already extends PolymorphicProps natively.

Changes:

  • BpkCollapsibleRoot.tsx: add the asChild discriminated union to BpkCollapsibleRootProps, import ReactElement, destructure and pass through asChild.
  • BpkCollapsible-test.tsx: add describe('Root asChild', ...) block mirroring the existing describe('RootProvider asChild', ...) tests.

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.
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@skyscanner-backpack-bot

skyscanner-backpack-bot Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor
Warnings
⚠️

Package source files (e.g. packages/package-name/src/Component.js) were updated, but snapshots weren't. Have you checked that the tests still pass?

Browser support

If this is a visual change, make sure you've tested it in multiple browsers.

Generated by 🚫 dangerJS against b48fe22

…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'> = {}) => (

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test attach to child element

variant?: BpkCollapsibleVariant;
};
} & (
| { asChild: true; children: ReactElement }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention this one alongside existing note in docs for RootProvider

@steviehailey-skyscanner Stephen Hailey (steviehailey-skyscanner) changed the title [IRN-6855][BpkCollapsible] Add asChild support to BpkCollapsibleRoot [IRN-6872][BpkCollapsible] Add asChild support to BpkCollapsibleRoot Jul 22, 2026
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

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 = () => (

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add demo of the new prop (based on the RootProvider one)

@steviehailey-skyscanner
Stephen Hailey (steviehailey-skyscanner) marked this pull request as ready for review July 22, 2026 08:48
Copilot AI review requested due to automatic review settings July 22, 2026 08:48
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 BpkCollapsibleRootProps with an asChild discriminated union and pass asChild through to Collapsible.Root.
  • Add Storybook example demonstrating Root with and without asChild.
  • Add unit tests for Root asChild behavior and update README docs to cover Root and RootProvider.

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.

Comment on lines 175 to 177
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding additional note to clarify this detail 👍

Comment on lines +477 to +485
<div>
<BpkCollapsible.Root defaultOpen>
<div>
<BpkCollapsible.Trigger>
<BpkText textStyle={TEXT_STYLES.heading5}>Without asChild</BpkText>
<BpkCollapsible.Indicator>
<ChevronIcon />
</BpkCollapsible.Indicator>
</BpkCollapsible.Trigger>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent with other example so this seems fine as-is - you can easily inspect the dom to properly see what elements are present

Comment on lines +341 to +344
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');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was following pattern of existing tests but if we adjust the demo markup we can use alternative selectors to avoid the cast - fixing...

Comment on lines +358 to +359
const wrapper = container.querySelector('[data-testid="wrapper"]') as HTMLElement;
expect(wrapper.firstElementChild?.tagName).toBe('DIV');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the existing HTMLElement cast since co-pilot flagged us following that pattern

@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

@wpe1arhy Iris Li (wpe1arhy) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wpe1arhy
Iris Li (wpe1arhy) merged commit 4c023c6 into main Jul 22, 2026
15 checks passed
@wpe1arhy
Iris Li (wpe1arhy) deleted the irn-6855-bpk-collapsible-aschild-root branch July 22, 2026 10:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

minor Non breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants