Visualize Your Ideas With Code
feat(ui): add color picker (#691)
Jacoba33059c0f455
diff --git a/packages/ui/src/components/footer/Footer.tsx b/packages/ui/src/components/footer/Footer.tsxindex bb7cc021..f9629822 100644--- a/packages/ui/src/components/footer/Footer.tsx+++ b/packages/ui/src/components/footer/Footer.tsx@@ -7,12 +7,14 @@ export function Footer() {return (<div className={styles.root}><div className={styles.shortcuts}>- {shortcuts[currentModule].map(({key, action}) => (- <div className={styles.shortcut}>- <span className={styles.key}>{key}</span>- <span className={styles.action}>{action}</span>- </div>- ))}+ {shortcuts[currentModule]+ .filter(({available}) => !available || available())+ .map(({key, action}) => (+ <div className={styles.shortcut}>+ <span className={styles.key}>{key}</span>+ <span className={styles.action}>{action}</span>+ </div>+ ))}</div><Versions /></div>diff --git a/packages/ui/src/components/icons/Colorize.tsx b/packages/ui/src/components/icons/Colorize.tsxnew file mode 100644index 00000000..7ff9b0dc--- /dev/null+++ b/packages/ui/src/components/icons/Colorize.tsx@@ -0,0 +1,7 @@+export function Colorize() {+ return (+ <svg viewBox="0 0 16 16" fill="currentColor">+ <path d="M6.954,4.818l-1.414,-1.414l1.414,-1.414l1.414,1.414l2.121,-2.122c0.188,-0.187 0.442,-0.292 0.707,-0.292c0.266,-0 0.52,0.105 0.708,0.292l2.828,2.829c0.188,0.187 0.293,0.442 0.293,0.707c-0,0.265 -0.105,0.52 -0.293,0.707l-2.121,2.121l1.414,1.415l-1.414,1.414l-1.415,-1.414l-4.949,4.949l-4.247,-0.004l0.004,-4.238l4.95,-4.95Zm1.414,1.414l-4.243,4.243l1.415,1.414l4.242,-4.243l-1.414,-1.414Z" />+ </svg>+ );+}diff --git a/packages/ui/src/components/icons/index.ts b/packages/ui/src/components/icons/index.tsindex 826fa8dd..709d1409 100644--- a/packages/ui/src/components/icons/index.ts+++ b/packages/ui/src/components/icons/index.ts@@ -6,6 +6,7 @@ export * from './Close';export * from './DragIndicator';export * from './Fullscreen';export * from './Grid';+export * from './Colorize';export * from './Locate';export * from './MotionCanvas';export * from './OpenInNew';diff --git a/packages/ui/src/components/viewport/ColorPicker.tsx b/packages/ui/src/components/viewport/ColorPicker.tsxnew file mode 100644index 00000000..76e78a14--- /dev/null+++ b/packages/ui/src/components/viewport/ColorPicker.tsx@@ -0,0 +1,46 @@+import {Colorize} from '../icons';+import {useState} from 'react';+import {ButtonCheckbox} from '../controls/ButtonCheckbox';+import {useCallback} from 'preact/hooks';+import {useDocumentEvent} from '../../hooks';++export function ColorPicker() {+ return typeof EyeDropper === 'function' ? <ColorPickerImpl /> : <></>;+}++function ColorPickerImpl() {+ const [active, setActive] = useState(false);+ const pickColor = useCallback(async () => {+ try {+ setActive(true);+ const dropper = new EyeDropper();+ const {sRGBHex} = await dropper.open();+ await window.navigator.clipboard.writeText(sRGBHex);+ } catch (_) {+ // User canceled the operation.+ }+ setActive(false);+ }, []);++ useDocumentEvent(+ 'keydown',+ useCallback(+ event => {+ if (event.key === 'i') {+ pickColor();+ }+ },+ [pickColor],+ ),+ );++ return (+ <ButtonCheckbox+ title={'Use color picker [I]'}+ checked={active}+ onClick={pickColor}+ >+ <Colorize />+ </ButtonCheckbox>+ );+}diff --git a/packages/ui/src/components/viewport/EditorPreview.tsx b/packages/ui/src/components/viewport/EditorPreview.tsxindex aa901f0f..ca87ef8e 100644--- a/packages/ui/src/components/viewport/EditorPreview.tsx+++ b/packages/ui/src/components/viewport/EditorPreview.tsx@@ -22,6 +22,7 @@ import clsx from 'clsx';import {Button, Select} from '../controls';import {Recenter, Grid as GridIcon} from '../icons';import {ButtonCheckbox} from '../controls/ButtonCheckbox';+import {ColorPicker} from './ColorPicker';const ZOOM_SPEED = 0.1;@@ -238,12 +239,13 @@ export function EditorPreview() {<Recenter /></Button><ButtonCheckbox- title={'Toggle grid'}+ title={"Toggle grid [']"}onChecked={setGrid}checked={grid}><GridIcon /></ButtonCheckbox>+ <ColorPicker /></div></div></ViewportContext.Provider>diff --git a/packages/ui/src/contexts/shortcuts.tsx b/packages/ui/src/contexts/shortcuts.tsxindex 9d95188e..6524581e 100644--- a/packages/ui/src/contexts/shortcuts.tsx+++ b/packages/ui/src/contexts/shortcuts.tsx@@ -4,6 +4,7 @@ import {useContext, useState} from 'preact/hooks';export type Shortcut = {key: string;action: string;+ available?: () => boolean;};export type ShortcutModules = 'global' | 'timeline' | 'viewport' | 'none';@@ -29,6 +30,11 @@ const InitialShortcuts: ShortcutsByModule = {{key: '=', action: 'Zoom in'},{key: '-', action: 'Zoom out'},{key: "'", action: 'Toggle grid'},+ {+ key: 'I',+ action: 'Use color picker',+ available: () => typeof EyeDropper === 'function',+ },],timeline: [{key: 'F', action: 'Focus playhead'}],none: [],diff --git a/packages/ui/src/global.d.ts b/packages/ui/src/global.d.tsindex d5cf927a..0d12885c 100644--- a/packages/ui/src/global.d.ts+++ b/packages/ui/src/global.d.ts@@ -1 +1,5 @@declare module '*.scss';++declare class EyeDropper {+ public open(): Promise<{sRGBHex: string}>;+}