Skip to content

🎥 Make videos programmatically with React

Merge main and preserve input dragger blur handling

JonnyBurger0689dd99ed2a
diff --cc packages/studio/src/components/NewComposition/InputDragger.tsx
index 3a77aff8d3,d74490641c..521049ea6e
--- a/packages/studio/src/components/NewComposition/InputDragger.tsx
+++ b/packages/studio/src/components/NewComposition/InputDragger.tsx
@@@ -3,20 -3,9 +3,21 @@@ import type
MouseEventHandler,
PointerEventHandler,
} from 'react';
-import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
+import React, {
+ useCallback,
++ useEffect,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
import {interpolate} from 'remotion';
-import {BLUE, BLUE_HOVERED, TRANSPARENT} from '../../helpers/colors';
+import {
+ BLUE,
+ BLUE_HOVERED,
+ TRANSPARENT,
+ WHITE_ALPHA_10,
+} from '../../helpers/colors';
import {noop} from '../../helpers/noop';
import {
isPointerSessionRelease,
@@@ -900,89 -857,116 +902,137 @@@ const InputDraggerForwardRefFn: React.F
}
}, [inputFallback]);
+ useEffect(() => {
+ if (!inputFallback) {
+ return;
+ }
+
+ const commitWithoutClickThrough = (event: PointerEvent) => {
+ const input = fallbackRef.current;
+ if (
+ event.button !== 0 ||
+ input === null ||
+ document.activeElement !== input ||
+ input.contains(event.target as Node)
+ ) {
+ return;
+ }
+
+ // Commit before timeline selection handlers can hide the edited row.
+ event.preventDefault();
+ event.stopImmediatePropagation();
+
+ const stopClick = (clickEvent: MouseEvent) => {
+ clickEvent.preventDefault();
+ clickEvent.stopImmediatePropagation();
+ };
+
+ window.addEventListener('click', stopClick, {capture: true, once: true});
+ observePointerRelease({
+ event,
+ onEnd: () => {
+ // The click follows pointerup, even though blurring unmounts the input.
+ setTimeout(() => {
+ window.removeEventListener('click', stopClick, true);
+ }, 0);
+ },
+ });
+ input.blur();
+ };
+
+ window.addEventListener('pointerdown', commitWithoutClickThrough, true);
+ return () => {
+ window.removeEventListener(
+ 'pointerdown',
+ commitWithoutClickThrough,
+ true,
+ );
+ };
+ }, [inputFallback]);
+
- if (inputFallback) {
- return (
- <HigherZIndex onEscape={onEscape} onOutsideClick={noop}>
- <RemotionInput
- ref={fallbackRef}
- autoFocus
- onKeyDown={onInputKeyDown}
- onBlur={onBlur}
- onChange={onInputChange}
- min={_min}
- max={_max}
- step={validationStep}
- defaultValue={formattedInputValueRef.current?.text ?? value}
- status={status}
- rightAlign={rightAlign}
- small={small}
- {...props}
- style={
- small
- ? {
- ...compactInputDraggerStyle,
- display: 'block',
- ...inputStyle,
- }
- : inputStyle
- }
- type="text"
- />
- </HigherZIndex>
- );
- }
-
+ // The first flex item determines the inline baseline, so keep the button first.
return (
- <button
- ref={ref}
- type="button"
- aria-label={props['aria-label']}
- className={'__remotion_input_dragger'}
- style={
- buttonStyle
- ? {
- ...(small
- ? compactInputDraggerContainerStyle
- : inputDraggerContainerStyle),
- ...buttonStyle,
- }
- : small
+ <span style={{...containerStyle, width: inputStyle?.width}}>
+ <button
+ ref={ref}
+ type="button"
+ aria-label={props['aria-label']}
+ aria-hidden={inputFallback}
+ tabIndex={inputFallback ? -1 : undefined}
+ className={'__remotion_input_dragger'}
+ style={{
+ ...(small
? compactInputDraggerContainerStyle
- : inputDraggerContainerStyle
- }
- onClick={onClick}
- onFocus={onFocus}
- onKeyDown={onKeyDown}
- onPointerDown={onPointerDown}
- >
- <span style={span}>{formatter(value as string | number)}</span>
- {formatterSubtitle ? (
- <span style={formatterSubtitleStyle}>
- {formatterSubtitle(value as string | number)}
+ : inputDraggerContainerStyle),
+ textAlign: rightAlign ? 'right' : 'left',
+ ...buttonStyle,
+ width: inputStyle?.width === undefined ? buttonStyle?.width : '100%',
+ // Reserve the same space while the editor is absolutely positioned above it.
+ visibility: inputFallback ? 'hidden' : undefined,
+ }}
+ onClick={onClick}
+ onFocus={onFocus}
+ onKeyDown={onKeyDown}
+ onPointerDown={onPointerDown}
+ >
+ <span ref={displayRef} style={span}>
+ {inputFallback
+ ? editingDisplayValueRef.current
+ : formatter(value as string | number)}
</span>
+ {formatterSubtitle ? (
+ <span style={formatterSubtitleStyle}>
+ {formatterSubtitle(value as string | number)}
+ </span>
+ ) : null}
+ </button>
+ {inputFallback ? (
+ <HigherZIndex onEscape={onEscape} onOutsideClick={noop}>
+ <RemotionInput
+ ref={fallbackRef}
+ onKeyDown={onInputKeyDown}
+ onBlur={onBlur}
+ onChange={onInputChange}
+ min={_min}
+ max={_max}
+ step={validationStep}
+ defaultValue={formattedInputValueRef.current?.text ?? value}
+ status={status}
+ rightAlign={rightAlign}
+ small={small}
+ {...props}
+ className={`__remotion_input_dragger_input ${props.className ?? ''}`}
+ style={{
+ ...inputDraggerStyle,
+ ...(small ? compactInputDraggerStyle : null),
+ ...buttonStyle,
+ ...formatterStyle,
+ userSelect: 'text',
+ WebkitUserSelect: 'text',
+ ...inputStyle,
+ backgroundColor: TRANSPARENT,
+ borderRadius: 3,
+ paddingTop: 0,
+ paddingBottom: 0,
+ outline: 'none',
+ boxShadow: 'none',
+ ...(status === 'ok' ? {borderColor: WHITE_ALPHA_10} : null),
+ ...(editingPaddingRightRef.current === null
+ ? null
+ : {paddingRight: editingPaddingRightRef.current}),
+ display: 'block',
+ position: 'absolute',
+ left: 0,
+ top: '50%',
+ transform: 'translateY(-50%)',
+ width: '100%',
+ height: 'auto',
+ }}
+ type="text"
+ />
+ </HigherZIndex>
) : null}
- </button>
+ </span>
);
};