@@ -13,7 +13,7 @@ const components = {
1313 // todo: this bg style might need to become global for all code blocks
1414 className = { clsx (
1515 props . className ,
16- "!bg-neu-0/[.48] backdrop-blur-[6px] max-xs:[&_span]:!text-xs" ,
16+ "!bg-neu-0/[.48] backdrop-blur-[6px] [scrollbar-width:none] [&::-webkit-scrollbar]:size-0 max-xs:[&_span]:!text-xs" ,
1717 ) }
1818 tabIndex = { - 1 }
1919 />
@@ -27,25 +27,31 @@ export function ConsistencyFigure() {
2727 const [ queryCode , responseCode ] = queryRef . current ! . querySelectorAll (
2828 "code" ,
2929 ) as unknown as HTMLElement [ ]
30- let line = 0
30+ let i = 0
3131 const typeLines = [ 1 , 5 , 6 , 5 , 7 , 12 , 13 , 8 , 17 , 18 , 19 , 12 ]
3232 const queryLines = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 9 , 10 , 11 , 12 , 13 ]
33- let timer : any
33+ let timer : ReturnType < typeof setTimeout > | undefined = undefined
3434
3535 const highlightLine = ( ) => {
3636 // Reset previous line
37- queryCode . children [ queryLines . at ( line - 1 ) ! ] . classList . remove (
38- "highlighted" ,
39- )
40- responseCode . children [ typeLines . at ( line - 1 ) ! ] . classList . remove (
37+ queryCode . children [ queryLines . at ( i - 1 ) ! ] . classList . remove ( "highlighted" )
38+ responseCode . children [ typeLines . at ( i - 1 ) ! ] . classList . remove (
4139 "highlighted" ,
4240 )
4341
44- // TODO: Whenever we're adding .highlighted, we should also ensure it's visible on screen.
45- // We can simplify this by scrolling to bottom in last 3 steps.
46- queryCode . children [ queryLines . at ( line ) ! ] . classList . add ( "highlighted" )
47- responseCode . children [ typeLines . at ( line ) ! ] . classList . add ( "highlighted" )
48- line = ( line + 1 ) % typeLines . length
42+ queryCode . children [ queryLines . at ( i ) ! ] . classList . add ( "highlighted" )
43+ responseCode . children [ typeLines . at ( i ) ! ] . classList . add ( "highlighted" )
44+
45+ // We're scrolling to top when the first line is highlighted and we're
46+ // scrolling to the bottom when the last line is highlighted.
47+ const pre = responseCode . parentElement !
48+ if ( i === 0 ) {
49+ pre . scrollTo ( { top : 0 , behavior : "smooth" } )
50+ } else if ( i === typeLines . length - 3 ) {
51+ pre . scrollTo ( { top : pre . scrollHeight , behavior : "smooth" } )
52+ }
53+
54+ i = ( i + 1 ) % typeLines . length
4955
5056 timer = setTimeout ( highlightLine , 1_000 + Math . random ( ) * 200 )
5157 }
0 commit comments