Skip to content

Commit c7183b1

Browse files
committed
Cleaned up typing, DRY props with interface extension, fix persistent type errors in Storybook
1 parent f814492 commit c7183b1

File tree

9 files changed

+58
-169
lines changed

9 files changed

+58
-169
lines changed

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"react-dom": "16.13.1",
2121
"react-github-corner": "^2.3.0",
2222
"react-scripts": "2.1.8",
23-
"styled-components": "^6.0.5"
23+
"styled-components": "^6.1.0"
2424
},
2525
"scripts": {
2626
"start": "react-scripts start",

packages/gatsby-mdx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"react-code-blocks": "*",
2525
"react-dom": "^16.11.0",
2626
"react-helmet": "^5.2.0",
27-
"styled-components": "^6.0.5",
27+
"styled-components": "^6.1.0",
2828
"styled-system": "^4.2.2"
2929
},
3030
"keywords": [

packages/react-code-blocks/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"@types/react": "^16.9.34",
7070
"@types/react-dom": "^16.9.7",
7171
"@types/react-syntax-highlighter": "^15.5.7",
72-
"@types/styled-components": "^5.1.0",
7372
"enzyme": "^3.11.0",
7473
"react": "^16.13.1",
7574
"react-docgen-typescript-loader": "^3.7.2",
@@ -84,7 +83,7 @@
8483
"@babel/runtime": "^7.10.4",
8584
"@types/react-syntax-highlighter": "^15.5.7",
8685
"react-syntax-highlighter": "^15.5.0",
87-
"styled-components": "^6.0.5",
86+
"styled-components": "^6.1.0",
8887
"tslib": "^2.6.0"
8988
},
9089
"pnpm": {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { withTheme } from 'styled-components';
2-
import Code from './components/Code';
2+
import Code, { CodeProps } from './components/Code';
33

4-
export default withTheme(Code);
4+
// HELP WANTED: I don't understand why this forced typing is necessary for CopyBlock and Code, but not CodeBlock
5+
export default withTheme(Code) as (props: CodeProps) => JSX.Element;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { withTheme } from 'styled-components';
2-
import CopyBlock from './components/CopyBlock';
3-
export default withTheme(CopyBlock);
2+
import CopyBlock, { CopyBlockProps } from './components/CopyBlock';
3+
4+
// HELP WANTED: I don't understand why this forced typing is necessary for CopyBlock and Code, but not CodeBlock
5+
export default withTheme(CopyBlock) as (props: CopyBlockProps) => JSX.Element;

packages/react-code-blocks/src/components/Code.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ export interface CodeProps {
1212
/** The language in which the code is written. [See LANGUAGES.md](https://github.com/rajinwonderland/react-code-blocks/blob/master/LANGUAGES.md) */
1313
language: string;
1414
/** The style object that will be combined with the top level style on the pre tag, styles here will overwrite earlier styles. */
15-
customStyle?: {};
15+
customStyle?: Record<string,string>;
1616

1717
/** The style object to apply to the container that shows line number */
1818
lineNumberContainerStyle?: {};
1919

2020
/** The element or custom react component to use in place of the default span tag */
21-
preTag: keyof JSX.IntrinsicElements | React.ComponentType<any> | undefined;
21+
preTag?: keyof JSX.IntrinsicElements | React.ComponentType<any> | undefined;
2222
/** Indicates whether or not to show line numbers */
23-
showLineNumbers: boolean;
23+
showLineNumbers?: boolean;
2424
/**For choosing starting line**/
25-
startingLineNumber :number;
25+
startingLineNumber?: number;
2626
/** The code to be formatted */
2727
text: string;
2828
/** A custom theme to be applied, implements the `CodeBlockTheme` interface. You can also pass pass a precomposed theme into here. For available themes. [See THEMES.md](https://github.com/rajinwonderland/react-code-blocks/blob/master/THEMES.md) */
2929
theme?: Theme;
3030

3131
/** If true, wrap long lines */
32-
wrapLongLines: boolean;
32+
wrapLongLines?: boolean;
3333

3434
/**
3535
* Lines to highlight comma delimited.
@@ -39,7 +39,7 @@ export interface CodeProps {
3939
* - To highlight a group of lines `highlight="1-5"`
4040
* - To highlight multiple groups `highlight="1-5,7,10,15-20"`
4141
*/
42-
highlight: string;
42+
highlight?: string;
4343
}
4444

4545
export default class Code extends PureComponent<CodeProps, {}> {
@@ -116,7 +116,7 @@ export default class Code extends PureComponent<CodeProps, {}> {
116116
{...props}
117117
// Wrap lines is needed to set styles on the line.
118118
// We use this to set opacity if highlight specific lines.
119-
wrapLines={this.props.highlight.length > 0}
119+
wrapLines={!!this.props.highlight}
120120
customStyle={this.props.customStyle}
121121
// Types are incorrect.
122122
// @ts-ignore

packages/react-code-blocks/src/components/CodeBlock.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,14 @@
11
import React, { PureComponent } from 'react';
22
import { applyTheme } from '../utils/themeBuilder';
3-
import { Theme } from '../types';
4-
import Code from './Code';
3+
import Code, { CodeProps } from './Code';
54

6-
export interface CodeBlockProps {
7-
/** The code to be formatted */
8-
text: string;
9-
/** The language in which the code is written. [See LANGUAGES.md](https://github.com/rajinwonderland/react-code-blocks/blob/master/LANGUAGES.md) */
10-
language: string;
11-
/** Indicates whether or not to show line numbers */
12-
showLineNumbers?: boolean;
5+
type BaseProps = Omit<CodeProps, 'codeStyle' | 'preTag'>;
136

14-
/**startingLineNumber - if showLineNumbers is enabled the line numbering will start from here.**/
15-
startingLineNumber :number;
16-
17-
/** A custom theme to be applied, implements the `CodeBlockTheme` interface. You can also pass pass a precomposed theme into here. For available themes. [See THEMES.md](https://github.com/rajinwonderland/react-code-blocks/blob/master/THEMES.md) */
18-
theme?: Theme;
19-
/** The element or custom react component to use in place of the default `span` tag */
20-
lineNumberContainerStyle?: {};
7+
export interface CodeBlockProps extends BaseProps {
218
/** The style object to apply to the `CodeBlock` text directly i.e `fontSize` and such */
22-
239
codeBlockStyle?: {};
2410
/** The style object that accesses the style parameter on the `codeTagProps` property on the `Code` component */
2511
codeContainerStyle?: {};
26-
27-
/** The style object that will be combined with the top level style on the pre tag, styles here will overwrite earlier styles. */
28-
customStyle?: {};
29-
30-
/** If true, wrap long lines */
31-
wrapLongLines: boolean;
32-
33-
/**
34-
* Lines to highlight comma delimited.
35-
* Example uses:
36-
37-
* - To highlight one line `highlight="3"`
38-
* - To highlight a group of lines `highlight="1-5"`
39-
* - To highlight multiple groups `highlight="1-5,7,10,15-20"`
40-
*/
41-
highlight?: string;
4212
}
4313

4414
const LANGUAGE_FALLBACK = 'text';

packages/react-code-blocks/src/components/CopyBlock.tsx

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
import React, { useState } from 'react';
22
import Code from './Code';
3-
import CodeBlock from './CodeBlock';
3+
import CodeBlock, { CodeBlockProps } from './CodeBlock';
44
import Copy from './CopyIcon';
55
import styled from 'styled-components';
66
import { Theme } from '../types';
77
import useClipboard from '../hooks/use-clipboard';
88

9-
export interface CopyBlockProps {
10-
/** A custom theme to be applied, implements the `CodeBlockTheme` interface. You can also pass pass a precomposed theme into here. For available themes. [See THEMES.md](https://github.com/rajinwonderland/react-code-blocks/blob/master/THEMES.md) */
11-
theme: Theme;
12-
13-
/** The code to be formatted */
14-
text: string;
15-
16-
/** If true, the component render a `CodeBlock` instead of a `Code` component */
17-
18-
codeBlock: boolean;
19-
9+
export interface CopyBlockProps extends CodeBlockProps {
2010
/** This is a prop used internally by the `CopyBlock`'s button component to toggle the icon to a success icon */
21-
copied: boolean;
22-
23-
/** If true, wrap long lines */
24-
wrapLongLines: boolean;
25-
11+
copied?: boolean;
12+
/** If true, the component render a `CodeBlock` instead of a `Code` component */
13+
codeBlock?: boolean;
2614
/** The onCopy function is called if the copy icon is clicked. This enables you to add a custom message that the code block is copied. */
27-
onCopy: (event: React.MouseEvent<HTMLButtonElement>) => void;
28-
29-
/** The language in which the code is written. [See LANGUAGES.md](https://github.com/rajinwonderland/react-code-blocks/blob/master/LANGUAGES.md) */
30-
31-
language: string;
32-
customStyle?: {};
33-
/** I know it's lazy, but I'll extend the interfaces later */
34-
[x: string]: any;
15+
onCopy?: (event: React.MouseEvent<HTMLButtonElement>) => void;
3516
}
3617

3718
type CascadedProps = Partial<CopyBlockProps> & { theme: Theme };
@@ -76,9 +57,10 @@ export default function CopyBlock({
7657
codeBlock = false,
7758
customStyle = {},
7859
onCopy,
60+
copied: startingCopied,
7961
...rest
8062
}: CopyBlockProps) {
81-
const [copied, toggleCopy] = useState(false);
63+
const [copied, toggleCopy] = useState(!!startingCopied);
8264
const { copy } = useClipboard();
8365
const handler = (event: React.MouseEvent<HTMLButtonElement>) => {
8466
copy(text);
@@ -96,7 +78,7 @@ export default function CopyBlock({
9678
)}
9779
<Button aria-label="Copy Code" type="button" onClick={handler} {...{ theme, copied }}>
9880
<Copy
99-
color={copied ? theme.stringColor : theme.textColor}
81+
color={copied ? theme?.stringColor : theme?.textColor}
10082
copied={copied}
10183
className="icon"
10284
size="16pt"

0 commit comments

Comments
 (0)