-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
617 additions
and
8 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { Navigate, RouteObject, useRoutes } from 'react-router-dom' | ||
import { UiThemeLink } from '@pubkey-ui/core' | ||
import { Link, Navigate, RouteObject, useRoutes } from 'react-router-dom' | ||
import { DemoFeature } from './features' | ||
import { DashboardFeature } from './features/dashboard/dashboard-feature' | ||
import { DevFeature } from './features/dev/dev-feature' | ||
|
||
const routes: RouteObject[] = [ | ||
{ path: '/', element: <Navigate to="/dashboard" replace /> }, | ||
{ path: '/dashboard', element: <DashboardFeature /> }, | ||
{ path: '/demo', element: <DemoFeature /> }, | ||
{ path: '/demo/*', element: <DemoFeature /> }, | ||
{ path: '/dev', element: <DevFeature /> }, | ||
] | ||
|
||
export function AppRoutes() { | ||
return useRoutes(routes) | ||
} | ||
|
||
export const ThemeLink: UiThemeLink = ({ children, ...props }) => <Link {...props}>{children}</Link> |
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
42 changes: 42 additions & 0 deletions
42
apps/web/src/app/features/demo/demo-feature-tab-routes.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,42 @@ | ||
import { SimpleGrid } from '@mantine/core' | ||
import { UiCard, UiTabRoutes } from '@pubkey-ui/core' | ||
import { DemoCard } from './demo-card' | ||
|
||
export function DemoFeatureTabRoutes() { | ||
return ( | ||
<DemoCard title="Time"> | ||
<UiTabRoutes | ||
baseUrl="/demo" | ||
tabs={[ | ||
{ | ||
value: 'overview', | ||
label: 'Overview', | ||
component: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<UiCard title="Overview">Overview</UiCard> | ||
</SimpleGrid> | ||
), | ||
}, | ||
{ | ||
value: 'content', | ||
label: 'Content', | ||
component: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<UiCard title="Content">Content</UiCard> | ||
</SimpleGrid> | ||
), | ||
}, | ||
{ | ||
value: 'settings', | ||
label: 'Settings', | ||
component: ( | ||
<SimpleGrid cols={2} spacing="md"> | ||
<UiCard title="Settings">Settings</UiCard> | ||
</SimpleGrid> | ||
), | ||
}, | ||
]} | ||
/> | ||
</DemoCard> | ||
) | ||
} |
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
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
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 './ui-tab-routes' |
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,54 @@ | ||
import { Box, Tabs, TabsProps, Text } from '@mantine/core' | ||
import { ReactElement, ReactNode } from 'react' | ||
import { Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom' | ||
|
||
export interface UiTabRoute { | ||
component: ReactNode | ||
label: ReactElement | string | ||
value: string | ||
} | ||
|
||
export function UiTabRoutes({ | ||
grow = false, | ||
tabs, | ||
baseUrl, | ||
...props | ||
}: Omit<TabsProps, 'children'> & { | ||
children?: ReactNode | ||
baseUrl?: string | ||
grow?: boolean | ||
tabs: UiTabRoute[] | ||
}) { | ||
const navigate = useNavigate() | ||
const location = useLocation() | ||
// Set the active tab based on matching the location pathname with the tab value | ||
const activeTab = tabs.find((tab) => location.pathname.includes(`/${tab.value}`))?.value | ||
// Set default redirect route to the first tab | ||
const redirect = tabs.length && tabs[0].value !== '' ? tabs[0].value : undefined | ||
|
||
return ( | ||
<Box> | ||
<Tabs | ||
value={activeTab} | ||
onChange={(value) => navigate(`${baseUrl ? `${baseUrl}/${value}` : value}`)} | ||
mb="md" | ||
{...props} | ||
> | ||
<Tabs.List grow={grow}> | ||
{tabs.map((tab) => ( | ||
<Tabs.Tab key={tab.value} value={tab.value}> | ||
<Text>{tab.label}</Text> | ||
</Tabs.Tab> | ||
))} | ||
</Tabs.List> | ||
</Tabs> | ||
<Routes> | ||
{redirect ? <Route index element={<Navigate replace to={`./${redirect}`} />} /> : null} | ||
{tabs.map((tab) => ( | ||
<Route key={tab.value} path={`${tab.value}/*`} element={tab.component} /> | ||
))} | ||
<Route path="*" element={<Navigate replace to={`./${redirect}`} />} /> | ||
</Routes> | ||
</Box> | ||
) | ||
} |
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
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
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"group", | ||
"search-input", | ||
"stack", | ||
"tab-routes", | ||
"theme", | ||
"time", | ||
"toast" | ||
|
54 changes: 54 additions & 0 deletions
54
...tors/src/generators/component/files/tab-routes/__prefixFileName__-tab-routes.tsx.template
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,54 @@ | ||
import { Box, Tabs, TabsProps, Text } from '@mantine/core' | ||
import { ReactElement, ReactNode } from 'react' | ||
import { Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom' | ||
|
||
export interface <%= prefix.className %>TabRoute { | ||
component: ReactNode | ||
label: ReactElement | string | ||
value: string | ||
} | ||
|
||
export function <%= prefix.className %>TabRoutes({ | ||
grow = false, | ||
tabs, | ||
baseUrl, | ||
...props | ||
}: Omit<TabsProps, 'children'> & { | ||
children?: ReactNode | ||
baseUrl?: string | ||
grow?: boolean | ||
tabs: <%= prefix.className %>TabRoute[] | ||
}) { | ||
const navigate = useNavigate() | ||
const location = useLocation() | ||
// Set the active tab based on matching the location pathname with the tab value | ||
const activeTab = tabs.find((tab) => location.pathname.includes(`/${tab.value}`))?.value | ||
// Set default redirect route to the first tab | ||
const redirect = tabs.length && tabs[0].value !== '' ? tabs[0].value : undefined | ||
|
||
return ( | ||
<Box> | ||
<Tabs | ||
value={activeTab} | ||
onChange={(value) => navigate(`${baseUrl ? `${baseUrl}/${value}` : value}`)} | ||
mb="md" | ||
{...props} | ||
> | ||
<Tabs.List grow={grow}> | ||
{tabs.map((tab) => ( | ||
<Tabs.Tab key={tab.value} value={tab.value}> | ||
<Text>{tab.label}</Text> | ||
</Tabs.Tab> | ||
))} | ||
</Tabs.List> | ||
</Tabs> | ||
<Routes> | ||
{redirect ? <Route index element={<Navigate replace to={`./${redirect}`} />} /> : null} | ||
{tabs.map((tab) => ( | ||
<Route key={tab.value} path={`${tab.value}/*`} element={tab.component} /> | ||
))} | ||
<Route path="*" element={<Navigate replace to={`./${redirect}`} />} /> | ||
</Routes> | ||
</Box> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
packages/generators/src/generators/component/files/tab-routes/index.ts.template
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 './<%= prefixFileName %>-tab-routes' |
Oops, something went wrong.