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: Try adding a notice if sticky is set at a non-root level (e.g. has no parents) #47207

Closed
wants to merge 1 commit into from
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
47 changes: 46 additions & 1 deletion packages/block-editor/src/hooks/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import classnames from 'classnames';
*/
import { __, sprintf } from '@wordpress/i18n';
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
import { BaseControl, CustomSelectControl } from '@wordpress/components';
import {
BaseControl,
CustomSelectControl,
Notice,
} from '@wordpress/components';
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import {
useContext,
useMemo,
Expand All @@ -25,6 +30,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 @@ -195,6 +201,40 @@ export function useIsPositionDisabled( { name: blockName } = {} ) {
return ! hasPositionSupport( blockName ) || isDisabled;
}

function StickyPositionChecker( { clientId, positionType } ) {
const { hasParents } = useSelect(
( select ) => {
const { getBlockParents } = select( blockEditorStore );
const parents = getBlockParents( clientId );

return {
hasParents: parents.length,
};
},
[ clientId ]
);

if ( positionType !== 'sticky' || ! hasParents ) {
return null;
}

const message = __(
'Sticky positioning works best with blocks that are set at the root of the document, as the sticky area locks to the next parent block. For sticky headers and footers, use a Group block at the root of the document.'
);

return (
<div className="block-editor-sticky-position-checker">
<Notice
spokenMessage={ null }
status="warning"
isDismissible={ false }
>
{ message }
</Notice>
</div>
);
}

/*
* Position controls to be rendered in an inspector control panel.
*
Expand All @@ -205,6 +245,7 @@ export function useIsPositionDisabled( { name: blockName } = {} ) {
export function PositionEdit( props ) {
const {
attributes: { style = {} },
clientId,
name: blockName,
setAttributes,
} = props;
Expand Down Expand Up @@ -275,6 +316,10 @@ export function PositionEdit( props ) {
size={ '__unstable-large' }
/>
</BaseControl>
<StickyPositionChecker
clientId={ clientId }
positionType={ value }
/>
</>
),
native: null,
Expand Down