Skip to content

Commit

Permalink
fix: Partially controllable tools (#2793)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgylobko authored Oct 4, 2024
1 parent 3202255 commit 227dd63
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/app-layout/__tests__/tools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { act, waitFor } from '@testing-library/react';
import { describeEachAppLayout, renderComponent, isDrawerClosed } from './utils';
import AppLayout, { AppLayoutProps } from '../../../lib/components/app-layout';

describeEachAppLayout({ themes: ['classic', 'refresh', 'refresh-toolbar'] }, () => {
describeEachAppLayout({ themes: ['classic', 'refresh', 'refresh-toolbar'] }, ({ theme }) => {
test('opens tools drawer', () => {
let ref: AppLayoutProps.Ref | null = null;
const { wrapper } = renderComponent(<AppLayout ref={newRef => (ref = newRef)} />);
Expand Down Expand Up @@ -56,4 +56,24 @@ describeEachAppLayout({ themes: ['classic', 'refresh', 'refresh-toolbar'] }, ()
expect(wrapper.find('#custom-button')!.getElement()).toEqual(document.activeElement);
});
});

test('should not open partially controllable tools', () => {
const { wrapper } = renderComponent(
<AppLayout tools={<button id="custom-button">Click me</button>} toolsOpen={false} />
);

wrapper.findToolsToggle()!.click();

if (theme === 'refresh-toolbar') {
expect(wrapper.findTools()).toBeFalsy();
}

if (theme === 'refresh') {
expect(wrapper.findTools().getElement()).toHaveAttribute('aria-hidden', 'true');
}

if (theme === 'classic') {
expect(wrapper.findTools().getElement().style).not.toContain('width');
}
});
});
4 changes: 3 additions & 1 deletion src/app-layout/utils/use-drawers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ export function useDrawers(
}

function onActiveDrawerChange(newDrawerId: string | null) {
setActiveDrawerId(newDrawerId);
if (newDrawerId !== TOOLS_DRAWER_ID) {
setActiveDrawerId(newDrawerId);
}
if (newDrawerId) {
onAddNewActiveDrawer?.(newDrawerId);
}
Expand Down
2 changes: 2 additions & 0 deletions src/app-layout/visual-refresh-toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ const AppLayoutVisualRefreshToolbar = React.forwardRef<AppLayoutProps.Ref, AppLa
onNavigationToggle,
onActiveDrawerChange: onActiveDrawerChangeHandler,
onActiveDrawerResize,
toolsOpen,
onToolsToggle,
};

const splitPanelInternals: SplitPanelProviderProps = {
Expand Down
2 changes: 2 additions & 0 deletions src/app-layout/visual-refresh-toolbar/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ export interface AppLayoutInternals {
onActiveDrawerChange: (newDrawerId: string | null) => void;
onActiveDrawerResize: (detail: { id: string; size: number }) => void;
onActiveGlobalDrawersChange: (newDrawerId: string) => void;
toolsOpen: boolean;
onToolsToggle: (value: boolean) => void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ interface DrawerTriggersProps {
globalDrawers: ReadonlyArray<AppLayoutProps.Drawer>;
onActiveGlobalDrawersChange?: (newDrawerId: string) => void;

toolsOpen: boolean;
onToolsToggle: (value: boolean) => void;

splitPanelOpen?: boolean;
splitPanelPosition?: AppLayoutProps.SplitPanelPreferences['position'];
splitPanelToggleProps: SplitPanelToggleProps | undefined;
Expand All @@ -62,6 +65,7 @@ export function DrawerTriggers({
globalDrawers,
globalDrawersFocusControl,
onActiveGlobalDrawersChange,
toolsOpen,
}: DrawerTriggersProps) {
const isMobile = useMobile();
const hasMultipleTriggers = drawers.length > 1;
Expand Down Expand Up @@ -152,11 +156,12 @@ export function DrawerTriggers({
)}
{visibleItems.slice(0, globalDrawersStartIndex).map(item => {
const isForPreviousActiveDrawer = previousActiveLocalDrawerId?.current === item.id;
const isActive = item.id === TOOLS_DRAWER_ID ? toolsOpen : item.id === activeDrawerId;
return (
<TriggerButton
ariaLabel={item.ariaLabels?.triggerButton}
ariaExpanded={item.id === activeDrawerId}
ariaControls={activeDrawerId === item.id ? item.id : undefined}
ariaExpanded={isActive}
ariaControls={isActive ? item.id : undefined}
className={clsx(
styles['drawers-trigger'],
!toolsOnlyMode && testutilStyles['drawers-trigger'],
Expand All @@ -167,7 +172,7 @@ export function DrawerTriggers({
key={item.id}
onClick={() => onActiveDrawerChange?.(activeDrawerId !== item.id ? item.id : null)}
ref={item.id === previousActiveLocalDrawerId.current ? drawersFocusRef : undefined}
selected={item.id === activeDrawerId}
selected={isActive}
badge={item.badge}
testId={`awsui-app-layout-trigger-${item.id}`}
hasTooltip={true}
Expand Down
4 changes: 4 additions & 0 deletions src/app-layout/visual-refresh-toolbar/toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export function AppLayoutToolbarImplementation({
setToolbarState,
setToolbarHeight,
globalDrawersFocusControl,
toolsOpen,
onToolsToggle,
} = appLayoutInternals;
const {
ariaLabels,
Expand Down Expand Up @@ -219,6 +221,8 @@ export function AppLayoutToolbarImplementation({
globalDrawers={globalDrawers?.filter(item => !!item.trigger) ?? []}
activeGlobalDrawersIds={activeGlobalDrawersIds ?? []}
onActiveGlobalDrawersChange={onActiveGlobalDrawersChange}
toolsOpen={toolsOpen}
onToolsToggle={onToolsToggle}
/>
</div>
)}
Expand Down

0 comments on commit 227dd63

Please sign in to comment.