-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add option for line-wrapping to prevent horizontal scrolling #20
Changes from 12 commits
59ecbce
9097cfc
6cd99c4
b40b2b0
6b9b6e7
c9dd827
81bc51e
4cd5711
1cdc45f
078fd46
d44f75b
fb13d49
6da84dc
0658716
73a2b33
e1982b3
a6277a0
d0550ce
529aeb4
6741164
4dc0603
d746e42
d3f469e
b015fba
b10273d
ceecca6
f543ac5
323c76f
7929a08
ed856c0
18ce8bb
6e86b8d
e4c46c7
f300bd1
5b7e6e6
713e49a
33411cc
b0659b6
4fc29e2
0dc2c7c
e4b2dae
4d69e81
3b8ad86
3de5396
df9f1b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { SpaceBetween } from "@cloudscape-design/components"; | ||
import { CodeView } from "../../lib/components"; | ||
import { ScreenshotArea } from "../screenshot-area"; | ||
export default function CodeViewPage() { | ||
return ( | ||
<ScreenshotArea> | ||
<h1>Code View</h1> | ||
<SpaceBetween direction="vertical" size="l"> | ||
No wrapping, no line numbers | ||
<CodeView | ||
content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
/> | ||
No wrapping, line numbers | ||
<CodeView | ||
lineNumbers={true} | ||
content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
/> | ||
Wrapping, no line numbers | ||
<CodeView | ||
lineWrapping={true} | ||
content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
/> | ||
Wrapping, line numbers | ||
<CodeView | ||
lineWrapping={true} | ||
lineNumbers={true} | ||
content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
/> | ||
<CodeView | ||
lineWrapping={true} | ||
lineNumbers={true} | ||
content={`public class HelloWorld {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n}`} | ||
/> | ||
</SpaceBetween> | ||
</ScreenshotArea> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,13 @@ export interface CodeViewProps { | |
*/ | ||
lineNumbers?: boolean; | ||
|
||
/** | ||
* Controls lines wrap when overflowing on the right side. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
lineWrapping?: boolean; | ||
|
||
/** | ||
* An optional slot to display a button to enable users to perform actions, such as copy or download the code snippet. | ||
* | ||
|
@@ -34,5 +41,5 @@ export interface CodeViewProps { | |
* A function to perform custom syntax highlighting. | ||
* | ||
*/ | ||
highlight?: (code: string) => React.ReactNode; | ||
highlight?: (code: string) => React.ReactElement; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this change needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it got it: we ensure the type is ReactElement so that Children.map() can be used. However, this change is not backward-compatible because the ReactElement type is more narrow than ReactNode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah true that |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,69 +4,102 @@ import { useCurrentMode } from "@cloudscape-design/component-toolkit/internal"; | |
import Box from "@cloudscape-design/components/box"; | ||
import clsx from "clsx"; | ||
import { useRef } from "react"; | ||
import { Children } from "react"; | ||
import { InternalBaseComponentProps, getBaseProps } from "../internal/base-component/use-base-component"; | ||
import { CodeViewProps } from "./interfaces"; | ||
import styles from "./styles.css.js"; | ||
|
||
const ACE_CLASSES = { light: "ace-cloud_editor", dark: "ace-cloud_editor_dark" }; | ||
|
||
function getLineNumbers(content: string) { | ||
return content.split("\n").map((_, n) => n + 1); | ||
} | ||
|
||
type InternalCodeViewProps = CodeViewProps & InternalBaseComponentProps; | ||
|
||
// Breaks down the input code for non-highlighted code-view into React | ||
// Elements similar to how a highlight function would do. | ||
const textHighlight = (code: string) => { | ||
const lines = code.split("\n"); | ||
return ( | ||
<span> | ||
{lines.map((line, lineIndex) => ( | ||
<span key={lineIndex}> | ||
{line} | ||
{"\n"} | ||
</span> | ||
))} | ||
</span> | ||
); | ||
}; | ||
|
||
export function InternalCodeView({ | ||
content, | ||
actions, | ||
lineNumbers, | ||
lineWrapping, | ||
highlight, | ||
ariaLabel, | ||
ariaLabelledby, | ||
__internalRootRef = null, | ||
...props | ||
}: InternalCodeViewProps) { | ||
const code = highlight ? highlight(content) : <span>{content}</span>; | ||
const baseProps = getBaseProps(props); | ||
const preRef = useRef<HTMLPreElement>(null); | ||
const darkMode = useCurrentMode(preRef) === "dark"; | ||
|
||
const regionProps = ariaLabel || ariaLabelledby ? { role: "region" } : {}; | ||
|
||
// Create tokenized elements of the content. | ||
const code = highlight ? highlight(content) : textHighlight(content); | ||
|
||
return ( | ||
<div | ||
className={styles.root} | ||
className={clsx(darkMode ? ACE_CLASSES.dark : ACE_CLASSES.light, styles.root)} | ||
{...regionProps} | ||
{...baseProps} | ||
aria-label={ariaLabel} | ||
aria-labelledby={ariaLabelledby} | ||
ref={__internalRootRef} | ||
> | ||
<div className={clsx(lineNumbers && styles["root-with-numbers"], actions && styles["root-with-actions"])}> | ||
<Box color="text-status-inactive" fontSize="body-m"> | ||
{lineNumbers && ( | ||
<div className={styles["line-numbers"]} aria-hidden={true}> | ||
{getLineNumbers(content).map((number) => ( | ||
<span key={number}>{number}</span> | ||
))} | ||
</div> | ||
)} | ||
</Box> | ||
<pre | ||
ref={preRef} | ||
<div className={styles["scroll-container"]}> | ||
<table | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it necessary for this to be semantically a table? or is this more just to get table layout, in which case could we do that purely with CSS? (thinking about a11y, and whether it makes sense for this to be announced as a "table") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The primary reason for choosing a table here is to align line numbers with their corresponding lines of code, now that we are introducing line-wrapping. Let us consult with A11Y to double-check. |
||
className={clsx( | ||
darkMode ? ACE_CLASSES.dark : ACE_CLASSES.light, | ||
styles.code, | ||
lineNumbers && styles["code-with-line-numbers"], | ||
actions && styles["code-with-actions"] | ||
styles["code-table"], | ||
actions && styles["code-table-with-actions"], | ||
lineWrapping && styles["code-table-with-line-wrapping"] | ||
)} | ||
> | ||
<Box color="inherit" variant="code" fontSize="body-m"> | ||
{code} | ||
</Box> | ||
</pre> | ||
{actions && <div className={styles.actions}>{actions}</div>} | ||
<colgroup> | ||
<col style={{ width: 1 } /* shrink to fit content */} /> | ||
<col style={{ width: "auto" }} /> | ||
</colgroup> | ||
<tbody> | ||
{Children.map(code.props.children, (child, index) => { | ||
return ( | ||
<tr key={index}> | ||
{lineNumbers && ( | ||
<td className={clsx(styles["line-number"], styles.unselectable)} aria-hidden={true}> | ||
<Box variant="code" color="text-status-inactive" fontSize="body-m"> | ||
{index + 1} | ||
</Box> | ||
</td> | ||
)} | ||
<td className={styles["code-line"]}> | ||
<Box variant="code" fontSize="body-m"> | ||
<span | ||
className={clsx( | ||
code.props.className, | ||
lineWrapping ? styles["code-line-wrap"] : styles["code-line-nowrap"] | ||
)} | ||
> | ||
{child} | ||
</span> | ||
</Box> | ||
</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
</div> | ||
{actions && <div className={styles.actions}>{actions}</div>} | ||
</div> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please update this page and also
with-syntax-highlighting.page.tsx
with wrapping text content inside SpaceBetween in some container, e.g. a Box? That is to satisfy the guidelines: https://cloudscape.aws.dev/components/space-between/?tabId=apiCurrently, there are React warnings in these pages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yeah good point, fixed in new commit