Skip to content

🎥 Make videos programmatically with React

Elements: Move source code into a dialog

Igor Samokhovetsf898b99d4eb9
diff --git a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx
index 36e2fa207b..3d5043a55b 100644
--- a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx
+++ b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx
@@ -108,7 +108,6 @@ export const ElementInstallFallbackModal: React.FC<{
posterRef={posterRef}
showDragHandle
sourceCode={sourceCode}
- title="Install in the most recently focused Remotion Studio"
/>
{installFailureCount > 1 ? (
<div
diff --git a/packages/docs/src/components/Elements/ElementPage.module.css b/packages/docs/src/components/Elements/ElementPage.module.css
index efbed493c8..e84e169d1c 100644
--- a/packages/docs/src/components/Elements/ElementPage.module.css
+++ b/packages/docs/src/components/Elements/ElementPage.module.css
@@ -18,66 +18,70 @@
min-width: 0;
}
-.previewAndSource {
+.previewSurface {
overflow: hidden;
border: 1px solid var(--ifm-color-emphasis-300);
border-radius: 12px;
background: var(--ifm-background-surface-color);
}
-.sourceArea {
- position: relative;
- border-top: 1px solid var(--ifm-color-emphasis-300);
- background: #282a36;
-}
-
-.sourceToggle {
- border-radius: 8px;
- font: inherit;
- font-weight: 700;
- min-height: 34px;
- padding: 5px 10px;
- font-size: 0.875rem;
- border: 1px solid #4b4d57;
- background: #18191f;
- box-shadow: 0 2px 10px rgb(0 0 0 / 35%);
- color: #fff;
- cursor: pointer;
-}
-
-.sourceToggle:hover {
- background: #23242c;
+.secondaryActions {
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ margin-top: 12px;
}
-.sourceToggle:focus-visible,
+.secondaryAction:focus-visible,
+.sourceViewport:focus-visible,
.contributor:focus-visible {
outline: 2px solid var(--ifm-color-primary);
outline-offset: 2px;
}
-.sourceViewport {
- background: #282a36;
+.sourceDialog {
+ width: min(960px, calc(100vw - 32px));
+ max-width: none;
+ max-height: min(760px, calc(100dvh - 32px));
+ margin: auto;
+ padding: 0;
+ overflow: hidden;
+ border: 1px solid var(--ifm-color-emphasis-300);
+ border-radius: 12px;
+ background: var(--ifm-background-surface-color);
+ color: var(--ifm-font-color-base);
}
-.sourceViewportCollapsed {
- height: 180px;
- overflow: hidden;
- pointer-events: none;
- user-select: none;
+.sourceDialog[open] {
+ display: flex;
+ flex-direction: column;
+}
+
+.sourceDialog::backdrop {
+ background: rgb(0 0 0 / 45%);
}
-.sourceReveal {
- position: absolute;
- inset: 0;
+.sourceDialogHeader {
display: flex;
align-items: center;
- justify-content: center;
- background: linear-gradient(
- to bottom,
- rgb(40 42 54 / 5%) 0%,
- rgb(40 42 54 / 72%) 58%,
- #282a36 100%
- );
+ justify-content: space-between;
+ gap: 16px;
+ flex: none;
+ padding: 12px 16px;
+ border-bottom: 1px solid var(--ifm-color-emphasis-300);
+}
+
+.sourceDialogTitle {
+ min-width: 0;
+ margin: 0;
+ font-size: 1rem;
+ font-weight: 600;
+}
+
+.sourceViewport {
+ min-height: 0;
+ overflow: auto;
+ background: #282a36;
}
.sourceViewport :global(.theme-code-block),
@@ -100,18 +104,18 @@
flex-direction: column;
}
-.browserStudioAction {
+.secondaryAction {
display: inline-flex;
align-items: center;
- justify-content: center;
- gap: 4px;
+ justify-content: flex-start;
+ gap: 8px;
+ min-width: 0;
min-height: 32px;
- margin-top: 24px;
padding: 4px 0;
border: 0;
appearance: none;
background: transparent;
- color: var(--ifm-color-emphasis-800);
+ color: var(--ifm-color-emphasis-700);
cursor: pointer;
font: inherit;
font-size: 0.875rem;
@@ -119,21 +123,25 @@
line-height: 1.5;
}
-.browserStudioAction svg {
+.secondaryActionIcon {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ width: 16px;
+ height: 16px;
flex: none;
}
-.browserStudioAction:hover {
+.secondaryActionIcon svg {
+ flex: none;
+}
+
+.secondaryAction:hover {
color: var(--ifm-color-primary);
text-decoration: underline;
text-underline-offset: 2px;
}
-.browserStudioAction:focus-visible {
- outline: 2px solid var(--ifm-color-primary);
- outline-offset: 2px;
-}
-
.studioGuidance {
margin-top: 12px;
padding: 12px;
@@ -326,15 +334,15 @@
}
@media (pointer: coarse) {
- .browserStudioAction {
+ .secondaryAction {
min-height: 48px;
padding: 8px;
}
}
@media (max-width: 600px) {
- .sourceViewportCollapsed {
- height: 160px;
+ .secondaryAction {
+ font-size: 1rem;
}
.contributors {
diff --git a/packages/docs/src/components/Elements/ElementPage.tsx b/packages/docs/src/components/Elements/ElementPage.tsx
index ee5672ec67..1352ebd5a2 100644
--- a/packages/docs/src/components/Elements/ElementPage.tsx
+++ b/packages/docs/src/components/Elements/ElementPage.tsx
@@ -14,8 +14,10 @@ import React, {
useState,
type ReactNode,
} from 'react';
+import {createPortal} from 'react-dom';
import {InlineStep} from '../../../components/InlineStep';
import {ExternalLinkIcon} from '../../theme/DocBreadcrumbs/icons';
+import {Blank} from '../icons/blank';
import {Seo} from '../Seo';
import type {ElementDefinition} from './element-definitions';
import {
@@ -58,6 +60,8 @@ export const ElementPage: React.FC<ElementPageProps> = ({
null,
);
const posterRef = useRef<HTMLImageElement>(null);
+ const sourceDialogRef = useRef<HTMLDialogElement>(null);
+ const sourceButtonRef = useRef<HTMLButtonElement>(null);
const sourceId = useId();
const {height: previewHeight, width: previewWidth} =
getElementPreviewDimensions(definition);
@@ -66,6 +70,30 @@ export const ElementPage: React.FC<ElementPageProps> = ({
setIsEmbeddedInStudio(isInsideStudio());
}, []);
+ useEffect(() => {
+ if (!isSourceVisible) {
+ return;
+ }
+
+ const dialog = sourceDialogRef.current;
+ if (!dialog) {
+ return;
+ }
+
+ const sourceButton = sourceButtonRef.current;
+ const {overflow} = document.body.style;
+ document.body.style.overflow = 'hidden';
+ dialog.showModal();
+ return () => {
+ if (dialog.open) {
+ dialog.close();
+ }
+
+ document.body.style.overflow = overflow;
+ sourceButton?.focus();
+ };
+ }, [isSourceVisible]);
+
useEffect(() => {
if (installStatus.type !== 'installing') {
return;
@@ -157,7 +185,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({
})}
</Head>
<section aria-label="Preview" className={styles.previewColumn}>
- <div className={styles.previewAndSource}>
+ <div className={styles.previewSurface}>
<ElementPreview
component={PreviewComponent}
durationInFrames={durationInFrames}
@@ -172,34 +200,6 @@ export const ElementPage: React.FC<ElementPageProps> = ({
previewLayout={definition.preview.previewLayout}
safeArea={definition.safeArea}
/>
- {children ? (
- <div className={styles.sourceArea}>
- <div
- aria-label="Element source code"
- className={`${styles.sourceViewport} ${
- isSourceVisible ? '' : styles.sourceViewportCollapsed
- }`}
- id={sourceId}
- inert={!isSourceVisible}
- role="region"
- >
- {children}
- </div>
- {isSourceVisible ? null : (
- <div className={styles.sourceReveal}>
- <button
- aria-controls={sourceId}
- aria-expanded={isSourceVisible}
- className={styles.sourceToggle}
- onClick={() => setIsSourceVisible(true)}
- type="button"
- >
- View code
- </button>
- </div>
- )}
- </div>
- ) : null}
</div>
</section>
@@ -225,11 +225,6 @@ export const ElementPage: React.FC<ElementPageProps> = ({
installStatus.code === 'no-installable-target')
}
sourceCode={sourceCode}
- title={
- isEmbeddedInStudio
- ? 'Use this Element in Studio'
- : 'Install in the most recently focused Remotion Studio'
- }
/>
</div>
{installStatus.type === 'error' &&
@@ -295,6 +290,45 @@ export const ElementPage: React.FC<ElementPageProps> = ({
</>
) : null}
+ {children || sourceCode ? (
+ <div className={styles.secondaryActions}>
+ {children ? (
+ <button
+ ref={sourceButtonRef}
+ aria-controls={sourceId}
+ aria-haspopup="dialog"
+ className={styles.secondaryAction}
+ onClick={() => setIsSourceVisible(true)}
+ type="button"
+ >
+ <span
+ aria-hidden="true"
+ className={styles.secondaryActionIcon}
+ >
+ <Blank height={16} width={16} />
+ </span>
+ View code
+ </button>
+ ) : null}
+ {sourceCode ? (
+ <button
+ aria-label="Try this Element in Browser Studio in a new tab"
+ className={styles.secondaryAction}
+ onClick={openInBrowserStudio}
+ type="button"
+ >
+ <span
+ aria-hidden="true"
+ className={styles.secondaryActionIcon}
+ >
+ <ExternalLinkIcon size={16} />
+ </span>
+ Try in remotion.dev/new
+ </button>
+ ) : null}
+ </div>
+ ) : null}
+
<div className={styles.details}>
<p className={styles.description}>{description}</p>
{definition.category === 'captions' ? (
@@ -372,19 +406,57 @@ export const ElementPage: React.FC<ElementPageProps> = ({
</div>
</div>
) : null}
- {sourceCode ? (
- <button
- aria-label="Try this Element in Browser Studio in a new tab"
- className={styles.browserStudioAction}
- onClick={openInBrowserStudio}
- type="button"
- >
- Try in Browser Studio
- <ExternalLinkIcon />
- </button>
- ) : null}
</div>
</aside>
+ {isSourceVisible && children
+ ? createPortal(
+ <dialog
+ ref={sourceDialogRef}
+ aria-labelledby={`${sourceId}-title`}
+ className={styles.sourceDialog}
+ id={sourceId}
+ onCancel={(event) => {
+ event.preventDefault();
+ setIsSourceVisible(false);
+ }}
+ onClick={(event) => {
+ if (event.target !== event.currentTarget) {
+ return;
+ }
+
+ const bounds = event.currentTarget.getBoundingClientRect();
+ if (
+ event.clientX < bounds.left ||
+ event.clientX > bounds.right ||
+ event.clientY < bounds.top ||
+ event.clientY > bounds.bottom
+ ) {
+ setIsSourceVisible(false);
+ }
+ }}
+ >
+ <div className={styles.sourceDialogHeader}>
+ <h2
+ className={styles.sourceDialogTitle}
+ id={`${sourceId}-title`}
+ >
+ Element source code
+ </h2>
+ <button
+ className={styles.secondaryAction}
+ onClick={() => setIsSourceVisible(false)}
+ type="button"
+ >
+ Close
+ </button>
+ </div>
+ <div className={styles.sourceViewport} tabIndex={0}>
+ {children}
+ </div>
+ </dialog>,
+ document.body,
+ )
+ : null}
</div>
);
};
diff --git a/packages/docs/src/components/Elements/ElementStudioAction.tsx b/packages/docs/src/components/Elements/ElementStudioAction.tsx
index ec0c89c9f8..7e1e9c6409 100644
--- a/packages/docs/src/components/Elements/ElementStudioAction.tsx
+++ b/packages/docs/src/components/Elements/ElementStudioAction.tsx
@@ -16,7 +16,6 @@ export const ElementStudioAction: React.FC<{
readonly posterRef: RefObject<HTMLImageElement | null>;
readonly showDragHandle: boolean;
readonly sourceCode: string;
- readonly title: string;
}> = ({
buttonLabel,
definition,
@@ -25,7 +24,6 @@ export const ElementStudioAction: React.FC<{
posterRef,
showDragHandle,
sourceCode,
- title,
}) => {
const onDragStart = (event: React.DragEvent<HTMLElement>) => {
setStudioDragData({
@@ -50,7 +48,6 @@ export const ElementStudioAction: React.FC<{
onDragStart={showDragHandle ? onDragStart : undefined}
size="sm"
style={{padding: '7px 12px'}}
- title={title}
>
{buttonLabel}
</BlueButton>