-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
BorderBoxControl: use Popover's new anchor prop #43789
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useRef } from '@wordpress/element'; | ||
import { useCallback, useState, useEffect } from '@wordpress/element'; | ||
import { useMergeRefs } from '@wordpress/compose'; | ||
|
||
/** | ||
|
@@ -14,7 +14,7 @@ import { Grid } from '../../grid'; | |
import { contextConnect, WordPressComponentProps } from '../../ui/context'; | ||
import { useBorderBoxControlSplitControls } from './hook'; | ||
|
||
import type { SplitControlsProps } from '../types'; | ||
import type { SplitControlsProps, PopoverPartialProps } from '../types'; | ||
|
||
const BorderBoxControlSplitControls = ( | ||
props: WordPressComponentProps< SplitControlsProps, 'div' >, | ||
|
@@ -36,16 +36,25 @@ const BorderBoxControlSplitControls = ( | |
__next36pxDefaultSize, | ||
...otherProps | ||
} = useBorderBoxControlSplitControls( props ); | ||
const containerRef = useRef(); | ||
const mergedRef = useMergeRefs( [ containerRef, forwardedRef ] ); | ||
const popoverProps = popoverPlacement | ||
? { | ||
const [ popoverProps, setPopoverProps ] = useState< PopoverPartialProps >(); | ||
const [ popoverAnchor, setPopoverAnchor ] = useState< Element >(); | ||
|
||
const containerRef = useCallback( ( node ) => { | ||
setPopoverAnchor( node ?? undefined ); | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( popoverPlacement ) { | ||
setPopoverProps( { | ||
placement: popoverPlacement, | ||
offset: popoverOffset, | ||
anchorRef: containerRef, | ||
anchor: popoverAnchor, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
__unstableShift: true, | ||
} | ||
: undefined; | ||
} ); | ||
} else { | ||
setPopoverProps( undefined ); | ||
} | ||
}, [ popoverPlacement, popoverOffset, popoverAnchor ] ); | ||
|
||
const sharedBorderControlProps = { | ||
colors, | ||
|
@@ -58,6 +67,8 @@ const BorderBoxControlSplitControls = ( | |
__next36pxDefaultSize, | ||
}; | ||
|
||
const mergedRef = useMergeRefs( [ containerRef, forwardedRef ] ); | ||
|
||
return ( | ||
<Grid { ...otherProps } ref={ mergedRef } gap={ 4 }> | ||
<BorderBoxControlVisualizer | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,7 @@ Default.args = { | |
width: '1px', | ||
}, | ||
__next36pxDefaultSize: false, | ||
popoverPlacement: 'right-start', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added But the component's stories definitely need to be rewritten in TypeScript so that they can show all props There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have it on my radar to rewrite the border control component stories in TypeScript and update them to follow the latest approach and guidelines. Unfortunately, I haven't had the bandwidth in the lead-up to 6.1 but hope to in the not-too-distant future. |
||
}; | ||
|
||
const WrapperView = styled.div` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { Placement, ReferenceType } from '@floating-ui/react-dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
@@ -14,6 +19,15 @@ export type AnyBorder = Border | Borders | undefined; | |
export type BorderProp = keyof Border; | ||
export type BorderSide = keyof Borders; | ||
|
||
// TODO: replace with Popover actual props once the component | ||
// gets converted to TypeScript | ||
export type PopoverPartialProps = { | ||
placement?: Placement; | ||
offset?: number; | ||
anchor?: ReferenceType; | ||
__unstableShift?: boolean; | ||
}; | ||
Comment on lines
+24
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be removed and replaced with the original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Working on it #43823 |
||
|
||
export type BorderBoxControlProps = ColorProps & | ||
LabelProps & { | ||
/** | ||
|
@@ -29,7 +43,7 @@ export type BorderBoxControlProps = ColorProps & | |
/** | ||
* The position of the color popovers compared to the control wrapper. | ||
*/ | ||
popoverPlacement?: string; | ||
popoverPlacement?: Placement; | ||
/** | ||
* The space between the popover and the control wrapper. | ||
*/ | ||
|
@@ -103,7 +117,7 @@ export type SplitControlsProps = ColorProps & { | |
/** | ||
* The position of the color popovers compared to the control wrapper. | ||
*/ | ||
popoverPlacement?: string; | ||
popoverPlacement?: Placement; | ||
/** | ||
* The space between the popover and the control wrapper. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: I'm not in love with the
popoverPlacement
andpopoverOffset
props, but these were added as part of #40740 and it's not in the scope of this PR to revisit the APIs ofBorderBoxControl