I have a component:
class PaddingStyle extends Component {
constructor( props ) {
super( ...arguments )
}
render() {
const { paddingTop, paddingRight, paddingBottom, paddingLeft } = this.props.attributes;
const top = paddingTop ? `${paddingTop}px` : 0;
const right = paddingRight ? `${paddingRight}px` : 0;
const bottom = paddingBottom ? `${paddingBottom}px` : 0;
const left = paddingLeft ? `${paddingLeft}px` : 0;
return (
`${top} ${right} ${bottom} ${left}`
)
}
}
export default PaddingStyle;
In another file I am trying to pass what is returned from the component into an inline style of another component:
import PaddingStyle from '../../components/padding/style';
<div
style={{
padding: <PaddingStyle paddingTop={ paddingTop } paddingRight={ paddingRight } paddingBottom={ paddingBottom } paddingLeft={ paddingLeft } />,
}}
>
</div>
I am using a component for the padding because i have to add the style in multiple places. Is there a better approach for this?
UPDATE
I have found a solution
export default function paddingStyle( props ) {
const { paddingTop, paddingRight, paddingBottom, paddingLeft } = props;
const top = paddingTop ? `${paddingTop}px` : 0;
const right = paddingRight ? `${paddingRight}px` : 0;
const bottom = paddingBottom ? `${paddingBottom}px` : 0;
const left = paddingLeft ? `${paddingLeft}px` : 0;
return (
`${top} ${right} ${bottom} ${left}`
)
}