Skip to content

🎥 Make videos programmatically with React

add a context menu to clear guide

Jonny Burgerf1a2f19659be
diff --git a/packages/cli/src/editor/components/ContextMenu.tsx b/packages/cli/src/editor/components/ContextMenu.tsx
new file mode 100644
index 0000000000..eb433cbb09
--- /dev/null
+++ b/packages/cli/src/editor/components/ContextMenu.tsx
@@ -0,0 +1,164 @@
+import {PlayerInternals} from '@remotion/player';
+import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
+import ReactDOM from 'react-dom';
+import {noop} from '../helpers/noop';
+import {HigherZIndex, useZIndex} from '../state/z-index';
+import {getPortal} from './Menu/portals';
+import {
+ fullScreenOverlay,
+ MAX_MENU_WIDTH,
+ menuContainerTowardsTop,
+ outerPortal,
+} from './Menu/styles';
+import type {ComboboxValue} from './NewComposition/ComboBox';
+import {MenuContent} from './NewComposition/MenuContent';
+
+type OpenState =
+ | {
+ type: 'not-open';
+ }
+ | {
+ type: 'open';
+ left: number;
+ top: number;
+ };
+
+export const ContextMenu: React.FC<{
+ children: React.ReactNode;
+}> = ({children}) => {
+ const ref = useRef<HTMLDivElement>(null);
+ const [opened, setOpened] = useState<OpenState>({type: 'not-open'});
+ const {currentZIndex} = useZIndex();
+
+ const style: React.CSSProperties = useMemo(() => {
+ return {};
+ }, []);
+
+ const size = PlayerInternals.useElementSize(ref, {
+ triggerOnWindowResize: true,
+ shouldApplyCssTransforms: true,
+ });
+
+ useEffect(() => {
+ const {current} = ref;
+ if (!current) {
+ return;
+ }
+
+ const onClick = (e: MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setOpened({type: 'open', left: e.clientX, top: e.clientY});
+
+ return false;
+ };
+
+ current.addEventListener('contextmenu', onClick);
+
+ return () => {
+ current.removeEventListener('contextmenu', onClick);
+ };
+ }, [size]);
+
+ 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 - (opened.left + size.width);
+ const minSpaceToRightRequired = MAX_MENU_WIDTH;
+
+ const verticalLayout = spaceToTop > spaceToBottom ? 'bottom' : 'top';
+ const horizontalLayout =
+ spaceToRight >= minSpaceToRightRequired ? 'left' : 'right';
+
+ return {
+ ...menuContainerTowardsTop,
+ ...(verticalLayout === 'top'
+ ? {
+ top: opened.top,
+ }
+ : {
+ bottom: size.windowSize.height - opened.top,
+ }),
+ ...(horizontalLayout === 'left'
+ ? {
+ left: opened.left,
+ }
+ : {
+ right: size.windowSize.width - opened.left - size.width,
+ }),
+ };
+ }, [opened, spaceToBottom, spaceToTop, size]);
+
+ const onHide = useCallback(() => {
+ setOpened({type: 'not-open'});
+ }, []);
+
+ const values = useMemo((): ComboboxValue[] => {
+ return [
+ {
+ id: '1',
+ keyHint: null,
+ label: 'Remove guide',
+ leftItem: null,
+ onClick: noop,
+ quickSwitcherLabel: null,
+ subMenu: null,
+ type: 'item',
+ value: 'remove',
+ },
+ ];
+ }, []);
+
+ return (
+ <>
+ <div ref={ref} onContextMenu={() => false} style={style}>
+ {children}
+ </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/cli/src/editor/components/EditorGuides/Guide.tsx b/packages/cli/src/editor/components/EditorGuides/Guide.tsx
index 9a6997a7f2..c850a5e417 100644
--- a/packages/cli/src/editor/components/EditorGuides/Guide.tsx
+++ b/packages/cli/src/editor/components/EditorGuides/Guide.tsx
@@ -2,6 +2,7 @@ import {memo, useCallback, useContext, useMemo} from 'react';
import type {Guide} from '../../state/editor-guides';
import {EditorShowGuidesContext} from '../../state/editor-guides';
import {RULER_WIDTH} from '../../state/editor-rulers';
+import {ContextMenu} from '../ContextMenu';
const PADDING_FOR_EASY_DRAG = 2;
@@ -34,7 +35,6 @@ const GuideComp: React.FC<{
padding: isVerticalGuide
? `0 ${PADDING_FOR_EASY_DRAG}px`
: `${PADDING_FOR_EASY_DRAG}px 0`,
- zIndex: 1000,
};
}, [guide, scale, canvasDimensions, isVerticalGuide]);
@@ -64,16 +64,18 @@ const GuideComp: React.FC<{
);
return (
- <div
- style={guideStyle}
- onMouseDown={onMouseDown}
- className="__remotion_editor_guide"
- >
+ <ContextMenu>
<div
- style={guideContentStyle}
- className="__remotion_editor_guide_content"
- />
- </div>
+ style={guideStyle}
+ onMouseDown={onMouseDown}
+ className="__remotion_editor_guide"
+ >
+ <div
+ style={guideContentStyle}
+ className="__remotion_editor_guide_content"
+ />
+ </div>
+ </ContextMenu>
);
};