Skip to content

Commit

Permalink
Use internal context system to apply toolbar variant to toolbar dropd…
Browse files Browse the repository at this point in the history
…owns (#51154)

* DropdownMenu: read variant from context

* Toolbar: do not set `variant` prop on popover (rely on context system instead)

* Use named export for better storybook doc generation

* Provide context value in more toolbar components

* Hook Dropdown into context system, update DropdownMenu and CircularOptionPicker

* Remove toolbar variant popover prop where the context system can be used instead

* Add comment where the context system currently doesn't work

* Use placement prop instead of legacy "position" prop

* Add some temporary TODO comments

* Set toolbar variant for Dropdown in the context system

* Remove explicit variant prop from block alignment matrix control

* Toolbar: Only set context in the top-level component

* Remove `toolbar` variant from instances using `Dropdown`

* Simplify Dropdown implementation

* Tidy up types

* Remove toolbar variant storybook example for Popover

* CHANGELOG

* Complete renaming internal type

* Remove duplicate hardcoded classnames(already added by context connexct)
  • Loading branch information
ciampo committed Jun 8, 2023
1 parent bdf728b commit f1d0bd5
Show file tree
Hide file tree
Showing 29 changed files with 176 additions and 170 deletions.
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/alignment-control/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const DEFAULT_ALIGNMENT_CONTROLS = [
];

const POPOVER_PROPS = {
position: 'bottom right',
variant: 'toolbar',
placement: 'bottom-start',
};

function AlignmentUI( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,3 @@ export const BLOCK_ALIGNMENTS_CONTROLS = {
};

export const DEFAULT_CONTROL = 'none';

export const POPOVER_PROPS = {
variant: 'toolbar',
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import {
* Internal dependencies
*/
import useAvailableAlignments from './use-available-alignments';
import {
BLOCK_ALIGNMENTS_CONTROLS,
DEFAULT_CONTROL,
POPOVER_PROPS,
} from './constants';
import { BLOCK_ALIGNMENTS_CONTROLS, DEFAULT_CONTROL } from './constants';

function BlockAlignmentUI( {
value,
Expand Down Expand Up @@ -69,7 +65,6 @@ function BlockAlignmentUI( {
}
: {
toggleProps: { describedBy: __( 'Change alignment' ) },
popoverProps: POPOVER_PROPS,
children: ( { onClose } ) => {
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function BlockAlignmentMatrixControl( props ) {

return (
<Dropdown
popoverProps={ { variant: 'toolbar', placement: 'bottom-start' } }
popoverProps={ { placement: 'bottom-start' } }
renderToggle={ ( { onToggle, isOpen } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import { useShowMoversGestures } from '../block-toolbar/utils';

const POPOVER_PROPS = {
className: 'block-editor-block-settings-menu__popover',
position: 'bottom right',
variant: 'toolbar',
placement: 'bottom-start',
};

function CopyMenuItem( { blocks, onCopy, label } ) {
Expand Down
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => {
className="block-editor-block-switcher"
label={ blockSwitcherLabel }
popoverProps={ {
position: 'bottom right',
variant: 'toolbar',
placement: 'bottom-start',
className: 'block-editor-block-switcher__popover',
} }
icon={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ function BlockVerticalAlignmentUI( {
BLOCK_ALIGNMENTS_CONTROLS[ DEFAULT_CONTROL ];

const UIComponent = isToolbar ? ToolbarGroup : ToolbarDropdownMenu;
const extraProps = isToolbar
? { isCollapsed }
: { popoverProps: { variant: 'toolbar' } };
const extraProps = isToolbar ? { isCollapsed } : {};

return (
<UIComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function DuotoneControl( {
popoverProps={ {
className: 'block-editor-duotone-control__popover',
headerTitle: __( 'Duotone' ),
variant: 'toolbar',
} }
renderToggle={ ( { isOpen, onToggle } ) => {
const openOnArrowDown = ( event ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export const MIN_ZOOM = 100;
export const MAX_ZOOM = 300;
export const POPOVER_PROPS = {
placement: 'bottom-start',
variant: 'toolbar',
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ const MediaReplaceFlow = ( {
multiple = false,
addToGallery,
handleUpload = true,
popoverProps = {
variant: 'toolbar',
},
popoverProps,
} ) => {
const mediaUpload = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().mediaUpload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function PreviewOptions( {
className,
'block-editor-post-preview__dropdown-content'
),
position: 'bottom left',
placement: 'bottom-end',
};
const toggleProps = {
variant: 'tertiary',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { chevronDown } from '@wordpress/icons';
import { orderBy } from '../../../utils/sorting';

const POPOVER_PROPS = {
position: 'bottom right',
variant: 'toolbar',
placement: 'bottom-start',
};

const FormatToolbar = () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/block-editor/src/layouts/flex.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ function FlexLayoutVerticalAlignmentControl( {
);
}

const POPOVER_PROPS = {
placement: 'bottom-start',
};

function FlexLayoutJustifyContentControl( {
layout,
onChange,
Expand All @@ -282,10 +286,7 @@ function FlexLayoutJustifyContentControl( {
allowedControls={ allowedControls }
value={ justifyContent }
onChange={ onJustificationChange }
popoverProps={ {
position: 'bottom right',
variant: 'toolbar',
} }
popoverProps={ POPOVER_PROPS }
/>
);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/block-library/src/buttons/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const ALLOWED_BLOCKS = [ buttonBlockName ];

const layoutProp = { type: 'default', alignments: [] };

const POPOVER_PROPS = {
placement: 'bottom-start',
};

export default function ButtonsEdit( {
attributes: { layout, align },
clientId,
Expand Down Expand Up @@ -137,10 +141,7 @@ export default function ButtonsEdit( {
},
} )
}
popoverProps={ {
position: 'bottom right',
variant: 'toolbar',
} }
popoverProps={ POPOVER_PROPS }
/>
</BlockControls>
) }
Expand Down
3 changes: 1 addition & 2 deletions packages/block-library/src/navigation/edit/leaf-more-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { BlockTitle, store as blockEditorStore } from '@wordpress/block-editor';

const POPOVER_PROPS = {
className: 'block-editor-block-settings-menu__popover',
position: 'bottom right',
variant: 'toolbar',
placement: 'bottom-start',
};

const BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = [
Expand Down
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Enhancements

- `BorderControl`: Improve color code readability in aria-label ([#51197](https://github.com/WordPress/gutenberg/pull/51197)).
- `Dropdown` and `DropdownMenu`: use internal context system to automatically pick the toolbar popover variant when rendered inside the `Toolbar` component ([#51154](https://github.com/WordPress/gutenberg/pull/51154)).

### Bug Fix

Expand Down
7 changes: 2 additions & 5 deletions packages/components/src/circular-option-picker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Icon } from '@wordpress/icons';
* Internal dependencies
*/
import type { ButtonAsButtonProps } from '../button/types';
import type Dropdown from '../dropdown';
import type { DropdownProps } from '../dropdown/types';
import type { WordPressComponentProps } from '../ui/context';

export type CircularOptionPickerProps = {
Expand Down Expand Up @@ -44,10 +44,7 @@ export type DropdownLinkActionProps = {
'children'
>;
linkText: string;
dropdownProps: Omit<
React.ComponentProps< typeof Dropdown >,
'className' | 'renderToggle'
>;
dropdownProps: Omit< DropdownProps, 'className' | 'renderToggle' >;
className?: string;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/dropdown-menu-v2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type {
DropdownMenuRadioItemProps,
DropdownMenuSeparatorProps,
DropdownSubMenuTriggerProps,
DropdownMenuContext,
DropdownMenuInternalContext,
DropdownMenuPrivateContext as DropdownMenuPrivateContextType,
} from './types';

Expand Down Expand Up @@ -70,7 +70,7 @@ const UnconnectedDropdownMenu = ( props: DropdownMenuProps ) => {
variant,
} = useContextSystem<
// Adding `className` to the context type to avoid a TS error
DropdownMenuProps & DropdownMenuContext & { className?: string }
DropdownMenuProps & DropdownMenuInternalContext & { className?: string }
>( props, 'DropdownMenu' );

// Render the portal in the default slot used by the legacy Popover component.
Expand Down
12 changes: 7 additions & 5 deletions packages/components/src/dropdown-menu-v2/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
import { COLORS, font, rtl, CONFIG } from '../utils';
import { space } from '../ui/utils/space';
import Icon from '../icon';
import type { DropdownMenuContext } from './types';
import type { DropdownMenuInternalContext } from './types';

const ANIMATION_PARAMS = {
SLIDE_AMOUNT: '2px',
Expand Down Expand Up @@ -62,7 +62,9 @@ const slideLeftAndFade = keyframes( {
'100%': { opacity: 1, transform: 'translateX(0)' },
} );

const baseContent = ( variant: DropdownMenuContext[ 'variant' ] ) => css`
const baseContent = (
variant: DropdownMenuInternalContext[ 'variant' ]
) => css`
min-width: 220px;
background-color: ${ COLORS.ui.background };
border-radius: ${ CONFIG.radiusBlockUi };
Expand Down Expand Up @@ -200,12 +202,12 @@ const baseItem = css`
`;

export const Content = styled( DropdownMenu.Content )<
Pick< DropdownMenuContext, 'variant' >
Pick< DropdownMenuInternalContext, 'variant' >
>`
${ ( props ) => baseContent( props.variant ) }
`;
export const SubContent = styled( DropdownMenu.SubContent )<
Pick< DropdownMenuContext, 'variant' >
Pick< DropdownMenuInternalContext, 'variant' >
>`
${ ( props ) => baseContent( props.variant ) }
`;
Expand Down Expand Up @@ -246,7 +248,7 @@ export const Label = styled( DropdownMenu.Label )`
`;

export const Separator = styled( DropdownMenu.Separator )<
Pick< DropdownMenuContext, 'variant' >
Pick< DropdownMenuInternalContext, 'variant' >
>`
height: ${ CONFIG.borderWidth };
/* TODO: doesn't match border color from variables */
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/dropdown-menu-v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export type DropdownMenuGroupProps = {

export type DropdownMenuSeparatorProps = {};

export type DropdownMenuContext = {
export type DropdownMenuInternalContext = {
/**
* This variant can be used to change the appearance of the component in
* specific contexts, ie. when rendered inside the `Toolbar` component.
Expand All @@ -258,7 +258,7 @@ export type DropdownMenuContext = {
};

export type DropdownMenuPrivateContext = Pick<
DropdownMenuContext,
DropdownMenuInternalContext,
'variant'
> & {
portalContainer: HTMLElement | null;
Expand Down
Loading

1 comment on commit f1d0bd5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in f1d0bd5.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5209441967
📝 Reported issues:

Please sign in to comment.