Skip to content

🎥 Make videos programmatically with React

Studio: Highlight source code in error overlays

Igor Samokhovetsa80bf7139aa5
diff --git a/packages/example/e2e/error-overlay.test.mts b/packages/example/e2e/error-overlay.test.mts
index b1c944cd90..ccd9763212 100644
--- a/packages/example/e2e/error-overlay.test.mts
+++ b/packages/example/e2e/error-overlay.test.mts
@@ -311,6 +311,16 @@ test.describe('error overlay dismissal', () => {
await expect(
page.getByText('ErrorOverlayRepro', {exact: true}),
).toBeVisible();
+ const highlightedSourceLine = page.getByText('effects={[blur({})]}', {
+ exact: true,
+ });
+ await expect(highlightedSourceLine).toHaveCSS(
+ 'color',
+ 'rgb(156, 220, 254)',
+ );
+ await expect(
+ highlightedSourceLine.locator('span.token.function', {hasText: /^blur$/}),
+ ).toHaveCSS('color', 'rgb(220, 220, 170)');
await expect.poll(getCurrentError).toEqual({
name: 'TypeError',
message: '"radius" must be a finite number, but got undefined',
diff --git a/packages/studio/src/components/ElementInstallConfirmation.tsx b/packages/studio/src/components/ElementInstallConfirmation.tsx
index 691243c334..b9a5c8c56a 100644
--- a/packages/studio/src/components/ElementInstallConfirmation.tsx
+++ b/packages/studio/src/components/ElementInstallConfirmation.tsx
@@ -1,7 +1,5 @@
import {StudioProtocolInternals} from '@remotion/studio-protocol';
import React, {
- lazy,
- Suspense,
useCallback,
useContext,
useEffect,
@@ -37,6 +35,7 @@ import {Button} from './Button';
import {prepareElementInstall} from './element-install-api';
import {insertElement} from './import-assets';
import {Flex, Row, Spacing} from './layout';
+import {LazySyntaxHighlightedSource} from './LazySyntaxHighlightedSource';
import {
HORIZONTAL_SCROLLBAR_CLASSNAME,
VERTICAL_SCROLLBAR_CLASSNAME,
@@ -63,10 +62,6 @@ import {
useResolvedStack,
} from './Timeline/use-resolved-stack';
-const HighlightedElementSource = lazy(
- () => import('./HighlightedElementSource'),
-);
-
const container: React.CSSProperties = {
display: 'flex',
flexDirection: 'column',
@@ -1032,9 +1027,7 @@ export const ElementInstallConfirmation: React.FC<{
style={sourceCodeBlockStyle}
>
<code style={sourceCodeStyle}>
- <Suspense fallback={sourcePreview}>
- <HighlightedElementSource source={sourcePreview} />
- </Suspense>
+ <LazySyntaxHighlightedSource source={sourcePreview} />
</code>
</pre>
</div>
diff --git a/packages/studio/src/components/LazySyntaxHighlightedSource.tsx b/packages/studio/src/components/LazySyntaxHighlightedSource.tsx
new file mode 100644
index 0000000000..91d294de04
--- /dev/null
+++ b/packages/studio/src/components/LazySyntaxHighlightedSource.tsx
@@ -0,0 +1,13 @@
+import React, {lazy, Suspense} from 'react';
+
+const SyntaxHighlightedSource = lazy(() => import('./SyntaxHighlightedSource'));
+
+export const LazySyntaxHighlightedSource: React.FC<{
+ readonly source: string;
+}> = ({source}) => {
+ return (
+ <Suspense fallback={source}>
+ <SyntaxHighlightedSource source={source} />
+ </Suspense>
+ );
+};
diff --git a/packages/studio/src/components/HighlightedElementSource.tsx b/packages/studio/src/components/SyntaxHighlightedSource.tsx
similarity index 92%
rename from packages/studio/src/components/HighlightedElementSource.tsx
rename to packages/studio/src/components/SyntaxHighlightedSource.tsx
index ddbf81c930..b712c1492b 100644
--- a/packages/studio/src/components/HighlightedElementSource.tsx
+++ b/packages/studio/src/components/SyntaxHighlightedSource.tsx
@@ -46,7 +46,7 @@ const renderTokens = (
);
};
-const HighlightedElementSource: React.FC<{readonly source: string}> = ({
+const SyntaxHighlightedSource: React.FC<{readonly source: string}> = ({
source,
}) => {
const tokens = useMemo(
@@ -60,4 +60,4 @@ const HighlightedElementSource: React.FC<{readonly source: string}> = ({
);
};
-export default HighlightedElementSource;
+export default SyntaxHighlightedSource;
diff --git a/packages/studio/src/error-overlay/remotion-overlay/CodeFrame.tsx b/packages/studio/src/error-overlay/remotion-overlay/CodeFrame.tsx
index 2c2ea01e9a..903084ed82 100644
--- a/packages/studio/src/error-overlay/remotion-overlay/CodeFrame.tsx
+++ b/packages/studio/src/error-overlay/remotion-overlay/CodeFrame.tsx
@@ -1,5 +1,6 @@
import type {ScriptLine} from '@remotion/studio-shared';
import React from 'react';
+import {LazySyntaxHighlightedSource} from '../../components/LazySyntaxHighlightedSource';
import {HORIZONTAL_SCROLLBAR_CLASSNAME} from '../../components/Menu/is-menu-item';
import {
ERROR_CODE_FRAME_BACKGROUND,
@@ -58,20 +59,23 @@ export const CodeFrame: React.FC<{
>
{String(s.lineNumber).padStart(lineNumberWidth, ' ')}
</div>
- <div
+ <code
+ className="language-tsx"
style={{
fontFamily: 'monospace',
+ fontSize: 14,
+ color: '#9cdcfe',
whiteSpace: 'pre',
tabSize: 2,
- color: s.highlight ? WHITE : WHITE_ALPHA_40,
+ opacity: s.highlight ? 1 : 0.4,
backgroundColor: TRANSPARENT,
lineHeight: 1.7,
paddingRight: 12,
paddingLeft: 12,
}}
>
- {s.content}
- </div>
+ <LazySyntaxHighlightedSource source={s.content} />
+ </code>
</div>
);
})}
diff --git a/packages/studio/src/helpers/inject-css.ts b/packages/studio/src/helpers/inject-css.ts
index f7f6de4acc..10eea6575e 100644
--- a/packages/studio/src/helpers/inject-css.ts
+++ b/packages/studio/src/helpers/inject-css.ts
@@ -9,7 +9,7 @@ import {
WHITE,
} from './colors';
import {FOCUS_VISIBLE_ONLY_CLASS_NAME, makeHoverableCSS} from './hoverable';
-import {prismTomorrow} from './prism-tomorrow';
+import {prismVscDarkPlus} from './prism-vsc-dark-plus';
const makeDefaultGlobalCSS = () => {
const dragAreaFactor = 2;
@@ -177,7 +177,7 @@ export const injectCSS = () => {
return;
}
- Internals.CSSUtils.injectCSS(prismTomorrow);
+ Internals.CSSUtils.injectCSS(prismVscDarkPlus);
Internals.CSSUtils.injectCSS(makeDefaultGlobalCSS());
injected = true;
};
diff --git a/packages/studio/src/helpers/prism-tomorrow.ts b/packages/studio/src/helpers/prism-tomorrow.ts
deleted file mode 100644
index 04d7e2ed30..0000000000
--- a/packages/studio/src/helpers/prism-tomorrow.ts
+++ /dev/null
@@ -1,148 +0,0 @@
-// Unmodified [email protected]/themes/prism-tomorrow.css, stored as a string for
-// Studio’s CSS injection (including Browser Studio, which has no CSS loader).
-/*
-MIT LICENSE
-
-Copyright (c) 2012 Lea Verou
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-*/
-export const prismTomorrow = `/**
- * prism.js tomorrow night eighties for JavaScript, CoffeeScript, CSS and HTML
- * Based on https://github.com/chriskempson/tomorrow-theme
- * @author Rose Pritchard
- */
-
-code[class*="language-"],
-pre[class*="language-"] {
- color: #ccc;
- background: none;
- font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
- font-size: 1em;
- text-align: left;
- white-space: pre;
- word-spacing: normal;
- word-break: normal;
- word-wrap: normal;
- line-height: 1.5;
-
- -moz-tab-size: 4;
- -o-tab-size: 4;
- tab-size: 4;
-
- -webkit-hyphens: none;
- -moz-hyphens: none;
- -ms-hyphens: none;
- hyphens: none;
-
-}
-
-/* Code blocks */
-pre[class*="language-"] {
- padding: 1em;
- margin: .5em 0;
- overflow: auto;
-}
-
-:not(pre) > code[class*="language-"],
-pre[class*="language-"] {
- background: #2d2d2d;
-}
-
-/* Inline code */
-:not(pre) > code[class*="language-"] {
- padding: .1em;
- border-radius: .3em;
- white-space: normal;
-}
-
-.token.comment,
-.token.block-comment,
-.token.prolog,
-.token.doctype,
-.token.cdata {
- color: #999;
-}
-
-.token.punctuation {
- color: #ccc;
-}
-
-.token.tag,
-.token.attr-name,
-.token.namespace,
-.token.deleted {
- color: #e2777a;
-}
-
-.token.function-name {
- color: #6196cc;
-}
-
-.token.boolean,
-.token.number,
-.token.function {
- color: #f08d49;
-}
-
-.token.property,
-.token.class-name,
-.token.constant,
-.token.symbol {
- color: #f8c555;
-}
-
-.token.selector,
-.token.important,
-.token.atrule,
-.token.keyword,
-.token.builtin {
- color: #cc99cd;
-}
-
-.token.string,
-.token.char,
-.token.attr-value,
-.token.regex,
-.token.variable {
- color: #7ec699;
-}
-
-.token.operator,
-.token.entity,
-.token.url {
- color: #67cdcc;
-}
-
-.token.important,
-.token.bold {
- font-weight: bold;
-}
-.token.italic {
- font-style: italic;
-}
-
-.token.entity {
- cursor: help;
-}
-
-.token.inserted {
- color: green;
-}
-`;
diff --git a/packages/studio/src/helpers/prism-vsc-dark-plus.ts b/packages/studio/src/helpers/prism-vsc-dark-plus.ts
new file mode 100644
index 0000000000..8183a4faf3
--- /dev/null
+++ b/packages/studio/src/helpers/prism-vsc-dark-plus.ts
@@ -0,0 +1,302 @@
+// Unmodified [email protected]/themes/prism-vsc-dark-plus.css, stored as a
+// string for Studio’s CSS injection (including Browser Studio, which has no
+// CSS loader).
+/*
+The MIT License (MIT)
+
+Copyright (c) 2015 PrismJS
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+export const prismVscDarkPlus = `pre[class*="language-"],
+code[class*="language-"] {
+ color: #d4d4d4;
+ font-size: 13px;
+ text-shadow: none;
+ font-family: Menlo, Monaco, Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace;
+ direction: ltr;
+ text-align: left;
+ white-space: pre;
+ word-spacing: normal;
+ word-break: normal;
+ line-height: 1.5;
+ -moz-tab-size: 4;
+ -o-tab-size: 4;
+ tab-size: 4;
+ -webkit-hyphens: none;
+ -moz-hyphens: none;
+ -ms-hyphens: none;
+ hyphens: none;
+}
+
+pre[class*="language-"]::selection,
+code[class*="language-"]::selection,
+pre[class*="language-"] *::selection,
+code[class*="language-"] *::selection {
+ text-shadow: none;
+ background: #264F78;
+}
+
+@media print {
+ pre[class*="language-"],
+ code[class*="language-"] {
+ text-shadow: none;
+ }
+}
+
+pre[class*="language-"] {
+ padding: 1em;
+ margin: .5em 0;
+ overflow: auto;
+ background: #1e1e1e;
+}
+
+:not(pre) > code[class*="language-"] {
+ padding: .1em .3em;
+ border-radius: .3em;
+ color: #db4c69;
+ background: #1e1e1e;
+}
+/*********************************************************
+* Tokens
+*/
+.namespace {
+ opacity: .7;
+}
+
+.token.doctype .token.doctype-tag {
+ color: #569CD6;
+}
+
+.token.doctype .token.name {
+ color: #9cdcfe;
+}
+
+.token.comment,
+.token.prolog {
+ color: #6a9955;
+}
+
+.token.punctuation,
+.language-html .language-css .token.punctuation,
+.language-html .language-javascript .token.punctuation {
+ color: #d4d4d4;
+}
+
+.token.property,
+.token.tag,
+.token.boolean,
+.token.number,
+.token.constant,
+.token.symbol,
+.token.inserted,
+.token.unit {
+ color: #b5cea8;
+}
+
+.token.selector,
+.token.attr-name,
+.token.string,
+.token.char,
+.token.builtin,
+.token.deleted {
+ color: #ce9178;
+}
+
+.language-css .token.string.url {
+ text-decoration: underline;
+}
+
+.token.operator,
+.token.entity {
+ color: #d4d4d4;
+}
+
+.token.operator.arrow {
+ color: #569CD6;
+}
+
+.token.atrule {
+ color: #ce9178;
+}
+
+.token.atrule .token.rule {
+ color: #c586c0;
+}
+
+.token.atrule .token.url {
+ color: #9cdcfe;
+}
+
+.token.atrule .token.url .token.function {
+ color: #dcdcaa;
+}
+
+.token.atrule .token.url .token.punctuation {
+ color: #d4d4d4;
+}
+
+.token.keyword {
+ color: #569CD6;
+}
+
+.token.keyword.module,
+.token.keyword.control-flow {
+ color: #c586c0;
+}
+
+.token.function,
+.token.function .token.maybe-class-name {
+ color: #dcdcaa;
+}
+
+.token.regex {
+ color: #d16969;
+}
+
+.token.important {
+ color: #569cd6;
+}
+
+.token.italic {
+ font-style: italic;
+}
+
+.token.constant {
+ color: #9cdcfe;
+}
+
+.token.class-name,
+.token.maybe-class-name {
+ color: #4ec9b0;
+}
+
+.token.console {
+ color: #9cdcfe;
+}
+
+.token.parameter {
+ color: #9cdcfe;
+}
+
+.token.interpolation {
+ color: #9cdcfe;
+}
+
+.token.punctuation.interpolation-punctuation {
+ color: #569cd6;
+}
+
+.token.boolean {
+ color: #569cd6;
+}
+
+.token.property,
+.token.variable,
+.token.imports .token.maybe-class-name,
+.token.exports .token.maybe-class-name {
+ color: #9cdcfe;
+}
+
+.token.selector {
+ color: #d7ba7d;
+}
+
+.token.escape {
+ color: #d7ba7d;
+}
+
+.token.tag {
+ color: #569cd6;
+}
+
+.token.tag .token.punctuation {
+ color: #808080;
+}
+
+.token.cdata {
+ color: #808080;
+}
+
+.token.attr-name {
+ color: #9cdcfe;
+}
+
+.token.attr-value,
+.token.attr-value .token.punctuation {
+ color: #ce9178;
+}
+
+.token.attr-value .token.punctuation.attr-equals {
+ color: #d4d4d4;
+}
+
+.token.entity {
+ color: #569cd6;
+}
+
+.token.namespace {
+ color: #4ec9b0;
+}
+/*********************************************************
+* Language Specific
+*/
+
+pre[class*="language-javascript"],
+code[class*="language-javascript"],
+pre[class*="language-jsx"],
+code[class*="language-jsx"],
+pre[class*="language-typescript"],
+code[class*="language-typescript"],
+pre[class*="language-tsx"],
+code[class*="language-tsx"] {
+ color: #9cdcfe;
+}
+
+pre[class*="language-css"],
+code[class*="language-css"] {
+ color: #ce9178;
+}
+
+pre[class*="language-html"],
+code[class*="language-html"] {
+ color: #d4d4d4;
+}
+
+.language-regex .token.anchor {
+ color: #dcdcaa;
+}
+
+.language-html .token.punctuation {
+ color: #808080;
+}
+/*********************************************************
+* Line highlighting
+*/
+pre[class*="language-"] > code[class*="language-"] {
+ position: relative;
+ z-index: 1;
+}
+
+.line-highlight.line-highlight {
+ background: #f7ebc6;
+ box-shadow: inset 5px 0 0 #f7d87c;
+ z-index: 0;
+}
+`;