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

BoxControl: Add ability to render custom icon to represent sides #25297

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions packages/components/src/box-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ const Example = () => {

## Props

### iconComponent

A custom icon to render that visualizes the selected `BoxControl` sides.

- Type: `React.Component`
- Required: No

### inputProps

Props for the internal [InputControl](../input-control) components.
Expand Down
9 changes: 5 additions & 4 deletions packages/components/src/box-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { noop } from 'lodash';
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { createElement, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -45,6 +45,7 @@ function useUniqueId( idProp ) {
}
export default function BoxControl( {
id: idProp,
iconComponent = BoxControlIcon,
inputProps = defaultInputProps,
onChange = noop,
onChangeShowVisualizer = noop,
Expand Down Expand Up @@ -110,6 +111,8 @@ export default function BoxControl( {
values: inputValues,
};

const BoxIcon = createElement( iconComponent, { side } );

return (
<Root id={ id } role="region" aria-labelledby={ headingId }>
<Header className="component-box-control__header">
Expand All @@ -134,9 +137,7 @@ export default function BoxControl( {
</FlexItem>
</Header>
<HeaderControlWrapper className="component-box-control__header-control-wrapper">
<FlexItem>
<BoxControlIcon side={ side } />
</FlexItem>
<FlexItem>{ BoxIcon }</FlexItem>
{ isLinked && (
<FlexBlock>
<AllInputControl { ...inputControlProps } />
Expand Down
37 changes: 37 additions & 0 deletions packages/components/src/box-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,40 @@ const Box = styled.div`
const Content = styled.div`
padding: 20px;
`;

function CustomBoxIcon( { side } ) {
let icon = '✊';
switch ( side ) {
case 'top':
icon = '☝️';
break;
case 'bottom':
icon = '👇';
break;
case 'left':
icon = '👈';
break;
case 'right':
icon = '👉';
break;
}
return (
<div
style={ {
fontSize: 18,
lineHeight: 1,
width: 24,
height: 24,
padding: 2,
} }
>
<span role="img" alt="icon">
{ icon }
</span>
</div>
);
}

export const customIcon = () => {
return <BoxControl iconComponent={ CustomBoxIcon } />;
};