Skip to content

Commit

Permalink
allow to pass custom styles to CodeField
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Feb 20, 2025
1 parent c210600 commit 5d02952
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/components/codefield/CodeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { prefixLineNumberExtension } from "./prefixLineNumberExtension";
import { createDefaultStylesOverridesExtension } from "./defaultStylesOverridesExtension";
import { CopyButton } from "../copy-button";
import { BUTTON_KIND, BUTTON_SIZE } from "../button";
import { CODE_FIELD_SIZE } from "./types";
import { CODE_FIELD_SIZE, CustomStyles } from "./types";

const MemoizedCopyButton = memo(CopyButton);

Expand All @@ -28,6 +28,7 @@ export type CodeFieldProps = {
size?: CODE_FIELD_SIZE;
codeMirrorClassName?: string;
highlightOnHover?: boolean;
customStyles?: CustomStyles;
} & HTMLAttributes<HTMLDivElement>;

const CodeFieldRenderFunction: ForwardRefRenderFunction<HTMLDivElement, CodeFieldProps> = (
Expand All @@ -46,14 +47,15 @@ const CodeFieldRenderFunction: ForwardRefRenderFunction<HTMLDivElement, CodeFiel
size = CODE_FIELD_SIZE.medium,
codeMirrorClassName,
highlightOnHover = true,
customStyles = {},
...rest
},
ref
) => {
const [css] = useStyletron();
const styleOverridesExtention = useMemo(
() => createDefaultStylesOverridesExtension(showLineNumbers),
[showLineNumbers]
() => createDefaultStylesOverridesExtension(showLineNumbers, customStyles),
[showLineNumbers, customStyles]
);
const mergedExtensions = [styleOverridesExtention, ...extensions];
const computedCn = className
Expand Down
4 changes: 3 additions & 1 deletion src/components/codefield/defaultStylesOverridesExtension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EditorView } from "@codemirror/view";
import { COLORS } from "../../shared";
import { expandProperty } from "inline-style-expand-shorthand";
import { CustomStyles } from "./types";

export const createDefaultStylesOverridesExtension = (showLineNumbers: boolean) =>
export const createDefaultStylesOverridesExtension = (showLineNumbers: boolean, customStyles: CustomStyles = {}) =>
EditorView.theme({
".cm-lineNumbers .cm-gutterElement": {
paddingLeft: "8px",
Expand Down Expand Up @@ -32,4 +33,5 @@ export const createDefaultStylesOverridesExtension = (showLineNumbers: boolean)
backgroundColor: COLORS.gray900,
...expandProperty("transition", "background 0.15s"),
},
...customStyles,
});
2 changes: 1 addition & 1 deletion src/components/codefield/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as CodeField } from "./CodeField";
export type { CodeFieldProps } from "./CodeField";
export { CODE_FIELD_SIZE } from "./types";
export { CODE_FIELD_SIZE, type CustomStyles } from "./types";
6 changes: 6 additions & 0 deletions src/components/codefield/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ export enum CODE_FIELD_SIZE {
small = "small",
medium = "medium",
}

export type CustomStyles = {
[selector: string]: {
[property: string]: string;
};
};

0 comments on commit 5d02952

Please sign in to comment.