Skip to content

🎥 Make videos programmatically with React

studio context menu ellipsis

Jonny Burger405ace347b6e
diff --git a/packages/studio/src/components/CompositionContextButton.tsx b/packages/studio/src/components/CompositionContextButton.tsx
new file mode 100644
index 0000000000..8703405803
--- /dev/null
+++ b/packages/studio/src/components/CompositionContextButton.tsx
@@ -0,0 +1,36 @@
+import type {SVGProps} from 'react';
+import React, {useCallback, useContext, useMemo} from 'react';
+import {StudioServerConnectionCtx} from '../helpers/client-id';
+import {EllipsisIcon} from '../icons/ellipsis';
+import type {RenderInlineAction} from './InlineAction';
+import {InlineDropdown} from './InlineDropdown';
+import type {ComboboxValue} from './NewComposition/ComboBox';
+
+export const CompositionContextButton: React.FC<{
+ readonly visible: boolean;
+ readonly values: ComboboxValue[];
+}> = ({visible, values}) => {
+ const iconStyle: SVGProps<SVGSVGElement> = useMemo(() => {
+ return {
+ style: {
+ height: 12,
+ },
+ };
+ }, []);
+
+ const connectionStatus = useContext(StudioServerConnectionCtx)
+ .previewServerState.type;
+
+ const renderAction: RenderInlineAction = useCallback(
+ (color) => {
+ return <EllipsisIcon fill={color} svgProps={iconStyle} />;
+ },
+ [iconStyle],
+ );
+
+ if (!visible || connectionStatus !== 'connected') {
+ return null;
+ }
+
+ return <InlineDropdown renderAction={renderAction} values={values} />;
+};
diff --git a/packages/studio/src/components/CompositionSelectorItem.tsx b/packages/studio/src/components/CompositionSelectorItem.tsx
index 6fb22ed096..0c1919210b 100644
--- a/packages/studio/src/components/CompositionSelectorItem.tsx
+++ b/packages/studio/src/components/CompositionSelectorItem.tsx
@@ -11,6 +11,7 @@ import {CollapsedFolderIcon, ExpandedFolderIcon} from '../icons/folder';
import {StillIcon} from '../icons/still';
import {FilmIcon} from '../icons/video';
import {ModalsContext} from '../state/modals';
+import {CompositionContextButton} from './CompositionContextButton';
import {ContextMenu} from './ContextMenu';
import type {ComboboxValue} from './NewComposition/ComboBox';
import {showNotification} from './Notifications/NotificationCenter';
@@ -69,12 +70,15 @@ export type CompositionSelectorItemType =
};
export const CompositionSelectorItem: React.FC<{
- item: CompositionSelectorItemType;
- currentComposition: string | null;
- tabIndex: number;
- selectComposition: (c: AnyComposition, push: boolean) => void;
- toggleFolder: (folderName: string, parentName: string | null) => void;
- level: number;
+ readonly item: CompositionSelectorItemType;
+ readonly currentComposition: string | null;
+ readonly tabIndex: number;
+ readonly selectComposition: (c: AnyComposition, push: boolean) => void;
+ readonly toggleFolder: (
+ folderName: string,
+ parentName: string | null,
+ ) => void;
+ readonly level: number;
}> = ({
item,
level,
@@ -299,12 +303,11 @@ export const CompositionSelectorItem: React.FC<{
<Spacing x={1} />
<div style={label}>{item.composition.id}</div>
<Spacing x={0.5} />
- <div>
- <SidebarRenderButton
- visible={hovered}
- composition={item.composition}
- />
- </div>
+ <CompositionContextButton values={contextMenu} visible={hovered} />
+ <SidebarRenderButton
+ visible={hovered}
+ composition={item.composition}
+ />
</a>
</Row>
</ContextMenu>
diff --git a/packages/studio/src/components/ContextMenu.tsx b/packages/studio/src/components/ContextMenu.tsx
index d211172556..3bc38402dc 100644
--- a/packages/studio/src/components/ContextMenu.tsx
+++ b/packages/studio/src/components/ContextMenu.tsx
@@ -26,8 +26,8 @@ type OpenState =
};
export const ContextMenu: React.FC<{
- children: React.ReactNode;
- values: ComboboxValue[];
+ readonly children: React.ReactNode;
+ readonly values: ComboboxValue[];
}> = ({children, values}) => {
const ref = useRef<HTMLDivElement>(null);
const [opened, setOpened] = useState<OpenState>({type: 'not-open'});
diff --git a/packages/studio/src/components/InlineAction.tsx b/packages/studio/src/components/InlineAction.tsx
index 0c158f8bd9..226f0be28a 100644
--- a/packages/studio/src/components/InlineAction.tsx
+++ b/packages/studio/src/components/InlineAction.tsx
@@ -4,12 +4,19 @@ import {useZIndex} from '../state/z-index';
export type RenderInlineAction = (color: string) => React.ReactNode;
-export const InlineAction: React.FC<{
- onClick: React.MouseEventHandler<HTMLButtonElement>;
- disabled?: boolean;
- renderAction: RenderInlineAction;
- title?: string;
-}> = ({renderAction, onClick, disabled, title}) => {
+export type InlineActionProps = {
+ readonly onClick: React.MouseEventHandler<HTMLButtonElement>;
+ readonly disabled?: boolean;
+ readonly renderAction: RenderInlineAction;
+ readonly title?: string;
+};
+
+export const InlineAction = ({
+ renderAction,
+ onClick,
+ disabled,
+ title,
+}: InlineActionProps) => {
const {tabIndex} = useZIndex();
const [hovered, setHovered] = useState(false);
diff --git a/packages/studio/src/components/InlineDropdown.tsx b/packages/studio/src/components/InlineDropdown.tsx
new file mode 100644
index 0000000000..376964105e
--- /dev/null
+++ b/packages/studio/src/components/InlineDropdown.tsx
@@ -0,0 +1,149 @@
+import {PlayerInternals} from '@remotion/player';
+import {useCallback, useMemo, useRef, useState} from 'react';
+import ReactDOM from 'react-dom';
+import {useMobileLayout} from '../helpers/mobile-layout';
+import {noop} from '../helpers/noop';
+import {HigherZIndex, useZIndex} from '../state/z-index';
+import {InlineAction, type InlineActionProps} from './InlineAction';
+import {getPortal} from './Menu/portals';
+import {
+ MAX_MENU_WIDTH,
+ MAX_MOBILE_MENU_WIDTH,
+ fullScreenOverlay,
+ menuContainerTowardsTop,
+ outerPortal,
+} from './Menu/styles';
+import type {ComboboxValue} from './NewComposition/ComboBox';
+import {MenuContent} from './NewComposition/MenuContent';
+
+// Some duplicate state with ContextMenu.tsx
+
+type OpenState =
+ | {
+ type: 'not-open';
+ }
+ | {
+ type: 'open';
+ left: number;
+ top: number;
+ };
+
+export const InlineDropdown = ({
+ values,
+ ...props
+}: Omit<InlineActionProps, 'onClick'> & {
+ readonly values: ComboboxValue[];
+}) => {
+ const ref = useRef<HTMLDivElement>(null);
+ const [opened, setOpened] = useState<OpenState>({type: 'not-open'});
+
+ const {currentZIndex} = useZIndex();
+
+ const size = PlayerInternals.useElementSize(ref, {
+ triggerOnWindowResize: true,
+ shouldApplyCssTransforms: true,
+ });
+
+ const isMobileLayout = useMobileLayout();
+
+ const onClick: React.MouseEventHandler<HTMLButtonElement> = useCallback(
+ (e) => {
+ console.log('click', e);
+ e.preventDefault();
+ e.stopPropagation();
+ setOpened({type: 'open', left: e.clientX, top: e.clientY});
+ },
+ [],
+ );
+
+ const spaceToBottom = useMemo(() => {
+ if (size && opened.type === 'open') {
+ return size.windowSize.height - opened.top;
+ }
+
+ return 0;
+ }, [opened, size]);
+
+ const spaceToTop = useMemo(() => {
+ if (size && opened.type === 'open') {
+ return opened.top;
+ }
+
+ return 0;
+ }, [opened, size]);
+
+ const portalStyle = useMemo(() => {
+ if (opened.type === 'not-open') {
+ return;
+ }
+
+ if (!size) {
+ return;
+ }
+
+ const spaceToRight = size.windowSize.width - size.left;
+ const spaceToLeft = size.left + size.width;
+
+ const minSpaceRequired = isMobileLayout
+ ? MAX_MOBILE_MENU_WIDTH
+ : MAX_MENU_WIDTH;
+
+ const verticalLayout = spaceToTop > spaceToBottom ? 'bottom' : 'top';
+ const canOpenOnLeft = spaceToLeft >= minSpaceRequired;
+ const canOpenOnRight = spaceToRight >= minSpaceRequired;
+ const horizontalLayout = canOpenOnRight ? 'left' : 'right';
+
+ return {
+ ...menuContainerTowardsTop,
+ ...(verticalLayout === 'top'
+ ? {
+ top: opened.top,
+ }
+ : {
+ bottom: size.windowSize.height - opened.top,
+ }),
+ ...(horizontalLayout === 'left'
+ ? {
+ left: opened.left,
+ }
+ : {
+ right: canOpenOnLeft ? size.windowSize.width - opened.left : 0,
+ }),
+ };
+ }, [opened, size, isMobileLayout, spaceToTop, spaceToBottom]);
+
+ const onHide = useCallback(() => {
+ setOpened({type: 'not-open'});
+ }, []);
+
+ return (
+ <>
+ <div ref={ref}>
+ <InlineAction onClick={onClick} {...props} />
+ </div>
+ {portalStyle
+ ? ReactDOM.createPortal(
+ <div style={fullScreenOverlay}>
+ <div style={outerPortal} className="css-reset">
+ <HigherZIndex onOutsideClick={onHide} onEscape={onHide}>
+ <div style={portalStyle}>
+ <MenuContent
+ onNextMenu={noop}
+ onPreviousMenu={noop}
+ values={values}
+ onHide={onHide}
+ leaveLeftSpace
+ preselectIndex={false}
+ topItemCanBeUnselected={false}
+ fixedHeight={null}
+ />
+ </div>
+ </HigherZIndex>
+ </div>
+ </div>,
+ getPortal(currentZIndex),
+ )
+ : null}
+ </>
+ );
+};
diff --git a/packages/studio/src/icons/ellipsis.tsx b/packages/studio/src/icons/ellipsis.tsx
new file mode 100644
index 0000000000..bb06a2428f
--- /dev/null
+++ b/packages/studio/src/icons/ellipsis.tsx
@@ -0,0 +1,13 @@
+export const EllipsisIcon: React.FC<{
+ svgProps: React.SVGProps<SVGSVGElement>;
+ fill: string;
+}> = (props) => {
+ return (
+ <svg {...props.svgProps} viewBox="0 0 448 512">
+ <path
+ fill={props.fill}
+ d="M8 256a56 56 0 1 1 112 0A56 56 0 1 1 8 256zm160 0a56 56 0 1 1 112 0 56 56 0 1 1 -112 0zm216-56a56 56 0 1 1 0 112 56 56 0 1 1 0-112z"
+ />
+ </svg>
+ );
+};