Skip to content

Commit

Permalink
Converted Color Picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Patty committed May 11, 2022
1 parent 06364f2 commit 6896849
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import React from "react";
import styles from "./ColorPicker.css";
import { Colors } from "../../typeBuilders";
import { Colors as ColorsType } from "../../types";

export default ({ x, y, onColorPicked, onRequestClose }) => {
const wrapper = React.useRef();
interface ColorPickerProps {
x: number;
y: number;
onColorPicked: (color: ColorsType) => void;
onRequestClose: () => void;
}

export default ({ x, y, onColorPicked, onRequestClose }: ColorPickerProps) => {
const wrapper = React.useRef<HTMLDivElement>(null);

const testClickOutside = React.useCallback(
e => {
if (wrapper.current && !wrapper.current.contains(e.target)) {
(e: MouseEvent) => {
if (wrapper.current && !wrapper.current.contains(e.target as Node)) {
onRequestClose();
document.removeEventListener("click", testClickOutside);
document.removeEventListener("contextmenu", testClickOutside);
Expand All @@ -17,7 +25,7 @@ export default ({ x, y, onColorPicked, onRequestClose }) => {
);

const testEscape = React.useCallback(
e => {
(e: KeyboardEvent) => {
if (e.keyCode === 27) {
onRequestClose();
document.removeEventListener("keydown", testEscape);
Expand Down Expand Up @@ -47,21 +55,30 @@ export default ({ x, y, onColorPicked, onRequestClose }) => {
top: y
}}
>
{Object.values(Colors).map(color => (
<ColorButton
onSelected={() => {
onColorPicked(color);
onRequestClose();
}}
color={color}
key={color}
/>
))}
{Object.values(Colors).map(colorString => {
const color = colorString as ColorsType;
return (
<ColorButton
onSelected={() => {
onColorPicked(color);
onRequestClose();
}}
color={color}
key={color}
/>
);
})}
</div>
);
};

const ColorButton = ({ color, onSelected }) => (
const ColorButton = ({
color,
onSelected
}: {
color: ColorsType;
onSelected: () => void;
}) => (
<div className={styles.colorButtonWrapper}>
<button
data-flume-component="color-button"
Expand Down
4 changes: 2 additions & 2 deletions src/typeBuilders.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Colors as ColorsType,
Control,
CustomControl,
MultiselectControl,
Expand Down Expand Up @@ -93,7 +94,7 @@ export const Controls = {
)
};

export const Colors = {
export const Colors: { [key: string]: ColorsType } = {
yellow: "yellow",
orange: "orange",
red: "red",
Expand Down Expand Up @@ -322,4 +323,3 @@ export class FlumeConfig {
return this;
}
}

0 comments on commit 6896849

Please sign in to comment.