Skip to content

Visualize Your Ideas With Code

feat(2d): expand animations and reduced motion (#671)

Jacobb8e9d03488f8
diff --git a/packages/ui/src/components/console/Console.module.scss b/packages/ui/src/components/console/Console.module.scss
index eee55bfb..c778a819 100644
--- a/packages/ui/src/components/console/Console.module.scss
+++ b/packages/ui/src/components/console/Console.module.scss
@@ -122,7 +122,7 @@ $colors: (
padding: 8px;
background-color: var(--surface-color);
opacity: 0;
- transition: opacity 0.1s;
+ transition: opacity var(--duration-fast);
&:focus,
.sourceCode:hover & {
diff --git a/packages/ui/src/components/console/Log.tsx b/packages/ui/src/components/console/Log.tsx
index 9317ffad..fe54b736 100644
--- a/packages/ui/src/components/console/Log.tsx
+++ b/packages/ui/src/components/console/Log.tsx
@@ -10,6 +10,7 @@ import {StackTrace} from './StackTrace';
import {SourceCodeFrame} from './SourceCodeFrame';
import clsx from 'clsx';
import {Locate} from '../icons';
+import {Collapse} from '../layout';
export interface LogProps {
payload: LogPayload;
@@ -60,8 +61,8 @@ export function Log({payload}: LogProps) {
</IconButton>
)}
</div>
- {hasBody && open && (
- <div>
+ {hasBody && (
+ <Collapse open={open}>
{payload.remarks && (
<div
className={clsx(styles.section, styles.remarks)}
@@ -81,7 +82,7 @@ export function Log({payload}: LogProps) {
<StackTrace entries={entries} />
</div>
)}
- </div>
+ </Collapse>
)}
</div>
);
diff --git a/packages/ui/src/components/controls/ColorInput.tsx b/packages/ui/src/components/controls/ColorInput.tsx
index 6b49e769..5832ba7f 100644
--- a/packages/ui/src/components/controls/ColorInput.tsx
+++ b/packages/ui/src/components/controls/ColorInput.tsx
@@ -5,6 +5,7 @@ import {Color} from '@motion-canvas/core/lib/types';
import {valid} from 'chroma-js';
import {Input} from './Input';
import {shake} from '../animations';
+import {useReducedMotion} from '../../hooks';
export interface ColorInputProps {
value: Color | null;
@@ -12,6 +13,7 @@ export interface ColorInputProps {
}
export function ColorInput({value, onChange}: ColorInputProps) {
+ const reducedMotion = useReducedMotion();
return (
<div className={clsx(styles.input, styles.color)}>
<Input
@@ -22,9 +24,11 @@ export function ColorInput({value, onChange}: ColorInputProps) {
onChange(newValue);
} else {
input.value = value?.serialize() ?? '';
- input.parentElement.animate(shake(2), {
- duration: 300,
- });
+ if (!reducedMotion) {
+ input.parentElement.animate(shake(2), {
+ duration: 300,
+ });
+ }
}
}}
placeholder="none"
diff --git a/packages/ui/src/components/controls/Controls.module.scss b/packages/ui/src/components/controls/Controls.module.scss
index 1b0fa87c..1e96f2a5 100644
--- a/packages/ui/src/components/controls/Controls.module.scss
+++ b/packages/ui/src/components/controls/Controls.module.scss
@@ -251,7 +251,7 @@
}
svg {
- transition: rotate 0.2s;
+ transition: rotate var(--duration-normal);
}
&.open svg {
diff --git a/packages/ui/src/components/fields/Expandable.tsx b/packages/ui/src/components/fields/Expandable.tsx
index ce178070..632b3505 100644
--- a/packages/ui/src/components/fields/Expandable.tsx
+++ b/packages/ui/src/components/fields/Expandable.tsx
@@ -2,6 +2,7 @@ import {ComponentChildren} from 'preact';
import {useStorage} from '../../hooks';
import {Toggle} from '../controls';
+import {Collapse} from '../layout';
import styles from './Expandable.module.scss';
@@ -40,7 +41,9 @@ export function ControlledExpandable({
<Toggle open={open} />
{title}
</div>
- {open && <div className={styles.content}>{children}</div>}
+ <Collapse open={open}>
+ <div className={styles.content}>{children}</div>
+ </Collapse>
</div>
);
}
diff --git a/packages/ui/src/components/fields/Layout.module.scss b/packages/ui/src/components/fields/Layout.module.scss
index c1310859..6186ae34 100644
--- a/packages/ui/src/components/fields/Layout.module.scss
+++ b/packages/ui/src/components/fields/Layout.module.scss
@@ -36,21 +36,13 @@
}
.fields {
- display: none;
-
&.nested {
+ display: flex;
flex-direction: column;
gap: 4px;
padding: 4px;
}
- .open > & {
- display: block;
- &.nested {
- display: flex;
- }
- }
-
& > .field {
border-top: 1px solid var(--field-color-border);
}
@@ -79,7 +71,7 @@
background-color: var(--field-color-hover);
color: white;
opacity: 0;
- transition: opacity 0.2s;
+ transition: opacity var(--duration-normal);
}
&.copied::before {
diff --git a/packages/ui/src/components/fields/Layout.tsx b/packages/ui/src/components/fields/Layout.tsx
index aed6eb67..204b90e6 100644
--- a/packages/ui/src/components/fields/Layout.tsx
+++ b/packages/ui/src/components/fields/Layout.tsx
@@ -4,6 +4,7 @@ import {ComponentChildren, JSX} from 'preact';
import {useRef, useState} from 'preact/hooks';
import {useFormattedNumber} from '../../hooks';
import {Toggle} from '../controls';
+import {Collapse} from '../layout';
import clsx from 'clsx';
export interface FieldSetProps {
@@ -11,18 +12,21 @@ export interface FieldSetProps {
header: ComponentChildren;
nested?: boolean;
}
+
export function FieldSet({children, header, nested}: FieldSetProps) {
const [open, setOpen] = useState(false);
return (
- <FieldSurface open={open}>
+ <FieldSurface open={true}>
<div className={styles.header}>
<Toggle open={open} onToggle={setOpen} />
{header}
</div>
- <div className={clsx(styles.fields, nested && styles.nested)}>
- {children}
- </div>
+ <Collapse open={open}>
+ <div className={clsx(styles.fields, nested && styles.nested)}>
+ {children}
+ </div>
+ </Collapse>
</FieldSurface>
);
}
diff --git a/packages/ui/src/components/layout/Collapse.module.scss b/packages/ui/src/components/layout/Collapse.module.scss
new file mode 100644
index 00000000..b4f1c02c
--- /dev/null
+++ b/packages/ui/src/components/layout/Collapse.module.scss
@@ -0,0 +1,5 @@
+.root {
+ height: 0;
+ overflow: hidden;
+ transition: height var(--duration-normal) ease-in-out;
+}
diff --git a/packages/ui/src/components/layout/Collapse.tsx b/packages/ui/src/components/layout/Collapse.tsx
new file mode 100644
index 00000000..e19de909
--- /dev/null
+++ b/packages/ui/src/components/layout/Collapse.tsx
@@ -0,0 +1,78 @@
+import {useEffect, useLayoutEffect, useRef, useState} from 'preact/hooks';
+import styles from './Collapse.module.scss';
+import {ComponentChildren, JSX} from 'preact';
+import clsx from 'clsx';
+import {useReducedMotion} from '../../hooks';
+
+export interface CollapseProps extends JSX.HTMLAttributes<HTMLDivElement> {
+ open: boolean;
+ children: ComponentChildren;
+}
+
+export function Collapse(props: CollapseProps) {
+ const reducedMotion = useReducedMotion();
+ const Component = reducedMotion ? ReducedCollapse : AnimatedCollapse;
+ return <Component {...props} />;
+}
+
+function ReducedCollapse({open, children, className, ...rest}: CollapseProps) {
+ return (
+ <div className={className} {...rest}>
+ {open && children}
+ </div>
+ );
+}
+
+function AnimatedCollapse({open, children, className, ...rest}: CollapseProps) {
+ const [visible, setVisible] = useState(false);
+ const ref = useRef<HTMLDivElement>();
+
+ useLayoutEffect(() => {
+ if (open) {
+ ref.current.dataset.open = open ? 'true' : 'false';
+ ref.current.style.height = open ? 'auto' : '0px';
+ }
+ }, []);
+
+ useEffect(() => {
+ const element = ref.current;
+ if (element) {
+ const handle = () => {
+ if (element.dataset.open === 'true') {
+ element.style.height = 'auto';
+ } else {
+ setVisible(false);
+ }
+ };
+ element.addEventListener('transitionend', handle);
+ return () => element.removeEventListener('transitionend', handle);
+ }
+ }, [ref.current]);
+
+ useLayoutEffect(() => {
+ if (open) {
+ setVisible(true);
+ }
+ }, [open]);
+
+ useEffect(() => {
+ if (!visible) return;
+
+ ref.current.dataset.open = open ? 'true' : 'false';
+ if (open) {
+ ref.current.style.height = `${ref.current.scrollHeight}px`;
+ } else {
+ if (ref.current.style.height === 'auto') {
+ ref.current.style.height = `${ref.current.scrollHeight}px`;
+ ref.current.scrollHeight;
+ }
+ ref.current.style.height = '0px';
+ }
+ }, [open, visible]);
+
+ return (
+ <div ref={ref} className={clsx(styles.root, className)} {...rest}>
+ {visible && children}
+ </div>
+ );
+}
diff --git a/packages/ui/src/components/layout/index.ts b/packages/ui/src/components/layout/index.ts
index a4eff26f..1606a2dd 100644
--- a/packages/ui/src/components/layout/index.ts
+++ b/packages/ui/src/components/layout/index.ts
@@ -1,2 +1,3 @@
+export * from './Collapse';
export * from './Header';
export * from './ResizeableLayout';
diff --git a/packages/ui/src/components/sidebar/Sidebar.tsx b/packages/ui/src/components/sidebar/Sidebar.tsx
index a6160ab9..3b2f073f 100644
--- a/packages/ui/src/components/sidebar/Sidebar.tsx
+++ b/packages/ui/src/components/sidebar/Sidebar.tsx
@@ -6,7 +6,7 @@ import {Console} from '../console';
import {useInspection, useLogger} from '../../contexts';
import {useEffect, useRef, useState} from 'preact/hooks';
import {shake} from '../animations';
-import {useStorage} from '../../hooks';
+import {useReducedMotion, useStorage} from '../../hooks';
import {MotionCanvas, Bug, Videocam, AccountTree} from '../icons';
import {Docs} from '../icons/Docs';
import {ViewTimeline} from '../icons/ViewTimeline';
@@ -18,6 +18,7 @@ interface SidebarProps {
export function Sidebar({setOpen}: SidebarProps) {
const [tab, setTab] = useStorage('sidebar', -1);
const {inspectedElement} = useInspection();
+ const reducedMotion = useReducedMotion();
const logger = useLogger();
const badge = useRef<HTMLDivElement>();
const [errorCount, setErrorCount] = useState(logger.onErrorLogged.current);
@@ -25,11 +26,13 @@ export function Sidebar({setOpen}: SidebarProps) {
() =>
logger.onErrorLogged.subscribe(value => {
setErrorCount(value);
- setTimeout(() => {
- badge.current?.animate(shake(2), {duration: 300});
- }, 0);
+ if (!reducedMotion) {
+ setTimeout(() => {
+ badge.current?.animate(shake(2), {duration: 300});
+ }, 0);
+ }
}),
- [logger],
+ [logger, reducedMotion],
);
useEffect(() => {
diff --git a/packages/ui/src/components/timeline/Timeline.module.scss b/packages/ui/src/components/timeline/Timeline.module.scss
index 9d4fbfeb..f12086d7 100644
--- a/packages/ui/src/components/timeline/Timeline.module.scss
+++ b/packages/ui/src/components/timeline/Timeline.module.scss
@@ -4,7 +4,7 @@
height: 100%;
padding-top: 1px;
opacity: 0;
- transition: opacity 0.2s;
+ transition: opacity var(--duration-normal);
display: flex;
align-items: stretch;
position: relative;
@@ -320,7 +320,7 @@
width: 100%;
margin: 24px 0;
opacity: 0;
- transition: opacity 0.2s;
+ transition: opacity var(--duration-normal);
&.show {
opacity: 1;
diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.ts
index 92e2a6bc..9c56d6c3 100644
--- a/packages/ui/src/hooks/index.ts
+++ b/packages/ui/src/hooks/index.ts
@@ -7,6 +7,7 @@ export * from './useKeyHold';
export * from './usePlayerState';
export * from './usePlayerTime';
export * from './usePresenterState';
+export * from './useReducedMotion';
export * from './useRendererState';
export * from './useScenes';
export * from './useSettings';
diff --git a/packages/ui/src/hooks/useReducedMotion.ts b/packages/ui/src/hooks/useReducedMotion.ts
new file mode 100644
index 00000000..c39c5bf5
--- /dev/null
+++ b/packages/ui/src/hooks/useReducedMotion.ts
@@ -0,0 +1,16 @@
+import {useEffect, useState} from 'preact/hooks';
+
+const MediaQuery = window.matchMedia(`(prefers-reduced-motion: reduce)`);
+
+export function useReducedMotion() {
+ const [reducedMotion, setReducedMotion] = useState(MediaQuery.matches);
+ useEffect(() => {
+ const handler = (event: MediaQueryListEvent) => {
+ setReducedMotion(event.matches);
+ };
+ MediaQuery.addEventListener('change', handler);
+ return () => MediaQuery.removeEventListener('change', handler);
+ }, []);
+
+ return reducedMotion;
+}
diff --git a/packages/ui/src/index.scss b/packages/ui/src/index.scss
index 7214c4ac..efea59de 100644
--- a/packages/ui/src/index.scss
+++ b/packages/ui/src/index.scss
@@ -12,11 +12,21 @@
--input-background: var(--surface-color-light);
--input-background-hover: var(--surface-color-hover);
+ --duration-normal: 0.2s;
+ --duration-fast: 0.1s;
+
--red: #e13238;
--yellow: #e6a700;
--blue: #4cb3d4;
}
+@media (prefers-reduced-motion) {
+ :root {
+ --duration-normal: 0;
+ --duration-fast: 0;
+ }
+}
+
.wait * {
cursor: wait !important;
}