-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs): add ContentRoutingTabs component
- Loading branch information
1 parent
a7e317f
commit 364028e
Showing
5 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/docs/components/ContentRoutingTabs/ContentRoutingTabs.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ContentRoutingTabs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/docs/components/ContentRoutingTabs/useContentRoutingTabs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters