Skip to content

Commit

Permalink
Sticky Position: Try re-enabling non-root sticky position (#49321)
Browse files Browse the repository at this point in the history
* Sticky Position: Try re-enabling non-root sticky position, and add a visualizer to define sticky area

* Fix whitespace

* Ensure the position visualizer does not interfere with the ability to select inner blocks

* Ensure resizing the block editor will update the block popover dimensions

* Try simplifying the display logic so it always shows the visualizer if the block is selected

* Small code quality tweaks

* Try dotted outline, subtle display on focus and hover label

* Fix help text and spacing

* Update visualizer to only display on hover

* Remove visualizer
  • Loading branch information
andrewserong committed Jun 16, 2023
1 parent d481efb commit b6fb0a6
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/block-editor/src/hooks/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { addFilter } from '@wordpress/hooks';
import BlockList from '../components/block-list';
import useSetting from '../components/use-setting';
import InspectorControls from '../components/inspector-controls';
import useBlockDisplayInformation from '../components/use-block-display-information';
import { cleanEmptyObject } from './utils';
import { unlock } from '../lock-unlock';
import { store as blockEditorStore } from '../store';
Expand Down Expand Up @@ -222,32 +223,39 @@ export function PositionPanel( props ) {
const allowSticky = hasStickyPositionSupport( blockName );
const value = style?.position?.type;

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

const blockInformation = useBlockDisplayInformation( firstParentClientId );
const stickyHelpText =
allowSticky && value === STICKY_OPTION.value && blockInformation
? sprintf(
/* translators: %s: the name of the parent block. */
__(
'The block will stick to the scrollable area of the parent %s block.'
),
blockInformation.title
)
: null;

const options = useMemo( () => {
const availableOptions = [ DEFAULT_OPTION ];
// 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
) {
// Display options if they are allowed, or if a block already has a valid value set.
// This allows for a block to be switched off from a position type that is not allowed.
if ( allowSticky || value === STICKY_OPTION.value ) {
availableOptions.push( STICKY_OPTION );
}
if ( allowFixed || value === FIXED_OPTION.value ) {
availableOptions.push( FIXED_OPTION );
}
return availableOptions;
}, [ allowFixed, allowSticky, hasParents, value ] );
}, [ allowFixed, allowSticky, value ] );

const onChangeType = ( next ) => {
// For now, use a hard-coded `0px` value for the position.
Expand Down Expand Up @@ -281,7 +289,11 @@ export function PositionPanel( props ) {
web:
options.length > 1 ? (
<InspectorControls group="position">
<BaseControl className="block-editor-hooks__position-selection">
<BaseControl
className="block-editor-hooks__position-selection"
__nextHasNoMarginBottom
help={ stickyHelpText }
>
<CustomSelectControl
__nextUnconstrainedWidth
__next36pxDefaultSize
Expand Down

1 comment on commit b6fb0a6

@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 b6fb0a6.
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/5286054605
📝 Reported issues:

Please sign in to comment.