Skip to content

Commit 4d33cff

Browse files
committed
[F] Implement Coloris color picker
1 parent 8379100 commit 4d33cff

File tree

8 files changed

+700
-21
lines changed

8 files changed

+700
-21
lines changed

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@emotion/server": "^11.11.0",
5959
"@emotion/styled": "^11.14.1",
6060
"@loadable/component": "^5.16.7",
61+
"@melloware/coloris": "^0.25.0",
6162
"@vidstack/react": "^1.12.13",
6263
"ace-builds": "^1.x",
6364
"actioncable": "~5.0.7",

client/src/global/components/form/BaseInput/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class FormBaseInput extends PureComponent {
119119
<InputComponent
120120
ref={input => {
121121
this.inputElement = input;
122+
if (this.props.colorRef) this.props.colorRef.current = input;
122123
}}
123124
id={id}
124125
name={this.props.name}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { useId, useEffect, useRef } from "react";
2+
import Coloris from "@melloware/coloris";
3+
import setter from "../setter";
4+
import * as Styled from "./styles";
5+
6+
function ColorInput({ defaultValue, ...props }) {
7+
const id = useId();
8+
const inputRef = useRef();
9+
const colorRef = useRef(props.value || defaultValue);
10+
11+
const inputId = `color-input-${id}`;
12+
13+
useEffect(() => {
14+
Coloris.init();
15+
16+
const setColor = event => {
17+
const isTarget = event.detail.currentEl.id === inputId;
18+
if (isTarget) colorRef.current = event.detail.color;
19+
};
20+
21+
document.addEventListener("coloris:pick", setColor);
22+
23+
return () => document.removeEventListener("coloris:pick", setColor);
24+
}, [inputId, props]);
25+
26+
useEffect(() => {
27+
const onClose = () => {
28+
props.onChange({ target: { value: colorRef.current } });
29+
};
30+
31+
const inputEl = inputRef.current;
32+
33+
if (inputEl) {
34+
Coloris({
35+
el: inputEl,
36+
format: "mixed",
37+
formatToggle: true,
38+
themeMode: "dark",
39+
clearButton: true,
40+
defaultColor: defaultValue,
41+
margin: 5
42+
});
43+
44+
inputEl.addEventListener("close", onClose);
45+
46+
return () => inputEl.removeEventListener("close", onClose);
47+
}
48+
}, [inputRef, defaultValue, colorRef, props]);
49+
50+
return (
51+
<Styled.ColorInput
52+
id={inputId}
53+
colorRef={inputRef}
54+
idForError={`color-input-error-${id}`}
55+
idForInstructions={`color-input-instructions-${id}`}
56+
placeholder={defaultValue}
57+
{...props}
58+
$defaultColor={defaultValue}
59+
/>
60+
);
61+
}
62+
63+
export default setter(ColorInput);
64+
65+
ColorInput.displayName = "Form.ColorInput";
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
import { useId } from "react";
2-
import PropTypes from "prop-types";
3-
import * as Styled from "./styles";
1+
import loadable from "@loadable/component";
42

5-
export default function ColorInput({ defaultValue, ...props }) {
6-
const id = useId();
3+
const ColorInput = loadable(() =>
4+
import(/* webpackChunkName: "coloris" */ "./ColorInput")
5+
);
76

8-
const renderValue = value => value ?? defaultValue;
9-
return (
10-
<Styled.ColorInput
11-
inputType="color"
12-
id={`color-input-${id}`}
13-
idForError={`color-input-error-${id}`}
14-
idForInstructions={`color-input-instructions-${id}`}
15-
renderValue={renderValue}
16-
{...props}
17-
/>
18-
);
19-
}
20-
21-
ColorInput.displayName = "Form.ColorInput";
7+
export default ColorInput;

client/src/global/components/form/ColorInput/styles.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import styled from "@emotion/styled";
2-
import BaseInput from "../BaseInput";
2+
import { FormBaseInput } from "../BaseInput";
3+
4+
export const ColorInput = styled(FormBaseInput)`
5+
--ColorInput-default-color: ${({ $defaultColor }) => $defaultColor};
36
4-
export const ColorInput = styled(BaseInput)`
57
input[type="color"] {
68
width: 24px;
79
height: 24px;

0 commit comments

Comments
 (0)