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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@radix-ui/react-accordion": "^1.2.12",
"@radix-ui/react-dropdown-menu": "^2.1.16",
"@radix-ui/react-select": "^2.2.6",
"@radix-ui/react-tabs": "^1.1.13",
"@radix-ui/react-tooltip": "^1.2.8",
"@react-hook/media-query": "^1.1.1",
"@sentry/gatsby": "^9.19.0",
Expand Down
29 changes: 10 additions & 19 deletions src/components/Layout/mdx/Tabs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Tabs, Tab } from './Tabs';

describe('Tabs', () => {
Expand Down Expand Up @@ -31,11 +31,11 @@ describe('Tabs', () => {
</Tabs>,
);

expect(screen.getByText('Content A')).toBeInTheDocument();
expect(screen.queryByText('Content B')).not.toBeInTheDocument();
expect(screen.getByRole('tabpanel')).toHaveTextContent('Content A');
});

it('switches content when a tab is clicked', () => {
it('switches content when a tab is clicked', async () => {
const user = userEvent.setup();
render(
<Tabs>
<Tab value="a" label="Alpha">
Expand All @@ -47,13 +47,13 @@ describe('Tabs', () => {
</Tabs>,
);

fireEvent.click(screen.getByRole('tab', { name: 'Beta' }));
await user.click(screen.getByRole('tab', { name: 'Beta' }));

expect(screen.queryByText('Content A')).not.toBeInTheDocument();
expect(screen.getByText('Content B')).toBeInTheDocument();
expect(screen.getByRole('tabpanel')).toHaveTextContent('Content B');
});

it('sets aria-selected correctly', () => {
it('sets aria-selected correctly', async () => {
const user = userEvent.setup();
render(
<Tabs>
<Tab value="a" label="Alpha">
Expand All @@ -68,7 +68,7 @@ describe('Tabs', () => {
expect(screen.getByRole('tab', { name: 'Alpha' })).toHaveAttribute('aria-selected', 'true');
expect(screen.getByRole('tab', { name: 'Beta' })).toHaveAttribute('aria-selected', 'false');

fireEvent.click(screen.getByRole('tab', { name: 'Beta' }));
await user.click(screen.getByRole('tab', { name: 'Beta' }));

expect(screen.getByRole('tab', { name: 'Alpha' })).toHaveAttribute('aria-selected', 'false');
expect(screen.getByRole('tab', { name: 'Beta' })).toHaveAttribute('aria-selected', 'true');
Expand All @@ -88,13 +88,4 @@ describe('Tabs', () => {

expect(screen.getByRole('tabpanel')).toHaveTextContent('Content A');
});

it('renders nothing for Tab used outside of Tabs', () => {
const { container } = render(
<Tab value="a" label="Alpha">
Orphan
</Tab>,
);
expect(container).toBeEmptyDOMElement();
});
});
88 changes: 38 additions & 50 deletions src/components/Layout/mdx/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,63 @@
import React, { useState, createContext, useContext, isValidElement, ReactNode, useId } from 'react';
import React, { isValidElement, ReactNode } from 'react';
import * as RadixTabs from '@radix-ui/react-tabs';
import cn from '@ably/ui/core/utils/cn';

type TabsContextType = {
activeTab: string;
tabsId: string;
};

const TabsContext = createContext<TabsContextType | undefined>(undefined);

interface TabProps {
value: string;
label: string;
children: ReactNode;
}

export const Tab: React.FC<TabProps> = ({ value, children }) => {
const context = useContext(TabsContext);
if (!context) {
return null;
}
return context.activeTab === value ? (
<div role="tabpanel" id={`${context.tabsId}-panel-${value}`} aria-labelledby={`${context.tabsId}-tab-${value}`}>
{children}
</div>
) : null;
export const Tab: React.FC<TabProps> = ({ children }) => {
// Tab is only used declaratively — Tabs reads its props and renders RadixTabs.Content.
// When used outside of Tabs, render nothing.
return <>{children}</>;
};

interface TabsProps {
children: ReactNode;
}

export const Tabs: React.FC<TabsProps> = ({ children }) => {
const tabsId = useId();

const tabs: { value: string; label: string }[] = [];
const contentByValue: Record<string, ReactNode> = {};

React.Children.forEach(children, (child) => {
if (isValidElement<TabProps>(child) && child.props.value) {
tabs.push({ value: child.props.value, label: child.props.label ?? child.props.value });
contentByValue[child.props.value] = child.props.children;
}
});

const [activeTab, setActiveTab] = useState(tabs[0]?.value ?? '');

return (
<TabsContext.Provider value={{ activeTab, tabsId }}>
<div className="my-5 border border-neutral-300 dark:border-neutral-800 rounded-lg overflow-hidden">
<div
className="flex gap-1 border-b border-neutral-300 dark:border-neutral-800 bg-neutral-100 dark:bg-neutral-1100 px-2 pt-2"
role="tablist"
>
{tabs.map(({ value, label }) => (
<button
key={value}
id={`${tabsId}-tab-${value}`}
role="tab"
aria-selected={activeTab === value}
aria-controls={`${tabsId}-panel-${value}`}
onClick={() => setActiveTab(value)}
className={cn(
'px-4 py-2 text-sm font-medium rounded-t-md transition-colors cursor-pointer',
activeTab === value
? 'bg-white dark:bg-neutral-1300 text-neutral-1300 dark:text-neutral-000 border border-neutral-300 dark:border-neutral-800 border-b-white dark:border-b-neutral-1300 -mb-px'
: 'text-neutral-700 dark:text-neutral-500 hover:text-neutral-1000 dark:hover:text-neutral-300',
)}
>
{label}
</button>
))}
</div>
<div className="p-5">{children}</div>
</div>
</TabsContext.Provider>
<RadixTabs.Root
defaultValue={tabs[0]?.value}
className="my-5 border border-neutral-300 dark:border-neutral-800 rounded-lg overflow-hidden"
>
<RadixTabs.List className="flex gap-1 border-b border-neutral-300 dark:border-neutral-800 bg-neutral-100 dark:bg-neutral-1100 px-2 pt-2">
{tabs.map(({ value, label }) => (
<RadixTabs.Trigger
key={value}
value={value}
style={{ outline: 'none', borderTopLeftRadius: '6px', borderTopRightRadius: '6px' }}
className={cn(
'px-4 py-2 ui-text-label3 transition-colors cursor-pointer',
'border border-transparent',
'text-neutral-700 dark:text-neutral-500 hover:text-neutral-1000 dark:hover:text-neutral-300',
'data-[state=active]:bg-white data-[state=active]:dark:bg-neutral-1300 data-[state=active]:text-neutral-1300 data-[state=active]:dark:text-neutral-000',
'data-[state=active]:border-neutral-300 data-[state=active]:dark:border-neutral-800',
'data-[state=active]:border-b-white data-[state=active]:dark:border-b-neutral-1300 data-[state=active]:-mb-px',
)}
>
{label}
</RadixTabs.Trigger>
))}
</RadixTabs.List>
{tabs.map(({ value }) => (
<RadixTabs.Content key={value} value={value} className="px-5 pt-5 pb-2.5">
{contentByValue[value]}
</RadixTabs.Content>
))}
</RadixTabs.Root>
);
};
33 changes: 4 additions & 29 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3403,7 +3403,7 @@
"@radix-ui/react-use-previous" "1.1.1"
"@radix-ui/react-use-size" "1.1.1"

"@radix-ui/react-tabs@^1.1.1":
"@radix-ui/react-tabs@^1.1.1", "@radix-ui/react-tabs@^1.1.13":
version "1.1.13"
resolved "https://registry.yarnpkg.com/@radix-ui/react-tabs/-/react-tabs-1.1.13.tgz#3537ce379d7e7ff4eeb6b67a0973e139c2ac1f15"
integrity sha512-7xdcatg7/U+7+Udyoj2zodtI9H/IIopqo+YOIcZOq1nJwXWBZ9p8xiu5llXlekDbZkca79a/fozEYQXIA4sW6A==
Expand Down Expand Up @@ -14732,16 +14732,7 @@ string-similarity@^1.2.2:
lodash.map "^4.6.0"
lodash.maxby "^4.6.0"

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -14851,7 +14842,7 @@ stringify-entities@^4.0.0:
character-entities-html4 "^2.0.0"
character-entities-legacy "^3.0.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand All @@ -14872,13 +14863,6 @@ strip-ansi@^5.2.0:
dependencies:
ansi-regex "^4.1.0"

strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.2"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.2.tgz#132875abde678c7ea8d691533f2e7e22bb744dba"
Expand Down Expand Up @@ -16328,7 +16312,7 @@ word-wrap@^1.2.5:
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34"
integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -16346,15 +16330,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.0.1, wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down