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

[IRN-6860][BpkCollapsible] Add useBpkCollapsibleContext hook and asChild on RootProvider - #4897

Merged
GC Zhu (gc-skyscanner) merged 3 commits into
mainfrom
IRN-6860/collapsible-context-asChild-types
Jul 15, 2026
Merged

GC Zhu (gc-skyscanner) merged 3 commits into
mainfrom
IRN-6860/collapsible-context-asChild-types

Conversation

@steviehailey-skyscanner

@steviehailey-skyscanner Stephen Hailey (steviehailey-skyscanner) commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

JIRA Link: https://skyscanner.atlassian.net/browse/IRN-6860

What has changed, and why?

Component: bpk-component-collapsible

Class of change: new API surface, no behaviour change to existing usage

Adds two capabilities to BpkCollapsible that are required groundwork for the FLEXP-661 Expandables MVP fare-card integration.

useBpkCollapsibleContext() — a consumer-side reader hook. Any component nested inside BpkCollapsible.Root or BpkCollapsible.RootProvider can now call this hook to read the current machine state (open, visible, disabled, setOpen) without prop-drilling. Follows the same pattern as useCollapsibleContext from Ark internally, but exposed under a stable BPK name. Precedent: useCheckboxCardContext in bpk-component-checkbox-card.

asChild on RootProvider — when true, the provider merges its props onto the single child element instead of injecting a wrapper <div>. This is load-bearing for pixel parity in the fare-card layout, where the provider needs to wrap an existing item element without adding a DOM node between it and its parent. Precedent: BpkModalV3Trigger already ships asChild.

Both are demonstrated in new Storybook stories (ContextReaderHook, AsChildOnRootProvider).


Remember to include the following changes:

  • Ensure the PR title includes the name of the component you are changing so it's clear in the release notes for consumers of the changes in the version e.g [Clover-123][BpkButton] Updating the colour
  • README.md (If you have created a new component)
  • Component README.md
  • Tests
  • Accessibility tests
    • The following checks were performed:
      • Ability to navigate using a keyboard only
      • Zoom functionality (Deque University explanation):
        • The page SHOULD be functional AND readable when only the text is magnified to 200% of its initial size
        • Pages must reflow as zoom increases up to 400% so that content continues to be presented in only one column i.e. Content MUST NOT require scrolling in two directions (both vertically and horizontally)
      • Ability to navigate using a screen reader only
  • Storybook examples created/updated
  • For breaking changes or deprecating components/properties, migration guides added to the description of the PR.

@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor


import BpkCollapsible from './BpkCollapsible';
import useBpkCollapsible from './useBpkCollapsible';
import useBpkCollapsibleContext from './useBpkCollapsibleContext';

@steviehailey-skyscanner Stephen Hailey (steviehailey-skyscanner) Jul 14, 2026 •

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.

Newly exposed Ark UI hook to allow reading the collapsable state when in "managed" mode

});
});

describe('useBpkCollapsibleContext', () => {

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 scenarios for hook


});

