Skip to content
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

Use internal context system to apply toolbar variant to toolbar dropdowns #51154

Merged
merged 19 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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
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 }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to a separate variable to avoid creating a new object every render

/>
</BlockControls>
) }
Expand Down
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 @@ -5,6 +5,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