Skip to content

Commit

Permalink
added nonce support (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpronto committed Jun 19, 2023
1 parent a9d6112 commit 8b5c579
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface ReactDiffViewerProps {
leftTitle?: string | JSX.Element;
// Title for left column
rightTitle?: string | JSX.Element;
// Nonce
nonce?: string;
}

export interface ReactDiffViewerState {
Expand All @@ -92,6 +94,7 @@ class DiffViewer extends React.Component<
showDiffOnly: true,
useDarkTheme: false,
linesOffset: 0,
nonce: '',
};

public static propTypes = {
Expand All @@ -111,6 +114,7 @@ class DiffViewer extends React.Component<
leftTitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
rightTitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
linesOffset: PropTypes.number,
nonce: PropTypes.string,
};

public constructor(props: ReactDiffViewerProps) {
Expand Down Expand Up @@ -157,6 +161,7 @@ class DiffViewer extends React.Component<
private computeStyles: (
styles: ReactDiffViewerStylesOverride,
useDarkTheme: boolean,
nonce: string,
) => ReactDiffViewerStyles = memoize(computeStyles);

/**
Expand Down Expand Up @@ -580,6 +585,7 @@ class DiffViewer extends React.Component<
rightTitle,
splitView,
hideLineNumbers,
nonce,
} = this.props;

if (this.props.compareMethod !== DiffMethod.JSON) {
Expand All @@ -588,7 +594,7 @@ class DiffViewer extends React.Component<
}
}

this.styles = this.computeStyles(this.props.styles, useDarkTheme);
this.styles = this.computeStyles(this.props.styles, useDarkTheme, nonce);
const nodes = this.renderDiff();
const colSpanOnSplitView = hideLineNumbers ? 2 : 3;
const colSpanOnInlineView = hideLineNumbers ? 2 : 4;
Expand Down
6 changes: 5 additions & 1 deletion src/styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { css, CSSInterpolation, cx } from '@emotion/css';
import { CSSInterpolation } from '@emotion/css';
import createEmotion from '@emotion/css/create-instance';

export interface ReactDiffViewerStyles {
diffContainer?: string;
Expand Down Expand Up @@ -85,6 +86,7 @@ export interface ReactDiffViewerStylesOverride {
export default (
styleOverride: ReactDiffViewerStylesOverride,
useDarkTheme = false,
nonce = '',
): ReactDiffViewerStyles => {
const { variables: overrideVariables = {}, ...styles } = styleOverride;

Expand Down Expand Up @@ -153,6 +155,8 @@ export default (

const variables = useDarkTheme ? themeVariables.dark : themeVariables.light;

const { css, cx } = createEmotion({ key: 'react-diff', nonce });

const content = css({
width: '100%',
label: 'content',
Expand Down

0 comments on commit 8b5c579

Please sign in to comment.