Skip to content

🎥 Make videos programmatically with React

Elements: Keep Use label while loading

JonnyBurger6b9519ab40e5
diff --git a/packages/docs/components/layout/Button.tsx b/packages/docs/components/layout/Button.tsx
index 3614989ca1..a0f1c5f59c 100644
--- a/packages/docs/components/layout/Button.tsx
+++ b/packages/docs/components/layout/Button.tsx
@@ -36,9 +36,10 @@ export const Button: React.FC<Props> = (props) => {
color,
size,
className,
+ disabled,
...other
} = props;
- const actualDisabled = other.disabled || loading;
+ const actualDisabled = disabled || loading;
return (
<button
@@ -50,6 +51,7 @@ export const Button: React.FC<Props> = (props) => {
}
disabled={actualDisabled}
{...other}
+ aria-busy={loading}
style={{
...(props.style ?? {}),
padding:
@@ -61,10 +63,41 @@ export const Button: React.FC<Props> = (props) => {
// @ts-expect-error
'--hover-color': props.hoverColor ?? props.background,
...(props.fullWidth ? {width: '100%'} : {}),
- opacity: props.disabled ? 0.7 : 1,
+ opacity: disabled ? 0.7 : 1,
}}
>
- {children}
+ <span
+ className={`${styles.buttonContent} ${loading ? styles.buttonContentLoading : ''}`}
+ >
+ {children}
+ </span>
+ {loading ? (
+ <span aria-hidden="true" className={styles.spinnerOverlay}>
+ <svg
+ className={styles.spinner}
+ focusable="false"
+ viewBox="0 0 100 100"
+ >
+ {[0, 45, 90, 135, 180, 225, 270, 315].map((rotation) => (
+ <path
+ key={rotation}
+ d="M 44 0 L 50 0 a 6 6 0 0 1 6 6 L 56 26 a 6 6 0 0 1 -6 6 L 50 32 a 6 6 0 0 1 -6 -6 L 44 6 a 6 6 0 0 1 6 -6 Z"
+ fill="currentColor"
+ transform={`rotate(${rotation} 50 50)`}
+ >
+ <animate
+ attributeName="opacity"
+ begin={`${rotation / 360}s`}
+ dur="1s"
+ from="1"
+ repeatCount="indefinite"
+ to="0.15"
+ />
+ </path>
+ ))}
+ </svg>
+ </span>
+ ) : null}
</button>
);
};
diff --git a/packages/docs/components/layout/button.module.css b/packages/docs/components/layout/button.module.css
index 1fb4df73b1..15582d6d7d 100644
--- a/packages/docs/components/layout/button.module.css
+++ b/packages/docs/components/layout/button.module.css
@@ -1,4 +1,5 @@
.buttoncontainer {
+ position: relative;
border-radius: 8px;
font-weight: bold;
appearance: none;
@@ -13,6 +14,29 @@
transition: background-color 0.1s;
}
+.buttonContent {
+ display: inline-flex;
+ align-items: center;
+}
+
+.buttonContentLoading {
+ opacity: 0;
+}
+
+.spinnerOverlay {
+ position: absolute;
+ inset: 0;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+}
+
+.spinner {
+ display: block;
+ width: 20px;
+ height: 20px;
+}
+
.buttoncontainer:not([disabled]):hover {
background-color: var(--hover-color) !important;
}
diff --git a/packages/docs/src/components/Elements/ElementLibrary.tsx b/packages/docs/src/components/Elements/ElementLibrary.tsx
index 9bd357fbd0..6a32143c5b 100644
--- a/packages/docs/src/components/Elements/ElementLibrary.tsx
+++ b/packages/docs/src/components/Elements/ElementLibrary.tsx
@@ -1,15 +1,5 @@
-import {
- installInStudio,
- isInsideStudio,
- setStudioDragData,
-} from '@remotion/studio-protocol';
-import React, {
- useEffect,
- useLayoutEffect,
- useMemo,
- useRef,
- useState,
-} from 'react';
+import {installInStudio, setStudioDragData} from '@remotion/studio-protocol';
+import React, {useEffect, useMemo, useRef, useState} from 'react';
import {BlueButton} from '../../../components/layout/Button';
import type {ElementDefinition} from './element-definitions';
import {
@@ -48,15 +38,13 @@ const usePrefersReducedMotion = () => {
const ElementCard: React.FC<{
readonly definition: ElementDefinition;
- readonly isEmbeddedInStudio: boolean;
readonly prefersReducedMotion: boolean;
readonly sourceCode: string;
-}> = ({definition, isEmbeddedInStudio, prefersReducedMotion, sourceCode}) => {
+}> = ({definition, prefersReducedMotion, sourceCode}) => {
const [isFocused, setIsFocused] = useState(false);
const [isPointerOver, setIsPointerOver] = useState(false);
const [playbackFailed, setPlaybackFailed] = useState(false);
const [isInstalling, setIsInstalling] = useState(false);
- const [wasSentToStudio, setWasSentToStudio] = useState(false);
const posterRef = useRef<HTMLImageElement>(null);
const videoRef = useRef<HTMLVideoElement>(null);
const shouldPlay =
@@ -82,15 +70,6 @@ const ElementCard: React.FC<{
[definition, sourceCode],
);
- useEffect(() => {
- if (!wasSentToStudio) {
- return;
- }
-
- const timeout = window.setTimeout(() => setWasSentToStudio(false), 3000);
- return () => window.clearTimeout(timeout);
- }, [wasSentToStudio]);
-
useEffect(() => {
const video = videoRef.current;
if (!video) {
@@ -122,7 +101,6 @@ const ElementCard: React.FC<{
};
const installElement = async () => {
- setWasSentToStudio(false);
setIsInstalling(true);
try {
const result = await installInStudio({
@@ -135,8 +113,6 @@ const ElementCard: React.FC<{
return;
}
- setWasSentToStudio(!isEmbeddedInStudio);
-
if (window.location.origin === 'https://www.remotion.dev') {
navigator.sendBeacon(
`https://www.remotion.pro/api/track/element-install-request?slug=${encodeURIComponent(definition.slug)}`,
@@ -154,16 +130,6 @@ const ElementCard: React.FC<{
}
};
- const installButtonLabel = isInstalling
- ? isEmbeddedInStudio
- ? 'Using…'
- : 'Finding Studio…'
- : wasSentToStudio
- ? 'Sent to Studio'
- : isEmbeddedInStudio
- ? 'Use'
- : 'Install in Studio';
-
return (
<li
className={styles.cardItem}
@@ -221,7 +187,7 @@ const ElementCard: React.FC<{
</a>
<div aria-live="polite" className={styles.installAction}>
<BlueButton
- aria-label={`${installButtonLabel} – ${definition.displayName}`}
+ aria-label={`Use – ${definition.displayName}`}
fullWidth={false}
loading={isInstalling}
onClick={installElement}
@@ -229,7 +195,7 @@ const ElementCard: React.FC<{
style={{padding: '5px 8px'}}
title="Install in the most recently focused Remotion Studio"
>
- {installButtonLabel}
+ Use
</BlueButton>
</div>
</li>
@@ -238,15 +204,9 @@ const ElementCard: React.FC<{
const ElementGrid: React.FC<{
readonly definitions: readonly ElementDefinition[];
- readonly isEmbeddedInStudio: boolean;
readonly prefersReducedMotion: boolean;
readonly sourceCodeBySlug: Readonly<Record<string, string>>;
-}> = ({
- definitions,
- isEmbeddedInStudio,
- prefersReducedMotion,
- sourceCodeBySlug,
-}) => {
+}> = ({definitions, prefersReducedMotion, sourceCodeBySlug}) => {
return (
// The Algolia recordExtractor must remove this subtree before extracting records.
// This marker requires explicit crawler configuration; it is not built in.
@@ -267,7 +227,6 @@ const ElementGrid: React.FC<{
<ElementCard
key={definition.slug}
definition={definition}
- isEmbeddedInStudio={isEmbeddedInStudio}
prefersReducedMotion={prefersReducedMotion}
sourceCode={sourceCode}
/>
@@ -283,11 +242,6 @@ export const ElementLibrary: React.FC<{
}> = ({category, sourceCodeBySlug}) => {
const sections = getElementLibrarySections(category);
const prefersReducedMotion = usePrefersReducedMotion();
- const [isEmbeddedInStudio, setIsEmbeddedInStudio] = useState(false);
-
- useLayoutEffect(() => {
- setIsEmbeddedInStudio(isInsideStudio());
- }, []);
return (
<div className={styles.library}>
@@ -301,7 +255,6 @@ export const ElementLibrary: React.FC<{
>
<ElementGrid
definitions={section.definitions}
- isEmbeddedInStudio={isEmbeddedInStudio}
prefersReducedMotion={prefersReducedMotion}
sourceCodeBySlug={sourceCodeBySlug}
/>
@@ -322,7 +275,6 @@ export const ElementLibrary: React.FC<{
</h2>
<ElementGrid
definitions={section.definitions}
- isEmbeddedInStudio={isEmbeddedInStudio}
prefersReducedMotion={prefersReducedMotion}
sourceCodeBySlug={sourceCodeBySlug}
/>
diff --git a/packages/docs/src/components/Elements/ElementPage.tsx b/packages/docs/src/components/Elements/ElementPage.tsx
index 3ea57174c2..95c72edc39 100644
--- a/packages/docs/src/components/Elements/ElementPage.tsx
+++ b/packages/docs/src/components/Elements/ElementPage.tsx
@@ -284,11 +284,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({
: 'Install in the most recently focused Remotion Studio'
}
>
- {installStatus.type === 'installing'
- ? 'Finding Studio…'
- : isEmbeddedInStudio
- ? 'Use'
- : 'Install in Studio'}
+ Use
</BlueButton>
{isEmbeddedInStudio === false ? (
<div
@@ -349,8 +345,7 @@ export const ElementPage: React.FC<ElementPageProps> = ({
<li>
<InlineStep>3</InlineStep>
<span>
- Return here and click <strong>Install in Studio</strong>{' '}
- again.
+ Return here and click <strong>Use</strong> again.
</span>
</li>
</ol>