From ea012368de00e33e02df32c552f56b46f1f1370e Mon Sep 17 00:00:00 2001 From: Leo McArdle Date: Thu, 5 Dec 2024 12:06:41 +0000 Subject: [PATCH] chore(playground): migrate console to web component https://mozilla-hub.atlassian.net/browse/MP-1739 --- client/src/playground/console.js | 49 ++++++++++++++++++++++++++++++ client/src/playground/console.scss | 34 +++++++++++++++++++++ client/src/playground/console.tsx | 30 ------------------ client/src/playground/index.scss | 31 ------------------- client/src/playground/index.tsx | 5 +-- client/src/playground/types.d.ts | 4 +++ 6 files changed, 90 insertions(+), 63 deletions(-) create mode 100644 client/src/playground/console.js create mode 100644 client/src/playground/console.scss delete mode 100644 client/src/playground/console.tsx create mode 100644 client/src/playground/types.d.ts diff --git a/client/src/playground/console.js b/client/src/playground/console.js new file mode 100644 index 000000000000..2897a3432004 --- /dev/null +++ b/client/src/playground/console.js @@ -0,0 +1,49 @@ +import { createComponent } from "@lit/react"; +import { html, LitElement } from "lit"; +import React from "react"; + +import styles from "./console.scss?css" with { type: "css" }; + +/** @import { VConsole } from "./types" */ + +export class PlayConsole extends LitElement { + static properties = { + vConsole: { attribute: false }, + }; + + static styles = styles; + + constructor() { + super(); + /** @type {VConsole[]} */ + this.vConsole = []; + } + + render() { + return html` + Console + + `; + } + + updated() { + const output = this.renderRoot.querySelector("ul"); + output?.scrollTo({ top: output.scrollHeight }); + } +} + +customElements.define("play-console", PlayConsole); + +export const ReactPlayConsole = createComponent({ + tagName: "play-console", + elementClass: PlayConsole, + react: React, +}); diff --git a/client/src/playground/console.scss b/client/src/playground/console.scss new file mode 100644 index 000000000000..15ae7aeadbf5 --- /dev/null +++ b/client/src/playground/console.scss @@ -0,0 +1,34 @@ +:host { + display: flex; + flex-direction: column; + font-size: smaller; + margin: 0; + width: 100%; + + > span { + background-color: var(--code-background-inline); + font-weight: 600; + text-align: center; + width: 100%; + } + + ul { + margin: 0; + padding: 0; + list-style: none; + + background-color: var(--code-background-inline); + height: 6rem; + max-height: 6rem; + overflow: auto; + width: 100%; + + li { + padding: 0 1rem; + + &::before { + content: ">"; + } + } + } +} diff --git a/client/src/playground/console.tsx b/client/src/playground/console.tsx deleted file mode 100644 index 6a182d441a86..000000000000 --- a/client/src/playground/console.tsx +++ /dev/null @@ -1,30 +0,0 @@ -import { useEffect, useRef } from "react"; - -export interface VConsole { - prop: string; - message: string; -} - -export function Console({ vConsole }: { vConsole: VConsole[] }) { - const consoleUl = useRef(null); - const scrollToBottom = () => { - consoleUl.current?.scrollTo({ top: consoleUl.current?.scrollHeight }); - }; - useEffect(() => { - scrollToBottom(); - }, [vConsole]); - return ( -
- Console -
    - {vConsole.map(({ prop, message }, i) => { - return ( -
  • - {message} -
  • - ); - })} -
-
- ); -} diff --git a/client/src/playground/index.scss b/client/src/playground/index.scss index d4a921affbeb..8789a49388b3 100644 --- a/client/src/playground/index.scss +++ b/client/src/playground/index.scss @@ -235,37 +235,6 @@ main.play { height: 100%; width: 100%; } - - #play-console { - display: flex; - flex-direction: column; - font-size: smaller; - margin: 0; - width: 100%; - - > span { - background-color: var(--code-background-inline); - font-weight: 600; - text-align: center; - width: 100%; - } - - ul { - background-color: var(--code-background-inline); - height: 6rem; - max-height: 6rem; - overflow: auto; - width: 100%; - - li { - padding: 0 1rem; - - &::before { - content: ">"; - } - } - } - } } } } diff --git a/client/src/playground/index.tsx b/client/src/playground/index.tsx index eb8e759b557d..0f2265e83112 100644 --- a/client/src/playground/index.tsx +++ b/client/src/playground/index.tsx @@ -21,9 +21,10 @@ import { import "./index.scss"; import { PLAYGROUND_BASE_HOST } from "../env"; import { FlagForm, ShareForm } from "./forms"; -import { Console, VConsole } from "./console"; +import { ReactPlayConsole } from "./console"; import { useGleanClick } from "../telemetry/glean-context"; import { PLAYGROUND } from "../telemetry/constants"; +import type { VConsole } from "./types"; const HTML_DEFAULT = ""; const CSS_DEFAULT = ""; @@ -418,7 +419,7 @@ export default function Playground() { src={iframeSrc} sandbox="allow-scripts allow-same-origin allow-forms" > - + diff --git a/client/src/playground/types.d.ts b/client/src/playground/types.d.ts new file mode 100644 index 000000000000..14cb634d04f2 --- /dev/null +++ b/client/src/playground/types.d.ts @@ -0,0 +1,4 @@ +export interface VConsole { + prop: string; + message: string; +}