Skip to content

Commit

Permalink
Sticky Position: Hide controls if the block is non-root (#47334)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong authored Jan 23, 2023
1 parent 70ba921 commit c095f06
Showing 1 changed file with 55 additions and 38 deletions.
93 changes: 55 additions & 38 deletions packages/block-editor/src/hooks/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
import { BaseControl, CustomSelectControl } from '@wordpress/components';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import {
useContext,
useMemo,
Expand All @@ -25,6 +26,7 @@ import BlockList from '../components/block-list';
import useSetting from '../components/use-setting';
import InspectorControls from '../components/inspector-controls';
import { cleanEmptyObject } from './utils';
import { store as blockEditorStore } from '../store';

const POSITION_SUPPORT_KEY = 'position';

Expand Down Expand Up @@ -196,15 +198,16 @@ export function useIsPositionDisabled( { name: blockName } = {} ) {
}

/*
* Position controls to be rendered in an inspector control panel.
* Position controls rendered in an inspector control panel.
*
* @param {Object} props
*
* @return {WPElement} Padding edit element.
* @return {WPElement} Position panel.
*/
export function PositionEdit( props ) {
export function PositionPanel( props ) {
const {
attributes: { style = {} },
clientId,
name: blockName,
setAttributes,
} = props;
Expand All @@ -213,16 +216,32 @@ export function PositionEdit( props ) {
const allowSticky = hasStickyPositionSupport( blockName );
const value = style?.position?.type;

const { hasParents } = useSelect(
( select ) => {
const { getBlockParents } = select( blockEditorStore );
const parents = getBlockParents( clientId );
return {
hasParents: parents.length,
};
},
[ clientId ]
);

const options = useMemo( () => {
const availableOptions = [ DEFAULT_OPTION ];
if ( allowSticky || value === STICKY_OPTION.value ) {
// Only display sticky option if the block has no parents (is at the root of the document),
// or if the block already has a sticky position value set.
if (
( allowSticky && ! hasParents ) ||
value === STICKY_OPTION.value
) {
availableOptions.push( STICKY_OPTION );
}
if ( allowFixed || value === FIXED_OPTION.value ) {
availableOptions.push( FIXED_OPTION );
}
return availableOptions;
}, [ allowFixed, allowSticky, value ] );
}, [ allowFixed, allowSticky, hasParents, value ] );

const onChangeType = ( next ) => {
// For now, use a hard-coded `0px` value for the position.
Expand Down Expand Up @@ -251,32 +270,37 @@ export function PositionEdit( props ) {
? options.find( ( option ) => option.value === value ) || DEFAULT_OPTION
: DEFAULT_OPTION;

// Only display position controls if there is at least one option to choose from.
return Platform.select( {
web: (
<>
<BaseControl className="block-editor-hooks__position-selection">
<CustomSelectControl
__nextUnconstrainedWidth
__next36pxDefaultSize
className="block-editor-hooks__position-selection__select-control"
label={ __( 'Position' ) }
hideLabelFromVision
describedBy={ sprintf(
// translators: %s: Currently selected font size.
__( 'Currently selected position: %s' ),
selectedOption.name
) }
options={ options }
value={ selectedOption }
__experimentalShowSelectedHint
onChange={ ( { selectedItem } ) => {
onChangeType( selectedItem.value );
} }
size={ '__unstable-large' }
/>
</BaseControl>
</>
),
web:
options.length > 1 ? (
<InspectorControls
key="position"
__experimentalGroup="position"
>
<BaseControl className="block-editor-hooks__position-selection">
<CustomSelectControl
__nextUnconstrainedWidth
__next36pxDefaultSize
className="block-editor-hooks__position-selection__select-control"
label={ __( 'Position' ) }
hideLabelFromVision
describedBy={ sprintf(
// translators: %s: Currently selected position.
__( 'Currently selected position: %s' ),
selectedOption.name
) }
options={ options }
value={ selectedOption }
__experimentalShowSelectedHint
onChange={ ( { selectedItem } ) => {
onChangeType( selectedItem.value );
} }
size={ '__unstable-large' }
/>
</BaseControl>
</InspectorControls>
) : null,
native: null,
} );
}
Expand All @@ -299,14 +323,7 @@ export const withInspectorControls = createHigherOrderComponent(
positionSupport && ! useIsPositionDisabled( props );

return [
showPositionControls && (
<InspectorControls
key="position"
__experimentalGroup="position"
>
<PositionEdit { ...props } />
</InspectorControls>
),
showPositionControls && <PositionPanel { ...props } />,
<BlockEdit key="edit" { ...props } />,
];
},
Expand Down

1 comment on commit c095f06

@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 c095f06.
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/3991455457
📝 Reported issues:

Please sign in to comment.