-
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 component
- Loading branch information
1 parent
90c1e7d
commit 776891f
Showing
52 changed files
with
1,545 additions
and
25 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 |
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
/** | ||
* 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 { View } from '../../view'; | ||
import { contextConnect, WordPressComponentProps } from '../../ui/context'; | ||
import { useBorderBoxControl } from './hook'; | ||
|
||
import type { BorderBoxControlProps } from '../types'; | ||
|
||
const BorderBoxControl = ( | ||
props: WordPressComponentProps< BorderBoxControlProps, 'div' >, | ||
forwardedRef: React.Ref< any > | ||
) => { | ||
const { | ||
className, | ||
colors, | ||
hasMixedBorders, | ||
isLinked, | ||
linkedControlClassName, | ||
onLinkedChange, | ||
onSplitChange, | ||
toggleLinked, | ||
value, | ||
__experimentalHasMultipleOrigins, | ||
__experimentalIsRenderedInSidebar, | ||
...otherProps | ||
} = useBorderBoxControl( props ); | ||
|
||
return ( | ||
<View className={ className } { ...otherProps } ref={ forwardedRef }> | ||
<HStack alignment={ 'start' } expanded={ true } spacing={ 3 }> | ||
{ isLinked ? ( | ||
<BorderControl | ||
className={ linkedControlClassName } | ||
colors={ colors } | ||
onChange={ onLinkedChange } | ||
placeholder={ | ||
hasMixedBorders ? __( 'Mixed' ) : undefined | ||
} | ||
value={ hasMixedBorders ? undefined : value?.top } | ||
withSlider={ true } | ||
width={ '110px' } | ||
__experimentalHasMultipleOrigins={ | ||
__experimentalHasMultipleOrigins | ||
} | ||
__experimentalIsRenderedInSidebar={ | ||
__experimentalIsRenderedInSidebar | ||
} | ||
/> | ||
) : ( | ||
<SplitBorderControl | ||
colors={ colors } | ||
onChange={ onSplitChange } | ||
value={ value } | ||
__experimentalHasMultipleOrigins={ | ||
__experimentalHasMultipleOrigins | ||
} | ||
__experimentalIsRenderedInSidebar={ | ||
__experimentalIsRenderedInSidebar | ||
} | ||
/> | ||
) } | ||
<LinkedButton onClick={ toggleLinked } isLinked={ isLinked } /> | ||
</HStack> | ||
</View> | ||
); | ||
}; | ||
|
||
const ConnectedBorderBoxControl = contextConnect( | ||
BorderBoxControl, | ||
'BorderBoxControl' | ||
); | ||
|
||
export default ConnectedBorderBoxControl; |
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo, useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as styles from '../styles'; | ||
import { hasMixedBorders } from '../utils'; | ||
import { useContextSystem, WordPressComponentProps } from '../../ui/context'; | ||
import { useCx } from '../../utils/hooks/use-cx'; | ||
|
||
import type { Border } from '../../border-control/types'; | ||
import type { BordersKey, BorderBoxControlProps } from '../types'; | ||
|
||
export function useBorderBoxControl( | ||
props: WordPressComponentProps< BorderBoxControlProps, 'div' > | ||
) { | ||
const { className, onChange, value, ...otherProps } = useContextSystem( | ||
props, | ||
'BorderBoxControl' | ||
); | ||
|
||
const mixedBorders = hasMixedBorders( value ); | ||
const [ isLinked, setIsLinked ] = useState( ! mixedBorders ); | ||
const toggleLinked = () => setIsLinked( ! isLinked ); | ||
|
||
const onLinkedChange = ( newBorder: Border | undefined ) => { | ||
if ( ! newBorder ) { | ||
onChange( undefined ); | ||
} | ||
|
||
onChange( { | ||
top: newBorder, | ||
right: newBorder, | ||
bottom: newBorder, | ||
left: newBorder, | ||
} ); | ||
}; | ||
|
||
const onSplitChange = ( | ||
newBorder: Border | undefined, | ||
side: BordersKey | ||
) => { | ||
onChange?.( { | ||
...value, | ||
[ side ]: 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, | ||
value, | ||
}; | ||
} |
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 |
42 changes: 42 additions & 0 deletions
42
packages/components/src/border-box-control/linked-button/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,42 @@ | ||
/** | ||
* 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 { useLinkedButton } from './hook'; | ||
|
||
import type { LinkedButtonProps } from '../types'; | ||
|
||
const LinkedButton = ( | ||
props: WordPressComponentProps< LinkedButtonProps, 'div' >, | ||
forwardedRef: React.Ref< any > | ||
) => { | ||
const { className, isLinked, ...buttonProps } = useLinkedButton( 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 ConnectedLinkedButton = contextConnect( LinkedButton, 'LinkedButton' ); | ||
export default ConnectedLinkedButton; |
30 changes: 30 additions & 0 deletions
30
packages/components/src/border-box-control/linked-button/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 { LinkedButtonProps } from '../types'; | ||
|
||
export function useLinkedButton( | ||
props: WordPressComponentProps< LinkedButtonProps, 'div' > | ||
) { | ||
const { className, ...otherProps } = useContextSystem( | ||
props, | ||
'LinkedButton' | ||
); | ||
|
||
// Generate class names. | ||
const cx = useCx(); | ||
const classes = useMemo( () => { | ||
return cx( styles.LinkedButton, className ); | ||
}, [ className ] ); | ||
|
||
return { ...otherProps, className: classes }; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/components/src/border-box-control/linked-button/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'; |
Oops, something went wrong.