ToolJet/frontend/src/Editor/Components/Button.jsx
Alvaro Cabrera Durán c824eb362b
chore: apply primary-color in more places (#4025)
* chore: apply primary-color in more places, ensure svg use currentColor too

* fix: remove hard-coded colors to use defaults

* fix: those images were intruducing noise when switch to dark-mode

* fix: setup colors for rc-slider

* fix: empty all predefined-colors to use defaults

* Use primary-rgb-darker
2022-09-29 13:33:01 +05:30

110 lines
2.7 KiB
JavaScript

import React, { useEffect, useState } from 'react';
import cx from 'classnames';
var tinycolor = require('tinycolor2');
export const Button = function Button({
height,
properties,
styles,
fireEvent,
registerAction,
component,
currentState,
}) {
const { backgroundColor, textColor, borderRadius, loaderColor, disabledState } = styles;
const [label, setLabel] = useState(properties.text);
const [disable, setDisable] = useState(disabledState);
const [visibility, setVisibility] = useState(styles.visibility);
const [loading, setLoading] = useState(properties.loadingState);
useEffect(() => setLabel(properties.text), [properties.text]);
useEffect(() => {
disable !== disabledState && setDisable(disabledState);
}, [disabledState]);
useEffect(() => {
visibility !== styles.visibility && setVisibility(styles.visibility);
}, [styles.visibility]);
useEffect(() => {
loading !== properties.loadingState && setLoading(properties.loadingState);
}, [properties.loadingState]);
const computedStyles = {
backgroundColor,
color: textColor,
width: '100%',
borderRadius: `${borderRadius}px`,
height,
display: visibility ? '' : 'none',
'--tblr-btn-color-darker': tinycolor(backgroundColor).darken(8).toString(),
'--loader-color': tinycolor(loaderColor ?? '#fff').toString(),
};
registerAction('click', async function () {
fireEvent('onClick');
});
registerAction(
'setText',
async function (text) {
setLabel(text);
},
[setLabel]
);
registerAction(
'disable',
async function (value) {
setDisable(value);
},
[setDisable]
);
registerAction(
'visibility',
async function (value) {
setVisibility(value);
},
[setVisibility]
);
registerAction(
'loading',
async function (value) {
setLoading(value);
},
[setLoading]
);
const hasCustomBackground = backgroundColor.charAt() === '#';
if (hasCustomBackground) {
computedStyles['--tblr-btn-color-darker'] = tinycolor(backgroundColor).darken(8).toString();
}
return (
<div className="widget-button">
<button
disabled={disable}
className={cx('jet-button btn btn-primary p-1 overflow-hidden', {
'btn-loading': loading,
'btn-custom': hasCustomBackground,
})}
style={computedStyles}
onClick={(event) => {
event.stopPropagation();
fireEvent('onClick');
}}
onMouseOver={(event) => {
event.stopPropagation();
fireEvent('onHover');
}}
data-cy={`draggable-widget-${String(component.name).toLowerCase()}`}
>
{label}
</button>
</div>
);
};