Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 5fba1bc

Browse files
committed
update code components
1 parent e8d0b83 commit 5fba1bc

File tree

7 files changed

+51
-53
lines changed

7 files changed

+51
-53
lines changed

src/components/code/CodeContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22

33
const CodeContainer: React.FC<React.PropsWithChildren> = ({ children }) => {
44
return (
5-
<div className="w-full">
5+
<div className="w-full max-w-full">
66
<div
77
tabIndex={0}
88
className={

src/components/code/CodeSwitcher.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ import { highlight, RawCode } from 'codehike/code'
33
import CodeContainer from './CodeContainer'
44
import { CodeSwitcherSelect } from './CodeSwitcherSelect'
55
import CODE_THEME from './theme'
6-
import { HandlerProps } from './Pre'
6+
import Pre, { HandlerProps } from './Pre'
77

88
export async function CodeSwitcher({
99
code,
1010
...props
11-
}: { code: RawCode[]; showPanel?: boolean } & HandlerProps) {
11+
}: {
12+
code: RawCode[]
13+
showPanel?: boolean
14+
className?: string
15+
} & HandlerProps) {
1216
const highlighted = await Promise.all(
1317
code.map((codeblock) => highlight(codeblock, CODE_THEME)),
1418
)
1519

16-
console.log(highlighted)
17-
1820
return (
1921
<CodeContainer>
20-
<Suspense fallback={null}>
22+
<Suspense
23+
fallback={<Pre highlighted={highlighted[0]} {...props} showPanel />}
24+
>
2125
<CodeSwitcherSelect highlighted={highlighted} {...props} />
2226
</Suspense>
2327
</CodeContainer>

src/components/code/CodeSwitcherSelect.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function CodeSwitcherSelect({
3030
...props
3131
}: {
3232
highlighted: HighlightedCode[]
33+
className?: string
3334
showPanel?: boolean
3435
} & HandlerProps) {
3536
const { currentLanguage, setCurrentLanguage } = useLang()
@@ -51,7 +52,13 @@ export function CodeSwitcherSelect({
5152
<>
5253
{showPanel && (
5354
<>
54-
<Select value={selectedLang} onValueChange={setCurrentLanguage}>
55+
<Select
56+
value={selectedLang}
57+
onValueChange={(lang) => {
58+
// This is necessary to fix a ui update delay causing 200ms lag
59+
setTimeout(() => setCurrentLanguage(lang as LanguageId), 0)
60+
}}
61+
>
5562
<SelectTrigger
5663
aria-label="Switch code language"
5764
className="absolute right-20 top-1.5 hidden h-8 w-[120px] bg-white/5 text-2xs font-medium text-zinc-400 ring-1 ring-inset ring-zinc-300/10 hover:bg-white/7.5 dark:bg-white/2.5 dark:hover:bg-white/5 md:flex"

src/components/code/CodeWithTabs.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ import Pre from './Pre'
77
import CodeContainer from './CodeContainer'
88
import CODE_THEME from './theme'
99

10-
export interface CodeTabsProps extends React.PropsWithChildren {
11-
title?: string
12-
tabs: RawCode[]
13-
}
14-
1510
const Schema = Block.extend({ tabs: z.array(CodeBlock) })
16-
export async function CodeWithTabs(props: CodeTabsProps) {
11+
export async function CodeWithTabs(props: unknown) {
1712
const { tabs } = parseProps(props, Schema)
1813
return <CodeTabs tabs={tabs} />
1914
}
@@ -46,11 +41,7 @@ export async function CodeTabs(props: { tabs: RawCode[] }) {
4641
>
4742
<Pre
4843
highlighted={highlighted[i]}
49-
// check meta exists and has a file extension
50-
showPanel={
51-
!!highlighted[i].meta &&
52-
highlighted[i].meta.split('.').length > 1
53-
}
44+
showPanel={!!highlighted[i].meta}
5445
/>
5546
</TabsContent>
5647
))}

src/components/code/Pre.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
collapseContent,
1010
collapseTrigger,
1111
} from './annotations/collapse'
12+
import { tokenTransitions } from './annotations/token-transitions'
13+
import { cn } from '@/lib/utils'
1214

1315
export interface HandlerProps {
1416
enableTransitions?: boolean
@@ -17,27 +19,22 @@ export interface HandlerProps {
1719
type Props = {
1820
highlighted: HighlightedCode
1921
showPanel?: boolean
22+
className?: string
2023
} & HandlerProps
2124

2225
const Pre: React.FC<Props> = ({
2326
highlighted,
2427
showPanel,
2528
enableTransitions,
29+
className,
2630
}) => {
2731
const fileName = highlighted.meta
2832

29-
const handlers = [
30-
callout,
31-
className,
32-
fold,
33-
collapse,
34-
collapseTrigger,
35-
collapseContent,
36-
]
33+
let handlers = [callout, fold, collapse, collapseTrigger, collapseContent]
3734
// TODO: Fix transitions, they currently break colours in the code when switching languages
38-
// if (enableTransitions) {
39-
// handlers = [...handlers, tokenTransitions]
40-
// }
35+
if (enableTransitions) {
36+
handlers = [...handlers, tokenTransitions]
37+
}
4138

4239
return (
4340
<>
@@ -53,7 +50,7 @@ const Pre: React.FC<Props> = ({
5350
<CodeHikePre
5451
code={highlighted}
5552
handlers={handlers}
56-
className="overflow-auto overscroll-x-contain p-4"
53+
className={cn('overflow-auto overscroll-x-contain p-4', className)}
5754
style={{
5855
...highlighted.style,
5956
fontSize: '0.875rem',

src/components/code/annotations/token-transitions.client.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class SmoothPre extends React.Component<CustomPreProps> {
4343
duration: options.duration * MAX_TRANSITION_DURATION,
4444
delay: options.delay * MAX_TRANSITION_DURATION,
4545
easing: options.easing,
46-
fill: 'both',
46+
fill: 'backwards',
4747
})
4848
})
4949
}

src/hooks/useLang.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
1+
import { useSearchParams } from 'next/navigation'
2+
import { useCallback } from 'react'
23

34
export type LanguageId =
45
| 'javascript'
@@ -26,36 +27,34 @@ const languages = ['javascript', 'python', 'go', 'typescript', 'dart']
2627

2728
const useLang = () => {
2829
const searchParams = useSearchParams()
29-
const router = useRouter()
30-
const pathname = usePathname()
3130

3231
const queryParamLang = searchParams.get('lang') as LanguageId
3332

3433
const currentLanguage = languages.includes(queryParamLang)
3534
? queryParamLang
3635
: languages[0]
3736

38-
const setLanguage = (id: LanguageId) => {
39-
// Recommended to use the Link component <Link href={`?lang=${id}`} scroll={false} />
40-
// Apparently this nonsense is necessary to update the URL.
41-
// See: https://github.com/vercel/next.js/discussions/47583
42-
const currentParams = new URLSearchParams(
43-
Array.from(searchParams.entries()),
44-
)
37+
const setLanguage = useCallback(
38+
(id: LanguageId) => {
39+
// Apparently this nonsense is necessary to update the URL.
40+
// See: https://github.com/vercel/next.js/discussions/47583
41+
const currentParams = new URLSearchParams(
42+
Array.from(searchParams.entries()),
43+
)
4544

46-
if (!id) {
47-
currentParams.delete('lang')
48-
} else {
49-
currentParams.set('lang', id)
50-
}
45+
if (!id) {
46+
currentParams.delete('lang')
47+
} else {
48+
currentParams.set('lang', id)
49+
}
5150

52-
const search = currentParams.toString()
53-
const query = search ? `?${search}` : ''
54-
router.push(`${pathname}${query}`, {
55-
// Don't return to the top of the page
56-
scroll: false,
57-
})
58-
}
51+
const search = currentParams.toString()
52+
const query = search ? `?${search}` : ''
53+
54+
window.history.pushState(null, '', query)
55+
},
56+
[searchParams],
57+
)
5958

6059
return {
6160
languages,

0 commit comments

Comments
 (0)