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

Sticky Position: Make help text aware of whether or not there is a parent block #47230

Closed
Closed
Changes from all commits
Commits
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
37 changes: 32 additions & 5 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 @@ -24,7 +25,9 @@ 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 { store as blockEditorStore } from '../store';

const POSITION_SUPPORT_KEY = 'position';

Expand All @@ -43,9 +46,7 @@ const STICKY_OPTION = {
value: 'sticky',
name: __( 'Sticky' ),
className: OPTION_CLASSNAME,
__experimentalHint: __(
'The block will stick to the top of the window instead of scrolling.'
),
__experimentalHint: __( 'Sticks to the top of the window when scrolling.' ),
};

const FIXED_OPTION = {
Expand Down Expand Up @@ -205,6 +206,7 @@ export function useIsPositionDisabled( { name: blockName } = {} ) {
export function PositionEdit( props ) {
const {
attributes: { style = {} },
clientId,
name: blockName,
setAttributes,
} = props;
Expand All @@ -213,16 +215,41 @@ export function PositionEdit( props ) {
const allowSticky = hasStickyPositionSupport( blockName );
const value = style?.position?.type;

const { firstParentClientId } = useSelect(
( select ) => {
const { getBlockParents } = select( blockEditorStore );
const parents = getBlockParents( clientId );
const _firstParentClientId = parents[ parents.length - 1 ];
return {
firstParentClientId: _firstParentClientId,
};
},
[ clientId ]
);
const blockInformation = useBlockDisplayInformation( firstParentClientId );
const parentBlockTitle = blockInformation?.title;

const options = useMemo( () => {
const availableOptions = [ DEFAULT_OPTION ];
if ( allowSticky || value === STICKY_OPTION.value ) {
availableOptions.push( STICKY_OPTION );
availableOptions.push( {
...STICKY_OPTION,
__experimentalHint: parentBlockTitle
? sprintf(
/* translators: %s: Name of the block's parent. */
__(
'Sticks to the top of the parent %s block when scrolling.'
),
parentBlockTitle
)
: __( 'Sticks to the top of the window when scrolling.' ),
} );
}
if ( allowFixed || value === FIXED_OPTION.value ) {
availableOptions.push( FIXED_OPTION );
}
return availableOptions;
}, [ allowFixed, allowSticky, value ] );
}, [ allowFixed, allowSticky, parentBlockTitle, value ] );

const onChangeType = ( next ) => {
// For now, use a hard-coded `0px` value for the position.
Expand Down