Skip to content

Commit d3f0563

Browse files
authored
Tweak plugin buttons to be function (#1274)
This is needed to support the use of hooks in the button components without breaking React rules of hooks or forcing the panel to rerender whenever a property of the buttons changes
1 parent 3cdc839 commit d3f0563

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010

1111
- Font-family variables that can be used to customise the sans-serif and monospace fonts used in the editor (#1264)
1212
- Material symbols font to web component preview page since the Design System depends on this (#1261)
13+
- Ability for plugins to add buttons to the SidebarPanel header (#1270, #1271, #1274)
1314

1415
### Changed
1516

src/components/Menus/Sidebar/Sidebar.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ const Sidebar = ({ options = [], plugins = [] }) => {
8787
title: plugin.title,
8888
position: plugin.position || "top",
8989
panel: () => (
90-
<SidebarPanel heading={plugin.heading} buttons={plugin.buttons || []}>
90+
<SidebarPanel
91+
heading={plugin.heading}
92+
buttons={plugin.buttons ? plugin.buttons() : []}
93+
>
9194
{plugin.panel()}
9295
</SidebarPanel>
9396
),

src/components/Menus/Sidebar/Sidebar.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ describe("When plugins are provided", () => {
336336
heading: "My amazing plugin",
337337
title: "my amazing plugin",
338338
panel: () => <p>My amazing content</p>,
339+
buttons: () => [<button>My amazing button</button>],
339340
};
340341

341342
describe("when plugin has autoOpen true", () => {
@@ -366,6 +367,10 @@ describe("When plugins are provided", () => {
366367
test("Renders the plugin content", () => {
367368
expect(screen.queryByText("My amazing content")).toBeInTheDocument();
368369
});
370+
371+
test("Renders the plugin buttons", () => {
372+
expect(screen.queryByText("My amazing button")).toBeInTheDocument();
373+
});
369374
});
370375

371376
describe("when plugin has autoOpen false", () => {

src/components/Menus/Sidebar/SidebarPanel.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ const SidebarPanel = (props) => {
1111
heading,
1212
Footer,
1313
className,
14-
buttons = [],
14+
buttons,
1515
defaultWidth = "320px",
1616
} = props;
1717
const isMobile = useMediaQuery({ query: MOBILE_MEDIA_QUERY });
1818

19+
const buttonsIsEmptyArray =
20+
buttons && Array.isArray(buttons) && buttons.length === 0;
21+
1922
const panelContent = (
2023
<>
2124
<div className="sidebar__panel-header">
2225
<h2 className="sidebar__panel-heading">{heading}</h2>
23-
{buttons?.length > 0 && (
26+
{buttons && !buttonsIsEmptyArray && (
2427
<div className="sidebar__panel-buttons">{buttons}</div>
2528
)}
2629
</div>

0 commit comments

Comments
 (0)