describe('RootProvider 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.

Test scenario for asChild option now available on RootProvider

// Reads state via useBpkCollapsibleContext() — no props passed in from above.
// Rendered adjacent to the trigger so the values are always visible regardless
// of open state, making it clear the hook works outside Content too.
const StateReadout = () => {

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.

Helper for demo of using new hook - simulates a component adjacent to the the content displaying the collapsable state held by parent provider.

<code>StateReadout</code> calls <code>useBpkCollapsibleContext()</code>{' '}
directly — no props passed to it from above.
</BpkText>
<BpkCollapsible.RootProvider value={collapsible}>

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.

Parent context

<ChevronIcon />
</BpkCollapsible.Indicator>
</BpkCollapsible.Trigger>
<StateReadout />

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.

Demo reading the state

</BpkCollapsible.Indicator>
</BpkCollapsible.Trigger>
<StateReadout />
<BpkCollapsible.Content>

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.

Actual collapsable content

@skyscanner-backpack-bot

skyscanner-backpack-bot Bot commented Jul 14, 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 f642ac9

// Demonstrates asChild: the RootProvider merges its props onto the child
// <li> element instead of injecting a wrapper <div>. Inspect the DOM to
// confirm the output is <li data-backpack-ds-component …> with no extra node.
const AsChildRootProvider = () => {

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.

Demo asChild to allow full control over the HTML element used for the collapsable root provider

</BpkText>
<ul>
<BpkCollapsible.RootProvider value={withAsChild} asChild>
<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.

Here the li becomes the collapsable root

// When true, merges all provider props onto the single child element instead
// of injecting a wrapper div — load-bearing for pixel parity in layouts that
// hoist the provider over an existing item element.
asChild?: boolean;

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.

New prop to control the root element type

variant = COLLAPSIBLE_VARIANTS.default,
}: BpkCollapsibleRootProviderProps) => (
<Collapsible.RootProvider
asChild={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.

Plain pass through to Ark UI

// Reader hook for the shared collapsible machine. Call from any component
// inside BpkCollapsible.Root or BpkCollapsible.RootProvider to access the
// current open/disabled/visible state and setOpen without prop-drilling.
const useBpkCollapsibleContext = (): BpkUseCollapsibleReturn =>

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.

Expose the Ark UI hook that allows reading collapsable state

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

Copy link
Copy Markdown
Contributor

import BpkCollapsible from './src/BpkCollapsible';

export { default as useBpkCollapsible } from './src/useBpkCollapsible';
export { default as useBpkCollapsibleContext } from './src/useBpkCollapsibleContext';

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.

barrel export

@chiefkes Max Liefkes (chiefkes) 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

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 no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

Adds new ergonomics and DOM-structure control APIs to bpk-component-collapsible to support deeper integrations without prop-drilling or unwanted wrapper elements.

Changes:

  • Introduces useBpkCollapsibleContext() hook to read the collapsible machine state from within Root / RootProvider.
  • Adds asChild support to BpkCollapsible.RootProvider to merge provider props onto a single child element (no wrapper <div>).
  • Updates Storybook stories, tests, exports, and component README to document and demonstrate the new APIs.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/backpack-web/src/bpk-component-collapsible/src/useBpkCollapsibleContext.ts Adds the new public reader hook for collapsible context.
packages/backpack-web/src/bpk-component-collapsible/src/BpkCollapsibleRootProvider.tsx Adds asChild prop plumbing to avoid wrapper element insertion.
packages/backpack-web/src/bpk-component-collapsible/src/BpkCollapsible.stories.tsx Adds Storybook examples showcasing the new hook and asChild behavior.
packages/backpack-web/src/bpk-component-collapsible/src/BpkCollapsible-test.tsx Adds unit tests for the hook and asChild DOM structure behavior.
packages/backpack-web/src/bpk-component-collapsible/index.ts Exposes useBpkCollapsibleContext from the package entrypoint.
packages/backpack-web/src/bpk-component-collapsible/README.md Documents useBpkCollapsibleContext and asChild usage patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +354 to +361
<ul>
<BpkCollapsible.RootProvider value={collapsible}>
<li>
<BpkCollapsible.Trigger>Toggle</BpkCollapsible.Trigger>
<BpkCollapsible.Content>Body</BpkCollapsible.Content>
</li>
</BpkCollapsible.RootProvider>
</ul>
@@ -31,14 +31,20 @@ export type BpkCollapsibleRootProviderProps = {
children: ReactNode;

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.

asChild gets passed straight through to ark.div, which runs it through Children.only (factory.ts). So it only accepts a single element child — pass two siblings and it throws at runtime, pass a string/number and it silently renders null.

children: ReactNode doesn’t enforce that, so it’s easy to trip. Could we narrow it, e.g.

export type BpkCollapsibleRootProviderProps = {
  value: BpkUseCollapsibleReturn;
  variant?: BpkCollapsibleVariant;
} & (
  | { asChild: true; children: ReactElement } 
  | { asChild?: false; children: ReactNode } 
);

so the array case is a compile error instead of a runtime one? If that’s more churn than it’s worth, a line in the README noting the single-child requirement would do.

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.

Added both for protection and clarity
Added note below and also added a discriminated union to enforce this single child scenario in TS https://github.com/Skyscanner/backpack/pull/4897/changes#diff-93f103dd76425f0b320d7adeb9235d39e0bc300879da5dbc178ba098569271e6R36

};
const { container } = render(<Harness />);
const list = container.querySelector('ul') as HTMLElement;
expect(list.firstElementChild?.tagName).not.toBe('LI');

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.

The asChild tests check that no wrapper <div> is inserted, but nothing verifies that the collapsible attributes actually land on the child. Could be worth adding:

it('merges collapsible attributes onto the child element when asChild is true', () => {
  const Harness = () => {
    const collapsible = useBpkCollapsible({ defaultOpen: true });
    return (
      <ul>
        <BpkCollapsible.RootProvider value={collapsible} asChild>
          <li data-testid="item">
            <BpkCollapsible.Trigger>Toggle</BpkCollapsible.Trigger>
            <BpkCollapsible.Content>Body</BpkCollapsible.Content>
          </li>
        </BpkCollapsible.RootProvider>
      </ul>
    );
  };
  render(<Harness />);

  const item = screen.getByTestId('item');
  expect(item.tagName).toBe('LI');
  expect(item).toHaveAttribute('data-state', 'open');
  expect(item).toHaveAttribute('data-backpack-ds-component');
});

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.

Addition assertions added

…text hook

- Discriminated union on BpkCollapsibleRootProviderProps: asChild:true enforces ReactElement child, default keeps ReactNode

- Add visible assertion to context hook test

- Replace invalid ul/li HTML in 'without asChild' test and story with div/span

- Add attribute-merge test verifying data-state and data-backpack-ds-component land on child when asChild is true

- Add single-child constraint note to README asChild section
@skyscanner-backpack-bot

Copy link
Copy Markdown
Contributor

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.

Accepted children values narrowed when specifying asChild because only a single element child is allowed by Ark UI for this use case.

@gc-skyscanner
GC Zhu (gc-skyscanner) merged commit f813715 into main Jul 15, 2026
15 checks passed
@gc-skyscanner
GC Zhu (gc-skyscanner) deleted the IRN-6860/collapsible-context-asChild-types branch July 15, 2026 07:47
Viviana Yanez (vivitt) pushed a commit that referenced this pull request Jul 15, 2026
…ild on RootProvider (#4897)

* [IRN-6860][BpkCollapsible] Add useBpkCollapsibleContext hook and asChild on RootProvider

* [IRN-6860][BpkCollapsible] Update README for useBpkCollapsibleContext and asChild

* [IRN-6860][BpkCollapsible] Address review feedback on asChild and context hook

- Discriminated union on BpkCollapsibleRootProviderProps: asChild:true enforces ReactElement child, default keeps ReactNode

- Add visible assertion to context hook test

- Replace invalid ul/li HTML in 'without asChild' test and story with div/span

- Add attribute-merge test verifying data-state and data-backpack-ds-component land on child when asChild is true

- Add single-child constraint note to README asChild section
Stephen Hailey (steviehailey-skyscanner) added a commit that referenced this pull request Jul 22, 2026
…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.
Iris Li (wpe1arhy) pushed a commit that referenced this pull request Jul 22, 2026
…4925)

* [IRN-6855][BpkCollapsible] Add asChild support to BpkCollapsibleRoot

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.

* [IRN-6855][BpkCollapsible] Fix React19 TS error; document Root asChild 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.

* [IRN-6855][BpkCollapsible] Add AsChildOnRoot Storybook story

Mirrors the existing AsChildOnRootProvider story — side-by-side without/with
asChild to make the wrapper injection visible in the DOM.

* [IRN-6855][BpkCollapsible] Address PR review: clearer test queries, forwardRef 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]>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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