Skip to content
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

HDS::CodeEditor with Code Mirror 6 #2573

Merged
merged 112 commits into from
Jan 17, 2025
Merged

Conversation

zamoore
Copy link
Contributor

@zamoore zamoore commented Nov 19, 2024

📌 Summary

This PR adds a code editor utilizing Code Mirror 6 to the HDS component library. The code editor can be instantiated via:

  • The hds-code-editor modifier, which can be added to any element to transform that element into a code editor instance
  • The Hds::CodeEditor component, which uses the hds-code-editor modifier under the hood, but adds support for title, description, and various tools.

Details

hds-code-editor modifier

This modifier will instantiate a Code Mirror editor on the element that it's added to. In order to reduce the up-front cost of loading the editor, we only load the CodeMirror package when the editor is scrolled into view. Because of this, there may be an exceptionally brief loading state before the editor is ready when it is viewed by the user.

// Example
<div {{hds-code-editor
  value=@value
  language=@language
  onInput=@onInput
  onSetup=this.onSetup
}} />

value: The initial value of the editor
language: optional, one of 4 possible supported languages (json, sql, go, hcl)
onInput: callback function when the code editor changes
onSetup: callback function when the modifier has completed loading dependencies and instantiating the editor, receives CodeMirror Editor instance.

This editor features support for:

  • Custom title
  • Custom description
  • Copy editor content
  • Fullscreen toggle
  • Keyboard support including directional navigation
  • History (support for cmd+z and other history commands)

Hds::CodeEditor component

This component uses the hds-code-editor modifier in combination with a customizable header and support for a fullscreen mode.

// Example
<Hds::CodeEditor
  @language="go"
  @value="test"
  @hasExpandButton={{true}}
  @hasCopyButton={{true}}
  @onInput={{this.noop}}
  @onSetup={{this.noop}}
  as |CE|
>
  <CE.Title>Code editor with title</CE.Title>
  <CE.Description>This is a code editor with a description</CE.Description>
  <Hds::Button @text="Custom button" />
  <Hds::Icon @name="code" @color="#fff" />
</Hds::CodeEditor>

value: The initial value of the editor
hasExpandButton: Whether the editor can be expanded into fullscreen mode
hasCopyButton: Whether the editor has a button that adds the current value to the clipboard
language: optional, one of 4 possible supported languages (json, sql, go, hcl)
onInput: callback function when the code editor changes
onSetup: callback function when the component has completed loading dependencies and instantiating the editor, receives CodeMirror Editor instance.

📸 Screenshots

Screenshot 2024-12-17 at 5 22 00 PM

🔗 External links

Jira ticket: HDS-4016
Figma


👀 Component checklist

💬 Please consider using conventional comments when reviewing this PR.

@zamoore zamoore self-assigned this Nov 19, 2024
Copy link

vercel bot commented Nov 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
hds-showcase ✅ Ready (Inspect) Visit Preview Jan 16, 2025 9:36pm
hds-website ✅ Ready (Inspect) Visit Preview Jan 16, 2025 9:36pm

alex-ju

This comment was marked as resolved.

didoo

This comment was marked as resolved.

shleewhite
shleewhite previously approved these changes Jan 16, 2025
Copy link
Contributor

@shleewhite shleewhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!!!

Copy link
Contributor

@majedelass majedelass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are three minor things I caught. Let me know when you address them so I can approve the PR 👍

Copy link
Member

@alex-ju alex-ju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the functionality again. One thing I noticed is that in full-screen mode the border-radius is preserved (not sure if it was intentional or not). Otherwise all looks good! Happy to get it in, and if anything comes up in testing we can address separately.

Comment on lines +96 to +98
&:active {
background-color: var(--hds-code-editor-color-surface-interactive-active)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Active still isn't showing up properly. I believe this needs to be moved below the hover pseudo class so it will show up properly on mouse click.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adressing in a followup

Copy link
Contributor

@majedelass majedelass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm approving because there is one last minor thing with the active state (in CSS) and I don't want to block entirely. I'd move the active state below the hover state in CSS so it will show up properly on mouse click.

@zamoore zamoore merged commit 2787f32 into main Jan 17, 2025
14 checks passed
@zamoore zamoore deleted the alex-ju/code-editor-codemirror-spike branch January 17, 2025 14:53
@hashibot-hds hashibot-hds mentioned this pull request Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants