-
Notifications
You must be signed in to change notification settings - Fork 515
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 <interactive-example> custom element #12413
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f7d0b20
chore(playground): migrate runner iframe to web component
LeoMcA 493be80
chore(playground): migrate editor to web component
LeoMcA 44166f0
fix(playground): editor was losing state if value set before componen…
LeoMcA 36ef938
chore(playground): migrate formatting to editor component
LeoMcA ba2f730
fix(playground): slash key would open search box
LeoMcA 10d99b6
feat: add <interactive-example> custom element
LeoMcA 8dbb3eb
wip(lit): attempt to fix importing css in client tests
LeoMcA 5b48c9c
fix(interactive-examples): telemetry, font-size, class-based hiding
LeoMcA 1ac9cd7
fix(playground): more consistent button disabling
LeoMcA 04ef562
fix(playground): don't add entries to browser history
LeoMcA 6bf9425
feat(playground): support console.log string replacement format
LeoMcA ffd3bd4
fix(playground): keep white bg when switching to dark mode
LeoMcA b07cd42
fix(playground): unbreakable string in console breaking layout
LeoMcA 9980c7a
fix(playground): don't break js on older browsers
LeoMcA 2e9b494
fix(playground): don't break when console.logging weird things
LeoMcA 58826b5
fix(interactive-examples): no page jump when loading
LeoMcA e63605e
Merge remote-tracking branch 'origin/main' into lit-interactive-examples
LeoMcA f82475a
Merge remote-tracking branch 'origin/main' into lit-interactive-examples
LeoMcA f622c64
fix(playground): update editor theme when changing browser/os light/d…
LeoMcA 0865aeb
fix(playground): clear sessionStore when clearing code
LeoMcA fa06176
fix(playground): don't leave play button in 'pressed' state when navi…
LeoMcA 03f68db
fix(playground): auto scroll console
LeoMcA d8480cc
chore(playground): remove unused state property
LeoMcA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @template {new (...args: any[]) => {}} TBase | ||
* @param {TBase} Base | ||
*/ | ||
export const GleanMixin = (Base) => | ||
class extends Base { | ||
/** @param {string} detail */ | ||
_gleanClick(detail) { | ||
window.dispatchEvent(new CustomEvent("glean-click", { detail })); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
import { MDNImageHistory, TeamMember } from "./about"; | ||
import { InteractiveExample } from "./interactive-example"; | ||
import { ContributorList } from "./community/contributor-list"; | ||
import { ScrimInline } from "./curriculum/scrim-inline"; | ||
import { PlayConsole } from "./play/console"; | ||
import { PlayController } from "./play/controller"; | ||
import { PlayEditor } from "./play/editor"; | ||
import { PlayRunner } from "./play/runner"; | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"mdn-image-history": MDNImageHistory; | ||
"team-member": TeamMember; | ||
"interactive-example": InteractiveExample; | ||
"contributor-list": ContributorList; | ||
"scrim-inline": ScrimInline; | ||
"play-console": PlayConsole; | ||
"play-controller": PlayController; | ||
"play-editor": PlayEditor; | ||
"play-runner": PlayRunner; | ||
} | ||
|
||
interface WindowEventMap { | ||
"glean-click": CustomEvent<string>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
interactive-example { | ||
display: block; | ||
height: 513px; | ||
margin: 1rem 0; | ||
|
||
&[height="shorter"] { | ||
height: 433px; | ||
} | ||
|
||
&[height="taller"] { | ||
height: 725px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { html, LitElement } from "lit"; | ||
import { ref, createRef } from "lit/directives/ref.js"; | ||
import "./play/editor.js"; | ||
import "./play/controller.js"; | ||
import "./play/console.js"; | ||
import "./play/runner.js"; | ||
import { GleanMixin } from "./glean-mixin.js"; | ||
|
||
import styles from "./interactive-example.scss?css" with { type: "css" }; | ||
|
||
/** | ||
* @import { Ref } from 'lit/directives/ref.js'; | ||
* @import { PlayController } from "./play/controller.js"; | ||
*/ | ||
|
||
export class InteractiveExample extends GleanMixin(LitElement) { | ||
static styles = styles; | ||
|
||
/** @type {Ref<PlayController>} */ | ||
_controller = createRef(); | ||
|
||
_run() { | ||
this._controller.value?.run(); | ||
} | ||
|
||
_reset() { | ||
this._controller.value?.reset(); | ||
} | ||
|
||
_initialCode() { | ||
const examples = this.closest("section")?.querySelectorAll( | ||
".code-example pre[class*=interactive-example]" | ||
); | ||
return Array.from(examples || []).reduce((acc, pre) => { | ||
const language = pre.classList[1]; | ||
return language && pre.textContent | ||
? { | ||
...acc, | ||
[language]: acc[language] | ||
? `${acc[language]}\n${pre.textContent}` | ||
: pre.textContent, | ||
} | ||
: acc; | ||
}, /** @type {Object<string, string>} */ ({})); | ||
} | ||
|
||
/** @param {Event} ev */ | ||
_telemetryHandler(ev) { | ||
let action = ev.type; | ||
if ( | ||
ev.type === "click" && | ||
ev.target instanceof HTMLElement && | ||
ev.target.id | ||
) { | ||
action = `click@${ev.target.id}`; | ||
} | ||
this._gleanClick(`interactive-examples-lit: ${action}`); | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
this._telemetryHandler = this._telemetryHandler.bind(this); | ||
this.renderRoot.addEventListener("focus", this._telemetryHandler); | ||
this.renderRoot.addEventListener("copy", this._telemetryHandler); | ||
this.renderRoot.addEventListener("cut", this._telemetryHandler); | ||
this.renderRoot.addEventListener("paste", this._telemetryHandler); | ||
this.renderRoot.addEventListener("click", this._telemetryHandler); | ||
} | ||
|
||
render() { | ||
return html` | ||
<play-controller ${ref(this._controller)}> | ||
<div class="template-javascript"> | ||
<h4>JavaScript Demo:</h4> | ||
<play-editor id="editor" language="javascript"></play-editor> | ||
<div class="buttons"> | ||
<button id="execute" @click=${this._run}>Run</button> | ||
<button id="reset" @click=${this._reset}>Reset</button> | ||
</div> | ||
<play-console id="console"></play-console> | ||
<play-runner></play-runner> | ||
</div> | ||
</play-controller> | ||
`; | ||
} | ||
|
||
firstUpdated() { | ||
const code = this._initialCode(); | ||
if (this._controller.value) { | ||
this._controller.value.code = code; | ||
} | ||
} | ||
|
||
disconnectedCallback() { | ||
super.disconnectedCallback(); | ||
this.renderRoot.removeEventListener("focus", this._telemetryHandler); | ||
this.renderRoot.removeEventListener("copy", this._telemetryHandler); | ||
this.renderRoot.removeEventListener("cut", this._telemetryHandler); | ||
this.renderRoot.removeEventListener("paste", this._telemetryHandler); | ||
this.renderRoot.removeEventListener("click", this._telemetryHandler); | ||
} | ||
} | ||
|
||
customElements.define("interactive-example", InteractiveExample); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
@use "../ui/vars" as *; | ||
@use "../ui/atoms/button/mixins" as button; | ||
|
||
h4 { | ||
border: 1px solid var(--border-secondary); | ||
border-top-left-radius: var(--elem-radius); | ||
border-top-right-radius: var(--elem-radius); | ||
font-size: 1rem; | ||
font-weight: normal; | ||
grid-area: header; | ||
line-height: 1.1876; | ||
margin: 0; | ||
padding: 0.5rem 1rem; | ||
} | ||
|
||
play-editor { | ||
border: 1px solid var(--border-secondary); | ||
border-bottom-left-radius: var(--elem-radius); | ||
border-bottom-right-radius: var(--elem-radius); | ||
border-top: none; | ||
grid-area: editor; | ||
margin-top: -0.5rem; | ||
overflow: auto; | ||
} | ||
|
||
.buttons { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.5rem; | ||
grid-area: buttons; | ||
|
||
button { | ||
@include button.secondary; | ||
} | ||
} | ||
|
||
play-console { | ||
border: 1px solid var(--border-secondary); | ||
border-radius: var(--elem-radius); | ||
grid-area: console; | ||
} | ||
|
||
.template-javascript { | ||
align-content: start; | ||
display: grid; | ||
gap: 0.5rem; | ||
grid-template-areas: | ||
"header header" | ||
"editor editor" | ||
"buttons console"; | ||
grid-template-columns: max-content 1fr; | ||
grid-template-rows: max-content 1fr; | ||
height: 100%; | ||
|
||
play-runner { | ||
display: none; | ||
} | ||
|
||
@media (max-width: $screen-sm) { | ||
grid-template-areas: | ||
"header" | ||
"editor" | ||
"buttons" | ||
"console"; | ||
grid-template-columns: 1fr; | ||
|
||
.buttons { | ||
flex-direction: row; | ||
justify-content: space-between; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I guess we want to change this to reflect the current ones?