Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add tab header "actions" #3293

Merged
merged 3 commits into from
Feb 17, 2025
Merged
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
10 changes: 10 additions & 0 deletions pages/tabs/permutations.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
import * as React from 'react';

import Button from '~components/button';
import ButtonDropdown from '~components/button-dropdown';
import SpaceBetween from '~components/space-between';
import Tabs, { TabsProps } from '~components/tabs';

import createPermutations from '../utils/permutations';
Expand Down Expand Up @@ -106,6 +108,14 @@ const tabActionPermutations = createPermutations<TabsProps>([
{
activeTabId: ['first', 'second'],
variant: ['default', 'container'],
actions: [
undefined,
<Button key="button">Add</Button>,
<SpaceBetween key="space-between" size="s" direction="horizontal">
<Button>Add</Button>
<Button>Add2</Button>
</SpaceBetween>,
],
tabs: [
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16664,7 +16664,16 @@ use the \`id\` attribute, consider setting it on a parent element instead.",
"visualRefreshTag": "\`stacked\` variant",
},
],
"regions": [],
"regions": [
{
"description": "Actions for the tabs header, displayed next to the list of tabs.
Use this to add a button or button dropdown that performs actions on the
entire tab list. We recommend a maximum of one interactive element to
minimize the number of keyboard tab stops between the tab list and content.",
"isDefault": false,
"name": "actions",
},
],
"releaseStatus": "stable",
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ exports[`test-utils selectors 1`] = `
"awsui_wrapper_wih1l",
],
"tabs": [
"awsui_actions-container_14rmt",
"awsui_disabled-reason-tooltip_14rmt",
"awsui_root_14rmt",
"awsui_tab-dismiss-button_1nq1i",
Expand Down
4 changes: 4 additions & 0 deletions src/tabs/__tests__/tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,8 @@ describe('Tabs', () => {
verifyTabContentLabelledBy(secondTabId);
});
});
test('renders actions', () => {
const wrapper = renderTabs(<Tabs tabs={defaultTabs} actions={<div>Actions content</div>} />).wrapper;
expect(wrapper.findActions()!.getElement()).toHaveTextContent('Actions content');
});
});
3 changes: 3 additions & 0 deletions src/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function Tabs({
disableContentPaddings = false,
i18nStrings,
fitHeight,
actions,
...rest
}: TabsProps) {
for (const tab of tabs) {
Expand All @@ -49,6 +50,7 @@ export default function Tabs({
props: { disableContentPaddings, variant, fitHeight },
metadata: {
hasActions: tabs.some(tab => !!tab.action),
hasHeaderActions: !!actions,
hasDisabledReasons: tabs.some(tab => !!tab.disabledReason),
},
});
Expand Down Expand Up @@ -123,6 +125,7 @@ export default function Tabs({
ariaLabel={ariaLabel}
ariaLabelledby={ariaLabelledby}
tabs={tabs}
actions={actions}
onChange={changeDetail => {
setActiveTabId(changeDetail.activeTabId);
fireNonCancelableEvent(onChange, changeDetail);
Expand Down
8 changes: 8 additions & 0 deletions src/tabs/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export interface TabsProps extends BaseComponentProps {
*/
tabs: ReadonlyArray<TabsProps.Tab>;

/**
* Actions for the tabs header, displayed next to the list of tabs.
* Use this to add a button or button dropdown that performs actions on the
* entire tab list. We recommend a maximum of one interactive element to
* minimize the number of keyboard tab stops between the tab list and content.
*/
actions?: React.ReactNode;

/**
* The possible visual variants of tabs are the following:
* * `default` - Use in any context.
Expand Down
15 changes: 15 additions & 0 deletions src/tabs/tab-header-bar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ $label-horizontal-spacing: awsui.$space-xs;
padding-block: 0;
padding-inline: 0;
display: flex;
flex-wrap: wrap;
}

.tab-header-scroll-container {
display: flex;
flex-grow: 1;
max-inline-size: 100%;
}

.tabs-header-list {
Expand Down Expand Up @@ -66,6 +73,14 @@ $label-horizontal-spacing: awsui.$space-xs;
}
}

.actions-container {
flex-shrink: 0;
align-self: center;
padding-block: awsui.$space-xs;
padding-inline: awsui.$space-xs;
margin-inline-start: auto;
}

.tabs-tab {
// Clear list formatting that comes with list item
list-style: none;
Expand Down
98 changes: 51 additions & 47 deletions src/tabs/tab-header-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
ariaLabel?: string;
ariaLabelledby?: string;
i18nStrings?: TabsProps.I18nStrings;
actions?: TabsProps['actions'];
}

export function TabHeaderBar({
Expand All @@ -90,6 +91,7 @@
ariaLabel,
ariaLabelledby,
i18nStrings,
actions,
}: TabHeaderBarProps) {
const headerBarRef = useRef<HTMLUListElement>(null);
const activeTabHeaderRef = useRef<null | HTMLElement>(null);
Expand Down Expand Up @@ -289,54 +291,56 @@
const TabList = hasActionOrDismissible ? 'div' : 'ul';

return (
//converted span to div as list should not be a child of span for HTML validation
<div className={classes} ref={containerRef}>
{horizontalOverflow && (
<span ref={inlineStartOverflowButton} className={leftButtonClasses}>
<InternalButton
formAction="none"
variant="icon"
iconName="angle-left"
disabled={!inlineStartOverflow}
__focusable={true}
onClick={() => onPaginationClick(headerBarRef, 'backward')}
ariaLabel={i18n('i18nStrings.scrollLeftAriaLabel', i18nStrings?.scrollLeftAriaLabel)}
/>
</span>
)}
<SingleTabStopNavigationProvider
ref={navigationAPI}
navigationActive={true}
getNextFocusTarget={getNextFocusTarget}
onUnregisterActive={onUnregisterActive}
>
<TabList
{...tabActionAttributes}
className={clsx(styles['tabs-header-list'], analyticsSelectors['tabs-header-list'])}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
ref={headerBarRef as never}
onScroll={onScroll}
onKeyDown={onKeyDown}
onFocus={onFocus}
onBlur={onBlur}
<div className={classes}>
<div className={styles['tab-header-scroll-container']} ref={containerRef}>
{horizontalOverflow && (
<span ref={inlineStartOverflowButton} className={leftButtonClasses}>
<InternalButton
formAction="none"
variant="icon"
iconName="angle-left"
disabled={!inlineStartOverflow}
__focusable={true}
onClick={() => onPaginationClick(headerBarRef, 'backward')}

Check warning on line 304 in src/tabs/tab-header-bar.tsx

View check run for this annotation

Codecov / codecov/patch

src/tabs/tab-header-bar.tsx#L304

Added line #L304 was not covered by tests
ariaLabel={i18n('i18nStrings.scrollLeftAriaLabel', i18nStrings?.scrollLeftAriaLabel)}
/>
</span>
)}
<SingleTabStopNavigationProvider
ref={navigationAPI}
navigationActive={true}
getNextFocusTarget={getNextFocusTarget}
onUnregisterActive={onUnregisterActive}
>
{tabs.map(renderTabHeader)}
</TabList>
</SingleTabStopNavigationProvider>
{horizontalOverflow && (
<span className={rightButtonClasses}>
<InternalButton
formAction="none"
variant="icon"
iconName="angle-right"
disabled={!inlineEndOverflow}
__focusable={true}
onClick={() => onPaginationClick(headerBarRef, 'forward')}
ariaLabel={i18n('i18nStrings.scrollRightAriaLabel', i18nStrings?.scrollRightAriaLabel)}
/>
</span>
)}
<TabList
{...tabActionAttributes}
className={clsx(styles['tabs-header-list'], analyticsSelectors['tabs-header-list'])}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
ref={headerBarRef as never}
onScroll={onScroll}
onKeyDown={onKeyDown}
onFocus={onFocus}
onBlur={onBlur}
>
{tabs.map(renderTabHeader)}
</TabList>
</SingleTabStopNavigationProvider>
{horizontalOverflow && (
<span className={rightButtonClasses}>
<InternalButton
formAction="none"
variant="icon"
iconName="angle-right"
disabled={!inlineEndOverflow}
__focusable={true}
onClick={() => onPaginationClick(headerBarRef, 'forward')}

Check warning on line 337 in src/tabs/tab-header-bar.tsx

View check run for this annotation

Codecov / codecov/patch

src/tabs/tab-header-bar.tsx#L337

Added line #L337 was not covered by tests
ariaLabel={i18n('i18nStrings.scrollRightAriaLabel', i18nStrings?.scrollRightAriaLabel)}
/>
</span>
)}
</div>
{actions && <div className={styles['actions-container']}>{actions}</div>}
</div>
);

Expand Down
4 changes: 4 additions & 0 deletions src/test-utils/dom/tabs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ export default class TabsWrapper extends ComponentWrapper<HTMLButtonElement> {
findActiveTabAction(): ElementWrapper | null {
return this.find(`.${styles['tabs-tab-active']} .${styles['tabs-tab-action']}`);
}

findActions(): ElementWrapper | null {
return this.find(`.${styles['actions-container']}`);
}
}
Loading