Repository navigation
[IRN-6860][BpkCollapsible] Add useBpkCollapsibleContext hook and asChild on RootProvider - #4897
Conversation
…ild on RootProvider
|
Visit https://backpack.github.io/storybook-prs/4897 to see this build running in a browser. |
|
|
||
| import BpkCollapsible from './BpkCollapsible'; | ||
| import useBpkCollapsible from './useBpkCollapsible'; | ||
| import useBpkCollapsibleContext from './useBpkCollapsibleContext'; |
There was a problem hiding this comment.
Newly exposed Ark UI hook to allow reading the collapsable state when in "managed" mode
| }); | ||
| }); | ||
|
|
||
| describe('useBpkCollapsibleContext', () => { |
There was a problem hiding this comment.
Test scenarios for hook
|
|
||
| }); | ||
|
|
||
| describe('RootProvider asChild', () => { |
There was a problem hiding this comment.
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 = () => { |
There was a problem hiding this comment.
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}> |
There was a problem hiding this comment.
Parent context
| <ChevronIcon /> | ||
| </BpkCollapsible.Indicator> | ||
| </BpkCollapsible.Trigger> | ||
| <StateReadout /> |
There was a problem hiding this comment.
Demo reading the state
| </BpkCollapsible.Indicator> | ||
| </BpkCollapsible.Trigger> | ||
| <StateReadout /> | ||
| <BpkCollapsible.Content> |
There was a problem hiding this comment.
Actual collapsable content
| // 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 = () => { |
There was a problem hiding this comment.
Demo asChild to allow full control over the HTML element used for the collapsable root provider
| </BpkText> | ||
| <ul> | ||
| <BpkCollapsible.RootProvider value={withAsChild} asChild> | ||
| <li> |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
New prop to control the root element type
| variant = COLLAPSIBLE_VARIANTS.default, | ||
| }: BpkCollapsibleRootProviderProps) => ( | ||
| <Collapsible.RootProvider | ||
| asChild={asChild} |
There was a problem hiding this comment.
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 => |
There was a problem hiding this comment.
Expose the Ark UI hook that allows reading collapsable state
|
Visit https://backpack.github.io/storybook-prs/4897 to see this build running in a browser. |
| import BpkCollapsible from './src/BpkCollapsible'; | ||
|
|
||
| export { default as useBpkCollapsible } from './src/useBpkCollapsible'; | ||
| export { default as useBpkCollapsibleContext } from './src/useBpkCollapsibleContext'; |
There was a problem hiding this comment.
barrel export
There was a problem hiding this comment.
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 withinRoot/RootProvider. - Adds
asChildsupport toBpkCollapsible.RootProviderto 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.
| <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; | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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');
});There was a problem hiding this comment.
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
|
Visit https://backpack.github.io/storybook-prs/4897 to see this build running in a browser. |
| variant?: BpkCollapsibleVariant; | ||
| }; | ||
| } & ( | ||
| | { asChild: true; children: ReactElement } |
There was a problem hiding this comment.
Accepted children values narrowed when specifying asChild because only a single element child is allowed by Ark UI for this use case.
…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
…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.
…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]>
JIRA Link: https://skyscanner.atlassian.net/browse/IRN-6860
What has changed, and why?
Component:
bpk-component-collapsibleClass of change: new API surface, no behaviour change to existing usage
Adds two capabilities to
BpkCollapsiblethat are required groundwork for the FLEXP-661 Expandables MVP fare-card integration.useBpkCollapsibleContext()— a consumer-side reader hook. Any component nested insideBpkCollapsible.RootorBpkCollapsible.RootProvidercan now call this hook to read the current machine state (open,visible,disabled,setOpen) without prop-drilling. Follows the same pattern asuseCollapsibleContextfrom Ark internally, but exposed under a stable BPK name. Precedent:useCheckboxCardContextinbpk-component-checkbox-card.asChildonRootProvider— whentrue, 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:BpkModalV3Triggeralready shipsasChild.Both are demonstrated in new Storybook stories (
ContextReaderHook,AsChildOnRootProvider).Remember to include the following changes:
[Clover-123][BpkButton] Updating the colourREADME.md(If you have created a new component)README.md