Skip to content

Commit d1ed282

Browse files
committed
fix(block-editor): add sticky offset control
Adds BoxControl in PositionPanelPure when position is sticky. Reuses existing style.position path (already supported by CSS and PHP renderer). Fixes WordPress#79732
1 parent fa55aa0 commit d1ed282

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

‎packages/block-editor/src/hooks/position.js‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ import clsx from 'clsx';
88
*/
99
import { __, _x, sprintf } from '@wordpress/i18n';
1010
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
11-
import { BaseControl, CustomSelectControl } from '@wordpress/components';
11+
import {
12+
BaseControl,
13+
BoxControl,
14+
CustomSelectControl,
15+
} from '@wordpress/components';
1216
import { useInstanceId } from '@wordpress/compose';
1317
import { useSelect } from '@wordpress/data';
1418
import { useMemo } from '@wordpress/element';
19+
import { __experimentalUseCustomUnits as useCustomUnits } from '@wordpress/components';
1520

1621
/**
1722
* Internal dependencies
@@ -201,6 +206,10 @@ export function PositionPanelPure( {
201206
const allowFixed = hasFixedPositionSupport( blockName );
202207
const allowSticky = hasStickyPositionSupport( blockName );
203208
const value = style?.position?.type;
209+
const [ spacingUnits ] = useSettings( 'spacing.units' );
210+
const units = useCustomUnits( {
211+
availableUnits: spacingUnits || [ '%', 'px', 'em', 'rem', 'vw' ],
212+
} );
204213

205214
const { firstParentClientId } = useSelect(
206215
( select ) => {
@@ -263,6 +272,20 @@ export function PositionPanelPure( {
263272
? options.find( ( option ) => option.value === value ) || DEFAULT_OPTION
264273
: DEFAULT_OPTION;
265274

275+
const onChangeSides = ( next ) => {
276+
const newStyle = {
277+
...style,
278+
position: {
279+
...style?.position,
280+
...next,
281+
},
282+
};
283+
284+
setAttributes( {
285+
style: cleanEmptyObject( newStyle ),
286+
} );
287+
};
288+
266289
// Only display position controls if there is at least one option to choose from.
267290
return options.length > 1 ? (
268291
<InspectorControls group="position">
@@ -281,6 +304,15 @@ export function PositionPanelPure( {
281304
onChangeType( selectedItem.value );
282305
} }
283306
/>
307+
{ value === STICKY_OPTION.value && (
308+
<BoxControl
309+
label={ __( 'Sticky offset' ) }
310+
values={ style?.position }
311+
onChange={ onChangeSides }
312+
units={ units }
313+
sides={ POSITION_SIDES }
314+
/>
315+
) }
284316
</BaseControl>
285317
</InspectorControls>
286318
) : null;

0 commit comments

Comments
 (0)