Skip to content

Visualize Your Ideas With Code

fix(ui): fix onChange handlers (#515)

Jacoba23d06cbf6e2
diff --git a/packages/ui/src/components/controls/ColorInput.tsx b/packages/ui/src/components/controls/ColorInput.tsx
index 52855a61..1e583b76 100644
--- a/packages/ui/src/components/controls/ColorInput.tsx
+++ b/packages/ui/src/components/controls/ColorInput.tsx
@@ -2,6 +2,9 @@ import styles from './Controls.module.scss';
import clsx from 'clsx';
import {ColorPreview} from './ColorPreview';
import {Color} from '@motion-canvas/core/lib/types';
+import {valid} from 'chroma-js';
+import {Input} from './Input';
+import {shake} from '../animations';
export interface ColorInputProps {
value: Color | null;
@@ -11,8 +14,19 @@ export interface ColorInputProps {
export function ColorInput({value, onChange}: ColorInputProps) {
return (
<div className={clsx(styles.input, styles.color)}>
- <input
- onChange={event => onChange((event.target as HTMLInputElement).value)}
+ <Input
+ onChange={event => {
+ const input = event.target as HTMLInputElement;
+ const newValue = input.value;
+ if (valid(newValue)) {
+ onChange(newValue);
+ } else {
+ input.value = value?.serialize() ?? '';
+ input.parentElement.animate(shake(2), {
+ duration: 300,
+ });
+ }
+ }}
placeholder="none"
type="text"
value={value?.serialize() ?? ''}
diff --git a/packages/ui/src/components/controls/Controls.module.scss b/packages/ui/src/components/controls/Controls.module.scss
index 41bfc95a..ba130d35 100644
--- a/packages/ui/src/components/controls/Controls.module.scss
+++ b/packages/ui/src/components/controls/Controls.module.scss
@@ -126,14 +126,13 @@
input {
width: 0;
- flex-shrink: 1;
- flex-grow: 1;
margin: 0 8px 0 0;
padding-left: 8px;
appearance: none;
- border: 0;
background-color: transparent;
- color: inherit;
+ &:hover {
+ background-color: transparent;
+ }
}
}
diff --git a/packages/ui/src/components/controls/Input.tsx b/packages/ui/src/components/controls/Input.tsx
index 52ca7944..8a97e412 100644
--- a/packages/ui/src/components/controls/Input.tsx
+++ b/packages/ui/src/components/controls/Input.tsx
@@ -3,6 +3,13 @@ import type {JSX} from 'preact';
type InputProps = JSX.HTMLAttributes<HTMLInputElement>;
-export function Input(props: InputProps) {
- return <input className={styles.input} {...props} />;
+export function Input({onChange, onChangeCapture, ...props}: InputProps) {
+ return (
+ <input
+ onChangeCapture={onChangeCapture ?? onChange}
+ onChange={onChangeCapture ? onChange : undefined}
+ className={styles.input}
+ {...props}
+ />
+ );
}