Skip to content

🎥 Make videos programmatically with React

show popup

Jonny Burger0555a610c348
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index e7eee35fb4..0f879cfaea 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -1,16 +1,13 @@
-import type { PreviewSize} from '@remotion/player';
+import type {PreviewSize} from '@remotion/player';
import {PlayerInternals} from '@remotion/player';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import type {
MediaVolumeContextValue,
SetMediaVolumeContextValue,
SetTimelineInOutContextValue,
- TimelineInOutContextValue} from 'remotion';
-import {
- continueRender,
- delayRender,
- Internals
+ TimelineInOutContextValue,
} from 'remotion';
+import {continueRender, delayRender, Internals} from 'remotion';
import {BACKGROUND} from '../helpers/colors';
import {noop} from '../helpers/noop';
import {
@@ -22,7 +19,7 @@ import {FolderContextProvider} from '../state/folders';
import {HighestZIndexProvider} from '../state/highest-z-index';
import {KeybindingContextProvider} from '../state/keybindings';
import type {ModalContextType, ModalState} from '../state/modals';
-import { ModalsContext} from '../state/modals';
+import {ModalsContext} from '../state/modals';
import {loadMuteOption} from '../state/mute';
import {
loadPreviewSizeOption,
@@ -42,6 +39,7 @@ import {GlobalKeybindings} from './GlobalKeybindings';
import {KeyboardShortcuts} from './KeyboardShortcutsModal';
import NewComposition from './NewComposition/NewComposition';
import {NoRegisterRoot} from './NoRegisterRoot';
+import {NotificationCenter} from './Notifications/NotificationCenter';
import {UpdateModal} from './UpdateModal/UpdateModal';
const background: React.CSSProperties = {
@@ -213,6 +211,7 @@ export const Editor: React.FC = () => {
)}
<GlobalKeybindings />
</div>
+ <NotificationCenter />
{modalContextType &&
modalContextType.type === 'new-comp' && (
<NewComposition
diff --git a/packages/cli/src/editor/components/MenuToolbar.tsx b/packages/cli/src/editor/components/MenuToolbar.tsx
index 8fc6cf8361..f1df6241bc 100644
--- a/packages/cli/src/editor/components/MenuToolbar.tsx
+++ b/packages/cli/src/editor/components/MenuToolbar.tsx
@@ -1,6 +1,6 @@
import React, {useCallback, useContext, useMemo, useState} from 'react';
import {Internals} from 'remotion';
-import {copyText} from '../helpers/copy-text';
+import {pickColor} from '../helpers/pick-color';
import {Checkmark} from '../icons/Checkmark';
import {CheckerboardContext} from '../state/checkerboard';
import {ModalsContext} from '../state/modals';
@@ -390,12 +390,7 @@ export const MenuToolbar: React.FC = () => {
id: 'color-picker',
value: 'color-picker',
label: 'Color Picker',
- onClick: () => {
- // @ts-expect-error
- new EyeDropper().open().then((color) => {
- copyText(color.sRGBHex);
- });
- },
+ onClick: () => pickColor(),
leftItem: null,
keyHint: null,
subMenu: null,
diff --git a/packages/cli/src/editor/components/Notifications/ColorDot.tsx b/packages/cli/src/editor/components/Notifications/ColorDot.tsx
new file mode 100644
index 0000000000..6a7cf8531c
--- /dev/null
+++ b/packages/cli/src/editor/components/Notifications/ColorDot.tsx
@@ -0,0 +1,22 @@
+import React, {useMemo} from 'react';
+
+const container: React.CSSProperties = {
+ height: 16,
+ width: 16,
+ backgroundColor: 'red',
+ border: '1px solid rgba(255, 255, 255, 0.2)',
+ borderRadius: 8,
+};
+
+export const ColorDot: React.FC<{
+ color: string;
+}> = ({color}) => {
+ const style = useMemo(() => {
+ return {
+ ...container,
+ backgroundColor: color,
+ };
+ }, [color]);
+
+ return <div style={style} />;
+};
diff --git a/packages/cli/src/editor/components/Notifications/Notification.tsx b/packages/cli/src/editor/components/Notifications/Notification.tsx
new file mode 100644
index 0000000000..af39ea00da
--- /dev/null
+++ b/packages/cli/src/editor/components/Notifications/Notification.tsx
@@ -0,0 +1,36 @@
+import React, {useEffect} from 'react';
+
+const notification: React.CSSProperties = {
+ backgroundColor: '#111111',
+ color: 'white',
+ fontFamily: 'Arial, Helvetica, sans-serif',
+ display: 'inline-flex',
+ padding: '8px 14px',
+ borderRadius: 4,
+ fontSize: 15,
+ border: '0.25px solid rgba(255, 255, 255, 0.1)',
+ boxShadow: '0 2px 3px rgba(0, 0, 0, 1)',
+ marginTop: 3,
+ marginBottom: 3,
+ alignItems: 'center',
+};
+
+export const Notification: React.FC<{
+ children: React.ReactNode;
+ created: number;
+ duration: number;
+ id: string;
+ onRemove: (id: string) => void;
+}> = ({children, id, duration, created, onRemove}) => {
+ useEffect(() => {
+ const timeout = setTimeout(() => {
+ onRemove(id);
+ }, duration - (Date.now() - created));
+
+ return () => {
+ clearTimeout(timeout);
+ };
+ }, [created, duration, id, onRemove]);
+
+ return <div style={notification}>{children}</div>;
+};
diff --git a/packages/cli/src/editor/components/Notifications/NotificationCenter.tsx b/packages/cli/src/editor/components/Notifications/NotificationCenter.tsx
new file mode 100644
index 0000000000..2ef88d549d
--- /dev/null
+++ b/packages/cli/src/editor/components/Notifications/NotificationCenter.tsx
@@ -0,0 +1,71 @@
+import React, {
+ createRef,
+ useCallback,
+ useImperativeHandle,
+ useState,
+} from 'react';
+import {Notification} from './Notification';
+
+const container: React.CSSProperties = {
+ position: 'absolute',
+ justifyContent: 'center',
+ alignItems: 'center',
+ display: 'flex',
+ width: '100%',
+ flexDirection: 'column',
+ paddingTop: 20,
+ pointerEvents: 'none',
+};
+
+type TNotification = {
+ id: string;
+ content: React.ReactNode;
+ created: number;
+ duration: number;
+};
+
+type TNotificationCenter = {
+ addNotification: (notification: TNotification) => void;
+};
+
+export const notificationCenter = createRef<TNotificationCenter>();
+
+export const NotificationCenter: React.FC = () => {
+ const [notifications, setNotifications] = useState<TNotification[]>([]);
+
+ const onRemove = useCallback((id: string) => {
+ setNotifications((not) => {
+ return not.filter((n) => n.id !== id);
+ });
+ }, []);
+
+ const addNotification = useCallback((notification: TNotification) => {
+ setNotifications((previousNotifications) => {
+ return [...previousNotifications, notification];
+ });
+ }, []);
+
+ useImperativeHandle(notificationCenter, () => {
+ return {
+ addNotification,
+ };
+ });
+
+ return (
+ <div style={container}>
+ {notifications.map((n) => {
+ return (
+ <Notification
+ key={n.id}
+ created={n.created}
+ duration={n.duration}
+ id={n.id}
+ onRemove={onRemove}
+ >
+ {n.content}
+ </Notification>
+ );
+ })}
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/helpers/pick-color.tsx b/packages/cli/src/editor/helpers/pick-color.tsx
new file mode 100644
index 0000000000..9348476840
--- /dev/null
+++ b/packages/cli/src/editor/helpers/pick-color.tsx
@@ -0,0 +1,40 @@
+import {Spacing} from '../components/layout';
+import {ColorDot} from '../components/Notifications/ColorDot';
+import {notificationCenter} from '../components/Notifications/NotificationCenter';
+import {copyText} from './copy-text';
+
+export const pickColor = () => {
+ // @ts-expect-error
+ const open = new EyeDropper().open();
+ (
+ open as Promise<{
+ sRGBHex: string;
+ }>
+ )
+ .then((color) => {
+ copyText(color.sRGBHex);
+ notificationCenter.current?.addNotification({
+ content: (
+ <>
+ <ColorDot color={color.sRGBHex} /> <Spacing x={1} /> Copied{' '}
+ {color.sRGBHex}
+ </>
+ ),
+ created: Date.now(),
+ duration: 2000,
+ id: String(Math.random()),
+ });
+ })
+ .catch((err: Error) => {
+ if (err.message.includes('canceled')) {
+ return;
+ }
+
+ notificationCenter.current?.addNotification({
+ content: `Could not pick color.`,
+ duration: 2000,
+ created: Date.now(),
+ id: String(Math.random()),
+ });
+ });
+};