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 option to make TabList button be a element #3508

Merged
merged 5 commits into from
Sep 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function FeedExploreHeader({
container: className.tabBarContainer,
}}
shouldMountInactive
tabTag="a"
>
{Object.entries(urlToTab).map(([url, label]) => (
<Tab key={label} label={label} url={url} />
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/components/tabs/TabContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, {
} from 'react';
import classNames from 'classnames';
import { useRouter } from 'next/router';
import TabList, { TabListProps } from './TabList';
import TabList, { AllowedTabTags, TabListProps } from './TabList';
import { RenderTab } from './common';

export interface TabProps<T extends string> {
Expand Down Expand Up @@ -48,6 +48,7 @@ export interface TabContainerProps<T extends string = string> {
tabListProps?: Pick<TabListProps, 'className' | 'autoScrollActive'>;
showBorder?: boolean;
renderTab?: RenderTab;
tabTag?: AllowedTabTags;
}

export function TabContainer<T extends string = string>({
Expand All @@ -61,6 +62,7 @@ export function TabContainer<T extends string = string>({
controlledActive,
tabListProps = {},
renderTab,
tabTag,
}: TabContainerProps<T>): ReactElement {
const router = useRouter();

Expand Down Expand Up @@ -144,6 +146,7 @@ export function TabContainer<T extends string = string>({
active={currentActive}
className={tabListProps?.className}
autoScrollActive={tabListProps?.autoScrollActive}
tag={tabTag}
/>
</header>
{render}
Expand Down
18 changes: 14 additions & 4 deletions packages/shared/src/components/tabs/TabList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import React, {
} from 'react';
import { RenderTab } from './common';

export type AllowedTabTags = keyof Pick<JSX.IntrinsicElements, 'a' | 'button'>;

interface ClassName {
indicator?: string;
item?: string;
Expand All @@ -25,6 +27,7 @@ export interface TabListProps<T extends string = string> {
className?: ClassName;
autoScrollActive?: boolean;
renderTab?: RenderTab;
tag?: AllowedTabTags;
}

function TabList<T extends string = string>({
Expand All @@ -34,13 +37,15 @@ function TabList<T extends string = string>({
className = {},
autoScrollActive,
renderTab,
tag: Tag = 'button',
}: TabListProps<T>): ReactElement {
const hasActive = items.includes(active);
const currentActiveTab = useRef<HTMLButtonElement>(null);
const [dimensions, setDimensions] = useState<DimensionProps>({
offset: 0,
indicatorOffset: 0,
});
const isAnchor = Tag === 'a';

const scrollIfNotInView = useCallback(() => {
const { activeTabRect, offset } = dimensions;
Expand Down Expand Up @@ -113,7 +118,7 @@ function TabList<T extends string = string>({
);

return (
<button
<Tag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If now is an anchor maybe we want to add useful attributes like aria-label, title, href (this should be required imo). What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added label and title. Adding href needs a bit more work I see

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's hard to implement, also we can put role="button" and href="#" if the behaviour is fully JS-side

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a chat with @rebelchris, we'll merge it without the href and then run it through the SEO tool to look. If it still complains, I'll refactor it so that it gets proper href.

key={tab}
ref={(el) => {
if (!el || !isActive) {
Expand All @@ -126,13 +131,18 @@ function TabList<T extends string = string>({
className.item,
'relative p-2 py-4 text-center font-bold typo-callout',
isActive ? '' : 'text-text-tertiary',
isAnchor && 'cursor-pointer',
Comment on lines 133 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non mandatory: if we have more conditional classes maybe the object structure can be more readable, wdyt?

classNames(
              className.item,
              'relative p-2 py-4 text-center font-bold typo-callout',
              { 'text-text-tertiary': !isActive, 'cursor-pointer': isAnchor},
)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we'll have any more now

)}
onClick={() => onClick(tab)}
type="button"
role="menuitem"
{...(isAnchor
? {
'aria-label': tab,
title: tab,
}
: { type: 'button', role: 'menuitem' })}
>
{renderedTab}
</button>
</Tag>
);
})}
{!!indicatorOffset && hasActive && (
Expand Down