1 "use client"
2
3 import React from "react"
4 import { useColorMode } from "@/providers/ColorMode"
5 import clsx from "clsx"
6 import { BaseTabType } from "../../../hooks/use-tabs"
7 import { useScrollPositionBlocker } from "../../../hooks/use-scroll-utils"
8
9 type CodeTabProps = BaseTabType & {
10 // Children are handled in MDX to allow for code blocks and inline code to be rendered
11 children: React.ReactNode
12 isSelected?: boolean
13 blockStyle?: string
14 changeSelectedTab?: (tab: BaseTabType) => void
15 pushRef?: (tabButton: HTMLButtonElement | null) => void
16 }
17
18 export const CodeTab = ({
19 label,
20 value,
21 isSelected = false,
22 blockStyle = "loud",
23 changeSelectedTab,
24 pushRef,
25 }: CodeTabProps) => {
26 const { colorMode } = useColorMode()
27 const { blockElementScrollPositionUntilNextRender } =
28 useScrollPositionBlocker()
29
30 return (
31 <li>
32 <button
33 className={clsx(
34 "text-compact-x-small-plus font-base xs:border-0 pb-docs_0.5 relative",
35 !isSelected && [
36 blockStyle === "loud" && "text-medusa-contrast-fg-secondary",
37 blockStyle === "subtle" && [
38 colorMode === "light" &&
39 "text-medusa-fg-subtle hover:bg-medusa-bg-base",
40 colorMode === "dark" &&
41 "text-medusa-contrast-fg-secondary hover:bg-medusa-code-bg-base",
42 ],
43 ],
44 isSelected && [
45 blockStyle === "loud" && "text-medusa-contrast-fg-primary",
46 blockStyle === "subtle" && [
47 colorMode === "light" &&
48 "xs:border-medusa-border-base text-medusa-contrast-fg-primary",
49 colorMode === "dark" &&
50 "xs:border-medusa-code-border text-medusa-contrast-fg-primary",
51 ],
52 ]
53 )}
54 ref={(tabButton) => pushRef?.(tabButton)}
55 onClick={(e) => {
56 blockElementScrollPositionUntilNextRender(
57 e.target as HTMLButtonElement
58 )
59 changeSelectedTab?.({ label, value })
60 }}
61 aria-selected={isSelected}
62 role="tab"
63 >
64 {label}
65 </button>
66 </li>
67 )
68 }