-
Notifications
You must be signed in to change notification settings - Fork 187
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
3 changed files
with
159 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** @jsxImportSource preact */ | ||
import type { ComponentChild } from "preact" | ||
import { useRef } from "preact/hooks" | ||
import { useTabState } from "./useTabState" | ||
import styles from "./Tabs.module.css" | ||
import { clsx } from "~/lib" | ||
const tabSlotKey = "tab." as const | ||
const panelSlotKey = "panel." as const | ||
|
||
type TabSlot = `${typeof tabSlotKey}${string}` | ||
type PanelSlot = `${typeof panelSlotKey}${string}` | ||
|
||
function isTabSlotEntry(entry: [string, ComponentChild]): entry is [TabSlot, ComponentChild] { | ||
const [key] = entry | ||
return key.startsWith(tabSlotKey) | ||
} | ||
|
||
function isPanelSlotEntry(entry: [string, ComponentChild]): entry is [PanelSlot, ComponentChild] { | ||
const [key] = entry | ||
return key.startsWith(panelSlotKey) | ||
} | ||
|
||
function getBaseKeyFromTab(slot: TabSlot) { | ||
return slot.replace(new RegExp(`^${tabSlotKey}`), "") | ||
} | ||
|
||
function getBaseKeyFromPanel(slot: PanelSlot) { | ||
return slot.replace(new RegExp(`^${panelSlotKey}`), "") | ||
} | ||
|
||
type Props = { | ||
[key: TabSlot | PanelSlot]: ComponentChild | ||
sharedStore?: string | ||
} | ||
|
||
export function TabsContent({ sharedStore, ...slots }: Props) { | ||
const tabs = Object.entries(slots).filter(isTabSlotEntry) | ||
const panels = Object.entries(slots).filter(isPanelSlotEntry) | ||
|
||
/** Used to focus next and previous tab on arrow key press */ | ||
const tabButtonRefs = useRef<Record<TabSlot, HTMLButtonElement | null>>({}) | ||
|
||
const firstPanelKey = panels[0] ? getBaseKeyFromPanel(panels[0][0]) : "" | ||
const [curr, setCurrStore] = useTabState(firstPanelKey, sharedStore) | ||
|
||
function moveFocus(event: KeyboardEvent) { | ||
if (event.key === "ArrowLeft") { | ||
const currIdx = tabs.findIndex(([key]) => getBaseKeyFromTab(key) === curr) | ||
if (currIdx > 0) { | ||
const [prevTabKey] = tabs[currIdx - 1] | ||
setCurrStore(getBaseKeyFromTab(prevTabKey)) | ||
tabButtonRefs.current[prevTabKey]?.focus() | ||
} | ||
} | ||
if (event.key === "ArrowRight") { | ||
const currIdx = tabs.findIndex(([key]) => getBaseKeyFromTab(key) === curr) | ||
if (currIdx < tabs.length - 1) { | ||
const [nextTabKey] = tabs[currIdx + 1] | ||
setCurrStore(getBaseKeyFromTab(nextTabKey)) | ||
tabButtonRefs.current[nextTabKey]?.focus() | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<div className={styles.contentContainer}> | ||
<div role="tablist" onKeyDown={moveFocus}> | ||
{tabs.map(([key, content]) => ( | ||
<button | ||
ref={(el) => (tabButtonRefs.current[key] = el)} | ||
onClick={() => { | ||
setCurrStore(getBaseKeyFromTab(key)) | ||
}} | ||
aria-selected={curr === getBaseKeyFromTab(key)} | ||
tabIndex={curr === getBaseKeyFromTab(key) ? 0 : -1} | ||
role="tab" | ||
type="button" | ||
data-astro-tab | ||
id={key} | ||
key={key} | ||
class={clsx( | ||
curr === getBaseKeyFromTab(key) ? styles.contentTabPrimary : styles.contentTabSecondary, | ||
styles.contentTab | ||
)} | ||
> | ||
{content} | ||
</button> | ||
))} | ||
</div> | ||
{panels.map(([key, content]) => ( | ||
<div | ||
hidden={curr !== getBaseKeyFromPanel(key)} | ||
role="tabpanel" | ||
aria-labelledby={`${tabSlotKey}${getBaseKeyFromPanel(key)}`} | ||
key={key} | ||
class={styles.panel} | ||
> | ||
{content} | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} |
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 +1,3 @@ | ||
export { Tabs } from "./Tabs" | ||
|
||
export { TabsContent } from "./TabsContent" |
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