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

Color component #561

Merged
merged 4 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ export const Background: React.FC<CssProprtyComponentType> = (props) => {
{/**Background Color */}
{backgroundTypes[selectedTypeIndex].color && (
<div style={{ display: "flex", alignItems: "center" }}>
<span style={styles.optionName}>Color</span>
<ColorComponent
name="Background Color"
styleItem="backgroundColor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,6 @@ export const Border: React.FC<CssProprtyComponentType> = (props) => {
</div>
</div>
<div style={styles.gridInputContainer}>
<div style={styles.optionName}>
<BC />
</div>
<ColorComponent
name="Border Color"
styleItem={setBorderTypeColor()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ const styles: { [key: string]: React.CSSProperties } = {
},
optionName: {
...smallText,
width: "1.5rem",
color: "white",
display: "flex",
alignItems: "center",
width: "4rem",
color: "white",
height: "25px",
justifyContent: "center",
},
gridContainer: {
...smallText,
color: gray400,
display: "grid",
gridTemplateColumns: "15px 60px 40px 40px",
rowGap: "1rem",
textAlign: "center",
columnGap: "1rem",
},
};

Expand Down Expand Up @@ -102,8 +111,9 @@ export const rgb2hex = ({ r, g, b, a }: Color["rgb"]) => {

export const getOpacityValue = (hex: Color["hex"]) => {
let convertedRgbValue = hex2rgb(hex);
console.log(convertedRgbValue);
if (convertedRgbValue.a === undefined) {
return "";
return "0";
} else if (convertedRgbValue.a) {
Math.ceil(convertedRgbValue.a * 100) - convertedRgbValue.a * 100 < 0.5
? (convertedRgbValue.a = Math.ceil(convertedRgbValue.a * 100))
Expand All @@ -116,6 +126,20 @@ export const getOpacityValue = (hex: Color["hex"]) => {
};

export const ColorComponent: React.FC<ColorComponentProps> = (props) => {
const [opacityValue, setOpacityValue] = useState<string>(
props.styles[props.styleItem]
? getOpacityValue(String(props.styles[props.styleItem]))
: "100"
);

useEffect(() => {
setOpacityValue(
props.styles[props.styleItem]
? getOpacityValue(String(props.styles[props.styleItem]))
: "100"
);
}, [props]);

const toggleTransparencyChange = (styleItem: keyof React.CSSProperties) => {
props.patchCb({
property: {
Expand All @@ -129,36 +153,33 @@ export const ColorComponent: React.FC<ColorComponentProps> = (props) => {
});
};

const handleOpacityInputChange = (
e: React.ChangeEvent<HTMLInputElement>,
styleItem: keyof React.CSSProperties
) => {
const handleOpacityInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
parseInt(e.target.value) > 100
? setOpacityValue("100")
: setOpacityValue(e.target.value);
};

props.patchCb({
property: {
styles: {
[styleItem]:
e.target.value !== ""
? handleOpacityChange(
String(Number(e.target.value) / 100),
String(props.styles[props.styleItem])
)
: handleOpacityChange(
String(e.target.value),
String(props.styles[props.styleItem])
),
},
},
});
const opacityDisabledHandler = (Color: string) => {
let Flag;
Color === "undefined" ? (Flag = true) : (Flag = false);
return Flag;
};

const opacityHelper = useCallback((opacity: string) => {
let opacityHelperValue;
opacity === ""
? (opacityHelperValue = 0)
: (opacityHelperValue = Number(opacity));
return opacityHelperValue;
}, []);

const handleOpacityChange = useCallback(
(opacityValue: string, hex: Color["hex"]) => {
console.log(opacityValue);
let convertedRgbValue = hex2rgb(hex);
console.log(convertedRgbValue);
if (opacityValue === "") {
convertedRgbValue.a = 0;
return rgb2hex(convertedRgbValue);
} else if (opacityHelper(opacityValue) >= 1) {
convertedRgbValue.a = 1;
} else if (opacityHelper(opacityValue) < 0) {
Expand All @@ -168,56 +189,77 @@ export const ColorComponent: React.FC<ColorComponentProps> = (props) => {
}
return rgb2hex(convertedRgbValue);
},
[]
[opacityHelper]
);
const opacityHelper = (opacityValue: string) => {
let opacityHelperValue;
opacityValue === ""
? (opacityHelperValue = 100)
: (opacityHelperValue = Number(opacityValue));
return opacityHelperValue;
};

const opacityDisabledHandler = (Color: string) => {
let Flag;
Color === "undefined" ? (Flag = true) : (Flag = false);
return Flag;
const applyOpacity = () => {
if (opacityValue === "") {
props.patchCb({
property: {
styles: {
[props.styleItem]: handleOpacityChange(
String(opacityValue),
String(props.styles[props.styleItem])
),
},
},
});
} else {
props.patchCb({
property: {
styles: {
[props.styleItem]: handleOpacityChange(
String(Number(opacityValue) / 100),
String(props.styles[props.styleItem])
),
},
},
});
}
};

const [opacityValue, setOpacityValue] = useState<string>(
props.styles[props.styleItem]
? getOpacityValue(String(props.styles[props.styleItem]))
: "100"
);
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter") {
applyOpacity();
}
};

const [isOpacityDisabled, setIsOpacityDisabled] = useState<boolean>(
opacityDisabledHandler(String(props.styles[props.styleItem]))
);

useEffect(() => {
setOpacityValue(
props.styles[props.styleItem]
? getOpacityValue(String(props.styles[props.styleItem]))
: "100"
);
}, [props]);

useEffect(() => {
setIsOpacityDisabled(
opacityDisabledHandler(String(props.styles[props.styleItem]))
);
}, [props]);

const [colorVal, setColorVal] = useState<string>("");

return (
<div style={{ display: "flex", alignItems: "center" }}>
<div style={styles.gridContainer}>
<div
style={styles.optionName}
onClick={() => {
props.openPalette(props.styleItem, props.name);
}}
style={{ width: "55px", marginRight: "10px" }}
>
<div
style={
props.styles[props.styleItem] === undefined ||
props.styles[props.styleItem] === ""
? {
height: "10px",
width: "10px",
backgroundColor: `black`,
}
: {
height: "10px",
width: "10px",
backgroundColor: `${props.styles[props.styleItem]}`,
}
}
></div>
</div>
<div style={{ width: "55px", marginRight: "10px" }}>
<ColorInput
styleItem={props.styleItem}
styles={props.styles}
Expand All @@ -226,10 +268,6 @@ export const ColorComponent: React.FC<ColorComponentProps> = (props) => {
getOpacityValue={getOpacityValue}
setOpacityValue={setOpacityValue}
rgb2hex={rgb2hex}
value={colorVal}
setValue={setColorVal}
applyFlag={true}
index={0}
/>
</div>
<div style={{ width: "45px", marginRight: "10px" }}>
Expand All @@ -238,7 +276,9 @@ export const ColorComponent: React.FC<ColorComponentProps> = (props) => {
type="text"
value={opacityValue}
disabled={isOpacityDisabled}
onChange={(e) => handleOpacityInputChange(e, props.styleItem)}
onChange={(e) => handleOpacityInputChange(e)}
onBlur={(e) => applyOpacity()}
onKeyDown={(e) => handleKeyDown(e)}
style={styles.inputContainerBox}
placeholder="100"
/>
Expand Down
Loading