Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add BorderBoxControl component
This provides a component through which you can configure separate borders for individual sides.
  • Loading branch information
aaronrobertshaw committed Mar 24, 2022
commit ac742b3a1ae0ae7049c7dfc52e8d8ad597036982
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,12 @@
"markdown_source": "../packages/components/src/base-field/README.md",
"parent": "components"
},
{
"title": "BorderBoxControl",
"slug": "border-box-control",
"markdown_source": "../packages/components/src/border-box-control/border-box-control/README.md",
"parent": "components"
},
{
"title": "BorderControl",
"slug": "border-control",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import { link, linkOff } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import Button from '../../button';
import Tooltip from '../../tooltip';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { useBorderBoxControlLinkedButton } from './hook';

import type { LinkedButtonProps } from '../types';

const BorderBoxControlLinkedButton = (
props: WordPressComponentProps< LinkedButtonProps, 'div' >,
forwardedRef: React.Ref< any >
) => {
const {
className,
isLinked,
...buttonProps
} = useBorderBoxControlLinkedButton( props );
const label = isLinked ? __( 'Unlink sides' ) : __( 'Link sides' );

return (
<Tooltip text={ label }>
<div className={ className }>
<Button
{ ...buttonProps }
variant={ isLinked ? 'primary' : 'secondary' }
isSmall
icon={ isLinked ? link : linkOff }
iconSize={ 16 }
aria-label={ label }
ref={ forwardedRef }
/>
</div>
</Tooltip>
);
};

const ConnectedBorderBoxControlLinkedButton = contextConnect(
BorderBoxControlLinkedButton,
'BorderBoxControlLinkedButton'
);
export default ConnectedBorderBoxControlLinkedButton;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import * as styles from '../styles';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useCx } from '../../utils/hooks/use-cx';

import type { LinkedButtonProps } from '../types';

export function useBorderBoxControlLinkedButton(
props: WordPressComponentProps< LinkedButtonProps, 'div' >
) {
const { className, ...otherProps } = useContextSystem(
props,
'BorderBoxControlLinkedButton'
);

// Generate class names.
const cx = useCx();
const classes = useMemo( () => {
return cx( styles.BorderBoxControlLinkedButton, className );
}, [ className ] );

return { ...otherProps, className: classes };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import BorderBoxControlVisualizer from '../border-box-control-visualizer';
import { BorderControl } from '../../border-control';
import { View } from '../../view';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { useBorderBoxControlSplitControls } from './hook';

import type { SplitControlsProps } from '../types';

const BorderBoxControlSplitControls = (
props: WordPressComponentProps< SplitControlsProps, 'div' >,
forwardedRef: React.Ref< any >
) => {
const {
centeredClassName,
colors,
disableCustomColors,
enableAlpha,
enableStyle,
onChange,
value,
__experimentalHasMultipleOrigins,
__experimentalIsRenderedInSidebar,
...otherProps
} = useBorderBoxControlSplitControls( props );

const sharedBorderControlProps = {
colors,
disableCustomColors,
enableAlpha,
enableStyle,
isCompact: true,
__experimentalHasMultipleOrigins,
__experimentalIsRenderedInSidebar,
};

return (
<View { ...otherProps } ref={ forwardedRef }>
<BorderBoxControlVisualizer value={ value } />
<BorderControl
className={ centeredClassName }
onChange={ ( newBorder ) => onChange( newBorder, 'top' ) }
value={ value?.top }
{ ...sharedBorderControlProps }
/>
<BorderControl
onChange={ ( newBorder ) => onChange( newBorder, 'left' ) }
value={ value?.left }
{ ...sharedBorderControlProps }
/>
<BorderControl
onChange={ ( newBorder ) => onChange( newBorder, 'right' ) }
value={ value?.right }
{ ...sharedBorderControlProps }
/>
<BorderControl
className={ centeredClassName }
onChange={ ( newBorder ) => onChange( newBorder, 'bottom' ) }
value={ value?.bottom }
{ ...sharedBorderControlProps }
/>
</View>
);
};

const ConnectedBorderBoxControlSplitControls = contextConnect(
BorderBoxControlSplitControls,
'BorderBoxControlSplitControls'
);
export default ConnectedBorderBoxControlSplitControls;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import * as styles from '../styles';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useCx } from '../../utils/hooks/use-cx';

import type { SplitControlsProps } from '../types';

export function useBorderBoxControlSplitControls(
props: WordPressComponentProps< SplitControlsProps, 'div' >
) {
const { className, ...otherProps } = useContextSystem(
props,
'BorderBoxControlSplitControls'
);

// Generate class names.
const cx = useCx();
const classes = useMemo( () => {
return cx( styles.BorderBoxControlSplitControls, className );
}, [ className ] );

const centeredClassName = useMemo( () => {
return cx( styles.CenteredBorderControl, className );
}, [] );

return { ...otherProps, centeredClassName, className: classes };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { View } from '../../view';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { getClampedWidthBorderStyle } from '../utils';
import { useBorderBoxControlVisualizer } from './hook';

import type { VisualizerProps } from '../types';

const BorderBoxControlVisualizer = (
props: WordPressComponentProps< VisualizerProps, 'div' >,
forwardedRef: React.Ref< any >
) => {
const { value, ...otherProps } = useBorderBoxControlVisualizer( props );
const styles = {
borderTop: getClampedWidthBorderStyle( value?.top ),
borderRight: getClampedWidthBorderStyle( value?.right ),
borderBottom: getClampedWidthBorderStyle( value?.bottom ),
borderLeft: getClampedWidthBorderStyle( value?.left ),
};

return <View { ...otherProps } ref={ forwardedRef } style={ styles } />;
};

const ConnectedBorderBoxControlVisualizer = contextConnect(
BorderBoxControlVisualizer,
'BorderBoxControlVisualizer'
);
export default ConnectedBorderBoxControlVisualizer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import * as styles from '../styles';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useCx } from '../../utils/hooks/use-cx';

import type { VisualizerProps } from '../types';

export function useBorderBoxControlVisualizer(
props: WordPressComponentProps< VisualizerProps, 'div' >
) {
const { className, ...otherProps } = useContextSystem(
props,
'BorderBoxControlVisualizer'
);

// Generate class names.
const cx = useCx();
const classes = useMemo( () => {
return cx( styles.BorderBoxControlVisualizer, className );
}, [ className ] );

return { ...otherProps, className: classes };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './component';
Loading