Skip to content

Commit

Permalink
fix: Panel/PanelGroup/PanelResizeHandle pass "id" prop through to DOM (
Browse files Browse the repository at this point in the history
…#299)



---------

Co-authored-by: Brian Vaughn <[email protected]>
  • Loading branch information
koralle and bvaughn authored Feb 13, 2024
1 parent 0205437 commit 54b6d18
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/react-resizable-panels/src/Panel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,38 @@ describe("PanelGroup", () => {
});
});

describe("a11y", () => {
it("should pass explicit id prop to DOM", () => {
act(() => {
root.render(
<PanelGroup direction="horizontal">
<Panel id="explicit-id" />
</PanelGroup>
);
});

const element = container.querySelector("[data-panel]");

expect(element).not.toBeNull();
expect(element?.getAttribute("id")).toBe("explicit-id");
});

it("should not pass auto-generated id prop to DOM", () => {
act(() => {
root.render(
<PanelGroup direction="horizontal">
<Panel />
</PanelGroup>
);
});

const element = container.querySelector("[data-panel]");

expect(element).not.toBeNull();
expect(element?.getAttribute("id")).toBeNull();
});
});

describe("DEV warnings", () => {
it("should warn about server rendered panels with no default size", () => {
jest.resetModules();
Expand Down
1 change: 1 addition & 0 deletions packages/react-resizable-panels/src/Panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export function PanelWithForwardedRef({

children,
className: classNameFromProps,
id: idFromProps,
style: {
...style,
...styleFromProps,
Expand Down
36 changes: 36 additions & 0 deletions packages/react-resizable-panels/src/PanelGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,42 @@ describe("PanelGroup", () => {
});
});

describe("a11y", () => {
it("should pass explicit id prop to DOM", () => {
act(() => {
root.render(
<PanelGroup direction="horizontal" id="explicit-id">
<Panel />
<PanelResizeHandle />
<Panel />
</PanelGroup>
);
});

const element = container.querySelector("[data-panel-group]");

expect(element).not.toBeNull();
expect(element?.getAttribute("id")).toBe("explicit-id");
});

it("should not pass auto-generated id prop to DOM", () => {
act(() => {
root.render(
<PanelGroup direction="horizontal">
<Panel />
<PanelResizeHandle />
<Panel />
</PanelGroup>
);
});

const element = container.querySelector("[data-panel-group]");

expect(element).not.toBeNull();
expect(element?.getAttribute("id")).toBeNull();
});
});

describe("DEV warnings", () => {
it("should warn about unstable layouts without id and order props", () => {
act(() => {
Expand Down
5 changes: 4 additions & 1 deletion packages/react-resizable-panels/src/PanelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,16 @@ function PanelGroupWithForwardedRef({
{ value: context },
createElement(Type, {
...rest,

children,
className: classNameFromProps,
id: idFromProps,
ref: panelGroupElementRef,
style: {
...style,
...styleFromProps,
},
ref: panelGroupElementRef,

// CSS selectors
"data-panel-group": "",
"data-panel-group-direction": direction,
Expand Down
36 changes: 36 additions & 0 deletions packages/react-resizable-panels/src/PanelResizeHandle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,40 @@ describe("PanelResizeHandle", () => {
verifyAttribute(rightElement, "data-resize-handle-active", null);
});
});

describe("a11y", () => {
it("should pass explicit id prop to DOM", () => {
act(() => {
root.render(
<PanelGroup direction="horizontal">
<Panel />
<PanelResizeHandle id="explicit-id" />
<Panel />
</PanelGroup>
);
});

const element = container.querySelector("[data-resize-handle]");

expect(element).not.toBeNull();
expect(element?.getAttribute("id")).toBe("explicit-id");
});

it("should not pass auto-generated id prop to DOM", () => {
act(() => {
root.render(
<PanelGroup direction="horizontal">
<Panel />
<PanelResizeHandle />
<Panel />
</PanelGroup>
);
});

const element = container.querySelector("[data-resize-handle]");

expect(element).not.toBeNull();
expect(element?.getAttribute("id")).toBeNull();
});
});
});
1 change: 1 addition & 0 deletions packages/react-resizable-panels/src/PanelResizeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export function PanelResizeHandle({

children,
className: classNameFromProps,
id: idFromProps,
onBlur: () => setIsFocused(false),
onFocus: () => setIsFocused(true),
ref: elementRef,
Expand Down

0 comments on commit 54b6d18

Please sign in to comment.