-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft combined border controls components
- Loading branch information
1 parent
5bad8e8
commit a650d77
Showing
52 changed files
with
2,565 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/components/src/border-box-control/border-box-control/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# BorderBoxControl | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
<br /> | ||
|
||
## Development guidelines | ||
## Usage | ||
## Props |
107 changes: 107 additions & 0 deletions
107
packages/components/src/border-box-control/border-box-control/component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import LinkedButton from '../linked-button'; | ||
import SplitBorderControl from '../split-border-control'; | ||
import { BorderControl } from '../../border-control'; | ||
import { HStack } from '../../h-stack'; | ||
import { StyledLabel } from '../../base-control/styles/base-control-styles'; | ||
import { View } from '../../view'; | ||
import { VisuallyHidden } from '../../visually-hidden'; | ||
import { contextConnect, WordPressComponentProps } from '../../ui/context'; | ||
import { useBorderBoxControl } from './hook'; | ||
|
||
import type { BorderBoxControlProps, BorderLabelProps } from '../types'; | ||
|
||
const BorderLabel = ( props: BorderLabelProps ) => { | ||
const { label, hideLabelFromVision } = props; | ||
|
||
if ( ! label ) { | ||
return null; | ||
} | ||
|
||
return hideLabelFromVision ? ( | ||
<VisuallyHidden as="label">{ label }</VisuallyHidden> | ||
) : ( | ||
<StyledLabel>{ label }</StyledLabel> | ||
); | ||
}; | ||
|
||
const BorderBoxControl = ( | ||
props: WordPressComponentProps< BorderBoxControlProps, 'div' >, | ||
forwardedRef: React.Ref< any > | ||
) => { | ||
const { | ||
className, | ||
colors, | ||
hasMixedBorders, | ||
hideLabelFromVision, | ||
isLinked, | ||
label, | ||
linkedControlClassName, | ||
linkedValue, | ||
onLinkedChange, | ||
onSplitChange, | ||
splitValue, | ||
toggleLinked, | ||
__experimentalHasMultipleOrigins, | ||
__experimentalIsRenderedInSidebar, | ||
...otherProps | ||
} = useBorderBoxControl( props ); | ||
|
||
return ( | ||
<View className={ className } { ...otherProps } ref={ forwardedRef }> | ||
<BorderLabel | ||
label={ label } | ||
hideLabelFromVision={ hideLabelFromVision } | ||
/> | ||
<HStack alignment={ 'start' } expanded={ true } spacing={ 3 }> | ||
{ isLinked ? ( | ||
<BorderControl | ||
className={ linkedControlClassName } | ||
colors={ colors } | ||
onChange={ onLinkedChange } | ||
placeholder={ | ||
hasMixedBorders ? __( 'Mixed' ) : undefined | ||
} | ||
shouldSanitizeBorder={ false } // This component will handle that. | ||
value={ linkedValue } | ||
withSlider={ true } | ||
width={ '110px' } | ||
__experimentalHasMultipleOrigins={ | ||
__experimentalHasMultipleOrigins | ||
} | ||
__experimentalIsRenderedInSidebar={ | ||
__experimentalIsRenderedInSidebar | ||
} | ||
/> | ||
) : ( | ||
<SplitBorderControl | ||
colors={ colors } | ||
onChange={ onSplitChange } | ||
value={ splitValue } | ||
__experimentalHasMultipleOrigins={ | ||
__experimentalHasMultipleOrigins | ||
} | ||
__experimentalIsRenderedInSidebar={ | ||
__experimentalIsRenderedInSidebar | ||
} | ||
/> | ||
) } | ||
<LinkedButton onClick={ toggleLinked } isLinked={ isLinked } /> | ||
</HStack> | ||
</View> | ||
); | ||
}; | ||
|
||
const ConnectedBorderBoxControl = contextConnect( | ||
BorderBoxControl, | ||
'BorderBoxControl' | ||
); | ||
|
||
export default ConnectedBorderBoxControl; |
119 changes: 119 additions & 0 deletions
119
packages/components/src/border-box-control/border-box-control/hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as styles from '../styles'; | ||
import { | ||
getBorderDiff, | ||
getCommonBorder, | ||
getSplitBorders, | ||
hasMixedBorders, | ||
hasSplitBorders, | ||
isCompleteBorder, | ||
isEmptyBorder, | ||
} from '../utils'; | ||
import { useContextSystem, WordPressComponentProps } from '../../ui/context'; | ||
import { useCx } from '../../utils/hooks/use-cx'; | ||
|
||
import type { Border } from '../../border-control/types'; | ||
import type { Borders, BorderSide, BorderBoxControlProps } from '../types'; | ||
|
||
export function useBorderBoxControl( | ||
props: WordPressComponentProps< BorderBoxControlProps, 'div' > | ||
) { | ||
const { className, onChange, value, ...otherProps } = useContextSystem( | ||
props, | ||
'BorderBoxControl' | ||
); | ||
|
||
const mixedBorders = hasMixedBorders( value ); | ||
const splitBorders = hasSplitBorders( value ); | ||
|
||
const linkedValue = splitBorders | ||
? getCommonBorder( value as Borders | undefined ) | ||
: ( value as Border ); | ||
|
||
const splitValue = splitBorders | ||
? ( value as Borders ) | ||
: getSplitBorders( value as Border | undefined ); | ||
|
||
const [ isLinked, setIsLinked ] = useState( ! mixedBorders ); | ||
const toggleLinked = () => setIsLinked( ! isLinked ); | ||
|
||
const onLinkedChange = ( newBorder: Border | undefined ) => { | ||
if ( ! newBorder ) { | ||
return onChange( undefined ); | ||
} | ||
|
||
// If we have all props defined on the new border apply it. | ||
if ( ! mixedBorders || isCompleteBorder( newBorder ) ) { | ||
return onChange( | ||
isEmptyBorder( newBorder ) ? undefined : newBorder | ||
); | ||
} | ||
|
||
// If we had mixed borders we might have had some shared border props | ||
// that we need to maintain. For example; we could have mixed borders | ||
// with all the same color but different widths. Then from the linked | ||
// control we change the color. We should keep the separate widths. | ||
const changes = getBorderDiff( | ||
linkedValue as Border, | ||
newBorder as Border | ||
); | ||
const updatedBorders = { | ||
top: { ...( value as Borders )?.top, ...changes }, | ||
right: { ...( value as Borders )?.right, ...changes }, | ||
bottom: { ...( value as Borders )?.bottom, ...changes }, | ||
left: { ...( value as Borders )?.left, ...changes }, | ||
}; | ||
|
||
if ( hasMixedBorders( updatedBorders ) ) { | ||
return onChange( updatedBorders ); | ||
} | ||
|
||
const filteredResult = isEmptyBorder( updatedBorders.top ) | ||
? undefined | ||
: updatedBorders.top; | ||
|
||
onChange( filteredResult ); | ||
}; | ||
|
||
const onSplitChange = ( | ||
newBorder: Border | undefined, | ||
side: BorderSide | ||
) => { | ||
const updatedBorders = { ...splitValue, [ side ]: newBorder }; | ||
|
||
if ( hasMixedBorders( updatedBorders ) ) { | ||
onChange( updatedBorders ); | ||
} else { | ||
onChange( newBorder ); | ||
} | ||
}; | ||
|
||
const cx = useCx(); | ||
const classes = useMemo( () => { | ||
return cx( styles.BorderBoxControl, className ); | ||
}, [ className ] ); | ||
|
||
const linkedControlClassName = useMemo( () => { | ||
return cx( styles.LinkedBorderControl ); | ||
}, [] ); | ||
|
||
return { | ||
...otherProps, | ||
className: classes, | ||
hasMixedBorders: mixedBorders, | ||
isLinked, | ||
linkedControlClassName, | ||
onLinkedChange, | ||
onSplitChange, | ||
toggleLinked, | ||
linkedValue, | ||
splitValue, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/components/src/border-box-control/border-box-control/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as BorderBoxControl } from './component'; | ||
export { useBorderBoxControl } from './hook'; |
10 changes: 10 additions & 0 deletions
10
packages/components/src/border-box-control/border-visualizer/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# BorderVisualizer | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
<br /> | ||
|
||
## Development guidelines | ||
## Usage | ||
## Props |
35 changes: 35 additions & 0 deletions
35
packages/components/src/border-box-control/border-visualizer/component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { useBorderVisualizer } from './hook'; | ||
|
||
import type { BorderVisualizerProps } from '../types'; | ||
|
||
const BorderVisualizer = ( | ||
props: WordPressComponentProps< BorderVisualizerProps, 'div' >, | ||
forwardedRef: React.Ref< any > | ||
) => { | ||
const { value, ...otherProps } = useBorderVisualizer( 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 ConnectedBorderVisualizer = contextConnect( | ||
BorderVisualizer, | ||
'BorderVisualizer' | ||
); | ||
export default ConnectedBorderVisualizer; |
30 changes: 30 additions & 0 deletions
30
packages/components/src/border-box-control/border-visualizer/hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { BorderVisualizerProps } from '../types'; | ||
|
||
export function useBorderVisualizer( | ||
props: WordPressComponentProps< BorderVisualizerProps, 'div' > | ||
) { | ||
const { className, ...otherProps } = useContextSystem( | ||
props, | ||
'BorderVisualizer' | ||
); | ||
|
||
// Generate class names. | ||
const cx = useCx(); | ||
const classes = useMemo( () => { | ||
return cx( styles.BorderVisualizer, className ); | ||
}, [ className ] ); | ||
|
||
return { ...otherProps, className: classes }; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/components/src/border-box-control/border-visualizer/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as BorderBoxControl } from './border-box-control/component'; | ||
export { useBorderBoxControl } from './border-box-control/hook'; |
10 changes: 10 additions & 0 deletions
10
packages/components/src/border-box-control/linked-button/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# LinkedButton | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
<br /> | ||
|
||
## Development guidelines | ||
## Usage | ||
## Props |
Oops, something went wrong.