Visualize Your Ideas With Code
feat(ui): list available shortcuts (#444)
Oscar Alfredo Castillejo Salazar443fcc9feb1a
diff --git a/packages/ui/src/Editor.tsx b/packages/ui/src/Editor.tsxindex 470e6fb6..59ba45da 100644--- a/packages/ui/src/Editor.tsx+++ b/packages/ui/src/Editor.tsx@@ -1,5 +1,6 @@import {Sidebar} from './components/sidebar';import {Timeline} from './components/timeline';+import {Footer} from './components/footer';import {Viewport} from './components/viewport';import {ResizeableLayout} from './components/layout';import {useState} from 'preact/hooks';@@ -24,7 +25,12 @@ export function Editor() {end={<Viewport />}/>}- end={<Timeline />}+ end={+ <>+ <Timeline />+ <Footer />+ </>+ }/>) : (<PresentationMode />diff --git a/packages/ui/src/components/footer/Footer.module.scss b/packages/ui/src/components/footer/Footer.module.scssnew file mode 100644index 00000000..b3cd0cc2--- /dev/null+++ b/packages/ui/src/components/footer/Footer.module.scss@@ -0,0 +1,28 @@+.root {+ display: flex;+ position: fixed;+ bottom: 0;+ width: 100%;+ height: 1.5rem;+ padding: 1px 4px;+ overflow-x: hidden;+}++.shortcut {+ &:not(:last-child) {+ border-right: 2px solid var(--surface-color);+ padding-right: 8px;+ margin-right: 8px;+ }+ .key {+ background-color: var(--surface-color-hover);+ font-weight: bold;+ border-radius: 4px;+ margin-right: 4px;+ padding: 0 4px;+ font-size: 12px;+ }+ .action {+ font-size: 12px;+ }+}diff --git a/packages/ui/src/components/footer/Footer.tsx b/packages/ui/src/components/footer/Footer.tsxnew file mode 100644index 00000000..c41ecb33--- /dev/null+++ b/packages/ui/src/components/footer/Footer.tsx@@ -0,0 +1,16 @@+import {useShortcuts} from '../../contexts/shortcuts';+import styles from './Footer.module.scss';++export function Footer() {+ const {shortcuts, currentModule} = useShortcuts();+ return (+ <div className={styles.root}>+ {shortcuts[currentModule].map(({key, action}) => (+ <div className={styles.shortcut}>+ <span className={styles.key}>{key}</span>+ <span className={styles.action}>{action}</span>+ </div>+ ))}+ </div>+ );+}diff --git a/packages/ui/src/components/footer/index.ts b/packages/ui/src/components/footer/index.tsnew file mode 100644index 00000000..ddcc5a9c--- /dev/null+++ b/packages/ui/src/components/footer/index.ts@@ -0,0 +1 @@+export * from './Footer';diff --git a/packages/ui/src/components/timeline/Timeline.module.scss b/packages/ui/src/components/timeline/Timeline.module.scssindex ff94466c..31de7f08 100644--- a/packages/ui/src/components/timeline/Timeline.module.scss+++ b/packages/ui/src/components/timeline/Timeline.module.scss@@ -3,6 +3,7 @@width: 100%;height: 100%;padding-top: 1px;+ padding-bottom: 1.5rem;opacity: 0;transition: opacity 0.2s;display: flex;diff --git a/packages/ui/src/components/timeline/Timeline.tsx b/packages/ui/src/components/timeline/Timeline.tsxindex 57eaa0fd..a3d8886f 100644--- a/packages/ui/src/components/timeline/Timeline.tsx+++ b/packages/ui/src/components/timeline/Timeline.tsx@@ -22,6 +22,7 @@ import {TimelineState,} from '../../contexts';import clsx from 'clsx';+import {useShortcut} from '../../hooks/useShortcut';const ZOOM_SPEED = 0.1;const ZOOM_MIN = 0.5;@@ -29,6 +30,7 @@ const TIMESTAMP_SPACING = 32;const MAX_FRAME_SIZE = 128;export function Timeline() {+ const [hoverRef] = useShortcut<HTMLDivElement>('timeline');const {player} = useApplication();const containerRef = useRef<HTMLDivElement>();const playheadRef = useRef<HTMLDivElement>();@@ -135,7 +137,7 @@ export function Timeline() {return (<TimelineContextProvider state={state}>- <div className={clsx(styles.root, isReady && styles.show)}>+ <div ref={hoverRef} className={clsx(styles.root, isReady && styles.show)}><divclassName={styles.timelineWrapper}ref={containerRef}diff --git a/packages/ui/src/components/viewport/Viewport.tsx b/packages/ui/src/components/viewport/Viewport.tsxindex 73a59c00..ede71b73 100644--- a/packages/ui/src/components/viewport/Viewport.tsx+++ b/packages/ui/src/components/viewport/Viewport.tsx@@ -6,15 +6,17 @@ import styles from './Viewport.module.scss';import {useApplication} from '../../contexts';import {CustomStage} from './CustomStage';import {RendererState} from '@motion-canvas/core';+import {useShortcut} from '../../hooks/useShortcut';export function Viewport() {+ const [hoverRef] = useShortcut<HTMLDivElement>('viewport');const {player, renderer} = useApplication();const duration = useDuration();const {speed} = usePlayerState();const state = useRendererState();return (- <div className={styles.root}>+ <div ref={hoverRef} className={styles.root}>{state === RendererState.Working ? (<CustomStage stage={renderer.stage} />) : (diff --git a/packages/ui/src/contexts/shortcuts.tsx b/packages/ui/src/contexts/shortcuts.tsxnew file mode 100644index 00000000..93f9b2c0--- /dev/null+++ b/packages/ui/src/contexts/shortcuts.tsx@@ -0,0 +1,56 @@+import {ComponentChildren, createContext} from 'preact';+import {useContext, useState} from 'preact/hooks';++export type Shortcut = {+ key: string;+ action: string;+};++export type ShortcutModules = 'global' | 'timeline' | 'viewport' | 'none';+type ShortcutsByModule = Record<ShortcutModules, Shortcut[]>;+type ShortcutsState = {+ currentModule: ShortcutModules;+ shortcuts: ShortcutsByModule;+ setCurrentModule?: (module: ShortcutModules) => void;+};++const InitialShortcuts: ShortcutsByModule = {+ global: [+ {key: 'Space', action: 'Toggle playback'},+ {key: '<-', action: 'Previous frame'},+ {key: '->', action: 'Next frame'},+ {key: 'Shift + <-', action: 'Reset to first frame'},+ {key: 'Shift + ->', action: 'Seek to last frame'},+ {key: 'M', action: 'Toggle audio'},+ {key: 'L', action: 'Toggle loop'},+ ],+ viewport: [+ {key: '0', action: 'Reset zoom'},+ {key: '=', action: 'Zoom in'},+ {key: '-', action: 'Zoom out'},+ {key: "'", action: 'Toggle grid'},+ ],+ timeline: [{key: 'F', action: 'Focus playhead'}],+ none: [],+};++const ShortcutsContext = createContext<ShortcutsState>({+ currentModule: 'none',+ shortcuts: InitialShortcuts,+});++export function ShortcutsProvider({children}: {children: ComponentChildren}) {+ const [currentModule, setCurrentModule] = useState<ShortcutModules>('none');++ return (+ <ShortcutsContext.Provider+ value={{currentModule, setCurrentModule, shortcuts: InitialShortcuts}}+ >+ {children}+ </ShortcutsContext.Provider>+ );+}++export function useShortcuts() {+ return useContext(ShortcutsContext);+}diff --git a/packages/ui/src/hooks/useHover.ts b/packages/ui/src/hooks/useHover.tsnew file mode 100644index 00000000..97e1b1c5--- /dev/null+++ b/packages/ui/src/hooks/useHover.ts@@ -0,0 +1,40 @@+import React, {useEffect, useRef, useState} from 'react';++type UseHoverType<T extends HTMLElement> = [React.RefObject<T>, boolean];++export function useHover<T extends HTMLElement>(+ handleMouseOver?: () => void,+ handleMouseOut?: () => void,+): UseHoverType<T> {+ const [value, setValue] = useState(false);++ const ref = useRef<T>(null);++ const handleMouseOverWrapper = () => {+ setValue(true);+ handleMouseOver?.();+ };++ const handleMouseOutWrapper = () => {+ setValue(false);+ handleMouseOut?.();+ };++ useEffect(+ () => {+ const node = ref.current;+ if (node) {+ node.addEventListener('mouseover', handleMouseOverWrapper);+ node.addEventListener('mouseout', handleMouseOutWrapper);++ return () => {+ node.removeEventListener('mouseover', handleMouseOverWrapper);+ node.removeEventListener('mouseout', handleMouseOutWrapper);+ };+ }+ },+ [ref.current], // Recall only if ref changes+ );++ return [ref, value];+}diff --git a/packages/ui/src/hooks/useShortcut.ts b/packages/ui/src/hooks/useShortcut.tsnew file mode 100644index 00000000..e480d3ee--- /dev/null+++ b/packages/ui/src/hooks/useShortcut.ts@@ -0,0 +1,21 @@+import React from 'react';+import {useShortcuts, ShortcutModules} from '../contexts/shortcuts';+import {useHover} from './useHover';++type UseHoverType<T extends HTMLElement> = [React.RefObject<T>, boolean];++export function useShortcut<T extends HTMLElement>(+ shortcutModule: ShortcutModules,+): UseHoverType<T> {+ const {setCurrentModule} = useShortcuts();+ const [ref, value] = useHover<T>(+ () => {+ setCurrentModule(shortcutModule);+ },+ () => {+ setCurrentModule('none');+ },+ );++ return [ref, value];+}diff --git a/packages/ui/src/main.tsx b/packages/ui/src/main.tsxindex 49c5405d..e22e3601 100644--- a/packages/ui/src/main.tsx+++ b/packages/ui/src/main.tsx@@ -11,6 +11,7 @@ import {ApplicationProvider,} from './contexts';import {getItem, setItem} from './utils';+import {ShortcutsProvider} from './contexts/shortcuts';function renderRoot(vnode: ComponentChild) {const root = document.createElement('main');@@ -67,11 +68,13 @@ export function editor(project: Project) {meta,}}>- <LoggerProvider>- <InspectionProvider>- <Editor />- </InspectionProvider>- </LoggerProvider>+ <ShortcutsProvider>+ <LoggerProvider>+ <InspectionProvider>+ <Editor />+ </InspectionProvider>+ </LoggerProvider>+ </ShortcutsProvider></ApplicationProvider>,);}