Skip to content

Commit

Permalink
[Collapsible] Fix trigger aria-controls referencing nonexistent id (
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert authored Dec 10, 2024
1 parent 3088be4 commit d6ffbb0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ describe('<Collapsible.Panel />', () => {
const trigger = getByRole('button');

expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(trigger.getAttribute('aria-controls')).to.equal(
queryByText(PANEL_CONTENT)?.getAttribute('id'),
);
expect(queryByText(PANEL_CONTENT)).to.not.equal(null);
expect(queryByText(PANEL_CONTENT)).not.toBeVisible();
expect(queryByText(PANEL_CONTENT)).to.have.attribute('data-closed');
Expand All @@ -76,6 +79,9 @@ describe('<Collapsible.Panel />', () => {
await flushMicrotasks();

expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(trigger.getAttribute('aria-controls')).to.equal(
queryByText(PANEL_CONTENT)?.getAttribute('id'),
);
expect(queryByText(PANEL_CONTENT)).not.toBeVisible();
expect(queryByText(PANEL_CONTENT)).to.have.attribute('data-closed');
});
Expand Down
9 changes: 7 additions & 2 deletions packages/react/src/collapsible/panel/useCollapsiblePanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ export function useCollapsiblePanel(
const isTransitioningRef = React.useRef(false);

useEnhancedEffect(() => {
setPanelId(id);
if (!keepMounted && !open) {
setPanelId(undefined);
} else {
setPanelId(id);
}
return () => {
setPanelId(undefined);
};
}, [id, setPanelId]);
}, [id, setPanelId, keepMounted, open]);

const handlePanelRef = useEventCallback((element: HTMLElement) => {
if (!element) {
Expand Down Expand Up @@ -204,6 +208,7 @@ export function useCollapsiblePanel(
registerCssTransitionListeners,
runOnceAnimationsFinish,
setContextMounted,
setPanelId,
]);

useOnMount(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/collapsible/root/CollapsibleRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('<Collapsible.Root />', () => {

const trigger = getByRole('button');

expect(trigger).to.not.have.attribute('aria-controls');
expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(queryByText(PANEL_CONTENT)).to.equal(null);

Expand All @@ -62,6 +63,7 @@ describe('<Collapsible.Root />', () => {
setProps({ open: false });
await flushMicrotasks();

expect(trigger).to.not.have.attribute('aria-controls');
expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(queryByText(PANEL_CONTENT)).to.equal(null);
});
Expand All @@ -81,6 +83,7 @@ describe('<Collapsible.Root />', () => {

const trigger = getByRole('button');

expect(trigger).to.not.have.attribute('aria-controls');
expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(queryByText(PANEL_CONTENT)).to.equal(null);

Expand All @@ -94,6 +97,7 @@ describe('<Collapsible.Root />', () => {

await user.pointer({ keys: '[MouseLeft]', target: trigger });

expect(trigger).to.not.have.attribute('aria-controls');
expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(trigger).to.not.have.attribute('data-panel-open');
expect(queryByText(PANEL_CONTENT)).to.equal(null);
Expand Down Expand Up @@ -141,6 +145,7 @@ describe('<Collapsible.Root />', () => {

await user.keyboard(`[${key}]`);

expect(trigger).to.not.have.attribute('aria-controls');
expect(trigger).to.have.attribute('aria-expanded', 'false');
expect(trigger).not.to.have.attribute('data-panel-open');
expect(queryByText(PANEL_CONTENT)).to.equal(null);
Expand Down

0 comments on commit d6ffbb0

Please sign in to comment.