Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/elements/common/types/SidebarNavigation.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ export type InternalSidebarNavigation = SidebarNavigation & {
export type SidebarNavigationHandler = (sidebar: SidebarNavigation, replace?: boolean) => void;

export type InternalSidebarNavigationHandler = (sidebar: InternalSidebarNavigation, replace?: boolean) => void;

export type SidebarToggleButtonProps = {
'data-resin-target'?: string,
'data-testid'?: string,
isOpen?: boolean,
onClick: (event: SyntheticMouseEvent<HTMLButtonElement>) => void,
};
7 changes: 7 additions & 0 deletions src/elements/common/types/SidebarNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ export type InternalSidebarNavigation = SidebarNavigation & {
export type SidebarNavigationHandler = (sidebar: SidebarNavigation, replace?: boolean) => void;

export type InternalSidebarNavigationHandler = (sidebar: InternalSidebarNavigation, replace?: boolean) => void;

export type SidebarToggleButtonProps = {
'data-resin-target'?: string;
'data-testid'?: string;
isOpen?: boolean;
onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
};
4 changes: 4 additions & 0 deletions src/elements/content-sidebar/ContentSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import type { MetadataEditor } from '../../common/types/metadata';
import type { StringMap, Token, User, BoxItem } from '../../common/types/core';
import type { AdditionalSidebarTab } from './flowTypes';
import type { FeatureConfig } from '../common/feature-checking';
import type { SidebarToggleButtonProps } from '../common/types/SidebarNavigation';
// $FlowFixMe TypeScript file
import type { Theme } from '../common/theming';
import type APICache from '../../utils/Cache';
Expand Down Expand Up @@ -90,6 +91,7 @@ type Props = {
onPanelChange?: (name: string, isInitialState: boolean) => void,
onVersionChange?: Function,
onVersionHistoryClick?: Function,
renderToggleButton?: (toggleButtonProps: SidebarToggleButtonProps) => React.Node,
requestInterceptor?: Function,
responseInterceptor?: Function,
sharedLink?: string,
Expand Down Expand Up @@ -377,6 +379,7 @@ class ContentSidebar extends React.Component<Props, State> {
onPanelChange,
onVersionChange,
onVersionHistoryClick,
renderToggleButton,
signSidebarProps,
theme,
versionsSidebarProps,
Expand Down Expand Up @@ -420,6 +423,7 @@ class ContentSidebar extends React.Component<Props, State> {
onPanelChange={onPanelChange}
onVersionChange={onVersionChange}
onVersionHistoryClick={onVersionHistoryClick}
renderToggleButton={renderToggleButton}
signSidebarProps={signSidebarProps}
theme={theme}
versionsSidebarProps={versionsSidebarProps}
Expand Down
4 changes: 4 additions & 0 deletions src/elements/content-sidebar/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { VersionsSidebarProps } from './versions';
import type { AdditionalSidebarTab } from './flowTypes';
import type { MetadataEditor } from '../../common/types/metadata';
import type { BoxItem, User } from '../../common/types/core';
import type { SidebarToggleButtonProps } from '../common/types/SidebarNavigation';
import type { SignSidebarProps } from './SidebarNavSign';
import type { Errors } from '../common/flowTypes';
// $FlowFixMe TypeScript file
Expand Down Expand Up @@ -70,6 +71,7 @@ type Props = {
onPanelChange?: (name: string, isInitialState: boolean) => void,
onVersionChange?: Function,
onVersionHistoryClick?: Function,
renderToggleButton?: (toggleButtonProps: SidebarToggleButtonProps) => React.Node,
signSidebarProps: SignSidebarProps,
theme?: Theme,
versionsSidebarProps: VersionsSidebarProps,
Expand Down Expand Up @@ -311,6 +313,7 @@ class Sidebar extends React.Component<Props, State> {
metadataSidebarProps,
onAnnotationSelect,
onVersionChange,
renderToggleButton,
signSidebarProps,
theme,
versionsSidebarProps,
Expand Down Expand Up @@ -351,6 +354,7 @@ class Sidebar extends React.Component<Props, State> {
hasDocGen={docGenSidebarProps.isDocGenTemplate}
isOpen={isOpen}
onPanelChange={this.handlePanelChange}
renderToggleButton={renderToggleButton}
signSidebarProps={signSidebarProps}
/>
)}
Expand Down
9 changes: 8 additions & 1 deletion src/elements/content-sidebar/SidebarNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import {
} from '../../constants';
import { useFeatureConfig } from '../common/feature-checking';
import type { NavigateOptions, AdditionalSidebarTab } from './flowTypes';
import type { InternalSidebarNavigation, InternalSidebarNavigationHandler } from '../common/types/SidebarNavigation';
import type {
InternalSidebarNavigation,
InternalSidebarNavigationHandler,
SidebarToggleButtonProps,
} from '../common/types/SidebarNavigation';
import './SidebarNav.scss';
import type { SignSidebarProps } from './SidebarNavSign';

Expand All @@ -58,6 +62,7 @@ type Props = {
onPanelChange?: (name: string, isInitialState: boolean) => void,
routerDisabled?: boolean,
signSidebarProps: SignSidebarProps,
renderToggleButton?: (toggleButtonProps: SidebarToggleButtonProps) => React.Node,
};

const SidebarNav = ({
Expand All @@ -77,6 +82,7 @@ const SidebarNav = ({
isOpen,
onNavigate,
onPanelChange = noop,
renderToggleButton,
routerDisabled,
signSidebarProps,
}: Props) => {
Expand Down Expand Up @@ -203,6 +209,7 @@ const SidebarNav = ({
internalSidebarNavigation={internalSidebarNavigation}
internalSidebarNavigationHandler={internalSidebarNavigationHandler}
isOpen={isOpen}
renderToggleButton={renderToggleButton}
routerDisabled={routerDisabled}
/>
</div>
Expand Down
60 changes: 42 additions & 18 deletions src/elements/content-sidebar/SidebarToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,64 @@ import { type RouterHistory } from 'react-router-dom';
import { withRouterIfEnabled } from '../common/routing';
import SidebarToggleButton from '../../components/sidebar-toggle-button/SidebarToggleButton';
import { SIDEBAR_NAV_TARGETS } from '../common/interactionTargets';
import type { InternalSidebarNavigation, InternalSidebarNavigationHandler } from '../common/types/SidebarNavigation';
import type {
InternalSidebarNavigation,
InternalSidebarNavigationHandler,
SidebarToggleButtonProps,
} from '../common/types/SidebarNavigation';

type Props = {
history?: RouterHistory,
internalSidebarNavigation?: InternalSidebarNavigation,
internalSidebarNavigationHandler?: InternalSidebarNavigationHandler,
isOpen?: boolean,
renderToggleButton?: (toggleButtonProps: SidebarToggleButtonProps) => React.Node,
routerDisabled?: boolean,
};

const SidebarToggle = ({
history,
const SidebarToggle = ({
history,
internalSidebarNavigation,
internalSidebarNavigationHandler,
isOpen,
renderToggleButton,
routerDisabled = false,
}: Props) => {
const handleToggleClick = event => {
event.preventDefault();

if (routerDisabled) {
// Use internal navigation handler when router is disabled
if (internalSidebarNavigationHandler && internalSidebarNavigation) {
internalSidebarNavigationHandler({
...internalSidebarNavigation,
open: !isOpen,
}, true); // Always use replace for toggle
const handleToggleClick = React.useCallback(
(event: SyntheticMouseEvent<HTMLButtonElement>) => {
event.preventDefault();

if (routerDisabled) {
// Use internal navigation handler when router is disabled
if (internalSidebarNavigationHandler && internalSidebarNavigation) {
internalSidebarNavigationHandler(
{
...internalSidebarNavigation,
open: !isOpen,
},
true,
); // Always use replace for toggle
}
} else if (history) {
history.replace({ state: { open: !isOpen } });
}
} else if (history) {
history.replace({ state: { open: !isOpen } });
}
};
},
[history, isOpen, routerDisabled, internalSidebarNavigationHandler, internalSidebarNavigation],
);

const renderProps = React.useMemo(
() => ({
isOpen,
onClick: handleToggleClick,
'data-resin-target': SIDEBAR_NAV_TARGETS.TOGGLE,
'data-testid': 'sidebartoggle',
}),
[isOpen, handleToggleClick],
);

return (
return renderToggleButton ? (
renderToggleButton(renderProps)
) : (
<SidebarToggleButton
data-resin-target={SIDEBAR_NAV_TARGETS.TOGGLE}
data-testid="sidebartoggle"
Expand Down
64 changes: 55 additions & 9 deletions src/elements/content-sidebar/__tests__/SidebarToggle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SidebarToggleComponent as SidebarToggle } from '../SidebarToggle';

describe('elements/content-sidebar/SidebarToggle', () => {
const historyMock = { replace: jest.fn() };

beforeEach(() => {
jest.clearAllMocks();
});
Expand All @@ -20,12 +20,12 @@ describe('elements/content-sidebar/SidebarToggle', () => {
`('should render and handle clicks correctly when isOpen is $isOpen', async ({ isOpen, expectedState }) => {
const user = userEvent();
renderSidebarToggle({ isOpen });

const toggleButton = screen.getByTestId('sidebartoggle');
expect(toggleButton).toBeInTheDocument();

await user.click(toggleButton);

expect(historyMock.replace).toHaveBeenCalledWith(expectedState);
});
});
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('elements/content-sidebar/SidebarToggle - Router Disabled', () => {
activeFeedEntryType: 'comments',
activeFeedEntryId: '456',
};

renderSidebarToggle({
isOpen: true,
internalSidebarNavigation: complexNavigation,
Expand All @@ -78,10 +78,56 @@ describe('elements/content-sidebar/SidebarToggle - Router Disabled', () => {
const toggleButton = screen.getByTestId('sidebartoggle');
await user.click(toggleButton);

expect(mockInternalSidebarNavigationHandler).toHaveBeenCalledWith({
...complexNavigation,
open: false,
}, true);
expect(mockInternalSidebarNavigationHandler).toHaveBeenCalledWith(
{
...complexNavigation,
open: false,
},
true,
);
});
});

describe('elements/content-sidebar/SidebarToggle - Custom Render', () => {
const historyMock = { replace: jest.fn() };
const renderToggleButton = jest.fn(props => (
<button {...props} type="button">
Custom Toggle
</button>
));
const user = userEvent();

beforeEach(() => {
jest.clearAllMocks();
});

test('should render a custom component if renderToggleButton is provided', () => {
render(<SidebarToggle history={historyMock} isOpen={false} renderToggleButton={renderToggleButton} />);

expect(renderToggleButton).toHaveBeenCalled();
expect(screen.getByText('Custom Toggle')).toBeInTheDocument();
expect(screen.getByTestId('sidebartoggle')).toBeInTheDocument();
});

test('should properly pass button props to the custom component', () => {
render(<SidebarToggle history={historyMock} isOpen={true} renderToggleButton={renderToggleButton} />);

expect(renderToggleButton).toHaveBeenCalledWith(
expect.objectContaining({
isOpen: true,
onClick: expect.any(Function),
'data-resin-target': 'sidebartoggle',
'data-testid': 'sidebartoggle',
}),
);
});

test('should handle clicks on custom toggle button', async () => {
render(<SidebarToggle history={historyMock} isOpen={true} renderToggleButton={renderToggleButton} />);

const toggleButton = screen.getByTestId('sidebartoggle');
await user.click(toggleButton);

expect(historyMock.replace).toHaveBeenCalledWith({ state: { open: false } });
});
});