Visualize Your Ideas With Code
feat(ui): add modal actions (#1100)
Jacob4e49e09ba4fe
diff --git a/packages/ui/src/components/footer/Footer.module.scss b/packages/ui/src/components/footer/Footer.module.scssindex 7d2f5ea6..cd986948 100644--- a/packages/ui/src/components/footer/Footer.module.scss+++ b/packages/ui/src/components/footer/Footer.module.scss@@ -19,6 +19,12 @@overflow: hidden;}+.action {+ color: var(--theme);+ font-weight: bold;+ margin-left: 4px;+}+.shortcut {display: flex;@@ -34,7 +40,7 @@margin-right: 4px;padding: 0 4px;}- .action {+ .description {white-space: nowrap;}}diff --git a/packages/ui/src/components/footer/Footer.tsx b/packages/ui/src/components/footer/Footer.tsxindex 1cc63a3b..0e178c2b 100644--- a/packages/ui/src/components/footer/Footer.tsx+++ b/packages/ui/src/components/footer/Footer.tsx@@ -1,10 +1,11 @@import {useComputed} from '@preact/signals';+import clsx from 'clsx';import {useShortcutContext} from '../../contexts/shortcuts';import styles from './Footer.module.scss';import {Versions} from './Versions';export function Footer() {- const {surface, configs} = useShortcutContext();+ const {action, surface, configs} = useShortcutContext();const hints = useComputed(() => {const config = configs.current.get(surface.value);if (!config) return [];@@ -14,10 +15,15 @@ export function Footer() {return (<div className={styles.root}><div className={styles.shortcuts}>+ {action.value && (+ <div className={clsx(styles.shortcut, styles.action)}>+ {action.value.name}+ </div>+ )}{hints.value.map(({display, description}) => (<div className={styles.shortcut}><code className={styles.key}>{display}</code>- <span className={styles.action}>{description}</span>+ <span className={styles.description}>{description}</span></div>))}</div>@@ -25,3 +31,4 @@ export function Footer() {</div>);}+('');diff --git a/packages/ui/src/contexts/shortcuts.tsx b/packages/ui/src/contexts/shortcuts.tsxindex 6b86fe03..ecf7263b 100644--- a/packages/ui/src/contexts/shortcuts.tsx+++ b/packages/ui/src/contexts/shortcuts.tsx@@ -1,17 +1,24 @@import {Signal, useSignal} from '@preact/signals';import {ComponentChildren, createContext} from 'preact';import {Ref, useContext, useEffect, useRef} from 'preact/hooks';+import {MouseButton} from '../utils';-export type Shortcut = {+export interface Action {+ name: string;+ update: (event: PointerEvent) => void;+ finish: (value: boolean) => void;+}++export interface Shortcut {key: string;modifiers: {shift?: boolean;ctrl?: boolean;alt?: boolean;};- display: string;+ display: string | false;description: string;-};+}export type ShortcutConfig<T extends string> = {context: string;@@ -22,8 +29,10 @@ export type ShortcutMap<T extends string> = {[Key in T]?: Shortcut;};+export type ShortcutCallback = () => Promise<Action | void> | Action | void;+export type ShortcutCallbacks<T extends string> = {- [Key in T]?: () => void;+ [Key in T]?: ShortcutCallback;};function makeShortcuts<T extends string>(@@ -185,9 +194,10 @@ interface ModifierState {}type ConfigMap = Map<string, ShortcutMap<string>>;-type CallbackMap = Map<string, Set<(key: string) => boolean>>;+type CallbackMap = Map<string, Set<(key: string) => Promise<Action | boolean>>>;interface ShortcutsContextValue {+ action: Signal<Action | null>;global: Signal<string | null>;surface: Signal<string | null>;modifiers: Signal<ModifierState>;@@ -206,6 +216,7 @@ export function ShortcutsProvider({}) {const global = useSignal<string | null>(null);const surface = useSignal<string | null>(null);+ const action = useSignal<Action | null>(null);const modifiers = useSignal<ModifierState>({shift: false,ctrl: false,@@ -228,7 +239,15 @@ export function ShortcutsProvider({const configsRef = useRef(configMap);configsRef.current = configMap;- function evaluate(event: KeyboardEvent, context: string) {+ const updateModifiers = (event: MouseEvent | KeyboardEvent) => {+ modifiers.value = {+ shift: event.shiftKey,+ ctrl: event.ctrlKey,+ alt: event.altKey,+ };+ };++ async function evaluate(event: KeyboardEvent, context: string) {const config = configsRef.current.get(context)!;const callbackSet = callbacks.current.get(context);@@ -244,7 +263,11 @@ export function ShortcutsProvider({event.stopPropagation();for (const callback of callbackSet) {- if (callback(key)) {+ const result = await callback(key);+ if (typeof result === 'object') {+ action.value = result;+ }+ if (result) {break;}}@@ -254,42 +277,83 @@ export function ShortcutsProvider({}}- useEffect(() => {- const updateModifiers = (event: MouseEvent | KeyboardEvent) => {- modifiers.value = {- shift: event.shiftKey,- ctrl: event.ctrlKey,- alt: event.altKey,- };- };+ const evaluateAll = async (event: KeyboardEvent) => {+ updateModifiers(event);- const keyDown = (event: KeyboardEvent) => {- updateModifiers(event);+ if (action.value) {+ if (event.key === 'Escape') {+ action.value.finish(false);+ action.value = null;+ } else if (event.key === 'Enter') {+ action.value.finish(true);+ action.value = null;+ }+ return;+ }- if (document.activeElement.tagName === 'INPUT') {+ if (document.activeElement.tagName === 'INPUT') {+ return;+ }+ if (surface.value && (await evaluate(event, surface.value))) {+ return;+ }+ if (global.value && (await evaluate(event, global.value))) {+ return;+ }+ };++ useEffect(() => {+ let keyDownLock = false;+ const keyDown = (event: KeyboardEvent) => {+ if (keyDownLock) {return;}- if (surface.value && evaluate(event, surface.value)) {- return;+ keyDownLock = true;+ evaluateAll(event).finally(() => {+ keyDownLock = false;+ });+ };++ const pointerMove = (event: PointerEvent) => {+ updateModifiers(event);+ if (action.value) {+ document.body.setPointerCapture(event.pointerId);+ action.value.update(event);}- if (global.value && evaluate(event, global.value)) {- return;+ };++ const pointerUp = (event: PointerEvent) => {+ const isPrimary = event.button === MouseButton.Left;+ const isSecondary = event.button === MouseButton.Right;+ if (action.value && (isPrimary || isSecondary)) {+ document.body.releasePointerCapture(event.pointerId);+ action.value.finish(isPrimary);+ action.value = null;}};window.addEventListener('keydown', keyDown, true);- window.addEventListener('pointermove', updateModifiers, true);+ window.addEventListener('pointermove', pointerMove, true);+ window.addEventListener('pointerup', pointerUp, true);window.addEventListener('keyup', updateModifiers, true);return () => {window.removeEventListener('keydown', keyDown, true);- window.removeEventListener('pointermove', updateModifiers, true);+ window.removeEventListener('pointermove', pointerMove, true);+ window.removeEventListener('pointerup', pointerUp, true);window.removeEventListener('keyup', updateModifiers, true);};}, []);return (<ShortcutsContext.Provider- value={{modifiers, callbacks, surface, global, configs: configsRef}}+ value={{+ action,+ modifiers,+ callbacks,+ surface,+ global,+ configs: configsRef,+ }}>{children}</ShortcutsContext.Provider>@@ -338,7 +402,7 @@ export function useSurfaceShortcuts<T extends HTMLElement>(export function useShortcut<T extends string>(config: ShortcutConfig<T>,name: T,- handler: () => void,+ handler: ShortcutCallback,) {return useShortcuts<T>(config, {[name]: handler} as ShortcutCallbacks<T>);}@@ -358,10 +422,15 @@ export function useShortcuts<T extends string>(callbacks.current.set(config.context, callbackSet);}- const handler = (key: T) => {+ const handler = async (key: T) => {const callback = handlersRef.current[key];- callback?.();- return !!callback;+ if (callback) {+ const result = await callback();+ if (typeof result === 'object') {+ return result;+ }+ }+ return true;};callbackSet.add(handler);