Skip to content

Commit

Permalink
feat(docs): add ContentRoutingTabs component
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Oct 5, 2021
1 parent a7e317f commit 364028e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/docs/components/ContentRoutingTabs/ContentRoutingTabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Box, PillTabs } from '@bigcommerce/big-design';
import React from 'react';

import { ContentRoutingTabsProps } from './types';
import { useContentRoutingTabs } from './useContentRoutingTabs';

const RawContentRoutingTabs: React.FC<ContentRoutingTabsProps> = ({ routes, id }) => {
const { activeContent, activePills, pills, handlePillClick } = useContentRoutingTabs(routes, id);

return (
<>
<PillTabs activePills={activePills} items={pills} onPillClick={handlePillClick} />
<Box marginTop="xSmall">{activeContent?.render()}</Box>
</>
);
};

export const ContentRoutingTabs: React.FC<ContentRoutingTabsProps> = (props) => {
if (props.routes.length === 0) {
return null;
}

return <RawContentRoutingTabs {...props} />;
};
1 change: 1 addition & 0 deletions packages/docs/components/ContentRoutingTabs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ContentRoutingTabs';
10 changes: 10 additions & 0 deletions packages/docs/components/ContentRoutingTabs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PillTabItem } from '@bigcommerce/big-design';

export interface Route extends PillTabItem {
render(): JSX.Element;
}

export interface ContentRoutingTabsProps {
routes: Route[];
id: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useRouter } from 'next/router';

import { Route } from './types';

export const useContentRoutingTabs = (routes: Route[], id: string) => {
const { query, push } = useRouter();
const pills = routes.map(({ render, ...pill }) => pill);
const queryValue = query[id];
const activePill = queryValue && !Array.isArray(queryValue) ? queryValue : routes[0].id;
const activeContent = routes.find((content) => content.id === activePill);

const handlePillClick = (pillId: string) => {
push({ query: { ...query, [id]: pillId } }, undefined, { shallow: true });
};

return {
activeContent,
activePills: activePill ? [activePill] : [],
pills,
handlePillClick,
};
};
1 change: 1 addition & 0 deletions packages/docs/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './Code';
export * from './CodePreview';
export * from './CodeSnippet';
export * from './Collapsible';
export * from './ContentRoutingTabs';
export * from './List';
export * from './MethodBadge';
export * from './MethodList';
Expand Down

0 comments on commit 364028e

Please sign in to comment.