Skip to content

Commit

Permalink
perf: line number blink
Browse files Browse the repository at this point in the history
  • Loading branch information
GaoNeng-wWw committed Nov 2, 2024
1 parent c25591a commit 271c969
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/docs/components/show/code/basic-usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import {Code, CodeFile} from '@qwqui/core';

export default function App(){
const codes = Array.from({length:10000}).fill('console.log("aaa");').join('\n');
const codes = Array.from({length:1000}).fill('console.log("aaa");').join('\n');
return (
<Code>
<CodeFile name="index.ts" code="// Comment" language="javascript" />
Expand Down
5 changes: 4 additions & 1 deletion packages/components/code/src/code-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const CodeBody = () => {
const {activeCode,codeFiles, cache, setCache,init} = useContext<CodeContextType>(CodeContext);
const [codeLanguage, setCodeLanguage] = useState('');
const [activeCodeFile, setActvieCodeFile] = useState<CodeFileProps>();
const [hidden, setHidden] = useState(false);
const codeBodyDom = useRef<HTMLDivElement>();
useEffect(()=>{
const [codeFile] = codeFiles.filter((file) => file.name === activeCode);
Expand All @@ -27,15 +28,17 @@ export const CodeBody = () => {
if(!codeBodyDom.current && loading){
return;
}
setHidden(true);
const ele = codeBodyDom.current;
const lastChild= ele.querySelectorAll('code > span:last-child > :first-child')[0];
if (lastChild){
const width = (lastChild as HTMLElement).offsetWidth;
ele.style['--line-number-width']=`${width}px`;
ele.style.setProperty('--line-number-width', `${width}px`)
}
setHidden(false);
},[init, loading]);
return (
<div ref={codeBodyDom} className={style.root} dangerouslySetInnerHTML={{__html: html}}></div>
<div ref={codeBodyDom} aria-hidden={hidden} className={style.root} dangerouslySetInnerHTML={{__html: html}}></div>
)
}
2 changes: 1 addition & 1 deletion packages/components/code/src/code-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export type CodeContextType = {
setPrevWidth: React.Dispatch<React.SetStateAction<number>>;
cache: Record<string, string>;
setCache: React.Dispatch<React.SetStateAction<Record<string,string>>>;
init: boolean
init: boolean;
}
4 changes: 2 additions & 2 deletions packages/components/code/src/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export const Code = factory<CodeWrapper>((props)=>{
}}>
<div className={style.root}>
<CodeHeader />
{!loading && <CodeBody />}
{!loading && props.children}
<CodeBody />
{props.children}
</div>
</CodeContext.Provider>
)
Expand Down
5 changes: 5 additions & 0 deletions packages/components/code/src/styles/code-body.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
padding: 11px 12px;
font-family: 'Inter';
font-weight: regular;
opacity: 1;
--line-number-width: 48px;
&[aria-hidden=true]{
opacity: 0;
}
}

0 comments on commit 271c969

Please sign in to comment.