Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: remove material-ui-color dependency #1670

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"date-fns": "^2.30.0",
"escodegen": "^2.1.0",
"jszip": "^3.10.1",
"material-ui-color": "^1.2.0",
"material-ui-popup-state": "^1.9.3",
"monaco-vim": "^0.3.5",
"notistack": "^2.0.8",
Expand Down
13 changes: 5 additions & 8 deletions src/ScriptEditor/ui/ThemeEditorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import _ from "lodash";

import { Grid, Box, Button, IconButton, Paper, TextField, Tooltip, Typography } from "@mui/material";
import { History, Reply } from "@mui/icons-material";
import { Color, ColorPicker } from "material-ui-color";

import { Settings } from "../../Settings/Settings";
import { useRerender } from "../../ui/React/hooks";
import { Modal } from "../../ui/React/Modal";
import { OptionSwitch } from "../../ui/React/OptionSwitch";

import { defaultMonacoTheme } from "./themes";
import { OpenColorPickerButton } from "../../Themes/ui/ColorPicker";

type ColorEditorProps = {
label: string;
Expand All @@ -26,7 +26,6 @@ function ColorEditor({ label, themePath, onColorChange, color, defaultColor }: C
console.error(`color ${themePath} was undefined, reverting to default`);
color = defaultColor;
}

return (
<Tooltip title={label}>
<span>
Expand All @@ -37,12 +36,10 @@ function ColorEditor({ label, themePath, onColorChange, color, defaultColor }: C
InputProps={{
readOnly: true,
startAdornment: (
<ColorPicker
hideTextfield
deferred
value={"#" + color}
onChange={(newColor: Color) => onColorChange(themePath, newColor.hex)}
disableAlpha
<OpenColorPickerButton
title={""}
color={`#${color}`}
onColorChange={(c) => onColorChange(themePath, c)}
/>
),
endAdornment: (
Expand Down
19 changes: 19 additions & 0 deletions src/Themes/ui/ColorPicker/ColorPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { RGB } from "./";
import React from "react";

type Props = {
rgb: RGB;
};

export function ColorPreview({ rgb: { r, g, b } }: Props) {
return (
<div
style={{
width: "50px",
height: "50px",
backgroundColor: `rgb(${r} ${g} ${b})`,
borderRadius: "20%",
}}
></div>
);
}
111 changes: 111 additions & 0 deletions src/Themes/ui/ColorPicker/HexInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { RGB, useSyncState } from "./";
import React, { ChangeEvent, useMemo, useState } from "react";

type Props = {
rgb: RGB;
setRGB: (rgb: RGB) => void;
};

export function formatRGBtoHEX(rgb: RGB) {
const digits = [
rgb.r.toString(16).padStart(2, "0"),
rgb.g.toString(16).padStart(2, "0"),
rgb.b.toString(16).padStart(2, "0"),
];

if (rgb.a) digits.push(rgb.a.toString(16).padStart(2, "0"));

if (digits.every((d) => d[0] == d[1])) return digits.reduce((prev, cur) => `${prev}${cur[0]}`, "");

return digits.join("");
}

export function HexInput({ rgb, setRGB }: Props) {
const hex = formatRGBtoHEX(rgb);

const [value, setValue] = useSyncState(hex);
const [error, setError] = useState(false);

//this is a memo instead of an effect to make it run in sync
useMemo(() => {
if (value.length != 3 && value.length != 6 && value.length != 8) return error || setError(true);

const rgb: RGB =
value.length == 3
? {
r: Number.parseInt(`${value[0]}${value[0]}`, 16),
g: Number.parseInt(`${value[1]}${value[1]}`, 16),
b: Number.parseInt(`${value[2]}${value[2]}`, 16),
}
: {
r: Number.parseInt(value.slice(0, 2), 16),
g: Number.parseInt(value.slice(2, 4), 16),
b: Number.parseInt(value.slice(4, 6), 16),
};

if (value.length == 8) rgb.a = Number.parseInt(value.slice(6, 8), 16);

if (Object.values(rgb).some((v) => isNaN(v))) return error || setError(true);
error && setError(false);

if (hex == formatRGBtoHEX(rgb)) return;

setRGB(rgb);

//this updates the current color so we only want to run this if `value` changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);

return (
<HexInputDisplay
value={value}
onChange={({ currentTarget: { value } }) => value.length <= 8 && setValue(value)}
error={error}
></HexInputDisplay>
);
}

type DisplayProps = {
error: boolean;
value: string;
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
};

function HexInputDisplay({ error, value, onChange }: DisplayProps) {
return (
<div
style={{
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
borderBottom: "1px solid currentColor",
}}
>
<span style={{ fontWeight: "bolder" }}>HEX</span>
<span
style={{
display: "flex",
justifyContent: "space-between",
gap: "0.3em",
}}
>
<span>#</span>
<input
type="text"
style={{
width: "3.5em",
appearance: "none",
MozAppearance: "none",
WebkitAppearance: "none",
border: "none",
background: "transparent",
color: "inherit",
}}
value={value}
onChange={onChange}
/>
<span style={{ display: "inline-block", width: "1em", textAlign: "right" }}>{error ? "X" : ""}</span>
</span>
</div>
);
}
79 changes: 79 additions & 0 deletions src/Themes/ui/ColorPicker/HueSlider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { useEffect } from "react";

type Props = {
hue: number;
setHue: (hue: number) => void;
};

const SLIDER_CLASS = "color_picker_hue_slider";

const GRADIENT_BACKGROUND = `\
rgba(0, 0, 0, 0)
linear-gradient(to right,
rgb(255, 0, 0) 0%,
rgb(255, 255, 0) 17%,
rgb(0, 255, 0) 33%,
rgb(0, 255, 255) 50%,
rgb(0, 0, 255) 67%,
rgb(255, 0, 255) 83%,
rgb(255, 0, 0) 100%
)
repeat scroll 0% 0%`;

const THUMB_SELECTORS = [`.${SLIDER_CLASS}::-moz-range-thumb`, `.${SLIDER_CLASS}::-webkit-slider-thumb`];

const THUMB_RULES = THUMB_SELECTORS.map(
(s) => /*css*/ `
${s} {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
background: white;
width: 17px;
height: 17px;
border-radius: 100%;
cursor: pointer;
}
`,
).join("");

const THUMB_HOVER_RULES = THUMB_SELECTORS.map(
(s) => /*css*/ `
${s}:hover {
background: lightgrey;
}
`,
).join("");

const CSS = /*css*/ `
${THUMB_RULES}
${THUMB_HOVER_RULES}
`;

export function HueSlider({ hue, setHue }: Props) {
useEffect(() => {
const styleSheet = new CSSStyleSheet();
styleSheet.replace(CSS);
document.adoptedStyleSheets.push(styleSheet);
return () => void (document.adoptedStyleSheets = document.adoptedStyleSheets.filter((s) => s != styleSheet));
}, []);

return (
<input
className={SLIDER_CLASS}
style={{
height: "15px",
background: GRADIENT_BACKGROUND,
appearance: "none",
MozAppearance: "none",
WebkitAppearance: "none",
}}
value={hue + ""}
type="range"
min={0}
max={360}
step={1}
onChange={(e) => setHue(+e.currentTarget.value)}
></input>
);
}
49 changes: 49 additions & 0 deletions src/Themes/ui/ColorPicker/RGBDigit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useSyncState } from "./";
import React, { useMemo } from "react";

type Props = {
digitLabel: "R" | "G" | "B";
digit: [number, (d: number) => void];
};

export function RGBDigit({ digitLabel, digit: [digit, setDigit] }: Props) {
const [value, setValue] = useSyncState(digit);

useMemo(() => {
if (digit == value) return;
if (value > 255) return setValue(255);
if (value < 0) return setValue(0);

setDigit(value);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);

return (
<div
style={{
display: "flex",
flexDirection: "column",
justifyContent: "space-between",
borderBottom: "1px solid currentColor",
}}
>
<span style={{ fontWeight: "bolder" }}>{digitLabel}</span>
<input
type="number"
min="0"
max="255"
style={{
width: "2.5em",
appearance: "textfield",
MozAppearance: "textfield",
WebkitAppearance: "textfield",
border: "none",
background: "transparent",
color: "inherit",
}}
value={value + ""}
onChange={({ currentTarget: { value } }) => setValue(Number.parseInt((value || "0").replaceAll(/[^0-9]/g, "")))}
/>
</div>
);
}
Loading