Skip to content

Commit

Permalink
add scrollable codefield #309
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Jul 4, 2024
1 parent 135ffa0 commit 2765027
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/components/codefield/CodeField.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ export const EditableTemplate = (args) => {
>
{EditableTemplate.bind({})}
</Story>
<Story
name="With fixed height"
args={{
code: `--cost <cost of the request> \\`,
displayCopy: true,
height: 200,
}}
parameters={getFigmaParameters(
"https://www.figma.com/file/YjE625ScDMf2ILjWB1sTMc/System?type=design&node-id=223-8049&mode=design&t=P3rN3pHdOl7FkE44-0"
)}
>
{Template.bind({})}
</Story>
</Canvas>

<ArgsTable of={CodeField} />
Expand Down
4 changes: 3 additions & 1 deletion src/components/codefield/CodeField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type CodeFieldProps = {
readOnly?: ReactCodeMirrorProps["readOnly"];
onChange?: ReactCodeMirrorProps["onChange"];
size?: CODE_FIELD_SIZE;
height?: number;
} & HTMLAttributes<HTMLDivElement>;

const CodeFieldRenderFunction: ForwardRefRenderFunction<HTMLDivElement, CodeFieldProps> = (
Expand All @@ -42,6 +43,7 @@ const CodeFieldRenderFunction: ForwardRefRenderFunction<HTMLDivElement, CodeFiel
readOnly = true,
onChange,
size = CODE_FIELD_SIZE.medium,
height,
...rest
},
ref
Expand All @@ -68,7 +70,7 @@ const CodeFieldRenderFunction: ForwardRefRenderFunction<HTMLDivElement, CodeFiel
onChange={onChange}
theme={getCodeMirrorTheme(themeOverrides)}
basicSetup={getCodeMirrorBasicSetup(showLineNumbers, editable)}
className={css(s.codemirrorStyles)}
className={css(s.getCodemirrorStyles(height))}
/>
{displayCopy && (
<MemoizedCopyButton
Expand Down
11 changes: 7 additions & 4 deletions src/components/codefield/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getContainerStyles = (size: CODE_FIELD_SIZE): StyleObject => ({
...expandProperty("padding", "24px"),
display: "flex",
justifyContent: "space-between",
alignItems: "center",
alignItems: "flex-start",
flexWrap: "nowrap",
gap: "16px",
...expandProperty("transition", "background 0.15s"),
Expand All @@ -21,14 +21,17 @@ const getContainerStyles = (size: CODE_FIELD_SIZE): StyleObject => ({
fontSize: size === CODE_FIELD_SIZE.small ? "14px" : "16px",
});

const codemirrorStyles: StyleObject = {
const getCodemirrorStyles = (height?: number): StyleObject => ({
minWidth: 0,
height: height ? `${height}px` : "auto",
overflow: height ? "scroll" : "auto",
flexGrow: 1,
":focus-within .cm-editor": {
outline: "none",
},
};
});

export const styles = {
getContainerStyles,
codemirrorStyles,
getCodemirrorStyles,
};

0 comments on commit 2765027

Please sign in to comment.