-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(headless SSR): add tabs to ssr samples (#4303)
- Loading branch information
Showing
15 changed files
with
269 additions
and
15 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
15 changes: 15 additions & 0 deletions
15
packages/samples/headless-ssr/common/components/common/tab-manager.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,15 @@ | ||
import {TabManager as TabManagerController} from '@coveo/headless/ssr'; | ||
|
||
interface TabManagerCommonProps { | ||
controller: Omit<TabManagerController, 'state' | 'subscribe'> | undefined; | ||
value: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export default function TabManagerCommon({ | ||
controller, | ||
value, | ||
children, | ||
}: TabManagerCommonProps) { | ||
return <div role="tablist">{children}</div>; | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/samples/headless-ssr/common/components/common/tab.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,32 @@ | ||
import {Tab, TabManager, TabState} from '@coveo/headless/ssr'; | ||
|
||
interface TabCommonProps { | ||
state: TabState; | ||
methods: Omit<Tab, 'state' | 'subscribe'> | undefined; | ||
activeTab: string; | ||
tabName: string; | ||
tabLabel: string; | ||
} | ||
|
||
export default function TabCommon({ | ||
state, | ||
methods, | ||
activeTab, | ||
tabName, | ||
tabLabel, | ||
}: TabCommonProps) { | ||
function handleClickTab() { | ||
if (activeTab !== tabName) methods?.select(); | ||
} | ||
|
||
return ( | ||
<button | ||
role="tab" | ||
aria-selected={state.isActive} | ||
key={tabName} | ||
onClick={() => handleClickTab()} | ||
> | ||
{state.isActive ? <strong>{tabLabel}</strong> : tabLabel} | ||
</button> | ||
); | ||
} |
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
36 changes: 36 additions & 0 deletions
36
packages/samples/headless-ssr/common/components/generic/tab.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,36 @@ | ||
import {Tab as TabController, TabManager, TabState} from '@coveo/headless/ssr'; | ||
import {useEffect, useState, FunctionComponent} from 'react'; | ||
import TabCommon from '../common/tab'; | ||
|
||
interface TabProps { | ||
staticState: TabState; | ||
controller?: TabController; | ||
tabManager?: TabManager; | ||
tabName: string; | ||
tabLabel: string; | ||
} | ||
|
||
export const Tab: FunctionComponent<TabProps> = ({ | ||
staticState, | ||
controller, | ||
tabManager, | ||
tabName, | ||
tabLabel, | ||
}) => { | ||
const [state, setState] = useState(staticState); | ||
|
||
useEffect( | ||
() => controller?.subscribe(() => setState({...controller.state})), | ||
[controller] | ||
); | ||
|
||
return ( | ||
<TabCommon | ||
state={state} | ||
methods={controller} | ||
activeTab={tabManager?.state.activeTab ?? ''} | ||
tabName={tabName} | ||
tabLabel={tabLabel} | ||
/> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/samples/headless-ssr/common/components/generic/tabs-manager.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,33 @@ | ||
import { | ||
TabManagerState, | ||
TabManager as TabManagerController, | ||
} from '@coveo/headless-react/ssr'; | ||
import {useEffect, useState, FunctionComponent} from 'react'; | ||
import TabManagerCommon from '../common/tab-manager'; | ||
|
||
interface TabManagerProps { | ||
staticState: TabManagerState; | ||
controller?: TabManagerController; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const TabManager: FunctionComponent<TabManagerProps> = ({ | ||
staticState, | ||
controller, | ||
children, | ||
}: TabManagerProps) => { | ||
const [state, setState] = useState(staticState); | ||
|
||
useEffect( | ||
() => controller?.subscribe?.(() => setState({...controller.state})), | ||
[controller] | ||
); | ||
|
||
return ( | ||
<TabManagerCommon | ||
controller={controller} | ||
value={state.activeTab} | ||
children={children} | ||
/> | ||
); | ||
}; |
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
17 changes: 17 additions & 0 deletions
17
packages/samples/headless-ssr/common/components/react/tab-manager.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,17 @@ | ||
'use client'; | ||
|
||
import {useTabManager} from '../../lib/react/engine'; | ||
import TabManagerCommon from '../common/tab-manager'; | ||
import Tab from './tab'; | ||
|
||
export default function TabManager() { | ||
const {state, methods} = useTabManager(); | ||
|
||
return ( | ||
<TabManagerCommon controller={methods} value={state.activeTab}> | ||
<Tab tabName={'all'} tabLabel="All"></Tab> | ||
<Tab tabName={'countries'} tabLabel="Countries"></Tab> | ||
<Tab tabName={'videos'} tabLabel="Videos"></Tab> | ||
</TabManagerCommon> | ||
); | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/samples/headless-ssr/common/components/react/tab.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,44 @@ | ||
'use client'; | ||
|
||
import {TabManager} from '@coveo/headless-react/ssr'; | ||
import { | ||
useTabAll, | ||
useTabCountries, | ||
useTabManager, | ||
useTabVideos, | ||
} from '../../lib/react/engine'; | ||
import TabCommon from '../common/tab'; | ||
|
||
export default function Tab({ | ||
tabName, | ||
tabLabel, | ||
}: { | ||
tabName: string; | ||
tabLabel: string; | ||
}) { | ||
const tabManager = useTabManager(); | ||
|
||
let controller; | ||
|
||
if (tabName === 'all') { | ||
controller = useTabAll; | ||
} else if (tabName === 'countries') { | ||
controller = useTabCountries; | ||
} else if (tabName === 'videos') { | ||
controller = useTabVideos; | ||
} else { | ||
throw new Error(`Unknown tab: ${tabName}`); | ||
} | ||
|
||
const {state, methods} = controller(); | ||
|
||
return ( | ||
<TabCommon | ||
state={state} | ||
methods={methods} | ||
activeTab={tabManager.state.activeTab} | ||
tabName={tabName} | ||
tabLabel={tabLabel} | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.