Skip to content

Commit

Permalink
fix presentify before release
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestTeluk committed May 6, 2023
1 parent add2623 commit 9762741
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
12 changes: 10 additions & 2 deletions packages/presentify/__tests__/Code.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ describe('Code', () => {
'1',
);
});
it('should render correctly with highlightLines', () => {
it('should render correctly without highlightLines', () => {
render(
<Code showLineNumbers highlightLines="1">
<Code showLineNumbers>
<code className="language-js">const test = 'test';</code>
</Code>,
);
expect(screen.getByTestId('code').children[0]).not.toHaveStyle(
'opacity: 0.3;',
);
});
it('should render correctly with highlightLines', () => {
render(
<Code showLineNumbers highlightLines="0">
<code className="language-js">const test = 'test';</code>
</Code>,
);
expect(screen.getByTestId('code').children[0]).toHaveStyle('opacity: 0.3;');
});
it('should render Fira Code correctly', () => {
render(
<PresentifyProvider options={{ useFiraCode: true }}>
Expand Down
4 changes: 2 additions & 2 deletions packages/presentify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"peerDependencies": {
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@types/react": "^18.0.0",
"react": "^18.0.0"
"@types/react": "^17.0.2",
"react": "^17.0.2"
},
"dependencies": {
"@mdx-js/react": "2.3.0",
Expand Down
32 changes: 24 additions & 8 deletions packages/presentify/src/components/Code.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Highlight, { defaultProps } from 'prism-react-renderer';
import React, { Key, ReactElement } from 'react';
import React, { Key, ReactElement, useEffect, useRef, useState } from 'react';

import { usePresentifyContext } from './PresentifyProvider';
import { calculateLinesToHighlight } from '../lib/calculateLinesToHighlight';
import { getLineNumberWidth } from '../lib/getLineNumberWidth';
import { getTheme } from '../lib/getTheme';
import { Line, LineNumber } from '../styles/Code.styled';
import { Line, LineNumber, StyledPre } from '../styles/Code.styled';

export const Code = ({
children,
Expand All @@ -16,6 +16,8 @@ export const Code = ({
showLineNumbers?: boolean;
highlightLines?: string;
}) => {
const [width, setWidth] = useState(0);
const ref = useRef<HTMLPreElement>(null);
const context = usePresentifyContext();
const { options } = context || {};
const className = children.props.className || '';
Expand All @@ -24,6 +26,23 @@ export const Code = ({
const lineWidth = getLineNumberWidth(code);
const shouldHighlightLine = calculateLinesToHighlight(highlightLines);

useEffect(() => {
const handleResize = () => {
setWidth(ref.current?.offsetWidth || 0);
};

// Initial measurement
handleResize();

// Add event listener on component mount
window.addEventListener('resize', handleResize);

// Clean up event listener on component unmount
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);

return (
<Highlight
{...defaultProps}
Expand All @@ -32,13 +51,10 @@ export const Code = ({
theme={getTheme(options?.theme)}
>
{({ tokens, getLineProps, getTokenProps }) => (
<pre
data-testid="code"
className={className}
style={{ width: '100vw', padding: '20px' }}
>
<StyledPre data-testid="code" className={className} ref={ref}>
{tokens.map((line, i: number) => (
<Line
elementWidth={width}
useFiraCode={options?.useFiraCode}
key={i}
{...getLineProps({ line, key: i })}
Expand All @@ -52,7 +68,7 @@ export const Code = ({
))}
</Line>
))}
</pre>
</StyledPre>
)}
</Highlight>
);
Expand Down
5 changes: 2 additions & 3 deletions packages/presentify/src/hooks/useQueryParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ export const useQueryParams = () => {
const getParams = (name: string) => searchParams.get(name);

const setParams = (name: string, value: string) => {
searchParams.set(name, value);
window.history.pushState(
undefined,
'',
`${window.location
.toString()
.replace(window.location.search, '')}?${name}=${value}`,
`${window.location.pathname}?${searchParams.toString()}`,
);
};

Expand Down
12 changes: 9 additions & 3 deletions packages/presentify/src/styles/Code.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ export const LineNumber = styled.span<{ lineWidth: number }>`
export const Line = styled.div<{
isHighlight: boolean;
useFiraCode?: boolean;
elementWidth: number;
}>`
${({ isHighlight }) => (isHighlight ? '' : 'opacity: 0.3')};
font-size: calc((100vw - 40px) / 50);
${({ useFiraCode }) =>
useFiraCode ? 'font-family: Fira Code, monospace;' : ''};
font-size: ${({ elementWidth }) => `calc((${elementWidth}px - 40px) / 50);`}
${({ useFiraCode }) =>
useFiraCode ? 'font-family: Fira Code, monospace;' : ''};
`;

export const StyledPre = styled.pre`
width: 100%;
padding: 20px;
`;

0 comments on commit 9762741

Please sign in to comment.