🎥 Make videos programmatically with React
Elements: Show install fallback modal
JonnyBurgere8649875ebf8
diff --git a/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.css b/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.cssnew file mode 100644index 0000000000..88bf003f01--- /dev/null+++ b/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.css@@ -0,0 +1,92 @@+.dialog {+ width: min(440px, calc(100vw - 32px));+ max-width: none;+ margin: auto;+ padding: 0;+ border: 1px solid var(--ifm-color-emphasis-300);+ border-radius: 14px;+ background: var(--ifm-background-surface-color);+ box-shadow: 0 24px 70px rgb(0 0 0 / 35%);+ color: var(--ifm-font-color-base);+}++.dialog::backdrop {+ background: rgb(0 0 0 / 45%);+ backdrop-filter: blur(2px);+}++.content {+ position: relative;+ padding: 24px;+}++.closeButton {+ position: absolute;+ top: 12px;+ right: 12px;+ display: flex;+ width: 32px;+ height: 32px;+ align-items: center;+ justify-content: center;+ padding: 0;+ border: 0;+ border-radius: 8px;+ background: transparent;+ color: var(--ifm-color-emphasis-700);+ cursor: pointer;+ font: inherit;+ font-size: 1.5rem;+ line-height: 1;+}++.closeButton:hover {+ background: var(--ifm-color-emphasis-100);+}++.closeButton:focus-visible,+.copyButton:focus-visible {+ outline: 2px solid var(--ifm-color-primary);+ outline-offset: 2px;+}++.title {+ margin: 0;+ padding-right: 32px;+ font-size: 1.25rem;+}++.description {+ margin: 12px 0 0;+ color: var(--ifm-color-emphasis-800);+ line-height: 1.5;+}++.installAction {+ width: min(100%, 260px);+ margin-top: 20px;+}++.hint,+.copyHint {+ margin: 16px 0 0;+ font-size: 0.875rem;+ line-height: 1.5;+}++.copyHint {+ margin-top: 8px;+ color: var(--ifm-color-emphasis-700);+}++.copyButton {+ margin: 0;+ padding: 0;+ border: 0;+ background: transparent;+ color: var(--ifm-link-color);+ cursor: pointer;+ font: inherit;+ text-decoration: underline;+ text-underline-offset: 2px;+}diff --git a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsxnew file mode 100644index 0000000000..16b4a31429--- /dev/null+++ b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx@@ -0,0 +1,116 @@+import type {StudioElementPayload} from '@remotion/studio-protocol';+import React, {type RefObject, useEffect, useId, useRef, useState} from 'react';+import {createPortal} from 'react-dom';+import {ElementStudioAction} from './ElementStudioAction';+import styles from './ElementInstallFallbackModal.module.css';++export const ElementInstallFallbackModal: React.FC<{+ readonly isInstalling: boolean;+ readonly isOpen: boolean;+ readonly onClose: () => void;+ readonly onInstall: () => void;+ readonly payload: StudioElementPayload;+ readonly posterRef: RefObject<HTMLImageElement | null>;+ readonly sourceCode: string;+}> = ({+ isInstalling,+ isOpen,+ onClose,+ onInstall,+ payload,+ posterRef,+ sourceCode,+}) => {+ const dialogRef = useRef<HTMLDialogElement>(null);+ const [hasCopied, setHasCopied] = useState(false);+ const titleId = useId();++ useEffect(() => {+ if (!isOpen) {+ return;+ }++ const dialog = dialogRef.current;+ if (!dialog) {+ return;+ }++ dialog.showModal();+ return () => {+ if (dialog.open) {+ dialog.close();+ }+ };+ }, [isOpen]);++ useEffect(() => {+ if (!hasCopied) {+ return;+ }++ const timeout = window.setTimeout(() => setHasCopied(false), 2000);+ return () => window.clearTimeout(timeout);+ }, [hasCopied]);++ if (!isOpen) {+ return null;+ }++ return createPortal(+ <dialog+ ref={dialogRef}+ aria-labelledby={titleId}+ className={styles.dialog}+ onCancel={(event) => {+ event.preventDefault();+ onClose();+ }}+ >+ <div className={styles.content}>+ <button+ aria-label="Close"+ className={styles.closeButton}+ onClick={onClose}+ type="button"+ >+ ×+ </button>+ <h3 className={styles.title} id={titleId}>+ Use this element+ </h3>+ <p className={styles.description}>+ Open a Remotion Studio and click below to import the Element.+ </p>+ <div className={styles.installAction}>+ <ElementStudioAction+ buttonLabel="Install"+ loading={isInstalling}+ onClick={onInstall}+ payload={payload}+ posterRef={posterRef}+ showDragHandle+ title="Install in the most recently focused Remotion Studio"+ />+ </div>+ <p className={styles.hint}>+ Not working? Drag the button into your Studio instead.+ </p>+ <p className={styles.copyHint}>+ You can also{' '}+ <button+ className={styles.copyButton}+ onClick={async () => {+ await navigator.clipboard.writeText(sourceCode);+ setHasCopied(true);+ }}+ type="button"+ >+ copy the code+ </button>+ .<span aria-live="polite">{hasCopied ? ' Copied.' : null}</span>+ </p>+ </div>+ </dialog>,+ document.body,+ );+};diff --git a/packages/docs/src/components/Elements/ElementLibrary.tsx b/packages/docs/src/components/Elements/ElementLibrary.tsxindex 6a32143c5b..ac94ef6a0f 100644--- a/packages/docs/src/components/Elements/ElementLibrary.tsx+++ b/packages/docs/src/components/Elements/ElementLibrary.tsx@@ -11,6 +11,7 @@ import {getElementLibrarySections,type ElementCategory,} from './element-library-data';+import {ElementInstallFallbackModal} from './ElementInstallFallbackModal';import {ELEMENT_PREVIEW_BACKGROUND} from './ElementPreviewComposition';import styles from './ElementLibrary.module.css';@@ -45,6 +46,7 @@ const ElementCard: React.FC<{const [isPointerOver, setIsPointerOver] = useState(false);const [playbackFailed, setPlaybackFailed] = useState(false);const [isInstalling, setIsInstalling] = useState(false);+ const [isInstallFallbackOpen, setIsInstallFallbackOpen] = useState(false);const posterRef = useRef<HTMLImageElement>(null);const videoRef = useRef<HTMLVideoElement>(null);const shouldPlay =@@ -108,23 +110,18 @@ const ElementCard: React.FC<{fallbackPayload: assetPayload === null ? undefined : elementPayload,});if (!result.success) {- // eslint-disable-next-line no-alert- window.alert(result.message);+ setIsInstallFallbackOpen(true);return;}+ setIsInstallFallbackOpen(false);if (window.location.origin === 'https://www.remotion.dev') {navigator.sendBeacon(`https://www.remotion.pro/api/track/element-install-request?slug=${encodeURIComponent(definition.slug)}`,);}- } catch (error) {- // eslint-disable-next-line no-alert- window.alert(- error instanceof Error- ? error.message- : 'Could not install this Element in Studio.',- );+ } catch {+ setIsInstallFallbackOpen(true);} finally {setIsInstalling(false);}@@ -198,6 +195,15 @@ const ElementCard: React.FC<{Use</BlueButton></div>+ <ElementInstallFallbackModal+ isInstalling={isInstalling}+ isOpen={isInstallFallbackOpen}+ onClose={() => setIsInstallFallbackOpen(false)}+ onInstall={installElement}+ payload={elementPayload}+ posterRef={posterRef}+ sourceCode={sourceCode}+ /></li>);};diff --git a/packages/docs/src/components/Elements/ElementPage.module.css b/packages/docs/src/components/Elements/ElementPage.module.cssindex 080d12e6d5..37c1826bc6 100644--- a/packages/docs/src/components/Elements/ElementPage.module.css+++ b/packages/docs/src/components/Elements/ElementPage.module.css@@ -101,91 +101,8 @@gap: 10px;}-.studioAction {- display: flex;- align-items: stretch;- width: 100%;- border-radius: 8px;- background: var(--blue-underlay);-}--.studioAction .installButtonWithDragHandle {- border-top-right-radius: 0;- border-right-width: 0;- border-bottom-right-radius: 0;-}--.dragHandle {- display: flex;- align-items: center;- justify-content: center;- width: 46px;- min-height: 46px;- flex: none;- border: 2px solid black;- border-bottom-width: 4px;- border-left: 1px solid rgb(0 0 0 / 30%);- border-radius: 0 8px 8px 0;- background: transparent;- color: white;- cursor: grab;- user-select: none;-}--.dragHandle:hover {- background: rgb(0 0 0 / 8%);-}--.dragHandle:active {- cursor: grabbing;-}--.dragHandleIcon {- font-size: 1.5rem;- line-height: 1;-}--.studioGuidance {- margin-top: 12px;- padding: 12px;- border: 1px solid var(--ifm-color-emphasis-300);- border-radius: 8px;- background: var(--ifm-color-emphasis-100);- font-size: 0.875rem;-}--.studioGuidanceTitle {- margin: 0;- font-weight: 600;-}--.studioGuidanceSteps {- display: flex;- flex-direction: column;- gap: 8px;- margin: 10px 0 0;- padding: 0;- list-style: none;-}--.studioGuidanceSteps li {- display: flex;- align-items: flex-start;- gap: 8px;- line-height: 1.5;-}--.studioGuidanceSteps li > div {- flex: none;-}--.studioGuidanceSteps li > span {- min-width: 0;-}-.installingStatus,-.successStatus,-.errorStatus {+.successStatus {margin: 12px 0 0;font-size: 0.875rem;}@@ -198,10 +115,6 @@color: var(--ifm-color-success-dark);}-.errorStatus {- color: var(--ifm-color-danger-dark);-}-.details {margin-top: 20px;}@@ -336,10 +249,6 @@height: 160px;}- .studioGuidance {- font-size: 1rem;- }-.contributors {align-items: flex-start;flex-direction: column;diff --git a/packages/docs/src/components/Elements/ElementPage.tsx b/packages/docs/src/components/Elements/ElementPage.tsxindex 95c72edc39..537e63a59e 100644--- a/packages/docs/src/components/Elements/ElementPage.tsx+++ b/packages/docs/src/components/Elements/ElementPage.tsx@@ -2,9 +2,7 @@ import Head from '@docusaurus/Head';import {installInStudio,isInsideStudio,- setStudioDragData,StudioProtocolInternals,- type InstallInStudioErrorCode,} from '@remotion/studio-protocol';import React, {useCallback,@@ -16,20 +14,18 @@ import React, {useState,type ReactNode,} from 'react';-import {InlineStep} from '../../../components/InlineStep';-import {BlueButton, PlainButton} from '../../../components/layout/Button';+import {PlainButton} from '../../../components/layout/Button';import {Seo} from '../Seo';import type {ElementDefinition} from './element-definitions';-import {- createElementPayloadFromDefinition,- setElementDragImage,-} from './element-drag-data';+import {createElementPayloadFromDefinition} from './element-drag-data';import {getElementDimensionsLabel} from './element-utils';+import {ElementInstallFallbackModal} from './ElementInstallFallbackModal';import {ElementPreview} from './ElementPreview';import {ElementPreviewComposition,getElementPreviewDimensions,} from './ElementPreviewComposition';+import {ElementStudioAction} from './ElementStudioAction';import styles from './ElementPage.module.css';type ElementPageProps = {@@ -41,8 +37,7 @@ type ElementPageProps = {type InstallStatus =| {type: 'idle'}| {type: 'installing'}- | {type: 'success'; message: string}- | {type: 'error'; code: InstallInStudioErrorCode; message: string};+ | {type: 'success'; message: string};export const ElementPage: React.FC<ElementPageProps> = ({children,@@ -53,6 +48,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({const [installStatus, setInstallStatus] = useState<InstallStatus>({type: 'idle',});+ const [isInstallFallbackOpen, setIsInstallFallbackOpen] = useState(false);const [isInstallHintVisible, setIsInstallHintVisible] = useState(false);const [isSourceVisible, setIsSourceVisible] = useState(false);const [isBrowserStudioActionVisible, setIsBrowserStudioActionVisible] =@@ -142,40 +138,41 @@ export const ElementPage: React.FC<ElementPageProps> = ({}setIsInstallHintVisible(false);- if (!isEmbeddedInStudio) {- setInstallStatus({type: 'installing'});- }+ setInstallStatus({type: 'installing'});- const result = await installInStudio({- payload: assetPayload ?? elementPayload,- fallbackPayload: assetPayload === null ? undefined : elementPayload,- });- if (!result.success) {- setInstallStatus({- type: 'error',- code: result.code,- message: result.message,+ try {+ const result = await installInStudio({+ payload: assetPayload ?? elementPayload,+ fallbackPayload: assetPayload === null ? undefined : elementPayload,});- return;- }+ if (!result.success) {+ setInstallStatus({type: 'idle'});+ setIsInstallFallbackOpen(true);+ return;+ }- if (isEmbeddedInStudio) {- setInstallStatus({type: 'idle'});- } else {- const {target} = result;- setInstallStatus({- type: 'success',- message:- target.compositionId === null- ? `Sent to ${target.projectName ?? 'Remotion Studio'}. Confirm the installation destination in Studio.`- : `Sent to ${target.projectName ?? 'Remotion Studio'} (currently ${target.compositionId}). Confirm the installation destination in Studio.`,- });- }+ setIsInstallFallbackOpen(false);+ if (isEmbeddedInStudio) {+ setInstallStatus({type: 'idle'});+ } else {+ const {target} = result;+ setInstallStatus({+ type: 'success',+ message:+ target.compositionId === null+ ? `Sent to ${target.projectName ?? 'Remotion Studio'}. Confirm the installation destination in Studio.`+ : `Sent to ${target.projectName ?? 'Remotion Studio'} (currently ${target.compositionId}). Confirm the installation destination in Studio.`,+ });+ }- if (window.location.origin === 'https://www.remotion.dev') {- navigator.sendBeacon(- `https://www.remotion.pro/api/track/element-install-request?slug=${encodeURIComponent(definition.slug)}`,- );+ if (window.location.origin === 'https://www.remotion.dev') {+ navigator.sendBeacon(+ `https://www.remotion.pro/api/track/element-install-request?slug=${encodeURIComponent(definition.slug)}`,+ );+ }+ } catch {+ setInstallStatus({type: 'idle'});+ setIsInstallFallbackOpen(true);}}, [assetPayload, definition.slug, elementPayload, isEmbeddedInStudio]);@@ -266,52 +263,19 @@ export const ElementPage: React.FC<ElementPageProps> = ({{elementPayload === null ? null : (<><div className={styles.actionRow}>- <div className={styles.studioAction}>- <BlueButton- className={- isEmbeddedInStudio === false- ? styles.installButtonWithDragHandle- : undefined- }- fullWidth- loading={installStatus.type === 'installing'}- onClick={installElement}- size="sm"- style={{padding: '7px 12px'}}- title={- isEmbeddedInStudio- ? 'Use this Element in Studio'- : 'Install in the most recently focused Remotion Studio'- }- >- Use- </BlueButton>- {isEmbeddedInStudio === false ? (- <div- aria-label="Drag into Studio"- className={styles.dragHandle}- draggable- onDragStart={(event) => {- setStudioDragData({- dataTransfer: event.dataTransfer,- payload: elementPayload,- });- setElementDragImage(- event.dataTransfer,- posterRef.current,- );- }}- title="Drag into your Studio browser tab to choose where the element is placed on the canvas or timeline"- >- <span- aria-hidden="true"- className={styles.dragHandleIcon}- >- ⠿- </span>- </div>- ) : null}- </div>+ <ElementStudioAction+ buttonLabel="Use"+ loading={installStatus.type === 'installing'}+ onClick={installElement}+ payload={elementPayload}+ posterRef={posterRef}+ showDragHandle={isEmbeddedInStudio === false}+ title={+ isEmbeddedInStudio+ ? 'Use this Element in Studio'+ : 'Install in the most recently focused Remotion Studio'+ }+ />{isBrowserStudioActionVisible ? (<PlainButtonfullWidth@@ -324,43 +288,14 @@ export const ElementPage: React.FC<ElementPageProps> = ({</PlainButton>) : null}</div>- {installStatus.type === 'error' &&- installStatus.code === 'no-compatible-studio' ? (- <div aria-live="polite" className={styles.studioGuidance}>- <p className={styles.studioGuidanceTitle}>- Connect to Remotion Studio- </p>- <ol className={styles.studioGuidanceSteps} role="list">- <li>- <InlineStep>1</InlineStep>- <span>- Open your Remotion project, or{' '}- <a href="/docs/">create a new one</a>.- </span>- </li>- <li>- <InlineStep>2</InlineStep>- <span>Start Studio and open a composition.</span>- </li>- <li>- <InlineStep>3</InlineStep>- <span>- Return here and click <strong>Use</strong> again.- </span>- </li>- </ol>- </div>- ) : installStatus.type !== 'idle' &&- (installStatus.type !== 'installing' ||- isInstallHintVisible) ? (+ {installStatus.type !== 'idle' &&+ (installStatus.type !== 'installing' || isInstallHintVisible) ? (<paria-live="polite"className={installStatus.type === 'installing'? styles.installingStatus- : installStatus.type === 'success'- ? styles.successStatus- : styles.errorStatus+ : styles.successStatus}>{installStatus.type === 'installing'@@ -451,6 +386,17 @@ export const ElementPage: React.FC<ElementPageProps> = ({) : null}</div></aside>+ {elementPayload === null || sourceCode === undefined ? null : (+ <ElementInstallFallbackModal+ isInstalling={installStatus.type === 'installing'}+ isOpen={isInstallFallbackOpen}+ onClose={() => setIsInstallFallbackOpen(false)}+ onInstall={installElement}+ payload={elementPayload}+ posterRef={posterRef}+ sourceCode={sourceCode}+ />+ )}</div>);};diff --git a/packages/docs/src/components/Elements/ElementStudioAction.module.css b/packages/docs/src/components/Elements/ElementStudioAction.module.cssnew file mode 100644index 0000000000..bb7986f09a--- /dev/null+++ b/packages/docs/src/components/Elements/ElementStudioAction.module.css@@ -0,0 +1,43 @@+.studioAction {+ display: flex;+ align-items: stretch;+ width: 100%;+ border-radius: 8px;+ background: var(--blue-underlay);+}++.studioAction .buttonWithDragHandle {+ border-top-right-radius: 0;+ border-right-width: 0;+ border-bottom-right-radius: 0;+}++.dragHandle {+ display: flex;+ align-items: center;+ justify-content: center;+ width: 46px;+ min-height: 46px;+ flex: none;+ border: 2px solid black;+ border-bottom-width: 4px;+ border-left: 1px solid rgb(0 0 0 / 30%);+ border-radius: 0 8px 8px 0;+ background: transparent;+ color: white;+ cursor: grab;+ user-select: none;+}++.dragHandle:hover {+ background: rgb(0 0 0 / 8%);+}++.dragHandle:active {+ cursor: grabbing;+}++.dragHandleIcon {+ font-size: 1.5rem;+ line-height: 1;+}diff --git a/packages/docs/src/components/Elements/ElementStudioAction.tsx b/packages/docs/src/components/Elements/ElementStudioAction.tsxnew file mode 100644index 0000000000..a697ff6dea--- /dev/null+++ b/packages/docs/src/components/Elements/ElementStudioAction.tsx@@ -0,0 +1,61 @@+import {+ setStudioDragData,+ type StudioElementPayload,+} from '@remotion/studio-protocol';+import React, {type RefObject} from 'react';+import {BlueButton} from '../../../components/layout/Button';+import {setElementDragImage} from './element-drag-data';+import styles from './ElementStudioAction.module.css';++export const ElementStudioAction: React.FC<{+ readonly buttonLabel: string;+ readonly loading: boolean;+ readonly onClick: () => void;+ readonly payload: StudioElementPayload;+ readonly posterRef: RefObject<HTMLImageElement | null>;+ readonly showDragHandle: boolean;+ readonly title: string;+}> = ({+ buttonLabel,+ loading,+ onClick,+ payload,+ posterRef,+ showDragHandle,+ title,+}) => {+ return (+ <div className={styles.studioAction}>+ <BlueButton+ className={showDragHandle ? styles.buttonWithDragHandle : undefined}+ fullWidth+ loading={loading}+ onClick={onClick}+ size="sm"+ style={{padding: '7px 12px'}}+ title={title}+ >+ {buttonLabel}+ </BlueButton>+ {showDragHandle ? (+ <div+ aria-label="Drag into Studio"+ className={styles.dragHandle}+ draggable+ onDragStart={(event) => {+ setStudioDragData({+ dataTransfer: event.dataTransfer,+ payload,+ });+ setElementDragImage(event.dataTransfer, posterRef.current);+ }}+ title="Drag into your Studio browser tab to choose where the element is placed on the canvas or timeline"+ >+ <span aria-hidden="true" className={styles.dragHandleIcon}>+ ⠿+ </span>+ </div>+ ) : null}+ </div>+ );+};