-
Notifications
You must be signed in to change notification settings - Fork 376
fix(CodeEditor): use codeEditorControls and clean up overall #7931
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
Changes from 7 commits
257ff8a
71ba55a
e28609b
191f1f0
42494c2
6889d36
c6e9bd6
888aba8
95298fa
d1bc327
44ffa43
96054c0
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 |
|---|---|---|
|
|
@@ -7,19 +7,19 @@ import { CodeEditorContext } from './CodeEditorUtils'; | |
| */ | ||
|
|
||
| export interface CodeEditorControlProps extends Omit<ButtonProps, 'onClick'> { | ||
| /** Accessible label for the code editor control. */ | ||
| /** Accessible label for the code editor control */ | ||
| 'aria-label'?: string; | ||
| /** Additional classes added to the code editor control. */ | ||
| className?: string; | ||
| /** Delay in ms before the tooltip appears. */ | ||
| /** @deprecated Delay in ms before the tooltip appears. */ | ||
|
Contributor
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. Can you open an issue to remove these for breaking change release please.
Contributor
Author
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. |
||
| entryDelay?: number; | ||
| /** Delay in ms before the tooltip disappears. */ | ||
| /** @deprecated Delay in ms before the tooltip disappears. */ | ||
| exitDelay?: number; | ||
| /** Icon rendered inside the code editor control. */ | ||
| icon: React.ReactNode; | ||
| /** Maximum width of the tooltip (default 150px). */ | ||
| /** @deprecated Maximum width of the tooltip (default 150px). */ | ||
| maxWidth?: string; | ||
| /** Copy button popover position. */ | ||
| /** @deprecated Copy button popover position. */ | ||
| position?: | ||
| | PopoverPosition | ||
| | 'auto' | ||
|
|
@@ -35,25 +35,28 @@ export interface CodeEditorControlProps extends Omit<ButtonProps, 'onClick'> { | |
| | 'left-end' | ||
| | 'right-start' | ||
| | 'right-end'; | ||
| /** Text to display in the tooltip. */ | ||
| toolTipText: React.ReactNode; | ||
| /** Event handler for the click of the button. */ | ||
| /** @deprecated Text to display in the tooltip*/ | ||
| toolTipText?: React.ReactNode; | ||
| /** Event handler for the click of the button */ | ||
| onClick: (code: string, event?: any) => void; | ||
| /** Flag indicating that the button is visible above the code editor. */ | ||
| isVisible?: boolean; | ||
| /** Additional tooltip props forwarded to the Tooltip component */ | ||
| tooltipProps?: any; | ||
| } | ||
|
|
||
| export const CodeEditorControl: React.FunctionComponent<CodeEditorControlProps> = ({ | ||
| icon, | ||
| className, | ||
| 'aria-label': ariaLabel, | ||
| toolTipText, | ||
| exitDelay = 0, | ||
| entryDelay = 300, | ||
| maxWidth = '100px', | ||
| position = 'top', | ||
| exitDelay, | ||
| entryDelay, | ||
| maxWidth, | ||
| position, | ||
| onClick = () => {}, | ||
| isVisible = true, | ||
| tooltipProps = {}, | ||
| ...props | ||
| }: CodeEditorControlProps) => { | ||
| const context = React.useContext(CodeEditorContext); | ||
|
|
@@ -64,12 +67,12 @@ export const CodeEditorControl: React.FunctionComponent<CodeEditorControlProps> | |
|
|
||
| return isVisible ? ( | ||
| <Tooltip | ||
| trigger="mouseenter focus click" | ||
| exitDelay={exitDelay} | ||
| entryDelay={entryDelay} | ||
| maxWidth={maxWidth} | ||
| position={position} | ||
| content={<div>{toolTipText}</div>} | ||
| {...tooltipProps} | ||
| > | ||
| <Button className={className} onClick={onCustomClick} variant="control" aria-label={ariaLabel} {...props}> | ||
| {icon} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,7 @@ import { ClipboardCopyToggle } from './ClipboardCopyToggle'; | |
| import { ClipboardCopyExpanded } from './ClipboardCopyExpanded'; | ||
|
|
||
| export const clipboardCopyFunc = (event: React.ClipboardEvent<HTMLDivElement>, text?: React.ReactNode) => { | ||
| const clipboard = event.currentTarget.parentElement; | ||
| const el = document.createElement('textarea'); | ||
| el.value = text.toString(); | ||
| clipboard.appendChild(el); | ||
| el.select(); | ||
| document.execCommand('copy'); | ||
| clipboard.removeChild(el); | ||
| navigator.clipboard.writeText(text.toString()); | ||
| }; | ||
|
|
||
| export enum ClipboardCopyVariant { | ||
|
|
@@ -76,7 +70,7 @@ export interface ClipboardCopyProps extends Omit<React.HTMLProps<HTMLDivElement> | |
| exitDelay?: number; | ||
| /** Delay in ms before the tooltip appears. */ | ||
| entryDelay?: number; | ||
| /** Delay in ms before the tooltip message switch to hover tip. */ | ||
| /** @deprecated Delay in ms before the tooltip message switch to hover tip. */ | ||
|
Contributor
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 being deprecated? Was its only purpose for the Code editor. Could consumers be relying on it?
Contributor
Author
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. It's being deprecated because of a subtle change in how the three components with a copy feature are handling toggling their tooltip text between 'copy' and 'copied' (or some similar variant). Before this change, each of the three copy components were handling it differently. CodeBlock was never changing its 'copied' text back to 'copy'. CodeEditor had it hardcoded so that the text changed after 2500 milliseconds. And ClipBoardCopy was using this switchDelay value. In the later two cases, these timers caused some weird cases where the 'copied' text would switch back before the tooltip closed and cause the text to quickly update before it closed (sometimes with barely enough time for a user to read it, and that is a bit jarring when it happens). I spent some time trying to tweak timers to figure out how to avoid it, but ultimately, since the user can keep the tooltip open for an indeterminate amount of time by interacting with the tooltip's toggle, it was never going to work ideally with timers. My solution was to wire all three components to trigger the switch between the 'copied' and 'copy' text only when the close transition has completed. That's now handled internally by the function passed to the |
||
| switchDelay?: number; | ||
| /** A function that is triggered on clicking the copy button. */ | ||
| onCopy?: (event: React.ClipboardEvent<HTMLDivElement>, text?: React.ReactNode) => void; | ||
|
|
@@ -111,7 +105,7 @@ export class ClipboardCopy extends React.Component<ClipboardCopyProps, Clipboard | |
| variant: 'inline', | ||
| position: PopoverPosition.top, | ||
| maxWidth: '150px', | ||
| exitDelay: 1600, | ||
| exitDelay: 1500, | ||
| entryDelay: 300, | ||
| switchDelay: 2000, | ||
| onCopy: clipboardCopyFunc, | ||
|
|
@@ -210,18 +204,10 @@ export class ClipboardCopy extends React.Component<ClipboardCopyProps, Clipboard | |
| textId={`text-input-${id}`} | ||
| aria-label={hoverTip} | ||
| onClick={(event: any) => { | ||
| if (this.timer) { | ||
| window.clearTimeout(this.timer); | ||
| this.setState({ copied: false }); | ||
| } | ||
| onCopy(event, this.state.text); | ||
| this.setState({ copied: true }, () => { | ||
| this.timer = window.setTimeout(() => { | ||
| this.setState({ copied: false }); | ||
| this.timer = null; | ||
| }, switchDelay); | ||
| }); | ||
| this.setState({ copied: true }); | ||
| }} | ||
| onTooltipHidden={() => this.setState({copied: false})} | ||
| > | ||
| {this.state.copied ? clickTip : hoverTip} | ||
| </ClipboardCopyButton> | ||
|
|
@@ -263,18 +249,10 @@ export class ClipboardCopy extends React.Component<ClipboardCopyProps, Clipboard | |
| textId={`text-input-${id}`} | ||
| aria-label={hoverTip} | ||
| onClick={(event: any) => { | ||
| if (this.timer) { | ||
| window.clearTimeout(this.timer); | ||
| this.setState({ copied: false }); | ||
| } | ||
| onCopy(event, this.state.text); | ||
| this.setState({ copied: true }, () => { | ||
| this.timer = window.setTimeout(() => { | ||
| this.setState({ copied: false }); | ||
| this.timer = null; | ||
| }, switchDelay); | ||
| }); | ||
| this.setState({ copied: true }); | ||
| }} | ||
| onTooltipHidden={() => this.setState({copied: false})} | ||
| > | ||
| {this.state.copied ? clickTip : hoverTip} | ||
| </ClipboardCopyButton> | ||
|
|
||
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.
🥳