🎥 Make videos programmatically with React
Elements: Share install fallback dialog scaffolding
Igor Samokhovets45c2760bc8b3
diff --git a/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.css b/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.cssindex 8af5c1b5d5..e4f6a67317 100644--- a/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.css+++ b/packages/docs/src/components/Elements/ElementInstallFallbackModal.module.css@@ -1,64 +1,12 @@-.dialog {- width: min(440px, calc(100vw - 32px));- max-width: none;- margin: auto;- padding: 0;- border: 0;- border-radius: 8px;- 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: 16.8px;- font-size: 0.875rem;- line-height: 1.5;-}--.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: inherit;- line-height: inherit;-}-.description {margin: 12px 0 0;}diff --git a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsxindex 2fba074d53..cd91569d8c 100644--- a/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx+++ b/packages/docs/src/components/Elements/ElementInstallFallbackModal.tsx@@ -1,8 +1,8 @@import Link from '@docusaurus/Link';-import React, {type RefObject, useEffect, useId, useRef, useState} from 'react';-import {createPortal} from 'react-dom';+import React, {type RefObject, useEffect, useState} from 'react';import type {ElementDefinition} from './element-definitions';import {ElementStudioAction} from './ElementStudioAction';+import {StudioInstallFallbackDialog} from './StudioInstallFallbackDialog';import styles from './ElementInstallFallbackModal.module.css';export const ElementInstallFallbackModal: React.FC<{@@ -15,6 +15,7 @@ export const ElementInstallFallbackModal: React.FC<{readonly onInstall: () => void;readonly posterRef: RefObject<HTMLImageElement | null>;readonly sourceCode: string;+ readonly triggerRef: RefObject<HTMLElement | null>;}> = ({buttonLabel,definition,@@ -25,28 +26,9 @@ export const ElementInstallFallbackModal: React.FC<{onInstall,posterRef,sourceCode,+ triggerRef,}) => {- 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) {@@ -57,112 +39,77 @@ export const ElementInstallFallbackModal: React.FC<{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();- }}- onClick={(event) => {- if (event.target !== event.currentTarget) {- return;- }-- const bounds = event.currentTarget.getBoundingClientRect();- const clickedOutside =- event.clientX < bounds.left ||- event.clientX > bounds.right ||- event.clientY < bounds.top ||- event.clientY > bounds.bottom;-- if (clickedOutside) {- onClose();- }- }}+ return (+ <StudioInstallFallbackDialog+ contentClassName={styles.content}+ isOpen={isOpen}+ onClose={onClose}+ title="Use this element"+ titleClassName={null}+ triggerRef={triggerRef}>- <div className={styles.content}>+ <p className={styles.description}>+ <Link to="/docs/studio/" target="_blank" rel="noreferrer">+ Open a Remotion Studio+ </Link>+ , then click below.+ </p>+ <div className={styles.installAction}>+ <ElementStudioAction+ buttonLabel={buttonLabel}+ definition={definition}+ loading={isInstalling}+ onClick={onInstall}+ posterRef={posterRef}+ showDragHandle+ sourceCode={sourceCode}+ />+ {installFailureCount > 1 ? (+ <div+ key={installFailureCount}+ aria-hidden="true"+ className={`${styles.dragCallout} ${+ installFailureCount > 2 ? styles.dragCalloutScale : ''+ }`}+ >+ <svg+ className={styles.dragCalloutLine}+ fill="none"+ viewBox="0 0 59 37"+ >+ <path+ d="M5 15C18 7.711 42 8 54 14"+ stroke="currentColor"+ strokeLinecap="round"+ strokeWidth="4"+ />+ </svg>+ <span className={styles.dragCalloutLabel}>+ Drag into+ <br />+ Studio+ </span>+ </div>+ ) : null}+ </div>+ <p className={styles.hint}>+ Not working? Drag the button into your Studio instead.+ </p>+ <p className={styles.copyHint}>+ You can also{' '}<button- aria-label="Close"- className={styles.closeButton}- onClick={onClose}+ className={styles.copyButton}+ onClick={async () => {+ await navigator.clipboard.writeText(sourceCode);+ setHasCopied(true);+ }}type="button">- ×- </button>- <h3 className={styles.title} id={titleId}>- Use this element- </h3>- <p className={styles.description}>- <Link to="/docs/studio/" target="_blank" rel="noreferrer">- Open a Remotion Studio- </Link>- , then click below.- </p>- <div className={styles.installAction}>- <ElementStudioAction- buttonLabel={buttonLabel}- definition={definition}- loading={isInstalling}- onClick={onInstall}- posterRef={posterRef}- showDragHandle- sourceCode={sourceCode}- />- {installFailureCount > 1 ? (- <div- key={installFailureCount}- aria-hidden="true"- className={`${styles.dragCallout} ${- installFailureCount > 2 ? styles.dragCalloutScale : ''- }`}- >- <svg- className={styles.dragCalloutLine}- fill="none"- viewBox="0 0 59 37"- >- <path- d="M5 15C18 7.711 42 8 54 14"- stroke="currentColor"- strokeLinecap="round"- strokeWidth="4"- />- </svg>- <span className={styles.dragCalloutLabel}>- Drag into- <br />- Studio- </span>- </div>- ) : null}- </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>{' '}- and create a new file.- <span aria-live="polite">{hasCopied ? ' Copied.' : null}</span>- </p>- </div>- </dialog>,- document.body,+ copy the code+ </button>{' '}+ and create a new file.+ <span aria-live="polite">{hasCopied ? ' Copied.' : null}</span>+ </p>+ </StudioInstallFallbackDialog>);};diff --git a/packages/docs/src/components/Elements/ElementLibrary.tsx b/packages/docs/src/components/Elements/ElementLibrary.tsxindex f865353595..8464d6209b 100644--- a/packages/docs/src/components/Elements/ElementLibrary.tsx+++ b/packages/docs/src/components/Elements/ElementLibrary.tsx@@ -57,6 +57,7 @@ const ElementCard: React.FC<{isFallbackOpen,showFallback,} = useStudioInstallFallback('Use');+ const installTriggerRef = useRef<HTMLElement>(null);const posterRef = useRef<HTMLImageElement>(null);const videoRef = useRef<HTMLVideoElement>(null);const shouldPlay =@@ -114,6 +115,11 @@ const ElementCard: React.FC<{}};+ const onInstallClick = (event: React.MouseEvent<HTMLElement>) => {+ installTriggerRef.current = event.currentTarget;+ installElement();+ };+const Card = isCaptionPicker ? 'button' : Link;return (@@ -128,7 +134,7 @@ const ElementCard: React.FC<{{...(isCaptionPicker? {type: 'button' as const,- onClick: installElement,+ onClick: onInstallClick,disabled: isInstalling,'aria-label': `Select ${definition.displayName}`,}@@ -187,7 +193,7 @@ const ElementCard: React.FC<{aria-label={`Use – ${definition.displayName}`}fullWidth={false}loading={isInstalling}- onClick={installElement}+ onClick={onInstallClick}size="sm"style={{padding: '5px 8px'}}title="Install in the most recently focused Remotion Studio"@@ -205,6 +211,7 @@ const ElementCard: React.FC<{onInstall={installElement}posterRef={posterRef}sourceCode={sourceCode}+ triggerRef={installTriggerRef}/></li>);diff --git a/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.module.css b/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.module.cssindex 9a4c9f9aa0..12f17f351d 100644--- a/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.module.css+++ b/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.module.css@@ -1,65 +1,18 @@-.dialog {- width: min(440px, calc(100vw - 32px));- max-width: none;- margin: auto;- padding: 0;- border: 0;- border-radius: 8px;- 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: 20px;- font-size: 0.875rem;- line-height: 1.5;-}--.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);+.content .title {+ font-size: 1rem;+ line-height: 1.5;}-.closeButton:focus-visible,.copyButton:focus-visible,.url:focus-visible {outline: 2px solid var(--ifm-color-primary);outline-offset: 2px;}-.title {- margin: 0;- padding-right: 32px;- font-size: 1rem;- line-height: 1.5;-}-.description p {margin: 12px 0;}diff --git a/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.tsx b/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.tsxindex d8a24fed1b..7d9736b92c 100644--- a/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.tsx+++ b/packages/docs/src/components/Elements/ElementLibraryInstallFallbackModal.tsx@@ -1,14 +1,7 @@import type {AddElementLibraryToStudioErrorCode} from '@remotion/studio-protocol';-import React, {- useEffect,- useId,- useLayoutEffect,- useRef,- useState,- type RefObject,-} from 'react';-import {createPortal} from 'react-dom';+import React, {type RefObject, useEffect, useState} from 'react';import {BlueButton} from '../../../components/layout/Button';+import {StudioInstallFallbackDialog} from './StudioInstallFallbackDialog';import type {ThirdPartyElementLibrary} from './third-party-element-library-data';import styles from './ElementLibraryInstallFallbackModal.module.css';@@ -32,34 +25,10 @@ export const ElementLibraryInstallFallbackModal: React.FC<{onAdd,triggerRef,}) => {- const dialogRef = useRef<HTMLDialogElement>(null);- const titleId = useId();const [copyState, setCopyState] = useState<'idle' | 'copied' | 'error'>('idle',);- useLayoutEffect(() => {- const dialog = dialogRef.current;- const trigger = triggerRef.current;- if (!dialog) {- return;- }-- dialog.showModal();- return () => {- if (dialog.open) {- dialog.close();- }-- // A successful retry re-enables the trigger in the same commit.- window.requestAnimationFrame(() => {- if (trigger?.isConnected) {- trigger.focus();- }- });- };- }, [triggerRef]);-useEffect(() => {if (copyState !== 'copied') {return;@@ -69,142 +38,112 @@ export const ElementLibraryInstallFallbackModal: React.FC<{return () => window.clearTimeout(timeout);}, [copyState]);- return createPortal(- <dialog- ref={dialogRef}- aria-labelledby={titleId}- className={styles.dialog}- onCancel={(event) => {- event.preventDefault();- onClose();- }}- 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- ) {- onClose();- }- }}+ return (+ <StudioInstallFallbackDialog+ contentClassName={styles.content}+ isOpen+ onClose={onClose}+ title={`Add ${library.displayName} to Studio`}+ titleClassName={styles.title}+ triggerRef={triggerRef}>- <div className={styles.content}>+ <div aria-live="polite" className={styles.description}>+ {isAdding ? (+ <p>Finding a compatible Remotion Studio…</p>+ ) : failure?.code === 'no-compatible-studio' ? (+ <p>+ <a href="/docs/studio" target="_blank" rel="noreferrer">+ Open a Remotion Studio+ </a>+ , then try again.+ </p>+ ) : failure?.code === 'no-configurable-target' ? (+ <p>Focus your Remotion Studio tab, then try again.</p>+ ) : failure?.code === 'studio-upgrade-required' ? (+ <p>+ {failure.message}{' '}+ <a href="/docs/upgrading" target="_blank" rel="noreferrer">+ How to upgrade Remotion.+ </a>+ </p>+ ) : (+ <p>{failure?.message}</p>+ )}+ </div>+ <BlueButton fullWidth loading={isAdding} onClick={onAdd} size="sm">+ {buttonLabel}+ </BlueButton>+ <h4 className={styles.manualTitle}>Add manually</h4>+ <p className={styles.instructions}>+ In your local Studio, open <strong>Settings</strong>, select{' '}+ <strong>Elements</strong>, then choose <strong>Add by URL</strong> and+ paste this URL:+ </p>+ <div className={styles.urlField}>+ <input+ aria-label={`${library.displayName} library URL`}+ className={styles.url}+ onFocus={(event) => event.currentTarget.select()}+ readOnly+ value={library.libraryUrl}+ /><button- aria-label="Close"- className={styles.closeButton}- onClick={onClose}+ aria-label="Copy URL"+ className={styles.copyButton}+ onClick={async () => {+ try {+ await navigator.clipboard.writeText(library.libraryUrl);+ setCopyState('copied');+ } catch {+ setCopyState('error');+ }+ }}+ title="Copy URL"type="button">- ×- </button>- <h3 className={styles.title} id={titleId}>- Add {library.displayName} to Studio- </h3>- <div aria-live="polite" className={styles.description}>- {isAdding ? (- <p>Finding a compatible Remotion Studio…</p>- ) : failure?.code === 'no-compatible-studio' ? (- <p>- <a href="/docs/studio" target="_blank" rel="noreferrer">- Open a Remotion Studio- </a>- , then try again.- </p>- ) : failure?.code === 'no-configurable-target' ? (- <p>Focus your Remotion Studio tab, then try again.</p>- ) : failure?.code === 'studio-upgrade-required' ? (- <p>- {failure.message}{' '}- <a href="/docs/upgrading" target="_blank" rel="noreferrer">- How to upgrade Remotion.- </a>- </p>- ) : (- <p>{failure?.message}</p>- )}- </div>- <BlueButton fullWidth loading={isAdding} onClick={onAdd} size="sm">- {buttonLabel}- </BlueButton>- <h4 className={styles.manualTitle}>Add manually</h4>- <p className={styles.instructions}>- In your local Studio, open <strong>Settings</strong>, select{' '}- <strong>Elements</strong>, then choose <strong>Add by URL</strong> and- paste this URL:- </p>- <div className={styles.urlField}>- <input- aria-label={`${library.displayName} library URL`}- className={styles.url}- onFocus={(event) => event.currentTarget.select()}- readOnly- value={library.libraryUrl}- />- <button- aria-label="Copy URL"- className={styles.copyButton}- onClick={async () => {- try {- await navigator.clipboard.writeText(library.libraryUrl);- setCopyState('copied');- } catch {- setCopyState('error');- }- }}- title="Copy URL"- type="button"- >- <svg- aria-hidden="true"- width="16"- height="16"- viewBox="0 0 24 24"- fill="none"- stroke="currentColor"- strokeWidth="2"- strokeLinecap="round"- strokeLinejoin="round"- >- {copyState === 'copied' ? (- <path d="m5 12 4 4L19 6" />- ) : (- <>- <rect x="9" y="9" width="13" height="13" rx="2" />- <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />- </>- )}- </svg>- </button>- </div>- <p aria-live="polite" className={styles.copyStatus}>- {copyState === 'copied'- ? 'Copied.'- : copyState === 'error'- ? 'Could not copy. Select the URL above and copy it manually.'- : null}- </p>- <p className={styles.configHint}>- Then click <strong>+ Add Element Library</strong> in Studio.- </p>- <p className={styles.configHint}>- You can also add this URL to <code>remotion.config.ts</code> using{' '}- <a- href="/docs/config#addelementlibrary"- target="_blank"- rel="noreferrer"+ <svg+ aria-hidden="true"+ width="16"+ height="16"+ viewBox="0 0 24 24"+ fill="none"+ stroke="currentColor"+ strokeWidth="2"+ strokeLinecap="round"+ strokeLinejoin="round">- <code>Config.addElementLibrary()</code>- </a>- .- </p>+ {copyState === 'copied' ? (+ <path d="m5 12 4 4L19 6" />+ ) : (+ <>+ <rect x="9" y="9" width="13" height="13" rx="2" />+ <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />+ </>+ )}+ </svg>+ </button></div>- </dialog>,- document.body,+ <p aria-live="polite" className={styles.copyStatus}>+ {copyState === 'copied'+ ? 'Copied.'+ : copyState === 'error'+ ? 'Could not copy. Select the URL above and copy it manually.'+ : null}+ </p>+ <p className={styles.configHint}>+ Then click <strong>+ Add Element Library</strong> in Studio.+ </p>+ <p className={styles.configHint}>+ You can also add this URL to <code>remotion.config.ts</code> using{' '}+ <a+ href="/docs/config#addelementlibrary"+ target="_blank"+ rel="noreferrer"+ >+ <code>Config.addElementLibrary()</code>+ </a>+ .+ </p>+ </StudioInstallFallbackDialog>);};diff --git a/packages/docs/src/components/Elements/StudioInstallFallbackDialog.module.css b/packages/docs/src/components/Elements/StudioInstallFallbackDialog.module.cssnew file mode 100644index 0000000000..ba57d6c8bd--- /dev/null+++ b/packages/docs/src/components/Elements/StudioInstallFallbackDialog.module.css@@ -0,0 +1,58 @@+.dialog {+ width: min(440px, calc(100vw - 32px));+ max-width: none;+ margin: auto;+ padding: 0;+ border: 0;+ border-radius: 8px;+ 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;+ font-size: 0.875rem;+ line-height: 1.5;+}++.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 {+ outline: 2px solid var(--ifm-color-primary);+ outline-offset: 2px;+}++.title {+ margin: 0;+ padding-right: 32px;+ font-size: inherit;+ line-height: inherit;+}diff --git a/packages/docs/src/components/Elements/StudioInstallFallbackDialog.tsx b/packages/docs/src/components/Elements/StudioInstallFallbackDialog.tsxnew file mode 100644index 0000000000..1d8fe6250a--- /dev/null+++ b/packages/docs/src/components/Elements/StudioInstallFallbackDialog.tsx@@ -0,0 +1,100 @@+import React, {useId, useLayoutEffect, useRef, type RefObject} from 'react';+import {createPortal} from 'react-dom';+import styles from './StudioInstallFallbackDialog.module.css';++export const StudioInstallFallbackDialog: React.FC<{+ readonly children: React.ReactNode;+ readonly contentClassName: string;+ readonly isOpen: boolean;+ readonly onClose: () => void;+ readonly title: string;+ readonly titleClassName: string | null;+ readonly triggerRef: RefObject<HTMLElement | null>;+}> = ({+ children,+ contentClassName,+ isOpen,+ onClose,+ title,+ titleClassName,+ triggerRef,+}) => {+ const dialogRef = useRef<HTMLDialogElement>(null);+ const titleId = useId();++ useLayoutEffect(() => {+ if (!isOpen) {+ return;+ }++ const dialog = dialogRef.current;+ const trigger = triggerRef.current;+ if (!dialog) {+ return;+ }++ dialog.showModal();+ return () => {+ if (dialog.open) {+ dialog.close();+ }++ // A successful retry re-enables the trigger in the same commit.+ window.requestAnimationFrame(() => {+ if (trigger?.isConnected && !dialog.open) {+ trigger.focus();+ }+ });+ };+ }, [isOpen, triggerRef]);++ if (!isOpen) {+ return null;+ }++ return createPortal(+ <dialog+ ref={dialogRef}+ aria-labelledby={titleId}+ className={styles.dialog}+ onCancel={(event) => {+ event.preventDefault();+ onClose();+ }}+ 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+ ) {+ onClose();+ }+ }}+ >+ <div className={`${styles.content} ${contentClassName}`}>+ <button+ aria-label="Close"+ className={styles.closeButton}+ onClick={onClose}+ type="button"+ >+ ×+ </button>+ <h3+ className={[styles.title, titleClassName].filter(Boolean).join(' ')}+ id={titleId}+ >+ {title}+ </h3>+ {children}+ </div>+ </dialog>,+ document.body,+ );+};