Skip to content

Commit 038f03e

Browse files
authored
fix(fab): render fab at all times (#652)
1 parent 535f787 commit 038f03e

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-global-floating-action-button': patch
3+
---
4+
5+
render fab on document body when the target element is not found

workspaces/global-floating-action-button/plugins/global-floating-action-button/src/components/FloatingButton.test.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,36 @@ beforeEach(() => {
4949
document.body.innerHTML = '<div class="BackstagePage-root-123"></div>';
5050
});
5151

52+
const renderFab = (htmlContent: string) => {
53+
document.body.innerHTML = htmlContent;
54+
render(
55+
<FloatingButton
56+
floatingButtons={[
57+
{
58+
icon: <AddIcon />,
59+
label: 'Add',
60+
},
61+
]}
62+
slot={Slot.BOTTOM_LEFT}
63+
/>,
64+
);
65+
};
66+
5267
describe('Floating Button', () => {
5368
it('should render a floating button', () => {
54-
render(
55-
<FloatingButton
56-
floatingButtons={[
57-
{
58-
icon: <AddIcon />,
59-
label: 'Add',
60-
color: 'primary',
61-
toolTip: 'Main menu',
62-
},
63-
]}
64-
slot={Slot.BOTTOM_LEFT}
65-
/>,
66-
);
69+
renderFab('<div class="BackstagePage-root-123"></div>');
70+
expect(screen.getByTestId('floating-button')).toBeInTheDocument();
71+
expect(screen.getByTestId('AddIcon')).toBeInTheDocument();
72+
});
73+
74+
it('should render a floating button in the UI when the `BackstagePage-root` classname is not found', () => {
75+
renderFab('<div class="BackstagePage-xxx-123"></div>');
76+
expect(screen.getByTestId('floating-button')).toBeInTheDocument();
77+
expect(screen.getByTestId('AddIcon')).toBeInTheDocument();
78+
});
79+
80+
it('should render a floating button when the `BackstagePage-root` classname is not found but the html tag is found', () => {
81+
renderFab('<main class="BackstagePage-xxx-123"></div>');
6782
expect(screen.getByTestId('floating-button')).toBeInTheDocument();
6883
expect(screen.getByTestId('AddIcon')).toBeInTheDocument();
6984
});

workspaces/global-floating-action-button/plugins/global-floating-action-button/src/components/FloatingButton.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export const FloatingButton = ({
5959

6060
React.useEffect(() => {
6161
const checkTargetElement = () => {
62-
const element = document.querySelector('[class^="BackstagePage-root"]');
62+
const element =
63+
document.querySelector('[class^="BackstagePage-root"]') ??
64+
document.querySelector('main');
6365
if (element) {
6466
setTargetElement(element);
6567
} else {
@@ -80,9 +82,7 @@ export const FloatingButton = ({
8082
if (fabs?.length === 0) {
8183
return null;
8284
}
83-
if (!targetElement) {
84-
return null;
85-
}
85+
8686
let fabDiv;
8787
if (fabs.length > 1) {
8888
fabDiv = (

0 commit comments

Comments
 (0)