Skip to content

Commit cda2b8f

Browse files
committed
[F] Add simple color input
1 parent d56f4d7 commit cda2b8f

File tree

5 files changed

+92
-38
lines changed

5 files changed

+92
-38
lines changed

client/src/backend/containers/settings/Theme.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,35 +90,31 @@ export class SettingsThemeContainer extends PureComponent {
9090
/>
9191
</Form.FieldGroup>
9292
<Form.FieldGroup label={t("settings.theme.colors_header")}>
93-
<Form.TextInput
93+
<Form.ColorInput
9494
label={t("settings.theme.accent_color_label")}
9595
name="attributes[theme][accentColor]"
96-
placeholder="#52e3ac"
96+
defaultValue="#52e3ac"
9797
instructions={t("settings.theme.accent_color_instructions")}
98-
wide
9998
/>
100-
<Form.TextInput
99+
<Form.ColorInput
101100
label={t("settings.theme.foreground_color_label")}
102101
name="attributes[theme][headerForegroundColor]"
103-
placeholder="#ffffff"
102+
defaultValue="#ffffff"
104103
instructions={t("settings.theme.foreground_color_instructions")}
105-
wide
106104
/>
107-
<Form.TextInput
105+
<Form.ColorInput
108106
label={t("settings.theme.active_foreground_color_label")}
109107
name="attributes[theme][headerForegroundActiveColor]"
110-
placeholder="#363636"
108+
defaultValue="#363636"
111109
instructions={t(
112110
"settings.theme.active_foreground_color_instructions"
113111
)}
114-
wide
115112
/>
116-
<Form.TextInput
113+
<Form.ColorInput
117114
label={t("settings.theme.background_color_label")}
118115
name="attributes[theme][headerBackgroundColor]"
119-
placeholder="#696969"
116+
defaultValue="#696969"
120117
instructions={t("settings.theme.background_color_instructions")}
121-
wide
122118
/>
123119
</Form.FieldGroup>
124120
<Form.FieldGroup label={t("settings.theme.typography_header")}>

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

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ export class FormBaseInput extends PureComponent {
9191
return props.renderValue(props.value);
9292
}
9393

94+
renderInputComponent() {
95+
const { id, idForError, idForInstructions } = this.props;
96+
97+
const InputComponent =
98+
this.context?.styleType === "secondary"
99+
? Styled.SecondaryInput
100+
: Styled.PrimaryInput;
101+
102+
return (
103+
<InputComponent
104+
ref={input => {
105+
this.inputElement = input;
106+
}}
107+
id={id}
108+
name={this.props.name}
109+
disabled={this.props.isDisabled}
110+
type={this.props.inputType ?? this.props.type}
111+
placeholder={this.props.placeholder}
112+
onChange={this.props.onChange}
113+
onKeyDown={e => {
114+
if (this.props.onKeyDown) this.props.onKeyDown(e, this.inputElement);
115+
}}
116+
value={this.renderValue(this.props)}
117+
aria-describedby={`${idForError || ""} ${idForInstructions || ""}`}
118+
autoComplete={this.props.autoComplete}
119+
defaultValue={this.props.defaultValue}
120+
required={this.props.required}
121+
/>
122+
);
123+
}
124+
94125
render() {
95126
const {
96127
id,
@@ -107,11 +138,6 @@ export class FormBaseInput extends PureComponent {
107138
});
108139
const Wrapper = buttons ? Styled.WrapperWithActions : Errorable;
109140

110-
const InputComponent =
111-
this.context?.styleType === "secondary"
112-
? Styled.SecondaryInput
113-
: Styled.PrimaryInput;
114-
115141
return (
116142
<Wrapper
117143
className={buttons ? undefined : fieldClasses}
@@ -126,26 +152,14 @@ export class FormBaseInput extends PureComponent {
126152
hasInstructions={isString(instructions)}
127153
styleType={this.context?.styleType}
128154
/>
129-
<InputComponent
130-
ref={input => {
131-
this.inputElement = input;
132-
}}
133-
id={id}
134-
name={this.props.name}
135-
disabled={this.props.isDisabled}
136-
type={this.props.inputType ?? this.props.type}
137-
placeholder={this.props.placeholder}
138-
onChange={this.props.onChange}
139-
onKeyDown={e => {
140-
if (this.props.onKeyDown)
141-
this.props.onKeyDown(e, this.inputElement);
142-
}}
143-
value={this.renderValue(this.props)}
144-
aria-describedby={`${idForError || ""} ${idForInstructions || ""}`}
145-
autoComplete={this.props.autoComplete}
146-
defaultValue={this.props.defaultValue}
147-
required={this.props.required}
148-
/>
155+
{this.props.inputType === "color" ? (
156+
<span className="ColorInput-wrapper">
157+
{this.renderInputComponent()}
158+
<span>{this.renderValue(this.props)}</span>
159+
</span>
160+
) : (
161+
this.renderInputComponent()
162+
)}
149163
{buttons && this.renderButtons(buttons)}
150164
{this.props.instructions && (
151165
<Instructions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useId } from "react";
2+
import PropTypes from "prop-types";
3+
import * as Styled from "./styles";
4+
5+
export default function ColorInput({ defaultValue, ...props }) {
6+
const id = useId();
7+
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";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import styled from "@emotion/styled";
2+
import BaseInput from "../BaseInput";
3+
4+
export const ColorInput = styled(BaseInput)`
5+
input[type="color"] {
6+
width: 24px;
7+
height: 24px;
8+
display: inline-block;
9+
cursor: pointer;
10+
border: none;
11+
}
12+
13+
span.ColorInput-wrapper {
14+
display: flex;
15+
gap: 8px;
16+
align-items: center;
17+
font-family: var(--font-family-sans);
18+
padding-block-end: 8px;
19+
border-bottom: 1px solid var(--input-border-color);
20+
}
21+
`;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Label from "./BaseLabel";
3131
import FieldWrapper from "./FieldWrapper";
3232
import { InputGroupPrimary, InputGroupSecondary } from "./InputGroup/styles";
3333
import DrawerButtons from "./DrawerButtons";
34+
import ColorInput from "./ColorInput";
3435

3536
export default {
3637
CoverUploadPlaceholder,
@@ -65,7 +66,8 @@ export default {
6566
FieldWrapper,
6667
InputGroupPrimary,
6768
InputGroupSecondary,
68-
DrawerButtons
69+
DrawerButtons,
70+
ColorInput
6971
};
7072

7173
export const Unwrapped = {

0 commit comments

Comments
 (0)